diff --git a/src/core/init.c b/src/core/init.c index 0013a8998..b906eff43 100644 --- a/src/core/init.c +++ b/src/core/init.c @@ -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" diff --git a/src/core/mem.c b/src/core/mem.c index 67ee56750..f94fb923b 100644 --- a/src/core/mem.c +++ b/src/core/mem.c @@ -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(). @@ -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)) @@ -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 { @@ -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 @@ -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. @@ -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) */ diff --git a/src/include/lwip/mem.h b/src/include/lwip/mem.h index ff208d25c..6819ee488 100644 --- a/src/include/lwip/mem.h +++ b/src/include/lwip/mem.h @@ -43,7 +43,7 @@ extern "C" { #endif -#if MEM_LIBC_MALLOC +#if MEM_CUSTOM_ALLOCATOR #include "lwip/arch.h" diff --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h index 1ce450829..279584053 100644 --- a/src/include/lwip/opt.h +++ b/src/include/lwip/opt.h @@ -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 /** @@ -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 /**