diff options
Diffstat (limited to 'src/common/malloc.c')
-rw-r--r-- | src/common/malloc.c | 133 |
1 files changed, 82 insertions, 51 deletions
diff --git a/src/common/malloc.c b/src/common/malloc.c index f80d11fb4..1769e0982 100644 --- a/src/common/malloc.c +++ b/src/common/malloc.c @@ -10,9 +10,63 @@ #include <string.h> #include <time.h> -// no logging for minicore -#if defined(MINICORE) && defined(LOG_MEMMGR) -#undef LOG_MEMMGR +////////////// Memory Libraries ////////////////// + +#if defined(MEMWATCH) + +# include <string.h> +# include "memwatch.h" +# define MALLOC(n,file,line,func) mwMalloc((n),(file),(line)) +# define CALLOC(m,n,file,line,func) mwCalloc((m),(n),(file),(line)) +# define REALLOC(p,n,file,line,func) mwRealloc((p),(n),(file),(line)) +# define STRDUP(p,file,line,func) mwStrdup((p),(file),(line)) +# define FREE(p,file,line,func) mwFree((p),(file),(line)) +# define MEMORY_USAGE() 0 +# define MEMORY_VERIFY(ptr) mwIsSafeAddr(ptr, 1) +# define MEMORY_CHECK() CHECK() + +#elif defined(DMALLOC) + +# include <string.h> +# include <stdlib.h> +# include "dmalloc.h" +# define MALLOC(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0) +# define CALLOC(m,n,file,line,func) dmalloc_malloc((file),(line),(m)*(n),DMALLOC_FUNC_CALLOC,0,0) +# define REALLOC(p,n,file,line,func) dmalloc_realloc((file),(line),(p),(n),DMALLOC_FUNC_REALLOC,0) +# define STRDUP(p,file,line,func) strdup(p) +# define FREE(p,file,line,func) free(p) +# define MEMORY_USAGE() dmalloc_memory_allocated() +# define MEMORY_VERIFY(ptr) (dmalloc_verify(ptr) == DMALLOC_VERIFY_NOERROR) +# define MEMORY_CHECK() dmalloc_log_stats(); dmalloc_log_unfreed() + +#elif defined(GCOLLECT) + +# include "gc.h" +# ifdef GC_ADD_CALLER +# define RETURN_ADDR 0, +# else +# define RETURN_ADDR +# endif +# define MALLOC(n,file,line,func) GC_debug_malloc((n), RETURN_ADDR (file),(line)) +# define CALLOC(m,n,file,line,func) GC_debug_malloc((m)*(n), RETURN_ADDR (file),(line)) +# define REALLOC(p,n,file,line,func) GC_debug_realloc((p),(n), RETURN_ADDR (file),(line)) +# define STRDUP(p,file,line,func) GC_debug_strdup((p), RETURN_ADDR (file),(line)) +# define FREE(p,file,line,func) GC_debug_free(p) +# define MEMORY_USAGE() GC_get_heap_size() +# define MEMORY_VERIFY(ptr) (GC_base(ptr) != NULL) +# define MEMORY_CHECK() GC_gcollect() + +#else + +# define MALLOC(n,file,line,func) malloc(n) +# define CALLOC(m,n,file,line,func) calloc((m),(n)) +# define REALLOC(p,n,file,line,func) realloc((p),(n)) +# define STRDUP(p,file,line,func) strdup(p) +# define FREE(p,file,line,func) free(p) +# define MEMORY_USAGE() 0 +# define MEMORY_VERIFY(ptr) true +# define MEMORY_CHECK() + #endif void* aMalloc_(size_t size, const char *file, int line, const char *func) @@ -26,17 +80,6 @@ void* aMalloc_(size_t size, const char *file, int line, const char *func) return ret; } -void* aMallocA_(size_t size, const char *file, int line, const char *func) -{ - void *ret = MALLOCA(size, file, line, func); - // ShowMessage("%s:%d: in func %s: aMallocA %d\n",file,line,func,size); - if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: aMallocA error out of memory!\n",file,line,func); - exit(EXIT_FAILURE); - } - - return ret; -} void* aCalloc_(size_t num, size_t size, const char *file, int line, const char *func) { void *ret = CALLOC(num, size, file, line, func); @@ -47,16 +90,6 @@ void* aCalloc_(size_t num, size_t size, const char *file, int line, const char * } return ret; } -void* aCallocA_(size_t num, size_t size, const char *file, int line, const char *func) -{ - void *ret = CALLOCA(num, size, file, line, func); - // ShowMessage("%s:%d: in func %s: aCallocA %d %d\n",file,line,func,num,size); - if (ret == NULL){ - ShowFatalError("%s:%d: in func %s: aCallocA error out of memory!\n",file,line,func); - exit(EXIT_FAILURE); - } - return ret; -} void* aRealloc_(void *p, size_t size, const char *file, int line, const char *func) { void *ret = REALLOC(p, size, file, line, func); @@ -86,29 +119,6 @@ void aFree_(void *p, const char *file, int line, const char *func) p = NULL; } -#ifdef GCOLLECT - -void* _bcallocA(size_t size, size_t cnt) -{ - void *ret = MALLOCA(size * cnt); - if (ret) memset(ret, 0, size * cnt); - return ret; -} -void* _bcalloc(size_t size, size_t cnt) -{ - void *ret = MALLOC(size * cnt); - if (ret) memset(ret, 0, size * cnt); - return ret; -} -char* _bstrdup(const char *chr) -{ - int len = strlen(chr); - char *ret = (char*)MALLOC(len + 1); - if (ret) memcpy(ret, chr, len + 1); - return ret; -} - -#endif #ifdef USE_MEMMGR @@ -654,21 +664,32 @@ static void memmgr_init (void) *-------------------------------------- */ -bool malloc_verify(void* ptr) + +/// Tests the memory for errors and memory leaks. +void malloc_memory_check(void) +{ + MEMORY_CHECK(); +} + + +/// Returns true if a pointer is valid. +/// The check is best-effort, false positives are possible. +bool malloc_verify_ptr(void* ptr) { #ifdef USE_MEMMGR - return memmgr_verify(ptr); + return memmgr_verify(ptr) && MEMORY_VERIFY(ptr); #else - return true; + return MEMORY_VERIFY(ptr); #endif } + size_t malloc_usage (void) { #ifdef USE_MEMMGR return memmgr_usage (); #else - return 0; + return MEMORY_USAGE(); #endif } @@ -677,10 +698,20 @@ void malloc_final (void) #ifdef USE_MEMMGR memmgr_final (); #endif + MEMORY_CHECK(); } void malloc_init (void) { +#if defined(DMALLOC) && defined(CYGWIN) + // http://dmalloc.com/docs/latest/online/dmalloc_19.html + dmalloc_debug_setup(getenv("DMALLOC_OPTIONS")); +#endif +#ifdef GCOLLECT + // don't garbage collect, only report inaccessible memory that was not deallocated + GC_find_leak = 1; + GC_INIT(); +#endif #ifdef USE_MEMMGR memmgr_init (); #endif |