From 90c985c6b4e8f753220a1825dc32e4d007e50445 Mon Sep 17 00:00:00 2001 From: Dave Beazley Date: Wed, 5 Apr 2000 22:46:46 +0000 Subject: [PATCH] Removed scope stuff. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@389 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- SWIG/Source/DOH/Doh/base.c | 24 +---- SWIG/Source/DOH/Doh/hash.c | 36 +------ SWIG/Source/DOH/Doh/list.c | 34 +----- SWIG/Source/DOH/Doh/memory.c | 193 +++++++--------------------------- SWIG/Source/DOH/Doh/string.c | 2 +- SWIG/Source/DOH/Include/doh.h | 19 +--- 6 files changed, 52 insertions(+), 256 deletions(-) diff --git a/SWIG/Source/DOH/Doh/base.c b/SWIG/Source/DOH/Doh/base.c index dc1b8b9e0..64957cf26 100644 --- a/SWIG/Source/DOH/Doh/base.c +++ b/SWIG/Source/DOH/Doh/base.c @@ -84,9 +84,10 @@ DohDelete(DOH *obj) { DohBase *b = (DohBase *) obj; DohTrace(DOH_CALLS,"DohDelete %x\n",obj); if (!DohCheck(b)) return; + assert(b->objinfo); if (b->flags & DOH_FLAG_INTERN) return; b->refcount--; - if (b->refcount == 0) { + if (b->refcount <= 0) { if (b->objinfo->doh_del) (b->objinfo->doh_del)(obj); } } @@ -145,27 +146,6 @@ DohClear(DOH *obj) { DohTrace(DOH_UNSUPPORTED, "No clear method defined for type '%s'\n", b->objinfo->objname); } -/* ----------------------------------------------------------------------------- - * DohSetScope() - * - * Manually change the scope level of an object. - * ----------------------------------------------------------------------------- */ - -void -DohSetScope(DOH *obj, int s) { - DohBase *b = (DohBase *) obj; - DohTrace(DOH_CALLS,"DohScope %x\n",obj); - if (!DohCheck(b)) { - DohTrace(DOH_UNKNOWN,"Unknown object %x passed to Scope.\n",obj); - return; - } - if (b->objinfo->doh_scope) { - (b->objinfo->doh_scope)(obj,s); - return; - } - if (s < b->scope) b->scope = s; -} - /* ----------------------------------------------------------------------------- * DohStr() * diff --git a/SWIG/Source/DOH/Doh/hash.c b/SWIG/Source/DOH/Doh/hash.c index 36f1591b6..582419285 100644 --- a/SWIG/Source/DOH/Doh/hash.c +++ b/SWIG/Source/DOH/Doh/hash.c @@ -53,7 +53,7 @@ static DOH *find_key(char *c) { if (d < 0) r = r->left; else r = r->right; } - /* fprintf(stderr,"Interning '%s'\n", c); */ + fprintf(stderr,"Interning '%s'\n", c); r = (KeyValue *) DohMalloc(sizeof(KeyValue)); r->cstr = (char *) DohMalloc(strlen(c)+1); strcpy(r->cstr,c); @@ -144,35 +144,6 @@ Hash_clear(DOH *ho) { h->nitems = 0; } -/* ----------------------------------------------------------------------------- - * Hash_scope() - * - * Change the scope of the hash table. - * ----------------------------------------------------------------------------- */ - -static void -Hash_scope(DOH *ho, int s) { - Hash *h; - HashNode *n; - int i; - h = (Hash *) ho; - if (h->flags & DOH_FLAG_SETSCOPE) return; - if (s < h->scope) h->scope = s; - h->flags = h->flags | DOH_FLAG_SETSCOPE; - if (h->scope != s) { - for (i = 0; i < h->hashsize; i++) { - if ((n = h->hashtable[i])) { - while (n) { - Setscope(n->object,s); - Setscope(n->key,s); - n = n->next; - } - } - } - } - h->flags = h->flags & ~DOH_FLAG_SETSCOPE; -} - /* resize the hash table */ static void resize(Hash *h) { HashNode *n, *next, **table; @@ -235,6 +206,7 @@ Hash_setattr(DOH *ho, DOH *k, DOH *obj) { if (!DohCheck(k)) k = find_key(k); if (!DohCheck(obj)) { obj = NewString((char *) obj); + Decref(obj); } h = (Hash *) ho; hv = (Hashval(k)) % h->hashsize; @@ -259,8 +231,6 @@ Hash_setattr(DOH *ho, DOH *k, DOH *obj) { } /* Add this to the table */ n = NewNode(k,obj); - Setscope(n->key,h->scope); - Setscope(n->object,h->scope); if (prev) prev->next = n; else h->hashtable[hv] = n; h->nitems++; @@ -504,7 +474,7 @@ static DohObjInfo HashType = { DelHash, /* doh_del */ CopyHash, /* doh_copy */ Hash_clear, /* doh_clear */ - Hash_scope, /* doh_scope */ + 0, /* doh_scope */ Hash_str, /* doh_str */ 0, /* doh_data */ 0, /* doh_dump */ diff --git a/SWIG/Source/DOH/Doh/list.c b/SWIG/Source/DOH/Doh/list.c index cf91cd106..37da41718 100644 --- a/SWIG/Source/DOH/Doh/list.c +++ b/SWIG/Source/DOH/Doh/list.c @@ -108,28 +108,6 @@ List_clear(DOH *lo) { l->nitems = 0; } -/* ----------------------------------------------------------------------------- - * List_scope() - * - * Change the scope setting of the list. - * ----------------------------------------------------------------------------- */ - -static void -List_scope(DOH *lo, int s) { - List *l; - int i; - l = (List *) lo; - if (l->flags & DOH_FLAG_SETSCOPE) return; - l->flags = l->flags | DOH_FLAG_SETSCOPE; - if (s < l->scope) l->scope = (unsigned char) s; - if (s != l->scope) { - for (i = 0; i < l->nitems; i++) { - Setscope(l->items[i],s); - } - } - l->flags = l->flags & ~DOH_FLAG_SETSCOPE; -} - /* ----------------------------------------------------------------------------- * List_insert() * @@ -141,7 +119,7 @@ static int List_insert(DOH *lo, int pos, DOH *item) { List *l; DohBase *b; - int i, no = 0; + int i; if (!item) return -1; l = (List *) lo; @@ -149,7 +127,7 @@ List_insert(DOH *lo, int pos, DOH *item) { if (!DohCheck(item)) { DohTrace(DOH_CONVERSION,"Unknown object %x being converted to a string in List_insert.\n", item); item = NewString(item); - no = 1; + Decref(item); } b = (DohBase *) item; if (pos == DOH_END) pos = l->nitems; @@ -161,9 +139,7 @@ List_insert(DOH *lo, int pos, DOH *item) { } l->items[pos] = item; b->refcount++; - Setscope(b,l->scope); l->nitems++; - if (no) Delete(item); return 0; } @@ -230,7 +206,6 @@ List_get(DOH *lo, int n) { static int List_set(DOH *lo, int n, DOH *val) { List *l; - int no = 0; l = (List *) lo; if (!val) return -1; if ((n < 0) || (n >= l->nitems)) { @@ -240,12 +215,11 @@ List_set(DOH *lo, int n, DOH *val) { if (!DohCheck(val)) { DohTrace(DOH_CONVERSION,"Unknown object %x being converted to a string in List_setitem.\n", val); val = NewString(val); - no = 1; + Decref(val); } Delete(l->items[n]); l->items[n] = val; Incref(val); - Setscope(val,l->scope); Delete(val); return 0; } @@ -344,7 +318,7 @@ static DohObjInfo ListType = { DelList, /* doh_del */ CopyList, /* doh_copy */ List_clear, /* doh_clear */ - List_scope, /* doh_scope */ + 0, /* doh_scope */ List_str, /* doh_str */ 0, /* doh_data */ List_dump, /* doh_dump */ diff --git a/SWIG/Source/DOH/Doh/memory.c b/SWIG/Source/DOH/Doh/memory.c index 3d603e6bf..79b82ac57 100644 --- a/SWIG/Source/DOH/Doh/memory.c +++ b/SWIG/Source/DOH/Doh/memory.c @@ -22,13 +22,14 @@ static char cvsroot[] = "$Header$"; #define DOH_MAX_FRAG 1024 #endif -#ifndef DOH_MAX_SCOPES -#define DOH_MAX_SCOPES 256 -#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; DOH *DohNone = 0; /* The DOH None object */ @@ -49,9 +50,6 @@ typedef struct pool { static Pool *Pools = 0; static int pools_initialized = 0; -static DohBase *scopes[DOH_MAX_SCOPES]; -static int nscopes = 0; - /* ---------------------------------------------------------------------- * CreatePool() @@ -89,10 +87,6 @@ InitPools() { } Pools = CreatePool(_PoolSize); /* Create initial pool */ pools_initialized = 1; - for (i = 0; i < DOH_MAX_SCOPES; i++) { - scopes[i] = 0; - } - DohNewScope(); /* Initialize the scope system */ DohNone = NewVoid(0,0); DohIntern(DohNone); } @@ -137,130 +131,6 @@ DohObjFreeCheck(DOH *ptr) { return 0; } -/* ---------------------------------------------------------------------- - * DohNewScope() - * - * Create a new scope in which objects will be placed. Returns a scope - * identifier. - * ---------------------------------------------------------------------- */ -int -DohNewScope() { - assert(nscopes < DOH_MAX_SCOPES); - if (!pools_initialized) InitPools(); - scopes[nscopes] = 0; - nscopes++; - return nscopes - 1; -} - -/* ---------------------------------------------------------------------- - * DohDelScope() - * - * Deletes a scope. - * ---------------------------------------------------------------------- */ - -void -DohDelScope(int s) { - int ns; - DohBase *b, *b1; - ns = s; - assert((s >= 0) && (s < nscopes)); - s = nscopes - 1; - while (s >= ns) { - b = scopes[s]; - b1 = 0; - while (b) { - if (s <= b->scope) { - if (!(b->flags & DOH_FLAG_DELSCOPE)) { - Delete(b); - b->flags = b->flags | DOH_FLAG_DELSCOPE; - } - } - b1 = b; - b = (DohBase *) (b->nextptr); - } - if (ns > 0) { /* Add objects to highest non-deleted scope */ - if (b1) { - b1->nextptr = (DOH *) scopes[ns-1]; - scopes[ns-1] = (DohBase *) scopes[s]; - } - } - scopes[s] = 0; - s--; - } - nscopes = ns; -} - -/* ---------------------------------------------------------------------- - * real_objfree() - * - * This is the function that actually frees an object. Invoked by the - * garbage collector. - * ---------------------------------------------------------------------- */ - -static void -real_objfree(DOH *ptr) { - DohBase *b; - Fragment *f; - b = (DohBase *) ptr; - - if (!b->objinfo) { - DohTrace(DOH_MEMORY,"DohObjFree. %x not properly defined. No objinfo structure.\n", ptr); - return; /* Improperly initialized object. leak some more */ - } - 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; - -} - -/* ---------------------------------------------------------------------- - * DohGarbageCollect() - * - * This walks through all of the scopes and does garbage collection. - * - * 1. Objects with refcount <= 0 are released. - * 2. The scopes data structures are rebuilt and compacted. - * ---------------------------------------------------------------------- */ - -void -DohGarbageCollect() { - int s; - DohBase *b, *b1; - int ndeleted = 1; - - DohTrace(DOH_MEMORY,"Garbage collecting.\n"); - while (ndeleted) { - ndeleted = 0; - s = nscopes - 1; - while (s >= 0) { - b = scopes[s]; - b1 = 0; - while (b) { - if ((b->refcount <= 0)) { - if (b1) { - b1->nextptr = b->nextptr; - } else { - scopes[s] = b->nextptr; - } - if (!(b->flags & DOH_FLAG_INTERN)) { - assert(!(b->flags & DOH_FLAG_GC)); - real_objfree(b); /* Release the object */ - b->flags = b->flags | DOH_FLAG_GC; - } - ndeleted++; - } else { - b1 = b; - } - b = (DohBase *) b->nextptr; - } - s--; - } - } - DohTrace(DOH_MEMORY,"Done garbage collecting.\n"); -} - /* ---------------------------------------------------------------------- * DohObjMalloc() * @@ -279,20 +149,20 @@ 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); DohInit(ptr); - if (nscopes) { - ((DohBase *) ptr)->scope = nscopes-1; - ((DohBase *) ptr)->nextptr = scopes[nscopes-1]; - scopes[nscopes-1] = (DohBase *) ptr; - } return ptr; } @@ -302,20 +172,9 @@ DohObjMalloc(size_t size) { /* p->current = (p->current + size + 7) & ~0x3; */ p->current = p->current + size; DohInit(ptr); - if (nscopes) { - ((DohBase *) ptr)->scope = nscopes-1; - ((DohBase *) ptr)->nextptr = scopes[nscopes-1]; - scopes[nscopes-1] = (DohBase *) ptr; - } return ptr; } - if (!garbage_collected) { - garbage_collected = 1; - DohGarbageCollect(); - goto retry; - } - /* Pool is not large enough. Create a new pool */ if (p->len - p->current > 0) { f = (Fragment *) DohMalloc(sizeof(Fragment)); @@ -324,8 +183,9 @@ DohObjMalloc(size_t size) { 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; Pools = p; @@ -340,6 +200,7 @@ DohObjMalloc(size_t size) { void DohObjFree(DOH *ptr) { + Fragment *f; DohBase *b; if (!DohCheck(ptr)) { DohTrace(DOH_MEMORY,"DohObjFree. %x not a DOH object!\n", ptr); @@ -350,6 +211,16 @@ DohObjFree(DOH *ptr) { DohTrace(DOH_MEMORY,"DohObjFree. %x not properly defined. No objinfo structure.\n", ptr); return; /* Improperly initialized object. leak some more */ } + 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--; + b->objinfo = 0; } /* ----------------------------------------------------------------------------- @@ -360,6 +231,7 @@ DohObjFree(DOH *ptr) { void * DohMalloc(size_t nbytes) { + data_total_allocated += nbytes; return (void *) malloc(nbytes); } @@ -401,7 +273,20 @@ DohPoolSize(int 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); +} diff --git a/SWIG/Source/DOH/Doh/string.c b/SWIG/Source/DOH/Doh/string.c index aaf9894ae..890cc1d8f 100644 --- a/SWIG/Source/DOH/Doh/string.c +++ b/SWIG/Source/DOH/Doh/string.c @@ -800,7 +800,7 @@ String_replace(DOH *stro, DOH *token, DOH *rep, int flags) String *str; if (!String_check(stro)) return 0; str = (String *) stro; - assert(str->refcount); + /* assert(str->refcount); */ /* assert(!str->refcount); */ if (flags & DOH_REPLACE_FIRST) count = 1; return replace_internal(str,Char(token),Char(rep),flags,str->str,count); diff --git a/SWIG/Source/DOH/Include/doh.h b/SWIG/Source/DOH/Include/doh.h index 498292472..875f91c1e 100644 --- a/SWIG/Source/DOH/Include/doh.h +++ b/SWIG/Source/DOH/Include/doh.h @@ -111,7 +111,7 @@ typedef struct DohObjInfo { void (*doh_del)(DOH *obj); /* Delete object */ DOH *(*doh_copy)(DOH *obj); /* Copy and object */ void (*doh_clear)(DOH *obj); /* Clear an object */ - void (*doh_scope)(DOH *obj, int s); /* Change the scope on an object */ + void (*doh_reserved)(void); /* Reserved */ /* Output methods */ DOH *(*doh_str)(DOH *obj); /* Make a full string */ @@ -152,18 +152,14 @@ typedef struct DohObjInfo { extern void DohXInit(DOH *obj); /* Initialize extended object */ extern int DohCheck(const DOH *ptr); /* Check if a DOH object */ extern int DohPoolSize(int); /* Set memory alloc size */ - extern int DohNewScope(); /* Create a new scope */ - extern void DohDelScope(int); /* Delete a scope */ - extern void DohGarbageCollect(); /* Invoke garbage collection */ - extern void DohIntern(DOH *); /* Intern an object */ + extern void DohMemoryInfo(); /* Basic object methods. Common to most objects */ extern void DohDelete(DOH *obj); /* Delete an object */ extern DOH *DohCopy(const DOH *obj); extern void DohClear(DOH *obj); - extern void DohSetScope(DOH *, int scp); /* Set scope of object */ extern DOHString *DohStr(const DOH *obj); extern void *DohData(const DOH *obj); extern int DohDump(const DOH *obj, DOHFile *out); @@ -290,8 +286,6 @@ typedef struct DohObjInfo { #define Readline DohReadline #define Replace DohReplace #define Chop DohChop -#define NewScope DohNewScope -#define DelScope DohDelScope #define Call DohCall #endif @@ -305,8 +299,7 @@ typedef struct DohObjInfo { DohObjInfo *objinfo; \ DOH *nextptr; \ int refcount; \ - unsigned char flags; \ - unsigned char scope + unsigned char flags typedef struct { DOHCOMMON; @@ -325,17 +318,11 @@ typedef struct { #define Decref(a) if (a) ((DohBase *) a)->refcount-- #define Incref(a) if (a) ((DohBase *) a)->refcount++ #define Refcount(a) ((DohBase *) a)->refcount - -#define Getscope(a) ((DohBase *) a)->scope -#define Setscope(a,s) DohSetScope(a,s) #define Objname(a) ((DohBase *) a)->objinfo->objname /* Flags for various internal operations */ -#define DOH_FLAG_SETSCOPE 0x01 #define DOH_FLAG_PRINT 0x02 -#define DOH_FLAG_DELSCOPE 0x04 -#define DOH_FLAG_GC 0x08 #define DOH_FLAG_INTERN 0x10 /* -----------------------------------------------------------------------------