Skip to content

Commit

Permalink
[WIP]: needs cleaner API interface, decision to move away from pseudo…
Browse files Browse the repository at this point in the history
…module, use conf instead?
  • Loading branch information
mguetschow committed Feb 8, 2024
1 parent c0f233d commit eb6c0f6
Showing 1 changed file with 97 additions and 23 deletions.
120 changes: 97 additions & 23 deletions sys/malloc_thread_safe/malloc_wrappers.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,100 @@ extern void *__real_realloc(void *ptr, size_t size);

static mutex_t _lock;

void __attribute__((used)) *__wrap_malloc(size_t size)
#define ALLOC_MAX (100)
struct {
void *addr[ALLOC_MAX];
size_t size[ALLOC_MAX];
size_t current;
size_t all_time_max;
} malloc_state = {
.addr = {NULL},
.current = 0,
.all_time_max = 0,
};

void malloc_state_add(void *ptr, size_t size)
{
uinttxtptr_t pc;
if (IS_USED(MODULE_MALLOC_TRACING)) {
pc = cpu_get_caller_pc();
if (ptr == NULL) return;

Check warning on line 49 in sys/malloc_thread_safe/malloc_wrappers.c

View workflow job for this annotation

GitHub Actions / static-tests

full block {} expected in the control structure
for (uint8_t i=0; i<ALLOC_MAX; i++) {
if (malloc_state.addr[i] == NULL) {
malloc_state.addr[i] = ptr;
malloc_state.size[i] = size;
malloc_state.current += size;
if (malloc_state.current > malloc_state.all_time_max)
malloc_state.all_time_max = malloc_state.current;

Check warning on line 56 in sys/malloc_thread_safe/malloc_wrappers.c

View workflow job for this annotation

GitHub Actions / static-tests

full block {} expected in the control structure
return;
}
}
}

void malloc_state_rm(void *ptr)
{
if (ptr == NULL) return;

Check warning on line 64 in sys/malloc_thread_safe/malloc_wrappers.c

View workflow job for this annotation

GitHub Actions / static-tests

full block {} expected in the control structure
for (uint8_t i=0; i<ALLOC_MAX; i++) {
if (malloc_state.addr[i] == ptr) {
malloc_state.addr[i] = NULL;
malloc_state.current -= malloc_state.size[i];
return;
}
}
}

void malloc_state_mv(void *ptr_old, void *ptr_new, size_t size_new)
{
if (ptr_old == NULL) return malloc_state_add(ptr_new, size_new);

Check warning on line 76 in sys/malloc_thread_safe/malloc_wrappers.c

View workflow job for this annotation

GitHub Actions / static-tests

full block {} expected in the control structure
if (size_new == 0) return malloc_state_rm(ptr_old);

Check warning on line 77 in sys/malloc_thread_safe/malloc_wrappers.c

View workflow job for this annotation

GitHub Actions / static-tests

full block {} expected in the control structure
if (ptr_new == NULL) return;

Check warning on line 78 in sys/malloc_thread_safe/malloc_wrappers.c

View workflow job for this annotation

GitHub Actions / static-tests

full block {} expected in the control structure
for (uint8_t i=0; i<ALLOC_MAX; i++) {
if (malloc_state.addr[i] == ptr_old) {
malloc_state.addr[i] = ptr_new;
size_t size_old = malloc_state.size[i];
malloc_state.size[i] = size_new;
if (size_new > size_old) {
malloc_state.current += size_new - size_old;
if (malloc_state.current > malloc_state.all_time_max)
malloc_state.all_time_max = malloc_state.current;

Check warning on line 87 in sys/malloc_thread_safe/malloc_wrappers.c

View workflow job for this annotation

GitHub Actions / static-tests

full block {} expected in the control structure
}
else {
malloc_state.current -= size_old - size_new;
}
return;
}
}
}

void __attribute__((used)) *__wrap_malloc(size_t size)
{
// uinttxtptr_t pc;
// if (IS_USED(MODULE_MALLOC_TRACING)) {
// pc = cpu_get_caller_pc();
// }
assert(!irq_is_in());
mutex_lock(&_lock);
void *ptr = __real_malloc(size);
mutex_unlock(&_lock);
if (IS_USED(MODULE_MALLOC_TRACING)) {
printf("malloc(%" PRIuSIZE ") @ 0x%" PRIxTXTPTR " returned %p\n",
size, pc, ptr);
malloc_state_add(ptr, size);
}
mutex_unlock(&_lock);
// if (IS_USED(MODULE_MALLOC_TRACING)) {
// printf("malloc(%" PRIuSIZE ") @ 0x%" PRIxTXTPTR " returned %p\n",
// size, pc, ptr);
// }
return ptr;
}

void __attribute__((used)) __wrap_free(void *ptr)
{
if (IS_USED(MODULE_MALLOC_TRACING)) {
uinttxtptr_t pc = cpu_get_caller_pc();
printf("free(%p) @ 0x%" PRIxTXTPTR ")\n", ptr, pc);
}
// if (IS_USED(MODULE_MALLOC_TRACING)) {
// uinttxtptr_t pc = cpu_get_caller_pc();
// printf("free(%p) @ 0x%" PRIxTXTPTR ")\n", ptr, pc);
// }
assert(!irq_is_in());
mutex_lock(&_lock);
__real_free(ptr);
if (IS_USED(MODULE_MALLOC_TRACING)) {
malloc_state_rm(ptr);
}
mutex_unlock(&_lock);
}

Expand All @@ -81,35 +149,41 @@ void * __attribute__((used)) __wrap_calloc(size_t nmemb, size_t size)

mutex_lock(&_lock);
void *res = __real_malloc(total_size);
if (IS_USED(MODULE_MALLOC_TRACING)) {
malloc_state_add(res, total_size);
}
mutex_unlock(&_lock);
if (res) {
memset(res, 0, total_size);
}

if (IS_USED(MODULE_MALLOC_TRACING)) {
printf("calloc(%" PRIuSIZE ", %" PRIuSIZE ") @ 0x%" PRIxTXTPTR " returned %p\n",
nmemb, size, pc, res);
}
// if (IS_USED(MODULE_MALLOC_TRACING)) {
// printf("calloc(%" PRIuSIZE ", %" PRIuSIZE ") @ 0x%" PRIxTXTPTR " returned %p\n",
// nmemb, size, pc, res);
// }

return res;
}

void * __attribute__((used))__wrap_realloc(void *ptr, size_t size)
{
uinttxtptr_t pc;
if (IS_USED(MODULE_MALLOC_TRACING)) {
pc = cpu_get_caller_pc();
}
// uinttxtptr_t pc;
// if (IS_USED(MODULE_MALLOC_TRACING)) {
// pc = cpu_get_caller_pc();
// }

assert(!irq_is_in());
mutex_lock(&_lock);
void *new = __real_realloc(ptr, size);
mutex_unlock(&_lock);

if (IS_USED(MODULE_MALLOC_TRACING)) {
printf("realloc(%p, %" PRIuSIZE ") @0x%" PRIxTXTPTR " returned %p\n",
ptr, size, pc, new);
malloc_state_mv(ptr, new, size);
}
mutex_unlock(&_lock);

// if (IS_USED(MODULE_MALLOC_TRACING)) {
// printf("realloc(%p, %" PRIuSIZE ") @0x%" PRIxTXTPTR " returned %p\n",
// ptr, size, pc, new);
// }
return new;
}

Expand Down

0 comments on commit eb6c0f6

Please sign in to comment.