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:
Dave Beazley 2000-06-06 18:04:18 +00:00
commit 2a1fc5ca87
7 changed files with 67 additions and 184 deletions

View file

@ -28,7 +28,7 @@ static DohObjInfo DohBaseType = {
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_mapping */
0, /* doh_sequence */
0, /* doh_list */
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
@ -229,7 +229,7 @@ DohHashval(const DOH *obj) {
DohTrace(DOH_CALLS,"DohHashval %x\n",obj);
if (DohCheck(b)) {
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);
} else {
@ -298,7 +298,7 @@ int
DohIsMapping(const DOH *obj) {
DohBase *b = (DohBase *) obj;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_mapping) return 1;
if (b->objinfo->doh_hash) return 1;
else return 0;
}
@ -313,8 +313,8 @@ DohGetattr(DOH *obj, const DOH *name) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohGetattr %x, %x\n",obj,name);
if (DohIsMapping(b)) {
if (b->objinfo->doh_mapping->doh_getattr) {
return (b->objinfo->doh_mapping->doh_getattr)(b,(DOH *) name);
if (b->objinfo->doh_hash->doh_getattr) {
return (b->objinfo->doh_hash->doh_getattr)(b,(DOH *) name);
}
}
if (DohCheck(b)) {
@ -336,8 +336,8 @@ DohSetattr(DOH *obj, const DOH *name, const DOH *value) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohSetattr %x, %x, %x\n",obj,name, value);
if (DohIsMapping(b)) {
if (b->objinfo->doh_mapping->doh_setattr) {
return (b->objinfo->doh_mapping->doh_setattr)(b,(DOH *) name,(DOH *) value);
if (b->objinfo->doh_hash->doh_setattr) {
return (b->objinfo->doh_hash->doh_setattr)(b,(DOH *) name,(DOH *) value);
}
}
if (DohCheck(b)) {
@ -359,8 +359,8 @@ DohDelattr(DOH *obj, const DOH *name) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohDelattr %x, %x\n",obj,name);
if (DohIsMapping(obj)) {
if (b->objinfo->doh_mapping->doh_delattr) {
(b->objinfo->doh_mapping->doh_delattr)(b,(DOH *) name);
if (b->objinfo->doh_hash->doh_delattr) {
(b->objinfo->doh_hash->doh_delattr)(b,(DOH *) name);
return;
}
}
@ -382,8 +382,8 @@ DohFirstkey(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohFirstkey %x\n",obj);
if (DohIsMapping(obj)) {
if (b->objinfo->doh_mapping->doh_firstkey) {
return (b->objinfo->doh_mapping->doh_firstkey)(obj);
if (b->objinfo->doh_hash->doh_firstkey) {
return (b->objinfo->doh_hash->doh_firstkey)(obj);
}
}
if (DohCheck(obj)) {
@ -405,8 +405,8 @@ DohNextkey(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohNextkey %x\n",obj);
if (DohIsMapping(obj)) {
if (b->objinfo->doh_mapping->doh_nextkey) {
return (b->objinfo->doh_mapping->doh_nextkey)(obj);
if (b->objinfo->doh_hash->doh_nextkey) {
return (b->objinfo->doh_hash->doh_nextkey)(obj);
}
}
if (DohCheck(obj)) {
@ -558,7 +558,7 @@ int
DohIsSequence(const DOH *obj) {
DohBase *b = (DohBase *) obj;
if (!DohCheck(b)) return 0;
if (b->objinfo->doh_sequence) return 1;
if (b->objinfo->doh_list) return 1;
else return 0;
}
@ -573,8 +573,8 @@ DohGetitem(DOH *obj, int index) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohGetitem %x, %d\n",obj,index);
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_getitem) {
return (b->objinfo->doh_sequence->doh_getitem)(obj,index);
if (b->objinfo->doh_list->doh_getitem) {
return (b->objinfo->doh_list->doh_getitem)(obj,index);
}
}
if (DohCheck(obj)) {
@ -596,8 +596,8 @@ DohSetitem(DOH *obj, int index, const DOH *value) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohSetitem %x, %d, %x\n",obj,index, value);
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_setitem) {
return (b->objinfo->doh_sequence->doh_setitem)(obj,index,(DOH *) value);
if (b->objinfo->doh_list->doh_setitem) {
return (b->objinfo->doh_list->doh_setitem)(obj,index,(DOH *) value);
}
}
if (DohCheck(obj)) {
@ -618,8 +618,8 @@ DohDelitem(DOH *obj, int index) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohDelitem %x, %d\n",obj,index);
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_delitem) {
return (b->objinfo->doh_sequence->doh_delitem)(obj,index);
if (b->objinfo->doh_list->doh_delitem) {
return (b->objinfo->doh_list->doh_delitem)(obj,index);
}
}
if (DohCheck(obj)) {
@ -641,8 +641,8 @@ DohInsertitem(DOH *obj, int index, const DOH *value) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohInsertitem %x, %d, %x\n",obj,index, value);
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_insitem) {
return (b->objinfo->doh_sequence->doh_insitem)(obj,index,(DOH *) value);
if (b->objinfo->doh_list->doh_insitem) {
return (b->objinfo->doh_list->doh_insitem)(obj,index,(DOH *) value);
}
}
if (DohCheck(obj)) {
@ -663,8 +663,8 @@ DohFirstitem(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohFirstitem %x\n");
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_firstitem) {
return (b->objinfo->doh_sequence->doh_firstitem)(obj);
if (b->objinfo->doh_list->doh_firstitem) {
return (b->objinfo->doh_list->doh_firstitem)(obj);
}
}
if (DohCheck(obj)) {
@ -686,8 +686,8 @@ DohNextitem(DOH *obj) {
DohBase *b = (DohBase *) obj;
DohTrace(DOH_CALLS,"DohNextitem %x\n");
if (DohIsSequence(obj)) {
if (b->objinfo->doh_sequence->doh_nextitem) {
return (b->objinfo->doh_sequence->doh_nextitem)(obj);
if (b->objinfo->doh_list->doh_nextitem) {
return (b->objinfo->doh_list->doh_nextitem)(obj);
}
}
if (DohCheck(obj)) {
@ -986,7 +986,7 @@ DohInit(DOH *b) {
* Set file location (default method).
* ----------------------------------------------------------------------------- */
void
DohSetFile(DOH *ho, DOH *file) {
DohSetfile(DOH *ho, DOH *file) {
DohBase *h = (DohBase *) ho;
if (DohCheck(h)) {
if (!DohCheck(file)) file = NewString(file);

View file

@ -459,7 +459,7 @@ CopyHash(DOH *ho) {
* type information
* ----------------------------------------------------------------------------- */
static DohMappingMethods HashMappingMethods = {
static DohHashMethods HashHashMethods = {
Hash_getattr,
Hash_setattr,
Hash_delattr,
@ -480,7 +480,7 @@ static DohObjInfo HashType = {
Hash_len, /* doh_len */
0, /* doh_hash */
0, /* doh_cmp */
&HashMappingMethods, /* doh_mapping */
&HashHashMethods, /* doh_mapping */
0, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */

View file

@ -44,7 +44,6 @@ void more(List *l) {
*
* Make a shallow copy of a list.
* ----------------------------------------------------------------------------- */
static DOH *
CopyList(DOH *lo) {
List *l,*nl;
@ -301,7 +300,7 @@ List_dump(DOH *lo, DOH *out) {
#define MAXLISTITEMS 8
static DohSequenceMethods ListSeqMethods = {
static DohListMethods ListListMethods = {
List_get,
List_set,
List_remove,
@ -324,7 +323,7 @@ static DohObjInfo ListType = {
0, /* doh_hash */
0, /* doh_cmp */
0, /* doh_mapping */
&ListSeqMethods, /* doh_sequence */
&ListListMethods, /* doh_sequence */
0, /* doh_file */
0, /* doh_string */
0, /* doh_callable */
@ -357,7 +356,6 @@ NewList() {
int i;
l = (List *) DohObjMalloc(sizeof(List));
l->objinfo = &ListType;
DohXInit(l);
l->nitems = 0;
l->maxitems = MAXLISTITEMS;
l->items = (void **) DohMalloc(l->maxitems*sizeof(void *));

View file

@ -2,7 +2,7 @@
* memory.c
*
* 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)
*
@ -22,24 +22,11 @@ static char cvsroot[] = "$Header$";
#define DOH_MAX_FRAG 1024
#endif
static int _DohMemoryCurrent = 0;
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;
static int PoolSize = DOH_POOL_SIZE;
DOH *DohNone = 0; /* The DOH None object */
typedef struct fragment {
char *ptr; /* Pointer to fragment */
int len; /* Length of fragment */
struct fragment *next; /* Next fragment */
} Fragment;
static Fragment *FreeFragments[DOH_MAX_FRAG];
static DohBase *FreeFragments[DOH_MAX_FRAG]; /* Array of free memory fragments */
typedef struct pool {
char *ptr; /* Start of pool */
@ -52,9 +39,7 @@ static Pool *Pools = 0;
static int pools_initialized = 0;
/* ----------------------------------------------------------------------
* CreatePool()
*
* Create a new memory pool
* CreatePool() - Create a new memory pool
* ---------------------------------------------------------------------- */
static Pool *
@ -63,7 +48,6 @@ CreatePool(int size) {
char *c;
c = (char *) DohMalloc(size);
if (!c) return 0;
p = (Pool *) DohMalloc(sizeof(Pool));
p->ptr = c;
p->len = size;
@ -73,9 +57,7 @@ CreatePool(int size) {
}
/* ----------------------------------------------------------------------
* InitPools()
*
* Initialize the memory allocator
* InitPools() - Initialize the memory allocator
* ---------------------------------------------------------------------- */
static void
@ -85,19 +67,19 @@ InitPools() {
for (i = 0; i < DOH_MAX_FRAG; i++) {
FreeFragments[i] = 0;
}
Pools = CreatePool(_PoolSize); /* Create initial pool */
Pools = CreatePool(PoolSize); /* Create initial pool */
pools_initialized = 1;
DohNone = NewVoid(0,0);
DohNone = NewVoid(0,0); /* Create the None object */
DohIntern(DohNone);
}
/* ----------------------------------------------------------------------
* DohCheck()
*
* Returns 1 if an arbitrary pointer is a DOH object. This determination
* is made according to the pointer value only.
* Returns 1 if an arbitrary pointer is a DOH object.
* ---------------------------------------------------------------------- */
/* Possible optimization: Reorder pools to move last match to the beginning */
int
DohCheck(const DOH *ptr) {
Pool *p = Pools;
@ -109,28 +91,6 @@ DohCheck(const DOH *ptr) {
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()
*
@ -140,9 +100,8 @@ DohObjFreeCheck(DOH *ptr) {
void *
DohObjMalloc(size_t size) {
Pool *p;
Fragment *f;
DohBase *f;
void *ptr = 0;
int garbage_collected = 0;
if (size > DOH_MAX_FRAG) return 0;
if (!pools_initialized) InitPools();
@ -150,18 +109,11 @@ DohObjMalloc(size_t size) {
/* adjust the size for double word alignment */
size = (size + 7) & ~0x07;
obj_total_allocated += size;
obj_ntotal_allocated++;
retry:
p = Pools;
f = FreeFragments[size];
if (f) {
ptr = (void *) f->ptr;
FreeFragments[size] = f->next;
num_fragments--;
fragment_size -= f->len;
DohFree(f);
ptr = (void *) f;
FreeFragments[size] = (DohBase *) f->file;
DohInit(ptr);
return ptr;
}
@ -169,39 +121,25 @@ DohObjMalloc(size_t size) {
/* No free fragments. See if the pool is large enough */
if ((int) size < (p->len - p->current)) {
ptr = (void *) (p->ptr + p->current);
/* p->current = (p->current + size + 7) & ~0x3; */
p->current = p->current + size;
DohInit(ptr);
return ptr;
}
/* Pool is not large enough. Create a new pool */
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);
PoolSize *= 2;
p = CreatePool(PoolSize);
p->next = Pools;
Pools = p;
return DohObjMalloc(size);
}
/* ----------------------------------------------------------------------
* DohObjFree()
*
* Frees a DOH object. Doesn't do much with GC.
* DohObjFree() - Free a DOH object
* ---------------------------------------------------------------------- */
void
DohObjFree(DOH *ptr) {
Fragment *f;
DohBase *b;
int len;
if (!DohCheck(ptr)) {
DohTrace(DOH_MEMORY,"DohObjFree. %x not a DOH object!\n", ptr);
return; /* Oh well. Guess we're leaky */
@ -212,34 +150,24 @@ DohObjFree(DOH *ptr) {
return; /* Improperly initialized object. leak some more */
}
if (b->file) DohDelete(b->file);
f = (Fragment *) DohMalloc(sizeof(Fragment));
f->ptr = (char *) ptr;
f->len = (b->objinfo->objsize + 7) & ~0x07;
f->next = FreeFragments[f->len];
FreeFragments[f->len] = f;
num_fragments++;
fragment_size += f->len;
obj_total_allocated -= f->len;
obj_ntotal_allocated--;
len = (b->objinfo->objsize + 7) & ~0x07;
b->file = (DOH *) FreeFragments[len];
FreeFragments[len] = b;
b->objinfo = 0;
b->flags = b->flags | DOH_FLAG_DELETED;
}
/* -----------------------------------------------------------------------------
* DohMalloc()
*
* Wrapper around malloc() function. Records memory usage.
* DohMalloc() - Wrapper around malloc()
* ----------------------------------------------------------------------------- */
void *
DohMalloc(size_t nbytes) {
data_total_allocated += nbytes;
return (void *) malloc(nbytes);
}
/* -----------------------------------------------------------------------------
* DohRealloc()
*
* Wrapper around realloc() function.
* DohRealloc() - Wrapper around realloc()
* ----------------------------------------------------------------------------- */
void *
@ -248,50 +176,10 @@ DohRealloc(void *ptr, size_t newsize) {
}
/* -----------------------------------------------------------------------------
* DohFree()
*
* Wrapper around free() function.
* DohFree() - Wrapper around free()
* ----------------------------------------------------------------------------- */
void
DohFree(void *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);
}

View file

@ -52,7 +52,7 @@ long String_tell(DOH *s);
int String_replace(DOH *str, DOH *token, DOH *rep, int flags);
void String_chop(DOH *str);
static DohSequenceMethods StringSeqMethods = {
static DohListMethods StringListMethods = {
0, /* doh_getitem */
0, /* doh_setitem */
String_delitem, /* doh_delitem */
@ -91,7 +91,7 @@ static DohObjInfo StringType = {
String_hash, /* doh_hash */
String_cmp, /* doh_cmp */
0, /* doh_mapping */
&StringSeqMethods, /* doh_sequence */
&StringListMethods, /* doh_sequence */
&StringFileMethods,/* doh_file */
&StringStringMethods, /* doh_string */
0, /* doh_callable */

View file

@ -2,7 +2,7 @@
* void.c
*
* 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)
*
@ -92,8 +92,7 @@ static DohObjInfo DohVoidType = {
/* -----------------------------------------------------------------------------
* NewVoid()
*
* Creates a new Void object given a void * and a pointer to an optional
* destruction function.
* Creates a new Void object given a void * and an optional destructor function.
* ----------------------------------------------------------------------------- */
DOH *

View file

@ -49,16 +49,17 @@ typedef void DOH;
* These structures define the interface to various categories of objects.
* ----------------------------------------------------------------------------- */
/* Mapping Objects (i.e., hash tables) */
/* Hash objects */
typedef struct {
DOH *(*doh_getattr)(DOH *obj, DOH *name); /* Get attribute */
int (*doh_setattr)(DOH *obj, DOH *name, DOH *value); /* Set attribute */
int (*doh_delattr)(DOH *obj, DOH *name); /* Del attribute */
DOH *(*doh_firstkey)(DOH *obj); /* First key */
DOH *(*doh_nextkey)(DOH *obj); /* Next key */
} DohMappingMethods;
} DohHashMethods;
/* List objects */
/* Sequence methods (i.e., lists) */
typedef struct {
DOH *(*doh_getitem)(DOH *obj, int index); /* Get 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 */
DOH *(*doh_firstitem)(DOH *obj); /* Iterators */
DOH *(*doh_nextitem)(DOH *obj);
} DohSequenceMethods;
} DohListMethods;
/* File methods */
typedef struct {
@ -110,13 +111,13 @@ typedef struct DohObjInfo {
/* Length and hash values */
int (*doh_len)(DOH *obj);
int (*doh_hash)(DOH *obj);
int (*doh_hashval)(DOH *obj);
/* Compare */
int (*doh_cmp)(DOH *obj1, DOH *obj2);
DohMappingMethods *doh_mapping; /* Mapping methods */
DohSequenceMethods *doh_sequence; /* Sequence methods */
DohHashMethods *doh_hash; /* Mapping methods */
DohListMethods *doh_list; /* List methods */
DohFileMethods *doh_file; /* File methods */
DohStringMethods *doh_string; /* String methods */
void *reserved2;
@ -137,12 +138,9 @@ typedef struct DohObjInfo {
extern void DohFree(DOH *ptr); /* Free memory */
extern void *DohObjMalloc(size_t size); /* Allocate 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 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 DohMemoryInfo();
/* Basic object methods. Common to most objects */
@ -280,7 +278,6 @@ typedef struct DohObjInfo {
#define DOHCOMMON \
DohObjInfo *objinfo; \
DOH *nextptr; \
int refcount; \
DOH *file; \
int line; \
@ -299,6 +296,7 @@ typedef struct {
/* Flags for various internal operations */
#define DOH_FLAG_PRINT 0x02
#define DOH_FLAG_DELETED 0x04
#define DOH_FLAG_INTERN 0x10
/* -----------------------------------------------------------------------------