/tags/rel-1-3-26/SWIG/Source/DOH/memory.c
C | 220 lines | 150 code | 28 blank | 42 comment | 26 complexity | db7d8b3d2880cf53676686619f37e58e MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
1/* ----------------------------------------------------------------------------- 2 * memory.c 3 * 4 * This file implements all of DOH's memory management including allocation 5 * of objects and checking of objects. 6 * 7 * Author(s) : David Beazley (beazley@cs.uchicago.edu) 8 * 9 * Copyright (C) 1999-2000. The University of Chicago 10 * See the file LICENSE for information on usage and redistribution. 11 * ----------------------------------------------------------------------------- */ 12 13char cvsroot_memory_c[] = "$Header$"; 14 15#include "dohint.h" 16 17#ifndef DOH_POOL_SIZE 18#define DOH_POOL_SIZE 16384 19#endif 20 21static int PoolSize = DOH_POOL_SIZE; 22 23DOH *DohNone = 0; /* The DOH None object */ 24 25typedef struct pool { 26 DohBase *ptr; /* Start of pool */ 27 int len; /* Length of pool */ 28 int blen; /* Byte length of pool */ 29 int current; /* Current position for next allocation */ 30 char* pbeg; /* Beg of pool */ 31 char* pend; /* End of pool */ 32 struct pool *next; /* Next pool */ 33} Pool; 34 35static DohBase *FreeList = 0; /* List of free objects */ 36static Pool *Pools = 0; 37static int pools_initialized = 0; 38 39/* ---------------------------------------------------------------------- 40 * CreatePool() - Create a new memory pool 41 * ---------------------------------------------------------------------- */ 42 43static void 44CreatePool() { 45 Pool *p = 0; 46 p = (Pool *) DohMalloc(sizeof(Pool)); 47 assert(p); 48 p->ptr = (DohBase *) DohMalloc(sizeof(DohBase)*PoolSize); 49 assert(p->ptr); 50 memset(p->ptr,0,sizeof(DohBase)*PoolSize); 51 p->len = PoolSize; 52 p->blen = PoolSize*sizeof(DohBase); 53 p->current = 0; 54 p->pbeg = ((char *)p->ptr); 55 p->pend = p->pbeg + p->blen; 56 p->next = Pools; 57 Pools = p; 58} 59 60/* ---------------------------------------------------------------------- 61 * InitPools() - Initialize the memory allocator 62 * ---------------------------------------------------------------------- */ 63 64static void 65InitPools() { 66 if (pools_initialized) return; 67 CreatePool(); /* Create initial pool */ 68 pools_initialized = 1; 69 DohNone = NewVoid(0,0); /* Create the None object */ 70 DohIntern(DohNone); 71} 72 73/* ---------------------------------------------------------------------- 74 * DohCheck() 75 * 76 * Returns 1 if an arbitrary pointer is a DOH object. 77 * ---------------------------------------------------------------------- */ 78 79int 80DohCheck(const DOH *ptr) { 81 register Pool *p = Pools; 82 register char *cptr = (char *) ptr; 83 while (p) { 84 if ((cptr >= p->pbeg) && (cptr < p->pend)) return 1; 85 /* 86 pptr = (char *) p->ptr; 87 if ((cptr >= pptr) && (cptr < (pptr+(p->current*sizeof(DohBase))))) return 1; */ 88 p = p->next; 89 } 90 return 0; 91} 92 93/* ----------------------------------------------------------------------------- 94 * DohIntern() 95 * ----------------------------------------------------------------------------- */ 96 97void 98DohIntern(DOH *obj) { 99 DohBase *b = (DohBase *) obj; 100 b->flag_intern = 1; 101} 102 103/* ---------------------------------------------------------------------- 104 * DohObjMalloc() 105 * 106 * Allocate memory for a new object. 107 * ---------------------------------------------------------------------- */ 108 109DOH * 110DohObjMalloc(DohObjInfo *type, void *data) { 111 DohBase *obj; 112 if (!pools_initialized) InitPools(); 113 if (FreeList) { 114 obj = FreeList; 115 FreeList = (DohBase *) obj->data; 116 } else { 117 while (Pools->current == Pools->len) { 118 PoolSize *= 2; 119 CreatePool(); 120 } 121 obj = Pools->ptr + Pools->current; 122 Pools->current++; 123 } 124 obj->type = type; 125 obj->data = data; 126 obj->meta = 0; 127 obj->refcount = 1; 128 obj->flag_intern = 0; 129 obj->flag_marked = 0; 130 obj->flag_user = 0; 131 obj->flag_usermark = 0; 132 return (DOH *) obj; 133} 134 135/* ---------------------------------------------------------------------- 136 * DohObjFree() - Free a DOH object 137 * ---------------------------------------------------------------------- */ 138 139void 140DohObjFree(DOH *ptr) { 141 DohBase *b, *meta; 142 b = (DohBase *) ptr; 143 if (b->flag_intern) return; 144 meta = (DohBase *)b->meta; 145 b->data = (void *) FreeList; 146 b->meta = 0; 147 b->type = 0; 148 FreeList = b; 149 if (meta) { 150 Delete(meta); 151 } 152} 153 154/* ---------------------------------------------------------------------- 155 * DohMemoryDebug() 156 * 157 * Display memory usage statistics 158 * ---------------------------------------------------------------------- */ 159 160void 161DohMemoryDebug(void) { 162 extern DohObjInfo DohStringType; 163 extern DohObjInfo DohListType; 164 extern DohObjInfo DohHashType; 165 166 Pool *p; 167 int totsize = 0; 168 int totused = 0; 169 int totfree = 0; 170 171 int numstring = 0; 172 int numlist = 0; 173 int numhash = 0; 174 175 printf("Memory statistics:\n\n"); 176 printf("Pools:\n"); 177 178 p = Pools; 179 while(p) { 180 /* Calculate number of used, free items */ 181 int i; 182 int nused = 0, nfree = 0; 183 for (i = 0; i < p->len; i++) { 184 if (p->ptr[i].refcount <= 0) nfree++; 185 else { 186 nused++; 187 if (p->ptr[i].type == &DohStringType) numstring++; 188 else if (p->ptr[i].type == &DohListType) numlist++; 189 else if (p->ptr[i].type == &DohHashType) numhash++; 190 } 191 } 192 printf(" Pool %8p: size = %10d. used = %10d. free = %10d\n", (void *)p, p->len, nused, nfree); 193 totsize += p->len; 194 totused+= nused; 195 totfree+= nfree; 196 p = p->next; 197 } 198 printf("\n Total: size = %10d, used = %10d, free = %10d\n", totsize, totused, totfree); 199 200 printf("\nObject types\n"); 201 printf(" Strings : %d\n", numstring); 202 printf(" Lists : %d\n", numlist); 203 printf(" Hashes : %d\n", numhash); 204 205#if 0 206 p = Pools; 207 while(p) { 208 int i; 209 for (i = 0; i < p->len; i++) { 210 if (p->ptr[i].refcount > 0) { 211 if (p->ptr[i].type == &DohStringType) { 212 Printf(stdout,"%s\n", p->ptr+i); 213 } 214 } 215 } 216 p = p->next; 217 } 218#endif 219 220}