1 /* -----------------------------------------------------------------------------
4 * This file implements all of DOH's memory management including allocation
5 * of objects and checking of objects.
7 * Author(s) : David Beazley (beazley@cs.uchicago.edu)
9 * Copyright (C) 1999-2000. The University of Chicago
10 * See the file LICENSE for information on usage and redistribution.
11 * ----------------------------------------------------------------------------- */
13 char cvsroot_memory_c[] = "$Id: memory.c 9607 2006-12-05 22:11:40Z beazley $";
18 #define DOH_POOL_SIZE 16384
21 static int PoolSize = DOH_POOL_SIZE;
23 DOH *DohNone = 0; /* The DOH None object */
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 */
35 static DohBase *FreeList = 0; /* List of free objects */
36 static Pool *Pools = 0;
37 static int pools_initialized = 0;
39 /* ----------------------------------------------------------------------
40 * CreatePool() - Create a new memory pool
41 * ---------------------------------------------------------------------- */
43 static void CreatePool() {
45 p = (Pool *) DohMalloc(sizeof(Pool));
47 p->ptr = (DohBase *) DohMalloc(sizeof(DohBase) * PoolSize);
49 memset(p->ptr, 0, sizeof(DohBase) * PoolSize);
51 p->blen = PoolSize * sizeof(DohBase);
53 p->pbeg = ((char *) p->ptr);
54 p->pend = p->pbeg + p->blen;
59 /* ----------------------------------------------------------------------
60 * InitPools() - Initialize the memory allocator
61 * ---------------------------------------------------------------------- */
63 static void InitPools() {
64 if (pools_initialized)
66 CreatePool(); /* Create initial pool */
67 pools_initialized = 1;
68 DohNone = NewVoid(0, 0); /* Create the None object */
72 /* ----------------------------------------------------------------------
75 * Returns 1 if an arbitrary pointer is a DOH object.
76 * ---------------------------------------------------------------------- */
78 int DohCheck(const DOH *ptr) {
79 register Pool *p = Pools;
80 register char *cptr = (char *) ptr;
82 if ((cptr >= p->pbeg) && (cptr < p->pend))
85 pptr = (char *) p->ptr;
86 if ((cptr >= pptr) && (cptr < (pptr+(p->current*sizeof(DohBase))))) return 1; */
92 /* -----------------------------------------------------------------------------
94 * ----------------------------------------------------------------------------- */
96 void DohIntern(DOH *obj) {
97 DohBase *b = (DohBase *) obj;
101 /* ----------------------------------------------------------------------
104 * Allocate memory for a new object.
105 * ---------------------------------------------------------------------- */
107 DOH *DohObjMalloc(DohObjInfo *type, void *data) {
109 if (!pools_initialized)
113 FreeList = (DohBase *) obj->data;
115 while (Pools->current == Pools->len) {
118 obj = Pools->ptr + Pools->current;
125 obj->flag_intern = 0;
126 obj->flag_marked = 0;
128 obj->flag_usermark = 0;
132 /* ----------------------------------------------------------------------
133 * DohObjFree() - Free a DOH object
134 * ---------------------------------------------------------------------- */
136 void DohObjFree(DOH *ptr) {
141 meta = (DohBase *) b->meta;
142 b->data = (void *) FreeList;
151 /* ----------------------------------------------------------------------
154 * Display memory usage statistics
155 * ---------------------------------------------------------------------- */
157 void DohMemoryDebug(void) {
158 extern DohObjInfo DohStringType;
159 extern DohObjInfo DohListType;
160 extern DohObjInfo DohHashType;
171 printf("Memory statistics:\n\n");
176 /* Calculate number of used, free items */
178 int nused = 0, nfree = 0;
179 for (i = 0; i < p->len; i++) {
180 if (p->ptr[i].refcount <= 0)
184 if (p->ptr[i].type == &DohStringType)
186 else if (p->ptr[i].type == &DohListType)
188 else if (p->ptr[i].type == &DohHashType)
192 printf(" Pool %8p: size = %10d. used = %10d. free = %10d\n", (void *) p, p->len, nused, nfree);
198 printf("\n Total: size = %10d, used = %10d, free = %10d\n", totsize, totused, totfree);
200 printf("\nObject types\n");
201 printf(" Strings : %d\n", numstring);
202 printf(" Lists : %d\n", numlist);
203 printf(" Hashes : %d\n", numhash);
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);