* Memory management
*******************/
-void *dm_malloc_aux(size_t s, const char *file, int line);
-#define dm_malloc(s) dm_malloc_aux((s), __FILE__, __LINE__)
-
char *dm_strdup(const char *str);
-#ifdef DEBUG_MEM
-
+void *dm_malloc_aux(size_t s, const char *file, int line);
+void *dm_malloc_aux_debug(size_t s, const char *file, int line);
void dm_free_aux(void *p);
void *dm_realloc_aux(void *p, unsigned int s, const char *file, int line);
-int dm_dump_memory(void);
-void dm_bounds_check(void);
+int dm_dump_memory_debug(void);
+void dm_bounds_check_debug(void);
+
+#ifdef DEBUG_MEM
+# define dm_malloc(s) dm_malloc_aux_debug((s), __FILE__, __LINE__)
# define dm_free(p) dm_free_aux(p)
# define dm_realloc(p, s) dm_realloc_aux(p, s, __FILE__, __LINE__)
+# define dm_dump_memory_debug()
+# define dm_bounds_check_debug()
#else
+# define dm_malloc(s) dm_malloc_aux((s), __FILE__, __LINE__)
# define dm_free(p) free(p)
# define dm_realloc(p, s) realloc(p, s)
# define dm_dump_memory()
#endif
+
/*
* The pool allocator is useful when you are going to allocate
* lots of memory, use the memory for a bit, and then free the
return ret;
}
-#ifdef DEBUG_MEM
-
struct memblock {
struct memblock *prev, *next; /* All allocated blocks are linked */
size_t length; /* Size of the requested block */
static struct memblock *_head = 0;
static struct memblock *_tail = 0;
-void *dm_malloc_aux(size_t s, const char *file, int line)
+void *dm_malloc_aux_debug(size_t s, const char *file, int line)
{
struct memblock *nb;
size_t tsize = s + sizeof(*nb) + sizeof(unsigned long);
nb->file = file;
nb->line = line;
-#ifdef BOUNDS_CHECK
dm_bounds_check();
-#endif
/* setup fields */
nb->magic = nb + 1;
if (!p)
return;
-#ifdef BOUNDS_CHECK
dm_bounds_check();
-#endif
/* sanity check */
assert(mb->magic == p);
void *r;
struct memblock *mb = ((struct memblock *) p) - 1;
- r = dm_malloc_aux(s, file, line);
+ r = dm_malloc_aux_debug(s, file, line);
if (p) {
memcpy(r, p, mb->length);
return r;
}
-int dm_dump_memory(void)
+int dm_dump_memory_debug(void)
{
unsigned long tot = 0;
struct memblock *mb;
return 1;
}
-void dm_bounds_check(void)
+void dm_bounds_check_debug(void)
{
struct memblock *mb = _head;
while (mb) {
}
}
-#else
-
void *dm_malloc_aux(size_t s, const char *file, int line)
{
if (s > 50000000) {
return malloc(s);
}
-
-#endif