From 31976f585ae8b258a493b6b5da93f44ba87752e4 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Thu, 11 Jun 2009 17:23:02 -0600 Subject: [PATCH] add DebugAllocation option to heap.cpp to help detect allocation and deallocation errors --- src/heap.cpp | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/heap.cpp b/src/heap.cpp index f04d5de2c3..d0dd5e8d45 100644 --- a/src/heap.cpp +++ b/src/heap.cpp @@ -33,6 +33,7 @@ const bool Verbose = false; const bool Verbose2 = false; const bool Debug = false; const bool DebugFixies = false; +const bool DebugAllocation = false; #define ACQUIRE(x) MutexLock MAKE_NAME(monitorLock_) (x) @@ -1673,11 +1674,23 @@ void* tryAllocate(Context* c, unsigned size) { ACQUIRE(c->lock); + if (DebugAllocation) { + size = pad(size); + size += 2 * BytesPerWord; + } + if (size + c->count < c->limit) { void* p = c->system->tryAllocate(size); if (p) { c->count += size; - return p; + + if (DebugAllocation) { + static_cast(p)[0] = 0x22377322; + static_cast(p)[(size / BytesPerWord) - 1] = 0x22377322; + return static_cast(p) + 1; + } else { + return p; + } } } return 0; @@ -1687,6 +1700,20 @@ void free(Context* c, const void* p, unsigned size) { ACQUIRE(c->lock); expect(c->system, c->count >= size); + + if (DebugAllocation) { + memset(const_cast(p), 0xFE, size); + + size = pad(size); + + p = static_cast(p) - 1; + + expect(c->system, static_cast(p)[0] == 0x22377322); + + expect(c->system, static_cast(p) + [1 + (size / BytesPerWord)] == 0x22377322); + } + c->system->free(p); c->count -= size; }