#include "memint.h"
Go to the source code of this file.
Data Structures | |
struct | list_ |
struct | rec_mgr_ |
Defines | |
#define | ALLOC_SIZE NICE_BLOCK_SIZE |
#define | MAGIC_COOKIE 0x34f21ab3l |
#define | MAGIC_COOKIE1 0x432fa13bl |
Typedefs | |
typedef struct list_ * | list |
Functions | |
pointer | mem_new_rec (rec_mgr mgr) |
void | mem_free_rec (rec_mgr mgr, pointer rec) |
rec_mgr | mem_new_rec_mgr (int size) |
void | mem_free_rec_mgr (rec_mgr mgr) |
Definition at line 91 of file memrec.c.
00095 { 00096 #if defined(DEBUG_MEM) 00097 if (mgr->size >= sizeof(long)+sizeof(struct list_)) 00098 if (*(long *)(sizeof(struct list_)+(INT_PTR)rec) == MAGIC_COOKIE) 00099 fprintf(stderr, "record at 0x%lx may already be freed\n", (INT_PTR)rec); 00100 #endif 00101 ((list)rec)->next=mgr->free; 00102 #if defined(DEBUG_MEM) 00103 if (mgr->size >= sizeof(long)+sizeof(struct list_)) 00104 *(long *)(sizeof(struct list_)+(INT_PTR)rec)=MAGIC_COOKIE; 00105 #endif 00106 mgr->free=(list)rec; 00107 }
void mem_free_rec_mgr | ( | rec_mgr | mgr | ) |
Definition at line 144 of file memrec.c.
00147 { 00148 list p, q; 00149 00150 for (p=mgr->blocks; p; p=q) 00151 { 00152 q=p->next; 00153 mem_free_block((pointer)p); 00154 } 00155 mem_free_block((pointer)mgr); 00156 }
Definition at line 39 of file memrec.c.
00042 { 00043 int i; 00044 pointer p; 00045 list new_; 00046 00047 if (!mgr->free) 00048 { 00049 /* Allocate a new block. */ 00050 new_=(list)mem_get_block(ALLOC_SIZE); 00051 new_->next=mgr->blocks; 00052 mgr->blocks=new_; 00053 mgr->free=(list)((INT_PTR)new_+ROUNDUP(sizeof(struct list_))); 00054 p=(pointer)(mgr->free); 00055 /* Carve the block into pieces. */ 00056 for (i=1; i < mgr->recs_per_block; ++i) 00057 { 00058 ((list)p)->next=(list)((INT_PTR)p+mgr->size); 00059 #if defined(DEBUG_MEM) 00060 if (mgr->size >= sizeof(long)+sizeof(struct list_)) 00061 *(long *)(sizeof(struct list_)+(INT_PTR)p)=MAGIC_COOKIE; 00062 #endif 00063 p=(pointer)((INT_PTR)p+mgr->size); 00064 } 00065 ((list)p)->next=0; 00066 #if defined(DEBUG_MEM) 00067 if (mgr->size >= sizeof(long)+sizeof(struct list_)) 00068 *(long *)(sizeof(struct list_)+(INT_PTR)p)=MAGIC_COOKIE; 00069 #endif 00070 } 00071 new_=mgr->free; 00072 #if defined(DEBUG_MEM) 00073 if (mgr->size >= sizeof(long)+sizeof(struct list_)) 00074 if (*(long *)(sizeof(struct list_)+(INT_PTR)new_) != MAGIC_COOKIE) 00075 fprintf(stderr, "record at 0x%lx may be in use\n", (INT_PTR)new_); 00076 else 00077 *(long *)(sizeof(struct list_)+(INT_PTR)new_)=MAGIC_COOKIE1; 00078 #endif 00079 mgr->free=mgr->free->next; 00080 return ((pointer)new_); 00081 }
rec_mgr mem_new_rec_mgr | ( | int | size | ) |
Definition at line 117 of file memrec.c.
00120 { 00121 rec_mgr mgr; 00122 00123 if (size < sizeof(struct list_)) 00124 size=sizeof(struct list_); 00125 size=ROUNDUP(size); 00126 if (size > ALLOC_SIZE-ROUNDUP(sizeof(struct list_))) 00127 mem_fatal("mem_new_rec_mgr: record size too large"); 00128 mgr=(rec_mgr)mem_get_block((SIZE_T)sizeof(struct rec_mgr_)); 00129 mgr->size=size; 00130 mgr->recs_per_block=(ALLOC_SIZE-ROUNDUP(sizeof(struct list_)))/size; 00131 mgr->free=0; 00132 mgr->blocks=0; 00133 return (mgr); 00134 }