3131#define SALT_LEN 36
3232
3333#ifdef DEBUG
34- static MemNode * mem_head = NULL ;
34+ #include <stdint.h>
35+ #define MEM_HASH_SIZE 8192
36+ static MemNode * mem_hash_table [MEM_HASH_SIZE ] = {NULL };
3537static pthread_mutex_t mem_mutex = PTHREAD_MUTEX_INITIALIZER ;
38+
39+ static inline size_t hash_ptr (const void * ptr )
40+ {
41+ uintptr_t v = (uintptr_t )ptr ;
42+ return ((v >> 3 ) ^ (v >> 16 )) & (MEM_HASH_SIZE - 1 );
43+ }
3644#endif
3745
3846char * path_append (const char * path , const char * filename )
@@ -290,8 +298,9 @@ static void *malloc_wrapper_internal(size_t size, const char *file,
290298 node -> line = line ;
291299
292300 pthread_mutex_lock (& mem_mutex );
293- node -> next = mem_head ;
294- mem_head = node ;
301+ size_t idx = hash_ptr (ptr );
302+ node -> next = mem_hash_table [idx ];
303+ mem_hash_table [idx ] = node ;
295304 pthread_mutex_unlock (& mem_mutex );
296305 }
297306#endif
@@ -325,8 +334,9 @@ void *CALLOC_wrapper(size_t nmemb, size_t size, const char *file,
325334 node -> line = line ;
326335
327336 pthread_mutex_lock (& mem_mutex );
328- node -> next = mem_head ;
329- mem_head = node ;
337+ size_t idx = hash_ptr (ptr );
338+ node -> next = mem_hash_table [idx ];
339+ mem_hash_table [idx ] = node ;
330340 pthread_mutex_unlock (& mem_mutex );
331341 }
332342#endif
@@ -371,7 +381,8 @@ void *REALLOC_wrapper(void *ptr, size_t size, const char *file,
371381
372382 // Look up in tracker
373383 pthread_mutex_lock (& mem_mutex );
374- MemNode * * curr = & mem_head ;
384+ size_t idx = hash_ptr (ptr );
385+ MemNode * * curr = & mem_hash_table [idx ];
375386 MemNode * found_node = NULL ;
376387 while (* curr ) {
377388 if ((* curr )-> ptr == ptr ) {
@@ -383,11 +394,12 @@ void *REALLOC_wrapper(void *ptr, size_t size, const char *file,
383394 }
384395
385396 if (found_node ) {
397+ pthread_mutex_unlock (& mem_mutex );
386398 void * new_ptr = realloc (ptr , size );
387399 if (!new_ptr && size != 0 ) {
388- // Re-link the node before releasing lock and failing
389- found_node -> next = mem_head ;
390- mem_head = found_node ;
400+ pthread_mutex_lock ( & mem_mutex );
401+ found_node -> next = mem_hash_table [ idx ] ;
402+ mem_hash_table [ idx ] = found_node ;
391403 pthread_mutex_unlock (& mem_mutex );
392404
393405 fatal_log_printf (file , func , line , "realloc failed: %s!\n" ,
@@ -396,7 +408,6 @@ void *REALLOC_wrapper(void *ptr, size_t size, const char *file,
396408 }
397409
398410 if (!new_ptr && size == 0 ) {
399- pthread_mutex_unlock (& mem_mutex );
400411 free (found_node );
401412 log_printf (debug , file , func , line ,
402413 "REALLOC result: NULL (size 0, freed)\n" );
@@ -408,8 +419,10 @@ void *REALLOC_wrapper(void *ptr, size_t size, const char *file,
408419 found_node -> func = func ;
409420 found_node -> line = line ;
410421
411- found_node -> next = mem_head ;
412- mem_head = found_node ;
422+ pthread_mutex_lock (& mem_mutex );
423+ size_t new_idx = hash_ptr (new_ptr );
424+ found_node -> next = mem_hash_table [new_idx ];
425+ mem_hash_table [new_idx ] = found_node ;
413426 pthread_mutex_unlock (& mem_mutex );
414427
415428 log_printf (debug , file , func , line , "REALLOC result: %p\n" ,
@@ -449,8 +462,9 @@ void *REALLOC_wrapper(void *ptr, size_t size, const char *file,
449462 node -> line = line ;
450463
451464 pthread_mutex_lock (& mem_mutex );
452- node -> next = mem_head ;
453- mem_head = node ;
465+ size_t new_idx = hash_ptr (new_ptr );
466+ node -> next = mem_hash_table [new_idx ];
467+ mem_hash_table [new_idx ] = node ;
454468 pthread_mutex_unlock (& mem_mutex );
455469
456470 log_printf (debug , file , func , line ,
@@ -491,8 +505,9 @@ char *REALPATH_wrapper(const char *path, char *resolved_path, const char *file,
491505 node -> line = line ;
492506
493507 pthread_mutex_lock (& mem_mutex );
494- node -> next = mem_head ;
495- mem_head = node ;
508+ size_t idx = hash_ptr (res );
509+ node -> next = mem_hash_table [idx ];
510+ mem_hash_table [idx ] = node ;
496511 pthread_mutex_unlock (& mem_mutex );
497512 }
498513#else
@@ -511,7 +526,8 @@ void FREE_wrapper(void *ptr, const char *file, const char *func, int line)
511526
512527#ifdef DEBUG
513528 pthread_mutex_lock (& mem_mutex );
514- MemNode * * curr = & mem_head ;
529+ size_t idx = hash_ptr (ptr );
530+ MemNode * * curr = & mem_hash_table [idx ];
515531 MemNode * found_node = NULL ;
516532 while (* curr ) {
517533 if ((* curr )-> ptr == ptr ) {
@@ -544,14 +560,16 @@ void mem_cleanup(void)
544560{
545561#ifdef DEBUG
546562 pthread_mutex_lock (& mem_mutex );
547- MemNode * curr = mem_head ;
548- while (curr ) {
549- MemNode * next = curr -> next ;
550- free (curr -> ptr );
551- free (curr );
552- curr = next ;
553- }
554- mem_head = NULL ;
563+ for (size_t i = 0 ; i < MEM_HASH_SIZE ; i ++ ) {
564+ MemNode * curr = mem_hash_table [i ];
565+ while (curr ) {
566+ MemNode * next = curr -> next ;
567+ free (curr -> ptr );
568+ free (curr );
569+ curr = next ;
570+ }
571+ mem_hash_table [i ] = NULL ;
572+ }
555573 pthread_mutex_unlock (& mem_mutex );
556574 pthread_mutex_destroy (& mem_mutex );
557575#endif
0 commit comments