Skip to content

Commit

Permalink
Added some debug on some memory functions. If MEMORY_DEBUG is set to …
Browse files Browse the repository at this point in the history
…1 (with DEBUG ON) at the end of program we can see if there are unfreed memory allocations
  • Loading branch information
afxgroup committed Jan 12, 2025
1 parent fd41a96 commit 7362b7e
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions library/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ STDLIB_DESTRUCTOR(stdlib_memory_exit) {
__memory_lock(__clib4);

if (__clib4->__wmem_allocator != NULL) {
SHOWMSG("Destroying Memory Allocator");
wmem_destroy_allocator(__clib4->__wmem_allocator);
#if MEMORY_DEBUG
if (__clib4->allocated_memory_by_malloc > 0) {
Printf("WARNING: There are %ld unfreed malloc!\n", __clib4->allocated_memory_by_malloc);
}
#endif
SHOWMSG("Done");
__clib4->__wmem_allocator = NULL;
}

Expand Down
3 changes: 3 additions & 0 deletions library/stdlib/realloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ realloc(void *ptr, size_t size) {
void *result = NULL;
struct _clib4 *__clib4 = __CLIB4;

ENTER();

assert((int) size >= 0);

__memory_lock(__clib4);
Expand All @@ -31,5 +33,6 @@ realloc(void *ptr, size_t size) {
if (result == NULL)
SHOWMSG("ouch! realloc failed");

LEAVE();
return result;
}
6 changes: 5 additions & 1 deletion library/wmem/wmem_allocator_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -1019,12 +1019,14 @@ wmem_block_gc(void *private_data) {
* completely destroying unused blocks. */
cur = allocator->block_list;
allocator->block_list = NULL;

ENTER();
while (cur) {
SHOWMSG("found block");
chunk = WMEM_BLOCK_TO_CHUNK(cur);
next = cur->next;

if (!chunk->jumbo && !chunk->used && chunk->last) {
SHOWMSG("free block");
/* If the first chunk is also the last, and is unused, then
* the block as a whole is entirely unused, so return it to
* the OS and remove it from whatever lists it is in. */
Expand All @@ -1047,11 +1049,13 @@ wmem_block_gc(void *private_data) {
wmem_free(NULL, cur);
} else {
/* part of this block is used, so add it to the new block list */
SHOWMSG("part of this block is used");
wmem_block_add_to_block_list(allocator, cur);
}

cur = next;
}
LEAVE();
}

static void
Expand Down
9 changes: 9 additions & 0 deletions library/wmem/wmem_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ static wmem_allocator_type_t override_type;

void *
wmem_alloc(wmem_allocator_t *allocator, const size_t size) {
#if MEMORY_DEBUG
__CLIB4->allocated_memory_by_malloc++;
D(("Allocated %ld bytes chunk of memory. Allocations now are: %ld", size, __CLIB4->allocated_memory_by_malloc));
#endif

if (allocator == NULL) {
void *r = AllocVecTags(size, AVT_Type, MEMF_PRIVATE, TAG_DONE);
#if MEMORY_DEBUG
Expand Down Expand Up @@ -66,6 +71,10 @@ wmem_alloc0(wmem_allocator_t *allocator, const size_t size) {

void
wmem_free(wmem_allocator_t *allocator, void *ptr) {
#if MEMORY_DEBUG
__CLIB4->allocated_memory_by_malloc--;
D(("Freed chunk of memory. Allocations now are %ld", __CLIB4->allocated_memory_by_malloc));
#endif
if (allocator == NULL) {
FreeVec(ptr);
ptr = NULL;
Expand Down
6 changes: 6 additions & 0 deletions library/wmem/wmem_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ __BEGIN_DECLS
*/

// Set to 1 if you want to track memory allocations
#ifndef MEMORY_DEBUG
#define MEMORY_DEBUG 0
#else
#ifndef DEBUG
#error "MEMORY_DEBUG NEEDS DEBUG FLAG ON"
#endif
#endif

struct _wmem_allocator_t;
/** A public opaque type representing one wmem allocation pool. */
Expand Down

0 comments on commit 7362b7e

Please sign in to comment.