From c494c517811c566a5c621f329188940e6ab8851e Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Mon, 6 Nov 2017 00:03:14 +0100 Subject: [PATCH] debug info added: DB some useful printouts DB_FULL all addresses created, freed and left over to files --- src/misc/alloc.c | 77 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/src/misc/alloc.c b/src/misc/alloc.c index 80d6073b1..09616d3e1 100644 --- a/src/misc/alloc.c +++ b/src/misc/alloc.c @@ -20,6 +20,12 @@ will use void __attribute__((destructor)) mem_delete(void) or atexit() The master program has to make copies of all the data that have to be kept for further use before detaching the shared lib! */ +// #define DB_FULL /* uncomment for debugging output, all addresses to files */ +#ifdef DB_FULL +#define DB +#else +#define DB /* uncomment for printing some debugging output */ +#endif /* for thread handling */ #if defined __MINGW32__ || defined _MSC_VER @@ -68,7 +74,13 @@ static void memdeleted(const void *ptr); int gc_is_on = 0; -static int mem_in = 0, mem_out = 0; +/* add some debugging printout */ +#ifdef DB +static int mem_in = 0, mem_out = 0, mem_freed = 0; +#endif +#ifdef DB_FULL +static FILE *alloclog, *freelog, *finallog; +#endif NGHASHPTR memory_table; @@ -164,6 +176,7 @@ trealloc(const void *ptr, size_t num) exit(EXIT_FAILURE); #endif } + /* remove the old and add the new memory address */ if (s != ptr) { memdeleted(ptr); memsaved(s); @@ -234,16 +247,30 @@ tcalloc(size_t num, size_t stype) /* initialize hash table to store allocated mem addresses */ void mem_init(void) { + gc_is_on = 0; memory_table = nghash_init_pointer(1024); gc_is_on = 1; +#ifdef DB_FULL + alloclog = fopen("alloc_log.txt", "wt"); + freelog = fopen("free_log.txt", "wt"); + finallog = fopen("final_log.txt", "wt"); +#endif } /* add to counter and hash table if memory is allocated */ static int memsaved(void *ptr) { if (gc_is_on) { gc_is_on = 0; - mem_in++; - nghash_insert(memory_table, ptr, NULL); + + if (nghash_insert(memory_table, ptr, NULL) == NULL) { +#ifdef DB + mem_in++; +#ifdef DB_FULL + fprintf(alloclog, "0x%p\n", ptr); +#endif +#endif + } else + fprintf(stderr, "Could not insert item into hashtable at 0x%p\n", ptr); gc_is_on = 1; } return OK; @@ -253,8 +280,18 @@ static int memsaved(void *ptr) { static void memdeleted(const void *ptr) { if (gc_is_on) { gc_is_on = 0; - mem_out++; - nghash_delete(memory_table, (void*)ptr); + if (nghash_delete_special(memory_table, (void*)ptr) == NULL) { +#ifdef DB + mem_out++; +#ifdef DB_FULL + fprintf(freelog, "0x%p\n", ptr); +#endif + } + else + fprintf(stderr, "Could not delete item from hashtable at 0x%p\n", ptr); +#else + } +#endif gc_is_on = 1; } } @@ -268,14 +305,40 @@ void my_free_func(void *data) void my_key_free(void * key) { - if (key) + if (key) { free(key); + key = NULL; +#ifdef DB + mem_freed++; +#endif + } } /* free hash table, all entries and then the table itself */ void mem_delete(void) { +#ifdef DB + char buf[128]; + printf("memory allocated %d times, freed %d times\n", mem_in, mem_out); + printf("Size of hash table to be freed: %d entries.\n", nghash_get_size(memory_table)); +#ifdef DB_FULL + void *data, *key; + data = nghash_enumeratek(memory_table, &key, TRUE); + for (void *hkey = key; hkey;) { + fprintf(finallog, "0x%p\n", hkey); + data = nghash_enumeratek(memory_table, &hkey, FALSE); + } + fclose(alloclog); + fclose(freelog); + fclose(finallog); +#endif +#endif gc_is_on = 0; - printf("mem allocated %d times, deleted %d times\n", mem_in, mem_out); nghash_free(memory_table, NULL, my_key_free); +#ifdef DB + /* printf via sh_printf will need some info from variables that have + been deleted already, therefore we use fputs */ + sprintf(buf, "Number of addresses freed: %d entries.\n", mem_freed); + fputs(buf, stdout); +#endif }