Skip to content

Commit

Permalink
Wrap newlib malloc and related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Nov 30, 2024
1 parent 34f79e4 commit 1355737
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,9 @@ add_definitions(-D__HEAP_SIZE=0)
add_definitions(-DMYNEWT_VAL_BLE_LL_RFMGMT_ENABLE_TIME=1500)
add_definitions(-DLFS_CONFIG=lfs_config.h)

# _sbrk is purposefully not implemented so that builds fail when it is used
add_link_options(-Wl,-wrap=malloc -Wl,-wrap=free -Wl,-wrap=calloc -Wl,-wrap=realloc -Wl,-wrap=_malloc_r -Wl,-wrap=_sbrk)

# Note: Only use this for debugging
# Derive the low frequency clock from the main clock (SYNT)
# add_definitions(-DCLOCK_CONFIG_LF_SRC=2)
Expand Down
31 changes: 24 additions & 7 deletions src/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,40 @@ void* malloc(size_t size) {
return pvPortMalloc(size);
}

void* __wrap_malloc(size_t size) {
return malloc(size);
}

void* __wrap__malloc_r(struct _reent* reent, size_t size) {
(void) reent;
return malloc(size);
}

void free(void* ptr) {
vPortFree(ptr);
}

void __wrap_free(void* ptr) {
free(ptr);
}

void* calloc(size_t num, size_t size) {
(void)(num);
(void)(size);
(void) num;
(void) size;
// Not supported
return NULL;
}

void *pvPortRealloc(void *ptr, size_t xWantedSize);
void* realloc( void *ptr, size_t newSize) {
void* __wrap_calloc(size_t num, size_t size) {
return calloc(num, size);
}

void* pvPortRealloc(void* ptr, size_t xWantedSize);

void* realloc(void* ptr, size_t newSize) {
return pvPortRealloc(ptr, newSize);
}

void* _sbrk(int size) {
(void)size;
return *(void **)0; // Force fault
void* __wrap_realloc(void* ptr, size_t newSize) {
return realloc(ptr, newSize);
}

0 comments on commit 1355737

Please sign in to comment.