diff options
author | Andrei Karas <akaras@inbox.ru> | 2015-10-17 19:40:08 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2015-10-19 21:12:25 +0300 |
commit | e625d7f186f104316031a39438bf644c67b1f4c4 (patch) | |
tree | 3a8fb83f5c01ff26fa2efb53377f4255d093b00b /src/common | |
parent | 54d1c8b93adbeee5838b8dadabeb789b9fa6c511 (diff) | |
download | hercules-e625d7f186f104316031a39438bf644c67b1f4c4.tar.gz hercules-e625d7f186f104316031a39438bf644c67b1f4c4.tar.bz2 hercules-e625d7f186f104316031a39438bf644c67b1f4c4.tar.xz hercules-e625d7f186f104316031a39438bf644c67b1f4c4.zip |
Fix reallocating memory without memory manager.
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/memmgr.c | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/src/common/memmgr.c b/src/common/memmgr.c index 18caef95f..0d9305b06 100644 --- a/src/common/memmgr.c +++ b/src/common/memmgr.c @@ -79,6 +79,26 @@ struct malloc_interface *iMalloc; #endif +#ifndef USE_MEMMGR + +#ifdef __APPLE__ +#include <malloc/malloc.h> +#define BUFFER_SIZE(ptr) malloc_size(ptr) +#elif __FreeBSD__ +#include <malloc_np.h> +#define BUFFER_SIZE(ptr) malloc_usable_size(ptr) +#elif defined __linux__ || defined __linux || defined CYGWIN +#include <malloc.h> +#define BUFFER_SIZE(ptr) malloc_usable_size(ptr) +#elif defined WIN32 +#include <malloc.h> +#define BUFFER_SIZE(ptr) _msize(ptr) +#else +#error Unsupported OS +#endif + +#endif + void* aMalloc_(size_t size, const char *file, int line, const char *func) { void *ret = MALLOC(size, file, line, func); @@ -110,6 +130,34 @@ void* aRealloc_(void *p, size_t size, const char *file, int line, const char *fu } return ret; } + +void* aReallocz_(void *p, size_t size, const char *file, int line, const char *func) +{ + void *ret; + // ShowMessage("%s:%d: in func %s: aReallocz %p %ld\n",file,line,func,p,size); +#ifdef USE_MEMMGR + ret = REALLOC(p, size, file, line, func); +#else + size_t newSize; + if (p) { + size_t oldSize = malloc_usable_size(p); + ret = REALLOC(p, size, file, line, func); + newSize = BUFFER_SIZE(ret); + if (ret && newSize > oldSize) + memset(ret + oldSize, 0, newSize - oldSize); + } else { + ret = REALLOC(p, size, file, line, func); + if (ret) + memset(ret, 0, BUFFER_SIZE(ret)); + } +#endif + if (ret == NULL){ + ShowFatalError("%s:%d: in func %s: aRealloc error out of memory!\n",file,line,func); + exit(EXIT_FAILURE); + } + return ret; +} + char* aStrdup_(const char *p, const char *file, int line, const char *func) { char *ret = STRDUP(p, file, line, func); @@ -888,7 +936,7 @@ void malloc_defaults(void) { iMalloc->malloc = aMalloc_; iMalloc->calloc = aCalloc_; iMalloc->realloc = aRealloc_; - iMalloc->reallocz = aRealloc_;/* not using memory manager huhum o.o perhaps we could still do something about */ + iMalloc->reallocz = aReallocz_;/* not using memory manager huhum o.o perhaps we could still do something about */ iMalloc->astrdup = aStrdup_; iMalloc->free = aFree_; #endif |