void * malloc (size_t n) { void *res; switch (wrapper_state) { case WRAPPER_UNINITIALIZED: wrapper_state = WRAPPER_INITIALIZING; wrapper_state = WRAPPER_INITIALIZED; /* !!!!!!! fall through !!!!!!! */ case WRAPPER_INITIALIZED: if (semaphore) { wrapper_num_allocated++; wrapper_bytes_allocated += n; res = MY_malloc(n); } else { semaphore++; res = ccmalloc_malloc (n); semaphore--; } break; default: case WRAPPER_INITIALIZING: res = static_malloc (n); break; } # ifdef LOG_EXTERNAL_MALLOC { print ("::: 0x"); print_hex ((unsigned) res); if (wrapper_state == WRAPPER_INITIALIZING) print (" static "); else if (semaphore) print (" internal"); else print (" external"); print (" malloc("); print_dec (n); print (")\n"); } # endif /* Initialize with something weird */ memset (res, 0x42, n); return res; }
/* Create a new hashtable. */ hashtable_t *ht_create( int size ) { hashtable_t *hashtable = NULL; int i; if( size < 1 ) return NULL; /* Allocate the table itself. */ if( ( hashtable = static_malloc( sizeof( hashtable_t ) ) ) == NULL ) { return NULL; } /* Allocate pointers to the head nodes. */ if( ( hashtable->table = static_malloc( sizeof( entry_t * ) * size ) ) == NULL ) { return NULL; } for( i = 0; i < size; i++ ) { hashtable->table[i] = NULL; } hashtable->size = size; return hashtable; }
/* Create a key-value pair. */ entry_t *ht_newpair( char *key, funct_t value ) { entry_t *newpair; if( ( newpair = static_malloc( sizeof( entry_t ) ) ) == NULL ) { return NULL; } if( ( newpair->key = strdup( key ) ) == NULL ) { return NULL; } if( ( newpair->value = value ) == NULL ) { return NULL; } newpair->next = NULL; return newpair; }