#include "mem.h"
#include <stdlib.h>
-#if (defined(LEAK_DETECTION) && !defined(NDEBUG))
+#ifdef LEAK_DETECTION
#include <stdbool.h>
#include <stdio.h>
#endif
intptr_t val;
} box_t;
-
/* If LEAK_DETECTION is turned on then we need to disable the mem_allocate macro
* for the duration of this file. This allows us to use the original
* mem_allocate function without modification in mem_allocate_ld */
-#if (defined(LEAK_DETECTION) && !defined(NDEBUG))
+#ifdef LEAK_DETECTION
#undef mem_allocate
#endif
return (void*)(p_obj+1);
}
-#if (defined(LEAK_DETECTION) && !defined(NDEBUG))
+#ifdef LEAK_DETECTION
typedef struct block_t {
void* p_obj;
const char* p_file;
while (NULL != p_curr)
{
block_t* to_be_freed = p_curr;
- printf("%#x %s (line %d): %d references to object\n",
- (unsigned int)p_curr->p_obj,
+ printf("%p %s (line %d): %d references to object\n",
+ p_curr->p_obj,
p_curr->p_file,
p_curr->line,
mem_num_references(p_curr->p_obj));
p_hdr->refcount += 1;
}
-#if (defined(LEAK_DETECTION) && !defined(NDEBUG))
+#ifdef LEAK_DETECTION
static void deregister_block(void* p_obj)
{
block_t* p_prev = NULL;
p_hdr->refcount -= 1;
if(p_hdr->refcount < 1)
{
- #if (defined(LEAK_DETECTION) && !defined(NDEBUG))
+ #ifdef LEAK_DETECTION
deregister_block(p_obj);
#endif
if(p_hdr->p_finalize)
/** A function pointer for object destructors */
typedef void (*destructor_t)(void* p_val);
+/* If debug is disabled, disable leak detection as well */
+#ifdef NDEBUG
+#undef LEAK_DETECTION
+#endif
+
#ifndef LEAK_DETECTION
/**
* @brief Allocates a new reference counted object of the given size which will