diff --git a/src/heap.cpp b/src/heap.cpp index d0dd5e8d45..5ec5ae724d 100644 --- a/src/heap.cpp +++ b/src/heap.cpp @@ -33,7 +33,12 @@ const bool Verbose = false; const bool Verbose2 = false; const bool Debug = false; const bool DebugFixies = false; + +#ifdef NDEBUG const bool DebugAllocation = false; +#else +const bool DebugAllocation = true; +#endif #define ACQUIRE(x) MutexLock MAKE_NAME(monitorLock_) (x) @@ -423,7 +428,9 @@ class Segment { } void dispose() { - free(context, data, (footprint(capacity())) * BytesPerWord); + if (data) { + free(context, data, (footprint(capacity())) * BytesPerWord); + } data = 0; map = 0; } @@ -1675,8 +1682,7 @@ void* tryAllocate(Context* c, unsigned size) ACQUIRE(c->lock); if (DebugAllocation) { - size = pad(size); - size += 2 * BytesPerWord; + size = pad(size) + 2 * BytesPerWord; } if (size + c->count < c->limit) { @@ -1699,21 +1705,21 @@ void* tryAllocate(Context* c, unsigned size) 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) + 2 * BytesPerWord; - size = pad(size); + memset(const_cast(p), 0xFE, size - (2 * BytesPerWord)); p = static_cast(p) - 1; expect(c->system, static_cast(p)[0] == 0x22377322); expect(c->system, static_cast(p) - [1 + (size / BytesPerWord)] == 0x22377322); + [(size / BytesPerWord) - 1] == 0x22377322); } + expect(c->system, c->count >= size); + c->system->free(p); c->count -= size; }