#include "pipe/p_config.h"
-#if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY)
-#include <windows.h>
-#include <winddi.h>
-#elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
-#include <wdm.h>
-#else
-#include <stdio.h>
-#include <stdlib.h>
-#endif
+#define DEBUG_MEMORY_IMPLEMENTATION
+
+#include "os/os_memory.h"
+#include "os/os_memory_debug.h"
+#include "os/os_thread.h"
#include "util/u_debug.h"
+#include "util/u_debug_stack.h"
#include "util/u_double_list.h"
#define DEBUG_MEMORY_MAGIC 0x6e34090aU
-
-
-#if defined(PIPE_SUBSYSTEM_WINDOWS_DISPLAY) && !defined(WINCE)
-#define real_malloc(_size) EngAllocMem(0, _size, 'D3AG')
-#define real_free(_ptr) EngFreeMem(_ptr)
-#elif defined(PIPE_SUBSYSTEM_WINDOWS_MINIPORT)
-#define real_malloc(_size) ExAllocatePool(0, _size)
-#define real_free(_ptr) ExFreePool(_ptr)
-#else
-#define real_malloc(_size) malloc(_size)
-#define real_free(_ptr) free(_ptr)
-#endif
+#define DEBUG_MEMORY_STACK 0 /* XXX: disabled until we have symbol lookup */
struct debug_memory_header
const char *file;
unsigned line;
const char *function;
+#if DEBUG_MEMORY_STACK
+ struct debug_stack_frame backtrace[DEBUG_MEMORY_STACK];
+#endif
size_t size;
+
unsigned magic;
};
static struct list_head list = { &list, &list };
+pipe_static_mutex(list_mutex);
+
static unsigned long last_no = 0;
struct debug_memory_header *hdr;
struct debug_memory_footer *ftr;
- hdr = real_malloc(sizeof(*hdr) + size + sizeof(*ftr));
+ hdr = os_malloc(sizeof(*hdr) + size + sizeof(*ftr));
if(!hdr) {
debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
file, line, function,
hdr->size = size;
hdr->magic = DEBUG_MEMORY_MAGIC;
+#if DEBUG_MEMORY_STACK
+ debug_backtrace_capture(hdr->backtrace, 0, DEBUG_MEMORY_STACK);
+#endif
+
ftr = footer_from_header(hdr);
ftr->magic = DEBUG_MEMORY_MAGIC;
+ pipe_mutex_lock(list_mutex);
LIST_ADDTAIL(&hdr->head, &list);
+ pipe_mutex_unlock(list_mutex);
return data_from_header(hdr);
}
debug_assert(0);
}
+ pipe_mutex_lock(list_mutex);
LIST_DEL(&hdr->head);
+ pipe_mutex_unlock(list_mutex);
hdr->magic = 0;
ftr->magic = 0;
- real_free(hdr);
+ os_free(hdr);
}
void *
}
/* alloc new */
- new_hdr = real_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr));
+ new_hdr = os_malloc(sizeof(*new_hdr) + new_size + sizeof(*new_ftr));
if(!new_hdr) {
debug_printf("%s:%u:%s: out of memory when trying to allocate %lu bytes\n",
file, line, function,
new_ftr = footer_from_header(new_hdr);
new_ftr->magic = DEBUG_MEMORY_MAGIC;
+ pipe_mutex_lock(list_mutex);
LIST_REPLACE(&old_hdr->head, &new_hdr->head);
+ pipe_mutex_unlock(list_mutex);
/* copy data */
new_ptr = data_from_header(new_hdr);
/* free old */
old_hdr->magic = 0;
old_ftr->magic = 0;
- real_free(old_hdr);
+ os_free(old_hdr);
return new_ptr;
}
if((start_no <= hdr->no && hdr->no < last_no) ||
(last_no < start_no && (hdr->no < last_no || start_no <= hdr->no))) {
- debug_printf("%s:%u:%s: %u bytes at %p not freed\n",
+ debug_printf("%s:%u:%s: %lu bytes at %p not freed\n",
hdr->file, hdr->line, hdr->function,
- hdr->size, ptr);
+ (unsigned long) hdr->size, ptr);
+#if DEBUG_MEMORY_STACK
+ debug_backtrace_dump(hdr->backtrace, DEBUG_MEMORY_STACK);
+#endif
total_size += hdr->size;
}
}
if(total_size) {
- debug_printf("Total of %u KB of system memory apparently leaked\n",
- (total_size + 1023)/1024);
+ debug_printf("Total of %lu KB of system memory apparently leaked\n",
+ (unsigned long) (total_size + 1023)/1024);
}
else {
debug_printf("No memory leaks detected.\n");