From 5f2106f5128314cc392881718d8c1794c9f117fa Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Thu, 15 May 2014 19:45:51 +0200 Subject: [PATCH] Maintain size of allocated memory in the heap. --- libc/stdlib/heap.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libc/stdlib/heap.cpp b/libc/stdlib/heap.cpp index af5dd0ec..ca0a9e74 100644 --- a/libc/stdlib/heap.cpp +++ b/libc/stdlib/heap.cpp @@ -221,6 +221,9 @@ size_t heapmaxsize = SIZE_MAX; // excludes the wilderness. size_t heapsize = 0; +// How many bytes of actual storage the heap contains. +size_t heapstorage = 0; + // bins[N] contain a linked list of chunks that are at least 2^(N+1) // bytes, but less than 2^(N+2) bytes. By selecting the proper bin in // constant time, we can allocate chunks in constant time. @@ -502,6 +505,8 @@ extern "C" void* malloc(size_t size) chunk->magic = MAGIC; chunk->GetTrailer()->magic = MAGIC; + heapstorage += chunk->size; + #if 3 <= PARANOIA assert(ValidateHeap()); #endif @@ -546,6 +551,8 @@ extern "C" void* malloc(size_t size) chunk->magic = MAGIC; trailer->magic = MAGIC; + heapstorage += chunk->size; + #if 3 <= PARANOIA assert(ValidateHeap()); #endif @@ -671,6 +678,8 @@ extern "C" void free(void* addr) assert(chunk->IsUsed()); assert(chunk->IsSane()); + heapstorage -= chunk->size; + UnifyNeighbors(&chunk); bool nexttowilderness = IsRightmostChunk(chunk);