Skip to content

Commit

Permalink
libutils: add malloc_flags() and free_flags()
Browse files Browse the repository at this point in the history
Add malloc_flags() and free_flags() for generic and flexible memory
allocations based on a passed flags field.

Signed-off-by: Jens Wiklander <[email protected]>
  • Loading branch information
jenswi-linaro committed Jan 17, 2025
1 parent ad3d684 commit 3979684
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/libutils/isoc/bget_malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ static void *mem_alloc(uint32_t flags, void *ptr, size_t alignment,
return p;
}

static void mem_free(void *ptr, bool wipe, uint32_t flags)
void free_flags(uint32_t flags, void *ptr)
{
struct malloc_ctx *ctx = get_ctx(flags);
uint32_t exceptions = 0;
Expand All @@ -630,7 +630,7 @@ static void mem_free(void *ptr, bool wipe, uint32_t flags)
ptr = hdr;
}

raw_free(ptr, ctx, wipe);
raw_free(ptr, ctx, flags & MAF_FREE_WIPE);

malloc_unlock(ctx, exceptions);
}
Expand Down Expand Up @@ -707,6 +707,12 @@ void *malloc(size_t size)
return mem_alloc(MAF_NULL, NULL, 1, 1, size, __FILE__, __LINE__);
}

#undef malloc_flags
void *malloc_flags(uint32_t flags, void *ptr, size_t alignment, size_t size)
{
return mem_alloc(flags, ptr, alignment, 1, size, __FILE__, __LINE__);
}

#undef calloc
void *calloc(size_t nmemb, size_t size)
{
Expand Down Expand Up @@ -741,12 +747,12 @@ void *aligned_alloc(size_t alignment, size_t size)

void free(void *ptr)
{
mem_free(ptr, false, MAF_NULL);
free_flags(MAF_NULL, ptr);
}

void free_wipe(void *ptr)
{
mem_free(ptr, true, MAF_NULL);
free_flags(MAF_FREE_WIPE, ptr);
}

static void gen_malloc_add_pool(struct malloc_ctx *ctx, void *buf, size_t len)
Expand Down Expand Up @@ -939,7 +945,7 @@ void nex_mdbg_check(int bufdump)

void nex_free(void *ptr)
{
mem_free(ptr, false, MAF_NEX);
free_flags(MAF_NEX, ptr);
}

void nex_malloc_add_pool(void *buf, size_t len)
Expand Down
6 changes: 6 additions & 0 deletions lib/libutils/isoc/include/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
*/
#define MALLOC_INITIAL_POOL_MIN_SIZE 1024

#define MALLOC_DEFAULT_ALIGNMENT (sizeof(long) * 2)

void *malloc(size_t size);
void *malloc_flags(uint32_t flags, void *ptr, size_t alignment, size_t size);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
void *memalign(size_t alignment, size_t size);
void free(void *ptr);
void free_flags(uint32_t flags, void *ptr);

#if __STDC_VERSION__ >= 201112L
void *aligned_alloc(size_t alignment, size_t size);
Expand All @@ -35,6 +39,8 @@ void mdbg_check(int bufdump);

#define malloc(size) __mdbg_alloc(MAF_NULL, NULL, 1, 1, \
(size), __FILE__, __LINE__)
#define malloc_flags(flags, ptr, align, size) \
__mdbg_alloc((flags), (ptr), (align), 1, (size), __FILE__, __LINE__)
#define calloc(nmemb, size) __mdbg_alloc(MAF_ZERO_INIT, NULL, 1, (nmemb), \
(size), __FILE__, __LINE__)
#define realloc(ptr, size) __mdbg_alloc(MAF_NULL, (ptr), 1, 1, \
Expand Down

0 comments on commit 3979684

Please sign in to comment.