Skip to content

Commit

Permalink
libc/common: Create z_ aliases for malloc family functions
Browse files Browse the repository at this point in the history
These aliases will be used to direct libc allocations to the common
allocator during linking.

Signed-off-by: Keith Packard <[email protected]>
  • Loading branch information
keith-packard committed Nov 19, 2023
1 parent a3157ce commit dc4b9b4
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/libc/common/source/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
#include <zephyr/sys/mem_manage.h>
#endif

#ifndef __clang__
#pragma GCC diagnostic ignored "-Wmissing-attributes"
#endif

#define __z_reference(sym) \
extern __typeof(sym) z_ ## sym __attribute__((__alias__(__STRING(sym))))


#define LOG_LEVEL CONFIG_KERNEL_LOG_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL);
Expand Down Expand Up @@ -188,6 +196,9 @@ void *memalign(size_t alignment, size_t size)
{
return aligned_alloc(alignment, size);
}

__z_reference(memalign);

#endif

static int malloc_prepare(void)
Expand Down Expand Up @@ -304,6 +315,9 @@ void *calloc(size_t nmemb, size_t size)

return ret;
}

__z_reference(calloc);

#endif /* CONFIG_COMMON_LIBC_CALLOC */

#ifdef CONFIG_COMMON_LIBC_REALLOCARRAY
Expand All @@ -315,4 +329,12 @@ void *reallocarray(void *ptr, size_t nmemb, size_t size)
}
return realloc(ptr, size);
}

__z_reference(reallocarray);

#endif /* CONFIG_COMMON_LIBC_REALLOCARRAY */

__z_reference(malloc);
__z_reference(aligned_alloc);
__z_reference(free);
__z_reference(realloc);

0 comments on commit dc4b9b4

Please sign in to comment.