From 07f0a8dbc9ff6638344f13875925408dd6e2aa2c Mon Sep 17 00:00:00 2001 From: Cyrill Gorcunov Date: Fri, 9 May 2014 15:00:09 +0400 Subject: [PATCH] debug: Drop LOGALLOC usage There are special tools (like valgrind and etc) to track memory leaks, no need for own trivial tracker. Signed-off-by: Cyrill Gorcunov --- misc/findleak.pl | 42 -------------------------- misc/findleak.txt | 59 ------------------------------------ nasm.c | 1 - nasmlib.c | 76 +---------------------------------------------- nasmlib.h | 25 ---------------- ndisasm.c | 1 - 6 files changed, 1 insertion(+), 203 deletions(-) delete mode 100755 misc/findleak.pl delete mode 100644 misc/findleak.txt diff --git a/misc/findleak.pl b/misc/findleak.pl deleted file mode 100755 index dbb33671..00000000 --- a/misc/findleak.pl +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/perl -# From: Ed Beroset - -my %mem = {}; -my %alloc = {}; -while(<>) -{ - if (/realloc\((0x[0-9a-f]+).*\).*returns \((0x[0-9a-f]+)/) - { - $mem{$1}--; - if ($mem{$1} != 0) { - print "free before alloc! $_"; - } - if ($mem{$2} != 0) { - print "memory leak! $_"; - } - $mem{$2}++; - $alloc{$2} = $_; - } - elsif (/free\((0x[0-9a-f]+)/) - { - $mem{$1}--; - if ($mem{$1} != 0) { - print "free before alloc! $_"; - } - } - elsif (m/returns (0x[0-9a-f]+)/) - { - if ($mem{$1} != 0) { - print "memory leak! $_"; - } - $mem{$1}++; - $alloc{$1} = $_; - } -} -foreach $goo (sort keys %mem) -{ - if ($mem{$goo}) - { - print "$mem{$goo} $alloc{$goo}"; - } -} diff --git a/misc/findleak.txt b/misc/findleak.txt deleted file mode 100644 index 8a1cfdc1..00000000 --- a/misc/findleak.txt +++ /dev/null @@ -1,59 +0,0 @@ -Subject: [nasm-devel] tool to help find memory leaks -Date: Fri, 02 Nov 2001 22:08:01 -0500 -From: Ed Beroset -Reply-To: nasm-devel@yahoogroups.com -To: nasm-devel@yahoogroups.com - -Here's a little Perl script I wrote a while ago to help track down memory -leaks in nasm. First, compile nasm with LOGALLOC defined (see -nasmlib.c). That creates a log file of all allocs and frees. This Perl -script reads that file and tells you which source code lines caused a leak -(or a free of unallocated memory). There are many leaks, almost all of -them in the preprocessor. - --+--- findleak.pl begins -#!/usr/bin/perl -my %mem = {}; -my %alloc = {}; -while(<>) -{ - if (/realloc\((0x[0-9a-f]+).*\).*returns \((0x[0-9a-f]+)/) - { - $mem{$1}--; - if ($mem{$1} != 0) { - print "free before alloc! $_"; - } - if ($mem{$2} != 0) { - print "memory leak! $_"; - } - $mem{$2}++; - $alloc{$2} = $_; - } - elsif (/free\((0x[0-9a-f]+)/) - { - $mem{$1}--; - if ($mem{$1} != 0) { - print "free before alloc! $_"; - } - } - elsif (m/returns (0x[0-9a-f]+)/) - { - if ($mem{$1} != 0) { - print "memory leak! $_"; - } - $mem{$1}++; - $alloc{$1} = $_; - } -} -foreach $goo (sort keys %mem) -{ - if ($mem{$goo}) - { - print "$mem{$goo} $alloc{$goo}"; - } -} --+--- findleak.pl ends - - - -Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ diff --git a/nasm.c b/nasm.c index 2bb30292..60601190 100644 --- a/nasm.c +++ b/nasm.c @@ -337,7 +337,6 @@ int main(int argc, char **argv) tolower_init(); - nasm_init_malloc_error(); offsets = raa_init(); forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo)); diff --git a/nasmlib.c b/nasmlib.c index ec1460ba..4588ff38 100644 --- a/nasmlib.c +++ b/nasmlib.c @@ -50,10 +50,6 @@ int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */ static vefunc nasm_verror; /* Global error handling function */ -#ifdef LOGALLOC -static FILE *logfp; -#endif - /* Uninitialized -> all zero by C spec */ const uint8_t zero_buffer[ZERO_BUF_SIZE]; @@ -86,93 +82,37 @@ void nasm_error(int severity, const char *fmt, ...) va_end(ap); } -void nasm_init_malloc_error(void) -{ -#ifdef LOGALLOC - logfp = fopen("malloc.log", "w"); - if (logfp) { - setvbuf(logfp, NULL, _IOLBF, BUFSIZ); - } else { - nasm_error(ERR_NONFATAL | ERR_NOFILE, "Unable to open %s", logfp); - logfp = stderr; - } - fprintf(logfp, "null pointer is %p\n", NULL); -#endif -} - -#ifdef LOGALLOC -void *nasm_malloc_log(const char *file, int line, size_t size) -#else void *nasm_malloc(size_t size) -#endif { void *p = malloc(size); if (!p) nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory"); -#ifdef LOGALLOC - else - fprintf(logfp, "%s %d malloc(%ld) returns %p\n", - file, line, (long)size, p); -#endif return p; } -#ifdef LOGALLOC -void *nasm_zalloc_log(const char *file, int line, size_t size) -#else void *nasm_zalloc(size_t size) -#endif { void *p = calloc(size, 1); if (!p) nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory"); -#ifdef LOGALLOC - else - fprintf(logfp, "%s %d calloc(%ld, 1) returns %p\n", - file, line, (long)size, p); -#endif return p; } -#ifdef LOGALLOC -void *nasm_realloc_log(const char *file, int line, void *q, size_t size) -#else void *nasm_realloc(void *q, size_t size) -#endif { void *p = q ? realloc(q, size) : malloc(size); if (!p) nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory"); -#ifdef LOGALLOC - else if (q) - fprintf(logfp, "%s %d realloc(%p,%ld) returns %p\n", - file, line, q, (long)size, p); - else - fprintf(logfp, "%s %d malloc(%ld) returns %p\n", - file, line, (long)size, p); -#endif return p; } -#ifdef LOGALLOC -void nasm_free_log(const char *file, int line, void *q) -#else void nasm_free(void *q) -#endif { - if (q) { -#ifdef LOGALLOC - fprintf(logfp, "%s %d free(%p)\n", file, line, q); -#endif + if (q) free(q); - } } -#ifdef LOGALLOC -char *nasm_strdup_log(const char *file, int line, const char *s) -#else char *nasm_strdup(const char *s) -#endif { char *p; int size = strlen(s) + 1; @@ -180,20 +120,11 @@ char *nasm_strdup(const char *s) p = malloc(size); if (!p) nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory"); -#ifdef LOGALLOC - else - fprintf(logfp, "%s %d strdup(%ld) returns %p\n", - file, line, (long)size, p); -#endif strcpy(p, s); return p; } -#ifdef LOGALLOC -char *nasm_strndup_log(const char *file, int line, const char *s, size_t len) -#else char *nasm_strndup(const char *s, size_t len) -#endif { char *p; int size = len + 1; @@ -201,11 +132,6 @@ char *nasm_strndup(const char *s, size_t len) p = malloc(size); if (!p) nasm_error(ERR_FATAL | ERR_NOFILE, "out of memory"); -#ifdef LOGALLOC - else - fprintf(logfp, "%s %d strndup(%ld) returns %p\n", - file, line, (long)size, p); -#endif strncpy(p, s, len); p[len] = '\0'; return p; diff --git a/nasmlib.h b/nasmlib.h index d11e6e0b..8f6c8852 100644 --- a/nasmlib.h +++ b/nasmlib.h @@ -64,15 +64,6 @@ extern unsigned char nasm_tolower_tab[256]; #define nasm_isalnum(x) isalnum((unsigned char)(x)) #define nasm_isxdigit(x) isxdigit((unsigned char)(x)) -/* - * If this is defined, the wrappers around malloc et al will - * transform into logging variants, which will cause NASM to create - * a file called `malloc.log' when run, and spew details of all its - * memory management into that. That can then be analysed to detect - * memory leaks and potentially other problems too. - */ -/* #define LOGALLOC */ - /* * ------------------------- * Error reporting functions @@ -144,28 +135,12 @@ void nasm_set_verror(vefunc); * passed a NULL pointer; nasm_free will do nothing if it is passed * a NULL pointer. */ -void nasm_init_malloc_error(void); -#ifndef LOGALLOC void *nasm_malloc(size_t); void *nasm_zalloc(size_t); void *nasm_realloc(void *, size_t); void nasm_free(void *); char *nasm_strdup(const char *); char *nasm_strndup(const char *, size_t); -#else -void *nasm_malloc_log(const char *, int, size_t); -void *nasm_zalloc_log(const char *, int, size_t); -void *nasm_realloc_log(const char *, int, void *, size_t); -void nasm_free_log(const char *, int, void *); -char *nasm_strdup_log(const char *, int, const char *); -char *nasm_strndup_log(const char *, int, const char *, size_t); -#define nasm_malloc(x) nasm_malloc_log(__FILE__,__LINE__,x) -#define nasm_zalloc(x) nasm_zalloc_log(__FILE__,__LINE__,x) -#define nasm_realloc(x,y) nasm_realloc_log(__FILE__,__LINE__,x,y) -#define nasm_free(x) nasm_free_log(__FILE__,__LINE__,x) -#define nasm_strdup(x) nasm_strdup_log(__FILE__,__LINE__,x) -#define nasm_strndup(x,y) nasm_strndup_log(__FILE__,__LINE__,x,y) -#endif /* * NASM assert failure diff --git a/ndisasm.c b/ndisasm.c index 3212629f..754033c4 100644 --- a/ndisasm.c +++ b/ndisasm.c @@ -95,7 +95,6 @@ int main(int argc, char **argv) tolower_init(); nasm_set_verror(ndisasm_verror); - nasm_init_malloc_error(); iflag_clear_all(&prefer); offset = 0;