moved MEM_LIBC_MALLOC code from mem.h to mem.c:

- it's not worth littering mem.h just to save some bytes to prevent the additional call;
- MEM_STATS now also work when MEM_LIBC_MALLOC is enabled
This commit is contained in:
goldsimon 2016-07-26 15:01:39 +02:00
parent 59295be4ef
commit e030118750
3 changed files with 111 additions and 41 deletions

View File

@ -54,9 +54,6 @@
*/ */
#include "lwip/opt.h" #include "lwip/opt.h"
#if !MEM_LIBC_MALLOC /* don't build if not configured for use in lwipopts.h */
#include "lwip/def.h" #include "lwip/def.h"
#include "lwip/mem.h" #include "lwip/mem.h"
#include "lwip/sys.h" #include "lwip/sys.h"
@ -65,7 +62,91 @@
#include <string.h> #include <string.h>
#if MEM_USE_POOLS #if MEM_LIBC_MALLOC || MEM_USE_POOLS
/** mem_init is not used when using pools instead of a heap or using
* C library malloc().
*/
void
mem_init(void)
{
}
/** mem_trim is not used when using pools instead of a heap or using
* C library malloc(): we can't free part of a pool element and the stack
* support mem_trim() to return a different pointer
*/
void*
mem_trim(void *mem, mem_size_t size)
{
LWIP_UNUSED_ARG(size);
return mem;
}
#endif /* MEM_LIBC_MALLOC || 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 LWIP_STATS && MEM_STATS
#define MEM_LIBC_STATSHELPER_SIZE LWIP_MEM_ALIGN_SIZE(sizeof(mem_size_t))
#else
#define MEM_LIBC_STATSHELPER_SIZE 0
#endif
/**
* Allocate a block of memory with a minimum of 'size' bytes.
*
* @param size is the minimum size of the requested block in bytes.
* @return pointer to allocated memory or NULL if no free memory was found.
*
* Note that the returned value must always be aligned (as defined by MEM_ALIGNMENT).
*/
void *
mem_malloc(mem_size_t size)
{
void* ret = mem_clib_malloc(size + MEM_LIBC_STATSHELPER_SIZE);
if (ret == NULL) {
MEM_STATS_INC(err);
} else {
LWIP_ASSERT("malloc() must return aligned memory", LWIP_MEM_ALIGN(ret) == ret);
#if LWIP_STATS && MEM_STATS
*(mem_size_t*)ret = size;
ret = (u8_t*)ret + MEM_LIBC_STATSHELPER_SIZE;
MEM_STATS_INC_USED(used, size);
#endif
}
return ret;
}
/** Put memory back on the heap
*
* @param rmem is the pointer as returned by a previous call to mem_malloc()
*/
void
mem_free(void *rmem)
{
LWIP_ASSERT("rmem != NULL", (rmem != NULL));
LWIP_ASSERT("rmem == MEM_ALIGN(rmem)", (rmem == LWIP_MEM_ALIGN(rmem)));
#if LWIP_STATS && MEM_STATS
rmem = (u8_t*)rmem - MEM_LIBC_STATSHELPER_SIZE;
MEM_STATS_DEC_USED(used, *(mem_size_t*)rmem);
#endif
mem_clib_free(rmem);
}
#elif MEM_USE_POOLS
/* lwIP heap implemented with different sized pools */ /* lwIP heap implemented with different sized pools */
@ -96,6 +177,7 @@ again:
} }
if (poolnr > MEMP_POOL_LAST) { if (poolnr > MEMP_POOL_LAST) {
LWIP_ASSERT("mem_malloc(): no pool is that big!", 0); LWIP_ASSERT("mem_malloc(): no pool is that big!", 0);
MEM_STATS_INC(err);
return NULL; return NULL;
} }
element = (struct memp_malloc_helper*)memp_malloc(poolnr); element = (struct memp_malloc_helper*)memp_malloc(poolnr);
@ -109,6 +191,7 @@ again:
goto again; goto again;
} }
#endif /* MEM_USE_POOLS_TRY_BIGGER_POOL */ #endif /* MEM_USE_POOLS_TRY_BIGGER_POOL */
MEM_STATS_INC(err);
return NULL; return NULL;
} }
@ -509,7 +592,6 @@ mem_trim(void *rmem, mem_size_t newsize)
} }
/** /**
* Adam's mem_malloc() plus solution for bug #17922
* Allocate a block of memory with a minimum of 'size' bytes. * Allocate a block of memory with a minimum of 'size' bytes.
* *
* @param size is the minimum size of the requested block in bytes. * @param size is the minimum size of the requested block in bytes.
@ -660,6 +742,15 @@ mem_malloc_adjust_lfree:
} }
#endif /* MEM_USE_POOLS */ #endif /* MEM_USE_POOLS */
#if MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS)
void *
mem_calloc(mem_size_t count, mem_size_t size)
{
return mem_clib_calloc(count, size);
}
#else /* MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS) */
/** /**
* Contiguously allocates enough space for count objects that are size bytes * Contiguously allocates enough space for count objects that are size bytes
* of memory each and returns a pointer to the allocated memory. * of memory each and returns a pointer to the allocated memory.
@ -670,7 +761,8 @@ mem_malloc_adjust_lfree:
* @param size size of the objects to allocate * @param size size of the objects to allocate
* @return pointer to allocated memory / NULL pointer if there is an error * @return pointer to allocated memory / NULL pointer if there is an error
*/ */
void *mem_calloc(mem_size_t count, mem_size_t size) void *
mem_calloc(mem_size_t count, mem_size_t size)
{ {
void *p; void *p;
@ -682,5 +774,4 @@ void *mem_calloc(mem_size_t count, mem_size_t size)
} }
return p; return p;
} }
#endif /* MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS) */
#endif /* !MEM_LIBC_MALLOC */

View File

@ -228,7 +228,9 @@ memp_overflow_init(const struct memp_desc *desc)
void void
memp_init_pool(const struct memp_desc *desc) memp_init_pool(const struct memp_desc *desc)
{ {
#if !MEMP_MEM_MALLOC #if MEMP_MEM_MALLOC
LWIP_UNUSED_ARG(desc);
#else
int i; int i;
struct memp *memp; struct memp *memp;
@ -277,7 +279,7 @@ memp_init(void)
for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) { for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
memp_init_pool(memp_pools[i]); memp_init_pool(memp_pools[i]);
#if LWIP_STATS #if LWIP_STATS && MEMP_STATS
lwip_stats.memp[i] = memp_pools[i]->stats; lwip_stats.memp[i] = memp_pools[i]->stats;
#endif #endif
} }
@ -436,6 +438,7 @@ do_memp_free_pool(const struct memp_desc* desc, void *mem)
#endif #endif
#if MEMP_MEM_MALLOC #if MEMP_MEM_MALLOC
LWIP_UNUSED_ARG(desc);
SYS_ARCH_UNPROTECT(old_level); SYS_ARCH_UNPROTECT(old_level);
mem_free(memp); mem_free(memp);
#else /* MEMP_MEM_MALLOC */ #else /* MEMP_MEM_MALLOC */

View File

@ -46,30 +46,15 @@ extern "C" {
#if MEM_LIBC_MALLOC #if MEM_LIBC_MALLOC
#include <stddef.h> /* for size_t */ #include <stddef.h> /* for size_t */
typedef size_t mem_size_t; typedef size_t mem_size_t;
#define MEM_SIZE_F SZT_F #define MEM_SIZE_F SZT_F
/* aliases for C library malloc() */ #elif MEM_USE_POOLS
#define mem_init()
/* in case C library malloc() needs extra protection, typedef u16_t mem_size_t;
* allow these defines to be overridden. #define MEM_SIZE_F U16_F
*/
#ifndef mem_free #else
#define mem_free free
#endif
#ifndef mem_malloc
#define mem_malloc malloc
#endif
#ifndef mem_calloc
#define mem_calloc calloc
#endif
/* Since there is no C library allocation function to shrink memory without
moving it, define this to nothing. */
#ifndef mem_trim
#define mem_trim(mem, size) (mem)
#endif
#else /* MEM_LIBC_MALLOC */
/* MEM_SIZE would have to be aligned, but using 64000 here instead of /* MEM_SIZE would have to be aligned, but using 64000 here instead of
* 65535 leaves some room for alignment... * 65535 leaves some room for alignment...
@ -81,22 +66,13 @@ typedef u32_t mem_size_t;
typedef u16_t mem_size_t; typedef u16_t mem_size_t;
#define MEM_SIZE_F U16_F #define MEM_SIZE_F U16_F
#endif /* MEM_SIZE > 64000 */ #endif /* MEM_SIZE > 64000 */
#endif
#if MEM_USE_POOLS
/** mem_init is not used when using pools instead of a heap */
#define mem_init()
/** mem_trim is not used when using pools instead of a heap:
we can't free part of a pool element and don't want to copy the rest */
#define mem_trim(mem, size) (mem)
#else /* MEM_USE_POOLS */
/* lwIP alternative malloc */
void mem_init(void); void mem_init(void);
void *mem_trim(void *mem, mem_size_t size); void *mem_trim(void *mem, mem_size_t size);
#endif /* MEM_USE_POOLS */
void *mem_malloc(mem_size_t size); void *mem_malloc(mem_size_t size);
void *mem_calloc(mem_size_t count, mem_size_t size); void *mem_calloc(mem_size_t count, mem_size_t size);
void mem_free(void *mem); void mem_free(void *mem);
#endif /* MEM_LIBC_MALLOC */
#ifdef __cplusplus #ifdef __cplusplus
} }