Go to the documentation of this file.
69 #if MEM_LIBC_MALLOC || MEM_USE_POOLS
98 #define mem_clib_free free
100 #ifndef mem_clib_malloc
101 #define mem_clib_malloc malloc
103 #ifndef mem_clib_calloc
104 #define mem_clib_calloc calloc
107 #if LWIP_STATS && MEM_STATS
108 #define MEM_LIBC_STATSHELPER_SIZE LWIP_MEM_ALIGN_SIZE(sizeof(mem_size_t))
110 #define MEM_LIBC_STATSHELPER_SIZE 0
124 void* ret = mem_clib_malloc(size + MEM_LIBC_STATSHELPER_SIZE);
129 #if LWIP_STATS && MEM_STATS
131 ret = (
u8_t*)ret + MEM_LIBC_STATSHELPER_SIZE;
147 #if LWIP_STATS && MEM_STATS
148 rmem = (
u8_t*)rmem - MEM_LIBC_STATSHELPER_SIZE;
169 struct memp_malloc_helper *element =
NULL;
173 for (poolnr = MEMP_POOL_FIRST; poolnr <= MEMP_POOL_LAST; poolnr = (
memp_t)(poolnr + 1)) {
176 if (required_size <=
memp_pools[poolnr]->size) {
177 element = (
struct memp_malloc_helper*)
memp_malloc(poolnr);
178 if (element ==
NULL) {
180 #if MEM_USE_POOLS_TRY_BIGGER_POOL
182 if (poolnr < MEMP_POOL_LAST) {
192 if (poolnr > MEMP_POOL_LAST) {
193 LWIP_ASSERT(
"mem_malloc(): no pool is that big!", 0);
199 element->poolnr = poolnr;
203 #if MEMP_OVERFLOW_CHECK || (LWIP_STATS && MEM_STATS)
205 element->size = (
u16_t)size;
208 #if MEMP_OVERFLOW_CHECK
225 struct memp_malloc_helper *hmem;
232 hmem = (
struct memp_malloc_helper*)(
void*)((
u8_t*)rmem -
LWIP_MEM_ALIGN_SIZE(
sizeof(
struct memp_malloc_helper)));
239 #if MEMP_OVERFLOW_CHECK
247 LWIP_ASSERT(
"MEM_USE_POOLS: mem overflow detected", data == 0xcd);
280 #define MIN_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MIN_SIZE)
281 #define SIZEOF_STRUCT_MEM LWIP_MEM_ALIGN_SIZE(sizeof(struct mem))
282 #define MEM_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEM_SIZE)
288 #ifndef LWIP_RAM_HEAP_POINTER
291 #define LWIP_RAM_HEAP_POINTER ram_heap
297 static struct mem *ram_end;
299 static struct mem *lfree;
306 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
308 static volatile u8_t mem_free_count;
311 #define LWIP_MEM_FREE_DECL_PROTECT() SYS_ARCH_DECL_PROTECT(lev_free)
312 #define LWIP_MEM_FREE_PROTECT() SYS_ARCH_PROTECT(lev_free)
313 #define LWIP_MEM_FREE_UNPROTECT() SYS_ARCH_UNPROTECT(lev_free)
314 #define LWIP_MEM_ALLOC_DECL_PROTECT() SYS_ARCH_DECL_PROTECT(lev_alloc)
315 #define LWIP_MEM_ALLOC_PROTECT() SYS_ARCH_PROTECT(lev_alloc)
316 #define LWIP_MEM_ALLOC_UNPROTECT() SYS_ARCH_UNPROTECT(lev_alloc)
321 #define LWIP_MEM_FREE_DECL_PROTECT()
322 #define LWIP_MEM_FREE_PROTECT() sys_mutex_lock(&mem_mutex)
323 #define LWIP_MEM_FREE_UNPROTECT() sys_mutex_unlock(&mem_mutex)
325 #define LWIP_MEM_ALLOC_DECL_PROTECT()
326 #define LWIP_MEM_ALLOC_PROTECT()
327 #define LWIP_MEM_ALLOC_UNPROTECT()
344 plug_holes(
struct mem *
mem)
357 if (
mem != nmem && nmem->
used == 0 && (
u8_t *)nmem != (
u8_t *)ram_end) {
368 if (pmem !=
mem && pmem->
used == 0) {
392 mem = (
struct mem *)(
void *)ram;
403 lfree = (
struct mem *)(
void *)ram;
461 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
518 LWIP_ASSERT(
"mem_trim can only shrink memory", newsize <= size);
519 if (newsize > size) {
523 if (newsize == size) {
532 if (mem2->
used == 0) {
540 lfree = (
struct mem *)(
void *)&ram[ptr2];
542 mem2 = (
struct mem *)(
void *)&ram[ptr2];
554 ((
struct mem *)(
void *)&ram[mem2->
next])->
prev = ptr2;
567 mem2 = (
struct mem *)(
void *)&ram[ptr2];
576 ((
struct mem *)(
void *)&ram[mem2->
next])->
prev = ptr2;
587 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
607 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
608 u8_t local_mem_free_count = 0;
632 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
635 local_mem_free_count = 0;
642 ptr = ((
struct mem *)(
void *)&ram[ptr])->
next) {
643 mem = (
struct mem *)(
void *)&ram[ptr];
644 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
649 if (mem_free_count != 0) {
652 local_mem_free_count = 1;
675 mem2 = (
struct mem *)(
void *)&ram[ptr2];
684 ((
struct mem *)(
void *)&ram[mem2->
next])->
prev = ptr2;
698 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
699 mem_malloc_adjust_lfree:
702 struct mem *cur = lfree;
704 while (cur->
used && cur != ram_end) {
705 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
710 if (mem_free_count != 0) {
713 goto mem_malloc_adjust_lfree;
716 cur = (
struct mem *)(
void *)&ram[cur->
next];
719 LWIP_ASSERT(
"mem_malloc: !lfree->used", ((lfree == ram_end) || (!lfree->
used)));
723 LWIP_ASSERT(
"mem_malloc: allocated memory not above ram_end.",
725 LWIP_ASSERT(
"mem_malloc: allocated memory properly aligned.",
733 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
735 }
while (local_mem_free_count != 0);
746 #if MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS)
750 return mem_clib_calloc(count, size);
773 memset(p, 0, (
size_t)count * (
size_t)size);
#define LWIP_MEM_ALLOC_PROTECT()
#define LWIP_ASSERT(message, assertion)
#define LWIP_MEM_ALLOC_DECL_PROTECT()
#define SYS_ARCH_UNPROTECT(lev)
#define LWIP_DBG_LEVEL_SEVERE
void * memp_malloc(memp_t type)
#define LWIP_MEM_FREE_UNPROTECT()
void sys_mutex_lock(sys_mutex_t *mutex)
#define MEM_STATS_DEC_USED(x, y)
void memp_free(memp_t type, void *mem)
void * mem_calloc(mem_size_t count, mem_size_t size)
LWIP_DECLARE_MEMORY_ALIGNED(ram_heap, LWIP_MEM_ALIGN_SIZE(MEM_SIZE)+(2U *LWIP_MEM_ALIGN_SIZE(sizeof(struct mem))))
#define LWIP_DBG_LEVEL_SERIOUS
#define LWIP_MEM_FREE_PROTECT()
#define LWIP_MEM_ALIGN_SIZE(size)
#define LWIP_UNUSED_ARG(x)
void * mem_trim(void *rmem, mem_size_t newsize)
#define SYS_ARCH_DECL_PROTECT(lev)
#define SYS_ARCH_PROTECT(lev)
#define LWIP_MEM_ALIGN(addr)
err_t sys_mutex_new(sys_mutex_t *mutex)
void sys_mutex_unlock(sys_mutex_t *mutex)
#define LWIP_MEM_ALLOC_UNPROTECT()
const struct memp_desc *const memp_pools[MEMP_MAX]
void * memset(void *dst, int c, size_t length)
void * mem_malloc(mem_size_t size)
#define LWIP_MEM_FREE_DECL_PROTECT()
#define MEM_STATS_AVAIL(x, y)
#define LWIP_RAM_HEAP_POINTER
void mem_free(void *rmem)
#define LWIP_DEBUGF(debug, message)
#define SIZEOF_STRUCT_MEM
#define MEM_STATS_INC_USED(x, y)