libc: minimal: Add C11 aligned_alloc

Extend capabilities of a minimal libc to support C11 capability
to allocate memory with requested alignment.

Signed-off-by: Piotr Pryga <piotr.pryga@nordicsemi.no>
This commit is contained in:
Piotr Pryga 2022-09-15 18:15:27 +02:00 committed by Carles Cufí
parent be18ed7a98
commit e2e06a74c3
2 changed files with 24 additions and 0 deletions

View file

@ -23,6 +23,8 @@ long long strtoll(const char *nptr, char **endptr, int base);
int atoi(const char *s);
void *malloc(size_t size);
void *aligned_alloc(size_t alignment, size_t size); /* From C11 */
void free(void *ptr);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);

View file

@ -54,6 +54,28 @@ void *malloc(size_t size)
return ret;
}
/* Compile in when C11 */
#if __STDC_VERSION__ >= 201112L
void *aligned_alloc(size_t alignment, size_t size)
{
int lock_ret;
lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER);
__ASSERT_NO_MSG(lock_ret == 0);
void *ret = sys_heap_aligned_alloc(&z_malloc_heap,
alignment,
size);
if (ret == NULL && size != 0) {
errno = ENOMEM;
}
(void) sys_mutex_unlock(&z_malloc_heap_mutex);
return ret;
}
#endif /* __STDC_VERSION__ >= 201112L */
static int malloc_prepare(const struct device *unused)
{
ARG_UNUSED(unused);