From 03971211efacad02e2a492a5d6f41184bb27ed63 Mon Sep 17 00:00:00 2001 From: jonas Date: Fri, 15 Nov 2024 15:44:16 +0800 Subject: [PATCH] [add]posix_memalign --- .../libc/compilers/armlibc/syscall_mem.c | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/components/libc/compilers/armlibc/syscall_mem.c b/components/libc/compilers/armlibc/syscall_mem.c index f831bd1..92655b4 100644 --- a/components/libc/compilers/armlibc/syscall_mem.c +++ b/components/libc/compilers/armlibc/syscall_mem.c @@ -27,10 +27,20 @@ #pragma import(__use_no_heap) #endif /* __CC_ARM */ +#if defined (__clang__) && defined (RT_USING_CPLUSPLUS) + #define RT_USING_POSIX_MEMALIGN +#endif + +#include + void *malloc(size_t n) { #ifdef RT_USING_HEAP + #ifndef RT_USING_POSIX_MEMALIGN return rt_malloc(n); + #else + return rt_malloc_align(n, RT_ALIGN_SIZE); + #endif #else _NO_HEAP_ERROR(); return RT_NULL; @@ -63,9 +73,36 @@ RTM_EXPORT(calloc); void free(void *rmem) { #ifdef RT_USING_HEAP + #ifndef RT_USING_POSIX_MEMALIGN rt_free(rmem); + #else + rt_free_align(rmem); + #endif #else _NO_HEAP_ERROR(); #endif } RTM_EXPORT(free); + +#if defined RT_USING_POSIX_MEMALIGN +int posix_memalign(void **ptr, size_t align, size_t size) +{ + if ((align & (align - 1)) != 0) + { + return -EINVAL; + } + + if(align != RT_ALIGN(align, sizeof(void *))) + { + return -EINVAL; + } + + *ptr = rt_malloc_align(size, align); + + if(*ptr == RT_NULL) + { + return -ENOMEM; + } + return 0; +} +#endif