Maintain size of allocated memory in the heap.
This commit is contained in:
parent
6971cc59b0
commit
5f2106f512
|
@ -221,6 +221,9 @@ size_t heapmaxsize = SIZE_MAX;
|
||||||
// excludes the wilderness.
|
// excludes the wilderness.
|
||||||
size_t heapsize = 0;
|
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)
|
// 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
|
// bytes, but less than 2^(N+2) bytes. By selecting the proper bin in
|
||||||
// constant time, we can allocate chunks in constant time.
|
// constant time, we can allocate chunks in constant time.
|
||||||
|
@ -502,6 +505,8 @@ extern "C" void* malloc(size_t size)
|
||||||
chunk->magic = MAGIC;
|
chunk->magic = MAGIC;
|
||||||
chunk->GetTrailer()->magic = MAGIC;
|
chunk->GetTrailer()->magic = MAGIC;
|
||||||
|
|
||||||
|
heapstorage += chunk->size;
|
||||||
|
|
||||||
#if 3 <= PARANOIA
|
#if 3 <= PARANOIA
|
||||||
assert(ValidateHeap());
|
assert(ValidateHeap());
|
||||||
#endif
|
#endif
|
||||||
|
@ -546,6 +551,8 @@ extern "C" void* malloc(size_t size)
|
||||||
chunk->magic = MAGIC;
|
chunk->magic = MAGIC;
|
||||||
trailer->magic = MAGIC;
|
trailer->magic = MAGIC;
|
||||||
|
|
||||||
|
heapstorage += chunk->size;
|
||||||
|
|
||||||
#if 3 <= PARANOIA
|
#if 3 <= PARANOIA
|
||||||
assert(ValidateHeap());
|
assert(ValidateHeap());
|
||||||
#endif
|
#endif
|
||||||
|
@ -671,6 +678,8 @@ extern "C" void free(void* addr)
|
||||||
assert(chunk->IsUsed());
|
assert(chunk->IsUsed());
|
||||||
assert(chunk->IsSane());
|
assert(chunk->IsSane());
|
||||||
|
|
||||||
|
heapstorage -= chunk->size;
|
||||||
|
|
||||||
UnifyNeighbors(&chunk);
|
UnifyNeighbors(&chunk);
|
||||||
|
|
||||||
bool nexttowilderness = IsRightmostChunk(chunk);
|
bool nexttowilderness = IsRightmostChunk(chunk);
|
||||||
|
|
Loading…
Reference in New Issue