diff options
author | Brian Paul <brianp@vmware.com> | 2013-04-03 13:36:50 -0600 |
---|---|---|
committer | Brian Paul <brianp@vmware.com> | 2013-04-04 08:50:15 -0600 |
commit | 92e5e45ff1147232699be9e517c70146e6ed0291 (patch) | |
tree | 57d5a076d2e036e419257f24cf36f71c4fc947e0 | |
parent | a408ea96920984ad2c5bf81f799cbb583f264105 (diff) |
util: add debug_memory_check_block(), debug_memory_tag()
The former just checks that the given block is valid by checking
the header and footer.
The later sets the memory block's tag. With extra debug code, we
can use that for monitoring/checking particular allocations.
Reviewed-by: José Fonseca <jfonseca@vmware.com>
-rw-r--r-- | src/gallium/auxiliary/os/os_memory_debug.h | 6 | ||||
-rw-r--r-- | src/gallium/auxiliary/util/u_debug_memory.c | 55 |
2 files changed, 61 insertions, 0 deletions
diff --git a/src/gallium/auxiliary/os/os_memory_debug.h b/src/gallium/auxiliary/os/os_memory_debug.h index 36b8fc63a6e..9a487dec05c 100644 --- a/src/gallium/auxiliary/os/os_memory_debug.h +++ b/src/gallium/auxiliary/os/os_memory_debug.h @@ -60,6 +60,12 @@ void * debug_realloc(const char *file, unsigned line, const char *function, void *old_ptr, size_t old_size, size_t new_size ); +void +debug_memory_tag(void *ptr, unsigned tag); + +void +debug_memory_check_block(void *ptr); + void debug_memory_check(void); diff --git a/src/gallium/auxiliary/util/u_debug_memory.c b/src/gallium/auxiliary/util/u_debug_memory.c index 4bf26a52414..4723547819e 100644 --- a/src/gallium/auxiliary/util/u_debug_memory.c +++ b/src/gallium/auxiliary/util/u_debug_memory.c @@ -76,6 +76,7 @@ struct debug_memory_header #endif unsigned magic; + unsigned tag; }; struct debug_memory_footer @@ -140,6 +141,7 @@ debug_malloc(const char *file, unsigned line, const char *function, hdr->function = function; hdr->size = size; hdr->magic = DEBUG_MEMORY_MAGIC; + hdr->tag = 0; #if DEBUG_FREED_MEMORY hdr->freed = FALSE; #endif @@ -263,6 +265,7 @@ debug_realloc(const char *file, unsigned line, const char *function, new_hdr->function = old_hdr->function; new_hdr->size = new_size; new_hdr->magic = DEBUG_MEMORY_MAGIC; + new_hdr->tag = 0; #if DEBUG_FREED_MEMORY new_hdr->freed = FALSE; #endif @@ -348,6 +351,58 @@ debug_memory_end(unsigned long start_no) /** + * Put a tag (arbitrary integer) on a memory block. + * Can be useful for debugging. + */ +void +debug_memory_tag(void *ptr, unsigned tag) +{ + struct debug_memory_header *hdr; + + if (!ptr) + return; + + hdr = header_from_data(ptr); + if (hdr->magic != DEBUG_MEMORY_MAGIC) { + debug_printf("%s corrupted memory at %p\n", __FUNCTION__, ptr); + debug_assert(0); + } + + hdr->tag = tag; +} + + +/** + * Check the given block of memory for validity/corruption. + */ +void +debug_memory_check_block(void *ptr) +{ + struct debug_memory_header *hdr; + struct debug_memory_footer *ftr; + + if (!ptr) + return; + + hdr = header_from_data(ptr); + ftr = footer_from_header(hdr); + + if (hdr->magic != DEBUG_MEMORY_MAGIC) { + debug_printf("%s:%u:%s: bad or corrupted memory %p\n", + hdr->file, hdr->line, hdr->function, ptr); + debug_assert(0); + } + + if (ftr->magic != DEBUG_MEMORY_MAGIC) { + debug_printf("%s:%u:%s: buffer overflow %p\n", + hdr->file, hdr->line, hdr->function, ptr); + debug_assert(0); + } +} + + + +/** * We can periodically call this from elsewhere to do a basic sanity * check of the heap memory we've allocated. */ |