diff options
Diffstat (limited to 'src/common/malloc.h')
-rw-r--r-- | src/common/malloc.h | 109 |
1 files changed, 58 insertions, 51 deletions
diff --git a/src/common/malloc.h b/src/common/malloc.h index d75ff548c..f65593eb1 100644 --- a/src/common/malloc.h +++ b/src/common/malloc.h @@ -3,6 +3,12 @@ #ifndef _MALLOC_H_
#define _MALLOC_H_
+// Q: What are the 'a'-variant allocation functions?
+// A: They allocate memory from the stack, which is automatically
+// freed when the invoking function returns.
+// But it's not portable (http://c-faq.com/malloc/alloca.html)
+// and I have doubts our implementation works.
+// -> They should NOT be used, period.
#define MEMSET_TURBO
@@ -36,29 +42,29 @@ # define aStrdup(p) _mstrdup(p,ALC_MARK)
# define aFree(p) _mfree(p,ALC_MARK)
- void* _mmalloc (size_t, const char *, int, const char *);
- void* _mcalloc (size_t, size_t, const char *, int, const char *);
- void* _mrealloc (void *, size_t, const char *, int, const char *);
- char* _mstrdup (const char *, const char *, int, const char *);
- void _mfree (void *, const char *, int, const char *);
+ void* _mmalloc (size_t size, const char *file, int line, const char *func);
+ void* _mcalloc (size_t num, size_t size, const char *file, int line, const char *func);
+ void* _mrealloc (void *p, size_t size, const char *file, int line, const char *func);
+ char* _mstrdup (const char *p, const char *file, int line, const char *func);
+ void _mfree (void *p, const char *file, int line, const char *func);
#else
-# define aMalloc(n) aMalloc_(n,ALC_MARK)
-# define aMallocA(n) aMallocA_(n,ALC_MARK)
-# define aCalloc(m,n) aCalloc_(m,n,ALC_MARK)
+# define aMalloc(n) aMalloc_((n),ALC_MARK)
+# define aMallocA(n) aMallocA_((n),ALC_MARK)
+# define aCalloc(m,n) aCalloc_((m),(n),ALC_MARK)
# define aCallocA(m,n) aCallocA_(m,n,ALC_MARK)
# define aRealloc(p,n) aRealloc_(p,n,ALC_MARK)
# define aStrdup(p) aStrdup_(p,ALC_MARK)
# define aFree(p) aFree_(p,ALC_MARK)
- void* aMalloc_ (size_t, const char *, int, const char *);
- void* aMallocA_ (size_t, const char *, int, const char *);
- void* aCalloc_ (size_t, size_t, const char *, int, const char *);
- void* aCallocA_ (size_t, size_t, const char *, int, const char *);
- void* aRealloc_ (void *, size_t, const char *, int, const char *);
- char* aStrdup_ (const char *, const char *, int, const char *);
- void aFree_ (void *, const char *, int, const char *);
+ void* aMalloc_ (size_t size, const char *file, int line, const char *func);
+ void* aMallocA_ (size_t size, const char *file, int line, const char *func);
+ void* aCalloc_ (size_t num, size_t size, const char *file, int line, const char *func);
+ void* aCallocA_ (size_t num, size_t size, const char *file, int line, const char *func);
+ void* aRealloc_ (void *p, size_t size, const char *file, int line, const char *func);
+ char* aStrdup_ (const char *p, const char *file, int line, const char *func);
+ void aFree_ (void *p, const char *file, int line, const char *func);
#endif
@@ -67,59 +73,60 @@ #ifdef MEMWATCH
# include "memwatch.h"
-# define MALLOC(n) mwMalloc(n,__FILE__, __LINE__)
-# define MALLOCA(n) mwMalloc(n,__FILE__, __LINE__)
-# define CALLOC(m,n) mwCalloc(m,n,__FILE__, __LINE__)
-# define CALLOCA(m,n) mwCalloc(m,n,__FILE__, __LINE__)
-# define REALLOC(p,n) mwRealloc(p,n,__FILE__, __LINE__)
-# define STRDUP(p) mwStrdup(p,__FILE__, __LINE__)
-# define FREE(p) mwFree(p,__FILE__, __LINE__)
+# define MALLOC(n,file,line,func) mwMalloc((n),(file),(line))
+# define MALLOCA(n,file,line,func) mwMalloc((n),(file),(line))
+# define CALLOC(m,n,file,line,func) mwCalloc((m),(n),(file),(line))
+# define CALLOCA(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))
#elif defined(DMALLOC)
# include "dmalloc.h"
-# define MALLOC(n) dmalloc_malloc(__FILE__, __LINE__, (n), DMALLOC_FUNC_MALLOC, 0, 0)
-# define MALLOCA(n) dmalloc_malloc(__FILE__, __LINE__, (n), DMALLOC_FUNC_MALLOC, 0, 0)
-# define CALLOC(m,n) dmalloc_malloc(__FILE__, __LINE__, (m)*(n), DMALLOC_FUNC_CALLOC, 0, 0)
-# define CALLOCA(m,n) dmalloc_malloc(__FILE__, __LINE__, (m)*(n), DMALLOC_FUNC_CALLOC, 0, 0)
-# define REALLOC(p,n) dmalloc_realloc(__FILE__, __LINE__, (p), (n), DMALLOC_FUNC_REALLOC, 0)
-# define STRDUP(p) strdup(p)
-# define FREE(p) free(p)
+# define MALLOC(n,file,line,func) dmalloc_malloc((file),(line),(n),DMALLOC_FUNC_MALLOC,0,0)
+# define MALLOCA(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 CALLOCA(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)
#elif defined(GCOLLECT)
# include "gc.h"
-# define MALLOC(n) GC_MALLOC(n)
-# define MALLOCA(n) GC_MALLOC_ATOMIC(n)
-# define CALLOC(m,n) _bcalloc(m,n)
-# define CALLOCA(m,n) _bcallocA(m,n)
-# define REALLOC(p,n) GC_REALLOC(p,n)
-# define STRDUP(p) _bstrdup(p)
-# define FREE(p) GC_FREE(p)
+# define MALLOC(n,file,line,func) GC_MALLOC(n)
+# define MALLOCA(n,file,line,func) GC_MALLOC_ATOMIC(n)
+# define CALLOC(m,n,file,line,func) _bcalloc((m),(n))
+# define CALLOCA(m,n,file,line,func) _bcallocA((m),(n))
+# define REALLOC(p,n,file,line,func) GC_REALLOC((p),(n))
+# define STRDUP(p,file,line,func) _bstrdup(p)
+# define FREE(p,file,line,func) GC_FREE(p)
void * _bcalloc(size_t, size_t);
void * _bcallocA(size_t, size_t);
char * _bstrdup(const char *);
+/* FIXME Why is this the same as #else? [FlavioJS]
#elif defined(BCHECK)
-# define MALLOC(n) malloc(n)
-# define MALLOCA(n) malloc(n)
-# define CALLOC(m,n) calloc(m,n)
-# define CALLOCA(m,n) calloc(m,n)
-# define REALLOC(p,n) realloc(p,n)
-# define STRDUP(p) strdup(p)
-# define FREE(p) free(p)
-
+# define MALLOC(n,file,line,func) malloc(n)
+# define MALLOCA(n,file,line,func) malloc(n)
+# define CALLOC(m,n,file,line,func) calloc((m),(n))
+# define CALLOCA(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)
+*/
#else
-# define MALLOC(n) malloc(n)
-# define MALLOCA(n) malloc(n)
-# define CALLOC(m,n) calloc(m,n)
-# define CALLOCA(m,n) calloc(m,n)
-# define REALLOC(p,n) realloc(p,n)
-# define STRDUP(p) strdup(p)
-# define FREE(p) free(p)
+# define MALLOC(n,file,line,func) malloc(n)
+# define MALLOCA(n,file,line,func) malloc(n)
+# define CALLOC(m,n,file,line,func) calloc((m),(n))
+# define CALLOCA(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)
#endif
|