Skip to content

Commit

Permalink
libutils: refactor the nexus malloc functions
Browse files Browse the repository at this point in the history
Refactor the malloc functions operating on the nexus heap as simple
wrappers around the recently added internal function mem_alloc().

Signed-off-by: Jens Wiklander <[email protected]>
Reviewed-by: Jerome Forissier <[email protected]>
  • Loading branch information
jenswi-linaro committed Jan 20, 2025
1 parent a3e702b commit 82ef6b1
Showing 1 changed file with 14 additions and 25 deletions.
39 changes: 14 additions & 25 deletions lib/libutils/isoc/bget_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,19 @@ void *realloc(void *ptr, size_t size)

#else /* ENABLE_MDBG */

static struct malloc_ctx *get_ctx(uint32_t flags __maybe_unused)
{
#ifdef CFG_NS_VIRTUALIZATION
if (flags & MAF_NEX)
return &nex_malloc_ctx;
#endif
return &malloc_ctx;
}

static void *mem_alloc(uint32_t flags, void *ptr, size_t alignment,
size_t nmemb, size_t size)
{
struct malloc_ctx *ctx = &malloc_ctx;
struct malloc_ctx *ctx = get_ctx(flags);
uint32_t exceptions = 0;
void *p = NULL;

Expand Down Expand Up @@ -996,42 +1005,22 @@ bool malloc_buffer_overlaps_heap(void *buf, size_t len)

void *nex_malloc(size_t size)
{
void *p;
uint32_t exceptions = malloc_lock(&nex_malloc_ctx);

p = raw_malloc(0, 0, size, &nex_malloc_ctx);
malloc_unlock(&nex_malloc_ctx, exceptions);
return p;
return mem_alloc(MAF_NEX, NULL, 1, 1, size);
}

void *nex_calloc(size_t nmemb, size_t size)
{
void *p;
uint32_t exceptions = malloc_lock(&nex_malloc_ctx);

p = raw_calloc(0, 0, nmemb, size, &nex_malloc_ctx);
malloc_unlock(&nex_malloc_ctx, exceptions);
return p;
return mem_alloc(MAF_NEX | MAF_ZERO_INIT, NULL, 1, nmemb, size);
}

void *nex_realloc(void *ptr, size_t size)
{
void *p;
uint32_t exceptions = malloc_lock(&nex_malloc_ctx);

p = realloc_unlocked(&nex_malloc_ctx, ptr, size);
malloc_unlock(&nex_malloc_ctx, exceptions);
return p;
return mem_alloc(MAF_NEX, ptr, 1, 1, size);
}

void *nex_memalign(size_t alignment, size_t size)
{
void *p;
uint32_t exceptions = malloc_lock(&nex_malloc_ctx);

p = raw_memalign(0, 0, alignment, size, &nex_malloc_ctx);
malloc_unlock(&nex_malloc_ctx, exceptions);
return p;
return mem_alloc(MAF_NEX, NULL, alignment, 1, size);
}

void nex_free(void *ptr)
Expand Down

0 comments on commit 82ef6b1

Please sign in to comment.