Skip to content

Commit

Permalink
Add MEM_CUSTOM_ALLOCATOR and make LIBC a subset of it
Browse files Browse the repository at this point in the history
Allow one to provide a custom implementation of free/malloc/calloc
instead of the lwip internal allocator. The code to use the libc's
implementation already existed, so generalize the existing code and make
the libc variant a specialized case of this new capability, retaining
full backwards compatibility.
  • Loading branch information
paravoid authored and goldsimon committed Oct 11, 2023
1 parent e01c9a9 commit 4f88651
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
7 changes: 5 additions & 2 deletions src/core/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,11 @@ PACK_STRUCT_END
#if (LWIP_ALTCP && LWIP_EVENT_API)
#error "The application layered tcp API does not work with LWIP_EVENT_API"
#endif
#if (MEM_LIBC_MALLOC && MEM_USE_POOLS)
#error "MEM_LIBC_MALLOC and MEM_USE_POOLS may not both be simultaneously enabled in your lwipopts.h"
#if (MEM_CUSTOM_ALLOCATOR && !(defined(MEM_CUSTOM_FREE) && defined(MEM_CUSTOM_MALLOC) && defined(MEM_CUSTOM_CALLOC)))
#error "All of MEM_CUSTOM_FREE/MEM_CUSTOM_MALLOC/MEM_CUSTOM_CALLOC must be provided if MEM_CUSTOM_ALLOCATOR is enabled in your lwipopts.h"
#endif
#if (MEM_USE_POOLS && MEM_CUSTOM_ALLOCATOR)
#error "MEM_USE_POOLS may not be used with a custom allocator (MEM_CUSTOM_ALLOCATOR or MEM_LIBC_MALLOC) enabled in your lwipopts.h"
#endif
#if (MEM_USE_POOLS && !MEMP_USE_CUSTOM_POOLS)
#error "MEM_USE_POOLS requires custom pools (MEMP_USE_CUSTOM_POOLS) to be enabled in your lwipopts.h"
Expand Down
32 changes: 9 additions & 23 deletions src/core/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ mem_overflow_init_raw(void *p, size_t size)
}
#endif /* MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK */

#if MEM_LIBC_MALLOC || MEM_USE_POOLS
#if MEM_CUSTOM_ALLOCATOR || MEM_USE_POOLS

/** mem_init is not used when using pools instead of a heap or using
* C library malloc().
Expand All @@ -172,23 +172,9 @@ mem_trim(void *mem, mem_size_t size)
LWIP_UNUSED_ARG(size);
return mem;
}
#endif /* MEM_LIBC_MALLOC || MEM_USE_POOLS */
#endif /* MEM_CUSTOM_ALLOCATOR || MEM_USE_POOLS */

#if MEM_LIBC_MALLOC
/* lwIP heap implemented using C library malloc() */

/* in case C library malloc() needs extra protection,
* allow these defines to be overridden.
*/
#ifndef mem_clib_free
#define mem_clib_free free
#endif
#ifndef mem_clib_malloc
#define mem_clib_malloc malloc
#endif
#ifndef mem_clib_calloc
#define mem_clib_calloc calloc
#endif
#if MEM_CUSTOM_ALLOCATOR

#if LWIP_STATS && MEM_STATS
#define MEM_LIBC_STATSHELPER_SIZE LWIP_MEM_ALIGN_SIZE(sizeof(mem_size_t))
Expand All @@ -207,7 +193,7 @@ mem_trim(void *mem, mem_size_t size)
void *
mem_malloc(mem_size_t size)
{
void *ret = mem_clib_malloc(size + MEM_LIBC_STATSHELPER_SIZE);
void *ret = MEM_CUSTOM_MALLOC(size + MEM_LIBC_STATSHELPER_SIZE);
if (ret == NULL) {
MEM_STATS_INC_LOCKED(err);
} else {
Expand All @@ -234,7 +220,7 @@ mem_free(void *rmem)
rmem = (u8_t *)rmem - MEM_LIBC_STATSHELPER_SIZE;
MEM_STATS_DEC_USED_LOCKED(used, *(mem_size_t *)rmem);
#endif
mem_clib_free(rmem);
MEM_CUSTOM_FREE(rmem);
}

#elif MEM_USE_POOLS
Expand Down Expand Up @@ -978,14 +964,14 @@ mem_malloc(mem_size_t size_in)

#endif /* MEM_USE_POOLS */

#if MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS)
#if MEM_CUSTOM_ALLOCATOR && (!LWIP_STATS || !MEM_STATS)
void *
mem_calloc(mem_size_t count, mem_size_t size)
{
return mem_clib_calloc(count, size);
return MEM_CUSTOM_CALLOC(count, size);
}

#else /* MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS) */
#else /* MEM_CUSTOM_ALLOCATOR && (!LWIP_STATS || !MEM_STATS) */
/**
* Contiguously allocates enough space for count objects that are size bytes
* of memory each and returns a pointer to the allocated memory.
Expand Down Expand Up @@ -1015,4 +1001,4 @@ mem_calloc(mem_size_t count, mem_size_t size)
}
return p;
}
#endif /* MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS) */
#endif /* MEM_CUSTOM_ALLOCATOR && (!LWIP_STATS || !MEM_STATS) */
2 changes: 1 addition & 1 deletion src/include/lwip/mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
extern "C" {
#endif

#if MEM_LIBC_MALLOC
#if MEM_CUSTOM_ALLOCATOR

#include "lwip/arch.h"

Expand Down
22 changes: 20 additions & 2 deletions src/include/lwip/opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,28 @@
/**
* MEM_LIBC_MALLOC==1: Use malloc/free/realloc provided by your C-library
* instead of the lwip internal allocator. Can save code size if you
* already use it.
* already use it. Specialized case of MEM_CUSTOM_ALLOCATOR.
* @see MEM_CUSTOM_ALLOCATOR
*/
#if !defined MEM_LIBC_MALLOC || defined __DOXYGEN__
#define MEM_LIBC_MALLOC 0
#elif MEM_LIBC_MALLOC
#define MEM_CUSTOM_ALLOCATOR 1
#define MEM_CUSTOM_FREE free
#define MEM_CUSTOM_MALLOC malloc
#define MEM_CUSTOM_CALLOC calloc
#endif

/**
* MEM_CUSTOM_ALLOCATOR==1: Use malloc/free/realloc provided by a custom
* implementation instead of the lwip internal allocator. Can save code size if
* you already use it. If enabled, you have to define those functions:
* \#define MEM_CUSTOM_FREE my_free
* \#define MEM_CUSTOM_MALLOC my_malloc
* \#define MEM_CUSTOM_CALLOC my_calloc
*/
#if !defined MEM_CUSTOM_ALLOCATOR || defined __DOXYGEN__
#define MEM_CUSTOM_ALLOCATOR 0
#endif

/**
Expand Down Expand Up @@ -2238,7 +2256,7 @@
* MEM_STATS==1: Enable mem.c stats.
*/
#if !defined MEM_STATS || defined __DOXYGEN__
#define MEM_STATS ((MEM_LIBC_MALLOC == 0) && (MEM_USE_POOLS == 0))
#define MEM_STATS ((MEM_CUSTOM_ALLOCATOR == 0) && (MEM_USE_POOLS == 0))
#endif

/**
Expand Down

0 comments on commit 4f88651

Please sign in to comment.