Extensive cleanup
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@453 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
7b03566fdd
commit
2a1fc5ca87
7 changed files with 67 additions and 184 deletions
|
|
@ -28,7 +28,7 @@ static DohObjInfo DohBaseType = {
|
||||||
0, /* doh_hash */
|
0, /* doh_hash */
|
||||||
0, /* doh_cmp */
|
0, /* doh_cmp */
|
||||||
0, /* doh_mapping */
|
0, /* doh_mapping */
|
||||||
0, /* doh_sequence */
|
0, /* doh_list */
|
||||||
0, /* doh_file */
|
0, /* doh_file */
|
||||||
0, /* doh_string */
|
0, /* doh_string */
|
||||||
0, /* doh_callable */
|
0, /* doh_callable */
|
||||||
|
|
@ -229,7 +229,7 @@ DohHashval(const DOH *obj) {
|
||||||
DohTrace(DOH_CALLS,"DohHashval %x\n",obj);
|
DohTrace(DOH_CALLS,"DohHashval %x\n",obj);
|
||||||
if (DohCheck(b)) {
|
if (DohCheck(b)) {
|
||||||
if (b->objinfo->doh_hash) {
|
if (b->objinfo->doh_hash) {
|
||||||
return (b->objinfo->doh_hash)(b);
|
return (b->objinfo->doh_hashval)(b);
|
||||||
}
|
}
|
||||||
DohTrace(DOH_UNSUPPORTED,"No hash method defined for type '%s'\n", b->objinfo->objname);
|
DohTrace(DOH_UNSUPPORTED,"No hash method defined for type '%s'\n", b->objinfo->objname);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -298,7 +298,7 @@ int
|
||||||
DohIsMapping(const DOH *obj) {
|
DohIsMapping(const DOH *obj) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
if (!DohCheck(b)) return 0;
|
if (!DohCheck(b)) return 0;
|
||||||
if (b->objinfo->doh_mapping) return 1;
|
if (b->objinfo->doh_hash) return 1;
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -313,8 +313,8 @@ DohGetattr(DOH *obj, const DOH *name) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohGetattr %x, %x\n",obj,name);
|
DohTrace(DOH_CALLS,"DohGetattr %x, %x\n",obj,name);
|
||||||
if (DohIsMapping(b)) {
|
if (DohIsMapping(b)) {
|
||||||
if (b->objinfo->doh_mapping->doh_getattr) {
|
if (b->objinfo->doh_hash->doh_getattr) {
|
||||||
return (b->objinfo->doh_mapping->doh_getattr)(b,(DOH *) name);
|
return (b->objinfo->doh_hash->doh_getattr)(b,(DOH *) name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(b)) {
|
if (DohCheck(b)) {
|
||||||
|
|
@ -336,8 +336,8 @@ DohSetattr(DOH *obj, const DOH *name, const DOH *value) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohSetattr %x, %x, %x\n",obj,name, value);
|
DohTrace(DOH_CALLS,"DohSetattr %x, %x, %x\n",obj,name, value);
|
||||||
if (DohIsMapping(b)) {
|
if (DohIsMapping(b)) {
|
||||||
if (b->objinfo->doh_mapping->doh_setattr) {
|
if (b->objinfo->doh_hash->doh_setattr) {
|
||||||
return (b->objinfo->doh_mapping->doh_setattr)(b,(DOH *) name,(DOH *) value);
|
return (b->objinfo->doh_hash->doh_setattr)(b,(DOH *) name,(DOH *) value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(b)) {
|
if (DohCheck(b)) {
|
||||||
|
|
@ -359,8 +359,8 @@ DohDelattr(DOH *obj, const DOH *name) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohDelattr %x, %x\n",obj,name);
|
DohTrace(DOH_CALLS,"DohDelattr %x, %x\n",obj,name);
|
||||||
if (DohIsMapping(obj)) {
|
if (DohIsMapping(obj)) {
|
||||||
if (b->objinfo->doh_mapping->doh_delattr) {
|
if (b->objinfo->doh_hash->doh_delattr) {
|
||||||
(b->objinfo->doh_mapping->doh_delattr)(b,(DOH *) name);
|
(b->objinfo->doh_hash->doh_delattr)(b,(DOH *) name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -382,8 +382,8 @@ DohFirstkey(DOH *obj) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohFirstkey %x\n",obj);
|
DohTrace(DOH_CALLS,"DohFirstkey %x\n",obj);
|
||||||
if (DohIsMapping(obj)) {
|
if (DohIsMapping(obj)) {
|
||||||
if (b->objinfo->doh_mapping->doh_firstkey) {
|
if (b->objinfo->doh_hash->doh_firstkey) {
|
||||||
return (b->objinfo->doh_mapping->doh_firstkey)(obj);
|
return (b->objinfo->doh_hash->doh_firstkey)(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(obj)) {
|
if (DohCheck(obj)) {
|
||||||
|
|
@ -405,8 +405,8 @@ DohNextkey(DOH *obj) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohNextkey %x\n",obj);
|
DohTrace(DOH_CALLS,"DohNextkey %x\n",obj);
|
||||||
if (DohIsMapping(obj)) {
|
if (DohIsMapping(obj)) {
|
||||||
if (b->objinfo->doh_mapping->doh_nextkey) {
|
if (b->objinfo->doh_hash->doh_nextkey) {
|
||||||
return (b->objinfo->doh_mapping->doh_nextkey)(obj);
|
return (b->objinfo->doh_hash->doh_nextkey)(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(obj)) {
|
if (DohCheck(obj)) {
|
||||||
|
|
@ -558,7 +558,7 @@ int
|
||||||
DohIsSequence(const DOH *obj) {
|
DohIsSequence(const DOH *obj) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
if (!DohCheck(b)) return 0;
|
if (!DohCheck(b)) return 0;
|
||||||
if (b->objinfo->doh_sequence) return 1;
|
if (b->objinfo->doh_list) return 1;
|
||||||
else return 0;
|
else return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -573,8 +573,8 @@ DohGetitem(DOH *obj, int index) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohGetitem %x, %d\n",obj,index);
|
DohTrace(DOH_CALLS,"DohGetitem %x, %d\n",obj,index);
|
||||||
if (DohIsSequence(obj)) {
|
if (DohIsSequence(obj)) {
|
||||||
if (b->objinfo->doh_sequence->doh_getitem) {
|
if (b->objinfo->doh_list->doh_getitem) {
|
||||||
return (b->objinfo->doh_sequence->doh_getitem)(obj,index);
|
return (b->objinfo->doh_list->doh_getitem)(obj,index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(obj)) {
|
if (DohCheck(obj)) {
|
||||||
|
|
@ -596,8 +596,8 @@ DohSetitem(DOH *obj, int index, const DOH *value) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohSetitem %x, %d, %x\n",obj,index, value);
|
DohTrace(DOH_CALLS,"DohSetitem %x, %d, %x\n",obj,index, value);
|
||||||
if (DohIsSequence(obj)) {
|
if (DohIsSequence(obj)) {
|
||||||
if (b->objinfo->doh_sequence->doh_setitem) {
|
if (b->objinfo->doh_list->doh_setitem) {
|
||||||
return (b->objinfo->doh_sequence->doh_setitem)(obj,index,(DOH *) value);
|
return (b->objinfo->doh_list->doh_setitem)(obj,index,(DOH *) value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(obj)) {
|
if (DohCheck(obj)) {
|
||||||
|
|
@ -618,8 +618,8 @@ DohDelitem(DOH *obj, int index) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohDelitem %x, %d\n",obj,index);
|
DohTrace(DOH_CALLS,"DohDelitem %x, %d\n",obj,index);
|
||||||
if (DohIsSequence(obj)) {
|
if (DohIsSequence(obj)) {
|
||||||
if (b->objinfo->doh_sequence->doh_delitem) {
|
if (b->objinfo->doh_list->doh_delitem) {
|
||||||
return (b->objinfo->doh_sequence->doh_delitem)(obj,index);
|
return (b->objinfo->doh_list->doh_delitem)(obj,index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(obj)) {
|
if (DohCheck(obj)) {
|
||||||
|
|
@ -641,8 +641,8 @@ DohInsertitem(DOH *obj, int index, const DOH *value) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohInsertitem %x, %d, %x\n",obj,index, value);
|
DohTrace(DOH_CALLS,"DohInsertitem %x, %d, %x\n",obj,index, value);
|
||||||
if (DohIsSequence(obj)) {
|
if (DohIsSequence(obj)) {
|
||||||
if (b->objinfo->doh_sequence->doh_insitem) {
|
if (b->objinfo->doh_list->doh_insitem) {
|
||||||
return (b->objinfo->doh_sequence->doh_insitem)(obj,index,(DOH *) value);
|
return (b->objinfo->doh_list->doh_insitem)(obj,index,(DOH *) value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(obj)) {
|
if (DohCheck(obj)) {
|
||||||
|
|
@ -663,8 +663,8 @@ DohFirstitem(DOH *obj) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohFirstitem %x\n");
|
DohTrace(DOH_CALLS,"DohFirstitem %x\n");
|
||||||
if (DohIsSequence(obj)) {
|
if (DohIsSequence(obj)) {
|
||||||
if (b->objinfo->doh_sequence->doh_firstitem) {
|
if (b->objinfo->doh_list->doh_firstitem) {
|
||||||
return (b->objinfo->doh_sequence->doh_firstitem)(obj);
|
return (b->objinfo->doh_list->doh_firstitem)(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(obj)) {
|
if (DohCheck(obj)) {
|
||||||
|
|
@ -686,8 +686,8 @@ DohNextitem(DOH *obj) {
|
||||||
DohBase *b = (DohBase *) obj;
|
DohBase *b = (DohBase *) obj;
|
||||||
DohTrace(DOH_CALLS,"DohNextitem %x\n");
|
DohTrace(DOH_CALLS,"DohNextitem %x\n");
|
||||||
if (DohIsSequence(obj)) {
|
if (DohIsSequence(obj)) {
|
||||||
if (b->objinfo->doh_sequence->doh_nextitem) {
|
if (b->objinfo->doh_list->doh_nextitem) {
|
||||||
return (b->objinfo->doh_sequence->doh_nextitem)(obj);
|
return (b->objinfo->doh_list->doh_nextitem)(obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (DohCheck(obj)) {
|
if (DohCheck(obj)) {
|
||||||
|
|
@ -986,7 +986,7 @@ DohInit(DOH *b) {
|
||||||
* Set file location (default method).
|
* Set file location (default method).
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
void
|
void
|
||||||
DohSetFile(DOH *ho, DOH *file) {
|
DohSetfile(DOH *ho, DOH *file) {
|
||||||
DohBase *h = (DohBase *) ho;
|
DohBase *h = (DohBase *) ho;
|
||||||
if (DohCheck(h)) {
|
if (DohCheck(h)) {
|
||||||
if (!DohCheck(file)) file = NewString(file);
|
if (!DohCheck(file)) file = NewString(file);
|
||||||
|
|
|
||||||
|
|
@ -459,7 +459,7 @@ CopyHash(DOH *ho) {
|
||||||
* type information
|
* type information
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
static DohMappingMethods HashMappingMethods = {
|
static DohHashMethods HashHashMethods = {
|
||||||
Hash_getattr,
|
Hash_getattr,
|
||||||
Hash_setattr,
|
Hash_setattr,
|
||||||
Hash_delattr,
|
Hash_delattr,
|
||||||
|
|
@ -480,7 +480,7 @@ static DohObjInfo HashType = {
|
||||||
Hash_len, /* doh_len */
|
Hash_len, /* doh_len */
|
||||||
0, /* doh_hash */
|
0, /* doh_hash */
|
||||||
0, /* doh_cmp */
|
0, /* doh_cmp */
|
||||||
&HashMappingMethods, /* doh_mapping */
|
&HashHashMethods, /* doh_mapping */
|
||||||
0, /* doh_sequence */
|
0, /* doh_sequence */
|
||||||
0, /* doh_file */
|
0, /* doh_file */
|
||||||
0, /* doh_string */
|
0, /* doh_string */
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,6 @@ void more(List *l) {
|
||||||
*
|
*
|
||||||
* Make a shallow copy of a list.
|
* Make a shallow copy of a list.
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
static DOH *
|
static DOH *
|
||||||
CopyList(DOH *lo) {
|
CopyList(DOH *lo) {
|
||||||
List *l,*nl;
|
List *l,*nl;
|
||||||
|
|
@ -301,7 +300,7 @@ List_dump(DOH *lo, DOH *out) {
|
||||||
|
|
||||||
#define MAXLISTITEMS 8
|
#define MAXLISTITEMS 8
|
||||||
|
|
||||||
static DohSequenceMethods ListSeqMethods = {
|
static DohListMethods ListListMethods = {
|
||||||
List_get,
|
List_get,
|
||||||
List_set,
|
List_set,
|
||||||
List_remove,
|
List_remove,
|
||||||
|
|
@ -324,7 +323,7 @@ static DohObjInfo ListType = {
|
||||||
0, /* doh_hash */
|
0, /* doh_hash */
|
||||||
0, /* doh_cmp */
|
0, /* doh_cmp */
|
||||||
0, /* doh_mapping */
|
0, /* doh_mapping */
|
||||||
&ListSeqMethods, /* doh_sequence */
|
&ListListMethods, /* doh_sequence */
|
||||||
0, /* doh_file */
|
0, /* doh_file */
|
||||||
0, /* doh_string */
|
0, /* doh_string */
|
||||||
0, /* doh_callable */
|
0, /* doh_callable */
|
||||||
|
|
@ -357,7 +356,6 @@ NewList() {
|
||||||
int i;
|
int i;
|
||||||
l = (List *) DohObjMalloc(sizeof(List));
|
l = (List *) DohObjMalloc(sizeof(List));
|
||||||
l->objinfo = &ListType;
|
l->objinfo = &ListType;
|
||||||
DohXInit(l);
|
|
||||||
l->nitems = 0;
|
l->nitems = 0;
|
||||||
l->maxitems = MAXLISTITEMS;
|
l->maxitems = MAXLISTITEMS;
|
||||||
l->items = (void **) DohMalloc(l->maxitems*sizeof(void *));
|
l->items = (void **) DohMalloc(l->maxitems*sizeof(void *));
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
* memory.c
|
* memory.c
|
||||||
*
|
*
|
||||||
* This file implements all of DOH's memory management including allocation
|
* This file implements all of DOH's memory management including allocation
|
||||||
* of objects, checking of objects, and garbage collection.
|
* of objects and checking of objects.
|
||||||
*
|
*
|
||||||
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
|
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
|
||||||
*
|
*
|
||||||
|
|
@ -22,24 +22,11 @@ static char cvsroot[] = "$Header$";
|
||||||
#define DOH_MAX_FRAG 1024
|
#define DOH_MAX_FRAG 1024
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int _DohMemoryCurrent = 0;
|
static int PoolSize = DOH_POOL_SIZE;
|
||||||
static int _DohMemoryHigh = 0;
|
|
||||||
static int _PoolSize = DOH_POOL_SIZE;
|
|
||||||
static int num_fragments = 0;
|
|
||||||
static int fragment_size = 0;
|
|
||||||
static int obj_total_allocated = 0;
|
|
||||||
static int obj_ntotal_allocated = 0;
|
|
||||||
static int data_total_allocated = 0;
|
|
||||||
|
|
||||||
DOH *DohNone = 0; /* The DOH None object */
|
DOH *DohNone = 0; /* The DOH None object */
|
||||||
|
|
||||||
typedef struct fragment {
|
static DohBase *FreeFragments[DOH_MAX_FRAG]; /* Array of free memory fragments */
|
||||||
char *ptr; /* Pointer to fragment */
|
|
||||||
int len; /* Length of fragment */
|
|
||||||
struct fragment *next; /* Next fragment */
|
|
||||||
} Fragment;
|
|
||||||
|
|
||||||
static Fragment *FreeFragments[DOH_MAX_FRAG];
|
|
||||||
|
|
||||||
typedef struct pool {
|
typedef struct pool {
|
||||||
char *ptr; /* Start of pool */
|
char *ptr; /* Start of pool */
|
||||||
|
|
@ -52,9 +39,7 @@ static Pool *Pools = 0;
|
||||||
static int pools_initialized = 0;
|
static int pools_initialized = 0;
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
* CreatePool()
|
* CreatePool() - Create a new memory pool
|
||||||
*
|
|
||||||
* Create a new memory pool
|
|
||||||
* ---------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------- */
|
||||||
|
|
||||||
static Pool *
|
static Pool *
|
||||||
|
|
@ -63,7 +48,6 @@ CreatePool(int size) {
|
||||||
char *c;
|
char *c;
|
||||||
c = (char *) DohMalloc(size);
|
c = (char *) DohMalloc(size);
|
||||||
if (!c) return 0;
|
if (!c) return 0;
|
||||||
|
|
||||||
p = (Pool *) DohMalloc(sizeof(Pool));
|
p = (Pool *) DohMalloc(sizeof(Pool));
|
||||||
p->ptr = c;
|
p->ptr = c;
|
||||||
p->len = size;
|
p->len = size;
|
||||||
|
|
@ -73,9 +57,7 @@ CreatePool(int size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
* InitPools()
|
* InitPools() - Initialize the memory allocator
|
||||||
*
|
|
||||||
* Initialize the memory allocator
|
|
||||||
* ---------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------- */
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|
@ -85,19 +67,19 @@ InitPools() {
|
||||||
for (i = 0; i < DOH_MAX_FRAG; i++) {
|
for (i = 0; i < DOH_MAX_FRAG; i++) {
|
||||||
FreeFragments[i] = 0;
|
FreeFragments[i] = 0;
|
||||||
}
|
}
|
||||||
Pools = CreatePool(_PoolSize); /* Create initial pool */
|
Pools = CreatePool(PoolSize); /* Create initial pool */
|
||||||
pools_initialized = 1;
|
pools_initialized = 1;
|
||||||
DohNone = NewVoid(0,0);
|
DohNone = NewVoid(0,0); /* Create the None object */
|
||||||
DohIntern(DohNone);
|
DohIntern(DohNone);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
* DohCheck()
|
* DohCheck()
|
||||||
*
|
*
|
||||||
* Returns 1 if an arbitrary pointer is a DOH object. This determination
|
* Returns 1 if an arbitrary pointer is a DOH object.
|
||||||
* is made according to the pointer value only.
|
|
||||||
* ---------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/* Possible optimization: Reorder pools to move last match to the beginning */
|
||||||
int
|
int
|
||||||
DohCheck(const DOH *ptr) {
|
DohCheck(const DOH *ptr) {
|
||||||
Pool *p = Pools;
|
Pool *p = Pools;
|
||||||
|
|
@ -109,28 +91,6 @@ DohCheck(const DOH *ptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
|
||||||
* DohObjFreeCheck()
|
|
||||||
*
|
|
||||||
* Checks to see if an object was already deleted. Useful when tracking
|
|
||||||
* down nasty double-free problems.
|
|
||||||
* ---------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
int
|
|
||||||
DohObjFreeCheck(DOH *ptr) {
|
|
||||||
int i;
|
|
||||||
Fragment *f;
|
|
||||||
char *cptr = (char *) ptr;
|
|
||||||
for (i = 0; i < DOH_MAX_FRAG; i++) {
|
|
||||||
f = FreeFragments[i];
|
|
||||||
while (f) {
|
|
||||||
if (f->ptr == cptr) return 1;
|
|
||||||
f = f->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
* DohObjMalloc()
|
* DohObjMalloc()
|
||||||
*
|
*
|
||||||
|
|
@ -140,9 +100,8 @@ DohObjFreeCheck(DOH *ptr) {
|
||||||
void *
|
void *
|
||||||
DohObjMalloc(size_t size) {
|
DohObjMalloc(size_t size) {
|
||||||
Pool *p;
|
Pool *p;
|
||||||
Fragment *f;
|
DohBase *f;
|
||||||
void *ptr = 0;
|
void *ptr = 0;
|
||||||
int garbage_collected = 0;
|
|
||||||
|
|
||||||
if (size > DOH_MAX_FRAG) return 0;
|
if (size > DOH_MAX_FRAG) return 0;
|
||||||
if (!pools_initialized) InitPools();
|
if (!pools_initialized) InitPools();
|
||||||
|
|
@ -150,18 +109,11 @@ DohObjMalloc(size_t size) {
|
||||||
/* adjust the size for double word alignment */
|
/* adjust the size for double word alignment */
|
||||||
size = (size + 7) & ~0x07;
|
size = (size + 7) & ~0x07;
|
||||||
|
|
||||||
obj_total_allocated += size;
|
|
||||||
obj_ntotal_allocated++;
|
|
||||||
|
|
||||||
retry:
|
|
||||||
p = Pools;
|
p = Pools;
|
||||||
f = FreeFragments[size];
|
f = FreeFragments[size];
|
||||||
if (f) {
|
if (f) {
|
||||||
ptr = (void *) f->ptr;
|
ptr = (void *) f;
|
||||||
FreeFragments[size] = f->next;
|
FreeFragments[size] = (DohBase *) f->file;
|
||||||
num_fragments--;
|
|
||||||
fragment_size -= f->len;
|
|
||||||
DohFree(f);
|
|
||||||
DohInit(ptr);
|
DohInit(ptr);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
@ -169,39 +121,25 @@ DohObjMalloc(size_t size) {
|
||||||
/* No free fragments. See if the pool is large enough */
|
/* No free fragments. See if the pool is large enough */
|
||||||
if ((int) size < (p->len - p->current)) {
|
if ((int) size < (p->len - p->current)) {
|
||||||
ptr = (void *) (p->ptr + p->current);
|
ptr = (void *) (p->ptr + p->current);
|
||||||
/* p->current = (p->current + size + 7) & ~0x3; */
|
|
||||||
p->current = p->current + size;
|
p->current = p->current + size;
|
||||||
DohInit(ptr);
|
DohInit(ptr);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
PoolSize *= 2;
|
||||||
/* Pool is not large enough. Create a new pool */
|
p = CreatePool(PoolSize);
|
||||||
if (p->len - p->current > 0) {
|
|
||||||
f = (Fragment *) DohMalloc(sizeof(Fragment));
|
|
||||||
f->ptr = (p->ptr + p->current);
|
|
||||||
f->len = (p->len - p->current);
|
|
||||||
f->next = FreeFragments[f->len];
|
|
||||||
p->current = p->len;
|
|
||||||
FreeFragments[f->len] = f;
|
|
||||||
num_fragments++;
|
|
||||||
fragment_size += f->len;
|
|
||||||
}
|
|
||||||
p = CreatePool(_PoolSize);
|
|
||||||
p->next = Pools;
|
p->next = Pools;
|
||||||
Pools = p;
|
Pools = p;
|
||||||
return DohObjMalloc(size);
|
return DohObjMalloc(size);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ----------------------------------------------------------------------
|
/* ----------------------------------------------------------------------
|
||||||
* DohObjFree()
|
* DohObjFree() - Free a DOH object
|
||||||
*
|
|
||||||
* Frees a DOH object. Doesn't do much with GC.
|
|
||||||
* ---------------------------------------------------------------------- */
|
* ---------------------------------------------------------------------- */
|
||||||
|
|
||||||
void
|
void
|
||||||
DohObjFree(DOH *ptr) {
|
DohObjFree(DOH *ptr) {
|
||||||
Fragment *f;
|
|
||||||
DohBase *b;
|
DohBase *b;
|
||||||
|
int len;
|
||||||
if (!DohCheck(ptr)) {
|
if (!DohCheck(ptr)) {
|
||||||
DohTrace(DOH_MEMORY,"DohObjFree. %x not a DOH object!\n", ptr);
|
DohTrace(DOH_MEMORY,"DohObjFree. %x not a DOH object!\n", ptr);
|
||||||
return; /* Oh well. Guess we're leaky */
|
return; /* Oh well. Guess we're leaky */
|
||||||
|
|
@ -212,34 +150,24 @@ DohObjFree(DOH *ptr) {
|
||||||
return; /* Improperly initialized object. leak some more */
|
return; /* Improperly initialized object. leak some more */
|
||||||
}
|
}
|
||||||
if (b->file) DohDelete(b->file);
|
if (b->file) DohDelete(b->file);
|
||||||
f = (Fragment *) DohMalloc(sizeof(Fragment));
|
len = (b->objinfo->objsize + 7) & ~0x07;
|
||||||
f->ptr = (char *) ptr;
|
b->file = (DOH *) FreeFragments[len];
|
||||||
f->len = (b->objinfo->objsize + 7) & ~0x07;
|
FreeFragments[len] = b;
|
||||||
f->next = FreeFragments[f->len];
|
|
||||||
FreeFragments[f->len] = f;
|
|
||||||
num_fragments++;
|
|
||||||
fragment_size += f->len;
|
|
||||||
obj_total_allocated -= f->len;
|
|
||||||
obj_ntotal_allocated--;
|
|
||||||
b->objinfo = 0;
|
b->objinfo = 0;
|
||||||
|
b->flags = b->flags | DOH_FLAG_DELETED;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
* DohMalloc()
|
* DohMalloc() - Wrapper around malloc()
|
||||||
*
|
|
||||||
* Wrapper around malloc() function. Records memory usage.
|
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
void *
|
void *
|
||||||
DohMalloc(size_t nbytes) {
|
DohMalloc(size_t nbytes) {
|
||||||
data_total_allocated += nbytes;
|
|
||||||
return (void *) malloc(nbytes);
|
return (void *) malloc(nbytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
* DohRealloc()
|
* DohRealloc() - Wrapper around realloc()
|
||||||
*
|
|
||||||
* Wrapper around realloc() function.
|
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
void *
|
void *
|
||||||
|
|
@ -248,50 +176,10 @@ DohRealloc(void *ptr, size_t newsize) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
* DohFree()
|
* DohFree() - Wrapper around free()
|
||||||
*
|
|
||||||
* Wrapper around free() function.
|
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
void
|
void
|
||||||
DohFree(void *ptr) {
|
DohFree(void *ptr) {
|
||||||
free(ptr);
|
free(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
|
||||||
* DohPoolSize()
|
|
||||||
*
|
|
||||||
* Change the size of the memory pools used by DOH
|
|
||||||
* ----------------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
int
|
|
||||||
DohPoolSize(int poolsize) {
|
|
||||||
int ps;
|
|
||||||
ps = _PoolSize;
|
|
||||||
if (poolsize > 0) {
|
|
||||||
_PoolSize = poolsize;
|
|
||||||
}
|
|
||||||
return ps;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
|
||||||
* DohMemoryInfo()
|
|
||||||
*
|
|
||||||
* Print memory information
|
|
||||||
* ----------------------------------------------------------------------------- */
|
|
||||||
|
|
||||||
void
|
|
||||||
DohMemoryInfo() {
|
|
||||||
|
|
||||||
fprintf(stderr,"DOH Memory Use\n");
|
|
||||||
fprintf(stderr," Num free fragments : %d (%d bytes)\n", num_fragments, fragment_size);
|
|
||||||
fprintf(stderr," Obj total allocate : %d (%d bytes)\n", obj_ntotal_allocated, obj_total_allocated);
|
|
||||||
fprintf(stderr," Data allocate : %d\n", data_total_allocated);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ long String_tell(DOH *s);
|
||||||
int String_replace(DOH *str, DOH *token, DOH *rep, int flags);
|
int String_replace(DOH *str, DOH *token, DOH *rep, int flags);
|
||||||
void String_chop(DOH *str);
|
void String_chop(DOH *str);
|
||||||
|
|
||||||
static DohSequenceMethods StringSeqMethods = {
|
static DohListMethods StringListMethods = {
|
||||||
0, /* doh_getitem */
|
0, /* doh_getitem */
|
||||||
0, /* doh_setitem */
|
0, /* doh_setitem */
|
||||||
String_delitem, /* doh_delitem */
|
String_delitem, /* doh_delitem */
|
||||||
|
|
@ -91,7 +91,7 @@ static DohObjInfo StringType = {
|
||||||
String_hash, /* doh_hash */
|
String_hash, /* doh_hash */
|
||||||
String_cmp, /* doh_cmp */
|
String_cmp, /* doh_cmp */
|
||||||
0, /* doh_mapping */
|
0, /* doh_mapping */
|
||||||
&StringSeqMethods, /* doh_sequence */
|
&StringListMethods, /* doh_sequence */
|
||||||
&StringFileMethods,/* doh_file */
|
&StringFileMethods,/* doh_file */
|
||||||
&StringStringMethods, /* doh_string */
|
&StringStringMethods, /* doh_string */
|
||||||
0, /* doh_callable */
|
0, /* doh_callable */
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
* void.c
|
* void.c
|
||||||
*
|
*
|
||||||
* Implements a "void" object that is really just a DOH container around
|
* Implements a "void" object that is really just a DOH container around
|
||||||
* an arbitrary C/C++ object represented as a void *.
|
* an arbitrary C object represented as a void *.
|
||||||
*
|
*
|
||||||
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
|
* Author(s) : David Beazley (beazley@cs.uchicago.edu)
|
||||||
*
|
*
|
||||||
|
|
@ -92,8 +92,7 @@ static DohObjInfo DohVoidType = {
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
* NewVoid()
|
* NewVoid()
|
||||||
*
|
*
|
||||||
* Creates a new Void object given a void * and a pointer to an optional
|
* Creates a new Void object given a void * and an optional destructor function.
|
||||||
* destruction function.
|
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
DOH *
|
DOH *
|
||||||
|
|
|
||||||
|
|
@ -49,16 +49,17 @@ typedef void DOH;
|
||||||
* These structures define the interface to various categories of objects.
|
* These structures define the interface to various categories of objects.
|
||||||
* ----------------------------------------------------------------------------- */
|
* ----------------------------------------------------------------------------- */
|
||||||
|
|
||||||
/* Mapping Objects (i.e., hash tables) */
|
/* Hash objects */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
DOH *(*doh_getattr)(DOH *obj, DOH *name); /* Get attribute */
|
DOH *(*doh_getattr)(DOH *obj, DOH *name); /* Get attribute */
|
||||||
int (*doh_setattr)(DOH *obj, DOH *name, DOH *value); /* Set attribute */
|
int (*doh_setattr)(DOH *obj, DOH *name, DOH *value); /* Set attribute */
|
||||||
int (*doh_delattr)(DOH *obj, DOH *name); /* Del attribute */
|
int (*doh_delattr)(DOH *obj, DOH *name); /* Del attribute */
|
||||||
DOH *(*doh_firstkey)(DOH *obj); /* First key */
|
DOH *(*doh_firstkey)(DOH *obj); /* First key */
|
||||||
DOH *(*doh_nextkey)(DOH *obj); /* Next key */
|
DOH *(*doh_nextkey)(DOH *obj); /* Next key */
|
||||||
} DohMappingMethods;
|
} DohHashMethods;
|
||||||
|
|
||||||
|
/* List objects */
|
||||||
|
|
||||||
/* Sequence methods (i.e., lists) */
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
DOH *(*doh_getitem)(DOH *obj, int index); /* Get item */
|
DOH *(*doh_getitem)(DOH *obj, int index); /* Get item */
|
||||||
int (*doh_setitem)(DOH *obj, int index, DOH *value); /* Set item */
|
int (*doh_setitem)(DOH *obj, int index, DOH *value); /* Set item */
|
||||||
|
|
@ -66,7 +67,7 @@ typedef struct {
|
||||||
int (*doh_insitem)(DOH *obj, int index, DOH *value); /* Insert item */
|
int (*doh_insitem)(DOH *obj, int index, DOH *value); /* Insert item */
|
||||||
DOH *(*doh_firstitem)(DOH *obj); /* Iterators */
|
DOH *(*doh_firstitem)(DOH *obj); /* Iterators */
|
||||||
DOH *(*doh_nextitem)(DOH *obj);
|
DOH *(*doh_nextitem)(DOH *obj);
|
||||||
} DohSequenceMethods;
|
} DohListMethods;
|
||||||
|
|
||||||
/* File methods */
|
/* File methods */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
@ -110,13 +111,13 @@ typedef struct DohObjInfo {
|
||||||
|
|
||||||
/* Length and hash values */
|
/* Length and hash values */
|
||||||
int (*doh_len)(DOH *obj);
|
int (*doh_len)(DOH *obj);
|
||||||
int (*doh_hash)(DOH *obj);
|
int (*doh_hashval)(DOH *obj);
|
||||||
|
|
||||||
/* Compare */
|
/* Compare */
|
||||||
int (*doh_cmp)(DOH *obj1, DOH *obj2);
|
int (*doh_cmp)(DOH *obj1, DOH *obj2);
|
||||||
|
|
||||||
DohMappingMethods *doh_mapping; /* Mapping methods */
|
DohHashMethods *doh_hash; /* Mapping methods */
|
||||||
DohSequenceMethods *doh_sequence; /* Sequence methods */
|
DohListMethods *doh_list; /* List methods */
|
||||||
DohFileMethods *doh_file; /* File methods */
|
DohFileMethods *doh_file; /* File methods */
|
||||||
DohStringMethods *doh_string; /* String methods */
|
DohStringMethods *doh_string; /* String methods */
|
||||||
void *reserved2;
|
void *reserved2;
|
||||||
|
|
@ -137,12 +138,9 @@ typedef struct DohObjInfo {
|
||||||
extern void DohFree(DOH *ptr); /* Free memory */
|
extern void DohFree(DOH *ptr); /* Free memory */
|
||||||
extern void *DohObjMalloc(size_t size); /* Allocate a DOH object */
|
extern void *DohObjMalloc(size_t size); /* Allocate a DOH object */
|
||||||
extern void DohObjFree(DOH *ptr); /* Free a DOH object */
|
extern void DohObjFree(DOH *ptr); /* Free a DOH object */
|
||||||
extern int DohObjFreeCheck(DOH *ptr); /* Check if already free */
|
|
||||||
extern void DohInit(DOH *obj); /* Initialize an object */
|
extern void DohInit(DOH *obj); /* Initialize an object */
|
||||||
extern int DohCheck(const DOH *ptr); /* Check if a DOH object */
|
extern int DohCheck(const DOH *ptr); /* Check if a DOH object */
|
||||||
extern int DohPoolSize(int); /* Set memory alloc size */
|
|
||||||
extern void DohIntern(DOH *); /* Intern an object */
|
extern void DohIntern(DOH *); /* Intern an object */
|
||||||
extern void DohMemoryInfo();
|
|
||||||
|
|
||||||
/* Basic object methods. Common to most objects */
|
/* Basic object methods. Common to most objects */
|
||||||
|
|
||||||
|
|
@ -280,7 +278,6 @@ typedef struct DohObjInfo {
|
||||||
|
|
||||||
#define DOHCOMMON \
|
#define DOHCOMMON \
|
||||||
DohObjInfo *objinfo; \
|
DohObjInfo *objinfo; \
|
||||||
DOH *nextptr; \
|
|
||||||
int refcount; \
|
int refcount; \
|
||||||
DOH *file; \
|
DOH *file; \
|
||||||
int line; \
|
int line; \
|
||||||
|
|
@ -299,6 +296,7 @@ typedef struct {
|
||||||
/* Flags for various internal operations */
|
/* Flags for various internal operations */
|
||||||
|
|
||||||
#define DOH_FLAG_PRINT 0x02
|
#define DOH_FLAG_PRINT 0x02
|
||||||
|
#define DOH_FLAG_DELETED 0x04
|
||||||
#define DOH_FLAG_INTERN 0x10
|
#define DOH_FLAG_INTERN 0x10
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------
|
/* -----------------------------------------------------------------------------
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue