Commit b16f42e4 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

dmemory.c: merge changes from branches/cdi-tilesAndFileDrivenInput

parent 723bb896
...@@ -2,6 +2,10 @@ ...@@ -2,6 +2,10 @@
* Version 1.6.9 released * Version 1.6.9 released
2015-04-21 Uwe Schulzweida
* dmemory.c: merge changes from branches/cdi-tilesAndFileDrivenInput
2015-03-30 Uwe Schulzweida 2015-03-30 Uwe Schulzweida
* gribapiDefLevel: use function grib2DefLevel() to write grib2 levels * gribapiDefLevel: use function grib2DefLevel() to write grib2 levels
......
#if defined (HAVE_CONFIG_H) #if defined(HAVE_CONFIG_H)
# include "config.h" # include "config.h"
#endif #endif
...@@ -11,18 +11,24 @@ ...@@ -11,18 +11,24 @@
#include "error.h" #include "error.h"
#if ! defined (HAVE_CONFIG_H) #if !defined(HAVE_CONFIG_H) && !defined(HAVE_MALLOC_H) && defined(SX)
#if ! defined (HAVE_MALLOC_H) # define HAVE_MALLOC_H
# if defined (SX)
# define HAVE_MALLOC_H
# endif
#endif
#endif #endif
#if defined (HAVE_MALLOC_H) #if defined(HAVE_MALLOC_H)
# include <malloc.h> # include <malloc.h>
#endif #endif
//There is no point in avoiding to include our own header, it is likely to be included before this file in the backed cdilib.c.
//As such, we may as well always include it and let the compiler check our function signatures.
#include "dmemory.h"
//However, we need to avoid clobbering our own `malloc()` calls, so we ensure that our own malloc calls cannot be interpreted as macro calls.
#define protected_realloc (realloc)
#define protected_calloc (calloc)
#define protected_malloc (malloc)
#define protected_free (free)
#define MALLOC_FUNC 0 #define MALLOC_FUNC 0
#define CALLOC_FUNC 1 #define CALLOC_FUNC 1
...@@ -269,7 +275,7 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj, ...@@ -269,7 +275,7 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
{ {
memTableSize = 8; memTableSize = 8;
memSize = memTableSize * sizeof(MemTable_t); memSize = memTableSize * sizeof(MemTable_t);
memTable = (MemTable_t *) malloc(memSize); memTable = (MemTable_t *) protected_malloc(memSize);
if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize); if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize);
for(size_t i = 0; i < memTableSize; i++) for(size_t i = 0; i < memTableSize; i++)
...@@ -290,7 +296,7 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj, ...@@ -290,7 +296,7 @@ int memListNewEntry(int mtype, void *ptr, size_t size, size_t nobj,
{ {
memTableSize = 2*memTableSize; memTableSize = 2*memTableSize;
memSize = memTableSize*sizeof(MemTable_t); memSize = memTableSize*sizeof(MemTable_t);
memTable = (MemTable_t*) realloc(memTable, memSize); memTable = (MemTable_t*) protected_realloc(memTable, memSize);
if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize); if( memTable == NULL ) memError(__func__, __FILE__, __LINE__, memSize);
for (size_t i = memID; i < memTableSize; i++) for (size_t i = memID; i < memTableSize; i++)
...@@ -414,7 +420,7 @@ void *Calloc(const char *caller, const char *file, int line, size_t nobjs, size_ ...@@ -414,7 +420,7 @@ void *Calloc(const char *caller, const char *file, int line, size_t nobjs, size_
if ( nobjs*size > 0 ) if ( nobjs*size > 0 )
{ {
ptr = calloc(nobjs, size); ptr = protected_calloc(nobjs, size);
if ( MEM_Debug ) if ( MEM_Debug )
{ {
...@@ -444,7 +450,7 @@ void *Malloc(const char *caller, const char *file, int line, size_t size) ...@@ -444,7 +450,7 @@ void *Malloc(const char *caller, const char *file, int line, size_t size)
if ( size > 0 ) if ( size > 0 )
{ {
ptr = malloc(size); ptr = protected_malloc(size);
if ( MEM_Debug ) if ( MEM_Debug )
{ {
...@@ -474,7 +480,7 @@ void *Realloc(const char *caller, const char *file, int line, void *ptrold, size ...@@ -474,7 +480,7 @@ void *Realloc(const char *caller, const char *file, int line, void *ptrold, size
if ( size > 0 ) if ( size > 0 )
{ {
ptr = realloc(ptrold, size); ptr = protected_realloc(ptrold, size);
if ( MEM_Debug ) if ( MEM_Debug )
{ {
...@@ -521,36 +527,33 @@ void Free(const char *caller, const char *file, int line, void *ptr) ...@@ -521,36 +527,33 @@ void Free(const char *caller, const char *file, int line, void *ptr)
} }
} }
free(ptr); protected_free(ptr);
} }
void *cdiXmalloc(size_t size, const char *filename, const char *functionname, void *cdiXmalloc(size_t size, const char *filename, const char *functionname,
int line) int line)
{ {
void *value = malloc(size); void *value = protected_malloc(size);
if (size == 0 || value != NULL) ; else if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "malloc failed: %s", cdiAbort(filename, functionname, line, "malloc failed: %s", strerror(errno));
strerror(errno));
return value; return value;
} }
void *cdiXcalloc(size_t nmemb, size_t size, const char *filename, void *cdiXcalloc(size_t nmemb, size_t size, const char *filename,
const char *functionname, int line) const char *functionname, int line)
{ {
void *value = calloc(nmemb, size); void *value = protected_calloc(nmemb, size);
if (size == 0 || value != NULL) ; else if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "calloc failed: %s", cdiAbort(filename, functionname, line, "calloc failed: %s", strerror(errno) );
strerror(errno) );
return value; return value;
} }
void *cdiXrealloc(void *p, size_t size, const char *functionname, void *cdiXrealloc(void *p, size_t size, const char *functionname,
const char *filename, int line) const char *filename, int line)
{ {
void *value = realloc(p, size); void *value = protected_realloc(p, size);
if (size == 0 || value != NULL) ; else if (size == 0 || value != NULL) ; else
cdiAbort(filename, functionname, line, "realloc failed: %s", cdiAbort(filename, functionname, line, "realloc failed: %s", strerror(errno));
strerror(errno));
return value; return value;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment