From 91c713cc1a83ae2e4fff09c3a8b2bc12fb4f1043 Mon Sep 17 00:00:00 2001 From: Rene Rivera Date: Thu, 7 Sep 2006 18:33:04 +0000 Subject: [PATCH] Implement gc of string only hash entries (note, doesn't work likely because a buffer overrun in var expand). Allocate all character data as atomic. [SVN r35036] --- historic/jam/src/command.c | 2 +- historic/jam/src/execnt.c | 8 ++--- historic/jam/src/filent.c | 2 +- historic/jam/src/fileunix.c | 2 +- historic/jam/src/hash.c | 59 ++++++++++++++++++++++++++++++++----- historic/jam/src/mem.h | 4 +-- historic/jam/src/newstr.c | 10 ++----- historic/jam/src/strings.c | 2 +- 8 files changed, 64 insertions(+), 25 deletions(-) diff --git a/historic/jam/src/command.c b/historic/jam/src/command.c index 712bd211e..ef8830601 100644 --- a/historic/jam/src/command.c +++ b/historic/jam/src/command.c @@ -55,7 +55,7 @@ cmd_new( { BJAM_FREE(cmd->buf); /* free any buffer from previous iteration */ - cmd->buf = (char*)BJAM_MALLOC(max_line + 1); + cmd->buf = (char*)BJAM_MALLOC_ATOMIC(max_line + 1); if (cmd->buf == 0) break; diff --git a/historic/jam/src/execnt.c b/historic/jam/src/execnt.c index 930ac3c11..e8ec2444e 100644 --- a/historic/jam/src/execnt.c +++ b/historic/jam/src/execnt.c @@ -149,7 +149,7 @@ string_to_args( const char* string ) /* Copy the input string into a buffer we can modify */ - line = (char*)BJAM_MALLOC( src_len+1 ); + line = (char*)BJAM_MALLOC_ATOMIC( src_len+1 ); if (!line) return 0; @@ -280,7 +280,7 @@ process_del( char* command ) if ( len <= 0 ) return 1; - line = (char*)BJAM_MALLOC( len+4+1 ); + line = (char*)BJAM_MALLOC_ATOMIC( len+4+1 ); if (!line) return 1; @@ -410,7 +410,7 @@ void execnt_unit_test() } { - char* long_command = BJAM_MALLOC(MAXLINE + 10); + char* long_command = BJAM_MALLOC_ATOMIC(MAXLINE + 10); assert( long_command != 0 ); memset( long_command, 'x', MAXLINE + 9 ); long_command[MAXLINE + 9] = 0; @@ -545,7 +545,7 @@ execcmd( DWORD procID = GetCurrentProcessId(); /* SVA - allocate 64 other just to be safe */ - cmdtab[ slot ].tempfile = BJAM_MALLOC( strlen( tempdir ) + 64 ); + cmdtab[ slot ].tempfile = BJAM_MALLOC_ATOMIC( strlen( tempdir ) + 64 ); sprintf( cmdtab[ slot ].tempfile, "%s\\jam%d-%02d.bat", tempdir, procID, slot ); diff --git a/historic/jam/src/filent.c b/historic/jam/src/filent.c index 9c37b4e45..7ec01c3ae 100644 --- a/historic/jam/src/filent.c +++ b/historic/jam/src/filent.c @@ -305,7 +305,7 @@ file_archscan( ** 15 characters (ie. don't fit into a ar_name */ - string_table = BJAM_MALLOC(lar_size+1); + string_table = BJAM_MALLOC_ATOMIC(lar_size+1); if (read(fd, string_table, lar_size) != lar_size) printf("error reading string table\n"); string_table[lar_size] = '\0'; diff --git a/historic/jam/src/fileunix.c b/historic/jam/src/fileunix.c index 1f86d7d0f..313040dd3 100644 --- a/historic/jam/src/fileunix.c +++ b/historic/jam/src/fileunix.c @@ -325,7 +325,7 @@ file_archscan( ** 15 characters (ie. don't fit into a ar_name */ - string_table = (char *)BJAM_MALLOC(lar_size); + string_table = (char *)BJAM_MALLOC_ATOMIC(lar_size); lseek(fd, offset + SARHDR, 0); if (read(fd, string_table, lar_size) != lar_size) printf("error reading string table\n"); diff --git a/historic/jam/src/hash.c b/historic/jam/src/hash.c index 5e28dd1f9..74a9aec45 100644 --- a/historic/jam/src/hash.c +++ b/historic/jam/src/hash.c @@ -90,6 +90,11 @@ struct hash static void hashrehash( struct hash *hp ); static void hashstat( struct hash *hp ); +static void * hash_mem_alloc(size_t datalen, size_t size); +static void hash_mem_free(size_t datalen, void * data); +#ifdef OPT_BOEHM_GC +static void hash_mem_finalizer(char * key, struct hash * hp); +#endif /* * hash_free() - remove the given item from the table if it's there. @@ -207,6 +212,12 @@ hashitem( i->hdr.next = *base; *base = i; *data = &i->data; + #ifdef OPT_BOEHM_GC + if (sizeof(HASHDATA) == hp->items.datalen) + { + GC_REGISTER_FINALIZER(i->data.key,&hash_mem_finalizer,hp,0,0); + } + #endif } #ifdef HASH_DEBUG_PROFILE @@ -224,7 +235,7 @@ static void hashrehash( register struct hash *hp ) { int i = ++hp->items.list; hp->items.more = i ? 2 * hp->items.nel : hp->inel; - hp->items.next = (char *)BJAM_MALLOC( hp->items.more * hp->items.size ); + hp->items.next = (char *)hash_mem_alloc( hp->items.datalen, hp->items.more * hp->items.size ); hp->items.free = 0; hp->items.lists[i].nel = hp->items.more; @@ -232,10 +243,10 @@ static void hashrehash( register struct hash *hp ) hp->items.nel += hp->items.more; if( hp->tab.base ) - BJAM_FREE( (char *)hp->tab.base ); + hash_mem_free( hp->items.datalen, (char *)hp->tab.base ); hp->tab.nel = hp->items.nel * hp->bloat; - hp->tab.base = (ITEM **)BJAM_MALLOC( hp->tab.nel * sizeof(ITEM **) ); + hp->tab.base = (ITEM **)hash_mem_alloc( hp->items.datalen, hp->tab.nel * sizeof(ITEM **) ); memset( (char *)hp->tab.base, '\0', hp->tab.nel * sizeof( ITEM * ) ); @@ -290,7 +301,7 @@ hashinit( int datalen, char *name ) { - struct hash *hp = (struct hash *)BJAM_MALLOC( sizeof( *hp ) ); + struct hash *hp = (struct hash *)hash_mem_alloc( datalen, sizeof( *hp ) ); hp->bloat = 3; hp->tab.nel = 0; @@ -323,12 +334,46 @@ hashdone( struct hash *hp ) hashstat( hp ); if( hp->tab.base ) - BJAM_FREE( (char *)hp->tab.base ); + hash_mem_free( hp->items.datalen, (char *)hp->tab.base ); for( i = 0; i <= hp->items.list; i++ ) - BJAM_FREE( hp->items.lists[i].base ); - BJAM_FREE( (char *)hp ); + hash_mem_free( hp->items.datalen, hp->items.lists[i].base ); + hash_mem_free( hp->items.datalen, (char *)hp ); } +static void * hash_mem_alloc(size_t datalen, size_t size) +{ + if (sizeof(HASHDATA) == datalen) + { + return BJAM_MALLOC_RAW(size); + } + else + { + return BJAM_MALLOC(size); + } +} + +static void hash_mem_free(size_t datalen, void * data) +{ + if (sizeof(HASHDATA) == datalen) + { + BJAM_FREE_RAW(data); + } + else + { + BJAM_FREE(data); + } +} + +#ifdef OPT_BOEHM_GC +static void hash_mem_finalizer(char * key, struct hash * hp) +{ + HASHDATA d; + d.key = (char*)key; + hash_free(hp,&d); +} +#endif + + /* ---- */ static void diff --git a/historic/jam/src/mem.h b/historic/jam/src/mem.h index db7a0779f..c29a04251 100644 --- a/historic/jam/src/mem.h +++ b/historic/jam/src/mem.h @@ -13,8 +13,8 @@ http://www.boost.org/LICENSE_1_0.txt) /* Use Boehm GC memory allocator. */ #include - #define bjam_malloc_x(s) GC_malloc(s) - #define bjam_malloc_atomic_x(s) GC_malloc_atomic(s) + #define bjam_malloc_x(s) memset(GC_malloc(s),0,s) + #define bjam_malloc_atomic_x(s) memset(GC_malloc_atomic(s),0,s) #define bjam_calloc_x(n,s) memset(GC_malloc((n)*(s)),0,(n)*(s)) #define bjam_calloc_atomic_x(n,s) memset(GC_malloc_atomic((n)*(s)),0,(n)*(s)) #define bjam_realloc_x(p,s) GC_realloc(p,s) diff --git a/historic/jam/src/newstr.c b/historic/jam/src/newstr.c index c7265c1b9..e4dfbe6c6 100644 --- a/historic/jam/src/newstr.c +++ b/historic/jam/src/newstr.c @@ -56,19 +56,13 @@ static strblock* strblock_chain = 0; static char* storage_start = 0; static char* storage_finish = 0; -/* */ -#define SIMPLE_ALLOC 0 -/*/ -#define SIMPLE_ALLOC 1 -/* */ - /* * allocate() - Allocate n bytes of immortal string storage */ static char* allocate(size_t n) { - #if SIMPLE_ALLOC - return (char*)BJAM_MALLOC(n); + #ifdef OPT_BOEHM_GC + return (char*)BJAM_MALLOC_ATOMIC(n); #else /* See if we can grab storage from an existing block */ size_t remaining = storage_finish - storage_start; diff --git a/historic/jam/src/strings.c b/historic/jam/src/strings.c index 67c8dc75e..e54434bfc 100644 --- a/historic/jam/src/strings.c +++ b/historic/jam/src/strings.c @@ -55,7 +55,7 @@ static void string_reserve_internal( string* self, size_t capacity ) { if ( self->value == self->opt ) { - self->value = (char*)BJAM_MALLOC( capacity + JAM_STRING_MAGIC_SIZE ); + self->value = (char*)BJAM_MALLOC_ATOMIC( capacity + JAM_STRING_MAGIC_SIZE ); self->value[0] = 0; strncat( self->value, self->opt, sizeof(self->opt) ); assert( strlen( self->value ) <= self->capacity ); /* This is a regression test */