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
static char cvsroot[] = "$Header: /cvs/projects/SWIG/Source/DOH/Doh/memory.c,v 1.23.4.1 2001/07/17 16:11:15 mkoeppe Exp $";
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
struct pool *next; /* Next pool */
33
DohBase *FreeList = 0; /* List of free objects */
35
static Pool *Pools = 0;
36
static int pools_initialized = 0;
38
/* ----------------------------------------------------------------------
39
* CreatePool() - Create a new memory pool
40
* ---------------------------------------------------------------------- */
45
p = (Pool *) DohMalloc(sizeof(Pool));
47
p->ptr = (DohBase *) DohMalloc(sizeof(DohBase)*PoolSize);
50
p->blen = PoolSize*sizeof(DohBase);
56
/* ----------------------------------------------------------------------
57
* InitPools() - Initialize the memory allocator
58
* ---------------------------------------------------------------------- */
62
if (pools_initialized) return;
63
CreatePool(); /* Create initial pool */
64
pools_initialized = 1;
65
DohNone = NewVoid(0,0); /* Create the None object */
69
/* ----------------------------------------------------------------------
72
* Returns 1 if an arbitrary pointer is a DOH object.
73
* ---------------------------------------------------------------------- */
76
DohCheck(const DOH *ptr) {
78
register char *cptr = (char *) ptr;
81
pptr = (char *) p->ptr;
82
if ((cptr >= pptr) && (cptr < (pptr + p->blen))) return 1;
84
pptr = (char *) p->ptr;
85
if ((cptr >= pptr) && (cptr < (pptr+(p->current*sizeof(DohBase))))) return 1; */
91
/* -----------------------------------------------------------------------------
93
* ----------------------------------------------------------------------------- */
97
DohBase *b = (DohBase *) obj;
101
/* ----------------------------------------------------------------------
104
* Allocate memory for a new object.
105
* ---------------------------------------------------------------------- */
108
DohObjMalloc(int type, void *data) {
110
if (!pools_initialized) InitPools();
113
FreeList = (DohBase *) obj->data;
115
while (Pools->current == Pools->len) {
119
obj = Pools->ptr + Pools->current;
125
obj->flag_intern = 0;
126
obj->flag_marked = 0;
130
/* ----------------------------------------------------------------------
131
* DohObjFree() - Free a DOH object
132
* ---------------------------------------------------------------------- */
135
DohObjFree(DOH *ptr) {
138
if (b->flag_intern) return;
139
b->data = (void *) FreeList;