3 * Dynamic memory manager
5 * This is a lightweight replacement for the standard C library malloc().
7 * If you want to use the standard C library malloc() instead, define
8 * MEM_LIBC_MALLOC to 1 in your lwipopts.h
10 * To let mem_malloc() use pools (prevents fragmentation and is much faster than
11 * a heap but might waste some memory), define MEM_USE_POOLS to 1, define
12 * MEMP_USE_CUSTOM_POOLS to 1 and create a file "lwippools.h" that includes a list
13 * of pools like this (more pools can be added between _START and _END):
15 * Define three pools with sizes 256, 512, and 1512 bytes
16 * LWIP_MALLOC_MEMPOOL_START
17 * LWIP_MALLOC_MEMPOOL(20, 256)
18 * LWIP_MALLOC_MEMPOOL(10, 512)
19 * LWIP_MALLOC_MEMPOOL(5, 1512)
20 * LWIP_MALLOC_MEMPOOL_END
24 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without modification,
28 * are permitted provided that the following conditions are met:
30 * 1. Redistributions of source code must retain the above copyright notice,
31 * this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright notice,
33 * this list of conditions and the following disclaimer in the documentation
34 * and/or other materials provided with the distribution.
35 * 3. The name of the author may not be used to endorse or promote products
36 * derived from this software without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
41 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
43 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
46 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
49 * This file is part of the lwIP TCP/IP stack.
51 * Author: Adam Dunkels <adam@sics.se>
60 #include "lwip/stats.h"
66 #include <stdlib.h> /* for malloc()/free() */
69 #if MEM_LIBC_MALLOC || MEM_USE_POOLS
71 /** mem_init is not used when using pools instead of a heap or using
79 /** mem_trim is not used when using pools instead of a heap or using
80 * C library malloc(): we can't free part of a pool element and the stack
81 * support mem_trim() to return a different pointer
84 mem_trim(void *mem, mem_size_t size)
86 LWIP_UNUSED_ARG(size);
89 #endif /* MEM_LIBC_MALLOC || MEM_USE_POOLS */
92 /* lwIP heap implemented using C library malloc() */
94 /* in case C library malloc() needs extra protection,
95 * allow these defines to be overridden.
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
114 * Allocate a block of memory with a minimum of 'size' bytes.
116 * @param size is the minimum size of the requested block in bytes.
117 * @return pointer to allocated memory or NULL if no free memory was found.
119 * Note that the returned value must always be aligned (as defined by MEM_ALIGNMENT).
122 mem_malloc(mem_size_t size)
124 void* ret = mem_clib_malloc(size + MEM_LIBC_STATSHELPER_SIZE);
128 LWIP_ASSERT("malloc() must return aligned memory", LWIP_MEM_ALIGN(ret) == ret);
129 #if LWIP_STATS && MEM_STATS
130 *(mem_size_t*)ret = size;
131 ret = (u8_t*)ret + MEM_LIBC_STATSHELPER_SIZE;
132 MEM_STATS_INC_USED(used, size);
138 /** Put memory back on the heap
140 * @param rmem is the pointer as returned by a previous call to mem_malloc()
145 LWIP_ASSERT("rmem != NULL", (rmem != NULL));
146 LWIP_ASSERT("rmem == MEM_ALIGN(rmem)", (rmem == LWIP_MEM_ALIGN(rmem)));
147 #if LWIP_STATS && MEM_STATS
148 rmem = (u8_t*)rmem - MEM_LIBC_STATSHELPER_SIZE;
149 MEM_STATS_DEC_USED(used, *(mem_size_t*)rmem);
156 /* lwIP heap implemented with different sized pools */
159 * Allocate memory: determine the smallest pool that is big enough
160 * to contain an element of 'size' and get an element from that pool.
162 * @param size the size in bytes of the memory needed
163 * @return a pointer to the allocated memory or NULL if the pool is empty
166 mem_malloc(mem_size_t size)
169 struct memp_malloc_helper *element = NULL;
171 mem_size_t required_size = size + LWIP_MEM_ALIGN_SIZE(sizeof(struct memp_malloc_helper));
173 for (poolnr = MEMP_POOL_FIRST; poolnr <= MEMP_POOL_LAST; poolnr = (memp_t)(poolnr + 1)) {
174 /* is this pool big enough to hold an element of the required size
175 plus a struct memp_malloc_helper that saves the pool this element came from? */
176 if (required_size <= memp_pools[poolnr]->size) {
177 element = (struct memp_malloc_helper*)memp_malloc(poolnr);
178 if (element == NULL) {
179 /* No need to DEBUGF or ASSERT: This error is already taken care of in memp.c */
180 #if MEM_USE_POOLS_TRY_BIGGER_POOL
181 /** Try a bigger pool if this one is empty! */
182 if (poolnr < MEMP_POOL_LAST) {
185 #endif /* MEM_USE_POOLS_TRY_BIGGER_POOL */
192 if (poolnr > MEMP_POOL_LAST) {
193 LWIP_ASSERT("mem_malloc(): no pool is that big!", 0);
198 /* save the pool number this element came from */
199 element->poolnr = poolnr;
200 /* and return a pointer to the memory directly after the struct memp_malloc_helper */
201 ret = (u8_t*)element + LWIP_MEM_ALIGN_SIZE(sizeof(struct memp_malloc_helper));
203 #if MEMP_OVERFLOW_CHECK || (LWIP_STATS && MEM_STATS)
204 /* truncating to u16_t is safe because struct memp_desc::size is u16_t */
205 element->size = (u16_t)size;
206 MEM_STATS_INC_USED(used, element->size);
207 #endif /* MEMP_OVERFLOW_CHECK || (LWIP_STATS && MEM_STATS) */
208 #if MEMP_OVERFLOW_CHECK
209 /* initialize unused memory (diff between requested size and selected pool's size) */
210 memset((u8_t*)ret + size, 0xcd, memp_pools[poolnr]->size - size);
211 #endif /* MEMP_OVERFLOW_CHECK */
216 * Free memory previously allocated by mem_malloc. Loads the pool number
217 * and calls memp_free with that pool number to put the element back into
220 * @param rmem the memory element to free
225 struct memp_malloc_helper *hmem;
227 LWIP_ASSERT("rmem != NULL", (rmem != NULL));
228 LWIP_ASSERT("rmem == MEM_ALIGN(rmem)", (rmem == LWIP_MEM_ALIGN(rmem)));
230 /* get the original struct memp_malloc_helper */
231 /* cast through void* to get rid of alignment warnings */
232 hmem = (struct memp_malloc_helper*)(void*)((u8_t*)rmem - LWIP_MEM_ALIGN_SIZE(sizeof(struct memp_malloc_helper)));
234 LWIP_ASSERT("hmem != NULL", (hmem != NULL));
235 LWIP_ASSERT("hmem == MEM_ALIGN(hmem)", (hmem == LWIP_MEM_ALIGN(hmem)));
236 LWIP_ASSERT("hmem->poolnr < MEMP_MAX", (hmem->poolnr < MEMP_MAX));
238 MEM_STATS_DEC_USED(used, hmem->size);
239 #if MEMP_OVERFLOW_CHECK
242 LWIP_ASSERT("MEM_USE_POOLS: invalid chunk size",
243 hmem->size <= memp_pools[hmem->poolnr]->size);
244 /* check that unused memory remained untouched (diff between requested size and selected pool's size) */
245 for (i = hmem->size; i < memp_pools[hmem->poolnr]->size; i++) {
246 u8_t data = *((u8_t*)rmem + i);
247 LWIP_ASSERT("MEM_USE_POOLS: mem overflow detected", data == 0xcd);
250 #endif /* MEMP_OVERFLOW_CHECK */
252 /* and put it in the pool we saved earlier */
253 memp_free(hmem->poolnr, hmem);
256 #else /* MEM_USE_POOLS */
257 /* lwIP replacement for your libc malloc() */
260 * The heap is made up as a list of structs of this type.
261 * This does not have to be aligned since for getting its size,
262 * we only use the macro SIZEOF_STRUCT_MEM, which automatically aligns.
265 /** index (-> ram[next]) of the next struct */
267 /** index (-> ram[prev]) of the previous struct */
269 /** 1: this area is used; 0: this area is unused */
273 /** All allocated blocks will be MIN_SIZE bytes big, at least!
274 * MIN_SIZE can be overridden to suit your needs. Smaller values save space,
275 * larger values could prevent too small blocks to fragment the RAM too much. */
278 #endif /* MIN_SIZE */
279 /* some alignment macros: we define them here for better source code layout */
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)
284 /** If you want to relocate the heap to external memory, simply define
285 * LWIP_RAM_HEAP_POINTER as a void-pointer to that location.
286 * If so, make sure the memory at that location is big enough (see below on
287 * how that space is calculated). */
288 #ifndef LWIP_RAM_HEAP_POINTER
289 /** the heap. we need one struct mem at the end and some room for alignment */
290 LWIP_DECLARE_MEMORY_ALIGNED(ram_heap, MEM_SIZE_ALIGNED + (2U*SIZEOF_STRUCT_MEM));
291 #define LWIP_RAM_HEAP_POINTER ram_heap
292 #endif /* LWIP_RAM_HEAP_POINTER */
294 /** pointer to the heap (ram_heap): for alignment, ram is now a pointer instead of an array */
296 /** the last entry, always unused! */
297 static struct mem *ram_end;
298 /** pointer to the lowest free block, this is used for faster search */
299 static struct mem *lfree;
301 /** concurrent access protection */
303 static sys_mutex_t mem_mutex;
306 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
308 static volatile u8_t mem_free_count;
310 /* Allow mem_free from other (e.g. interrupt) context */
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)
318 #else /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
320 /* Protect the heap only by using a semaphore */
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)
324 /* mem_malloc is protected using semaphore AND LWIP_MEM_ALLOC_PROTECT */
325 #define LWIP_MEM_ALLOC_DECL_PROTECT()
326 #define LWIP_MEM_ALLOC_PROTECT()
327 #define LWIP_MEM_ALLOC_UNPROTECT()
329 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
333 * "Plug holes" by combining adjacent empty struct mems.
334 * After this function is through, there should not exist
335 * one empty struct mem pointing to another empty struct mem.
337 * @param mem this points to a struct mem which just has been freed
338 * @internal this function is only called by mem_free() and mem_trim()
340 * This assumes access to the heap is protected by the calling function
344 plug_holes(struct mem *mem)
349 LWIP_ASSERT("plug_holes: mem >= ram", (u8_t *)mem >= ram);
350 LWIP_ASSERT("plug_holes: mem < ram_end", (u8_t *)mem < (u8_t *)ram_end);
351 LWIP_ASSERT("plug_holes: mem->used == 0", mem->used == 0);
353 /* plug hole forward */
354 LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE_ALIGNED", mem->next <= MEM_SIZE_ALIGNED);
356 nmem = (struct mem *)(void *)&ram[mem->next];
357 if (mem != nmem && nmem->used == 0 && (u8_t *)nmem != (u8_t *)ram_end) {
358 /* if mem->next is unused and not end of ram, combine mem and mem->next */
362 mem->next = nmem->next;
363 ((struct mem *)(void *)&ram[nmem->next])->prev = (mem_size_t)((u8_t *)mem - ram);
366 /* plug hole backward */
367 pmem = (struct mem *)(void *)&ram[mem->prev];
368 if (pmem != mem && pmem->used == 0) {
369 /* if mem->prev is unused, combine mem and mem->prev */
373 pmem->next = mem->next;
374 ((struct mem *)(void *)&ram[mem->next])->prev = (mem_size_t)((u8_t *)pmem - ram);
379 * Zero the heap and initialize start, end and lowest-free
386 LWIP_ASSERT("Sanity check alignment",
387 (SIZEOF_STRUCT_MEM & (MEM_ALIGNMENT-1)) == 0);
390 ram = (u8_t *)LWIP_MEM_ALIGN(LWIP_RAM_HEAP_POINTER);
391 /* initialize the start of the heap */
392 mem = (struct mem *)(void *)ram;
393 mem->next = MEM_SIZE_ALIGNED;
396 /* initialize the end of the heap */
397 ram_end = (struct mem *)(void *)&ram[MEM_SIZE_ALIGNED];
399 ram_end->next = MEM_SIZE_ALIGNED;
400 ram_end->prev = MEM_SIZE_ALIGNED;
402 /* initialize the lowest-free pointer to the start of the heap */
403 lfree = (struct mem *)(void *)ram;
405 MEM_STATS_AVAIL(avail, MEM_SIZE_ALIGNED);
407 if (sys_mutex_new(&mem_mutex) != ERR_OK) {
408 LWIP_ASSERT("failed to create mem_mutex", 0);
413 * Put a struct mem back on the heap
415 * @param rmem is the data portion of a struct mem as returned by a previous
416 * call to mem_malloc()
422 LWIP_MEM_FREE_DECL_PROTECT();
425 LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("mem_free(p == NULL) was called.\n"));
428 LWIP_ASSERT("mem_free: sanity check alignment", (((mem_ptr_t)rmem) & (MEM_ALIGNMENT-1)) == 0);
430 LWIP_ASSERT("mem_free: legal memory", (u8_t *)rmem >= (u8_t *)ram &&
431 (u8_t *)rmem < (u8_t *)ram_end);
433 if ((u8_t *) rmem < ram || (u8_t *) rmem >= (u8_t *) ram_end)
435 SYS_ARCH_DECL_PROTECT(lev);
436 LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_free: illegal memory\n"));
437 /* protect mem stats from concurrent access */
438 SYS_ARCH_PROTECT(lev);
439 MEM_STATS_INC(illegal);
440 SYS_ARCH_UNPROTECT(lev);
443 /* protect the heap from concurrent access */
444 LWIP_MEM_FREE_PROTECT();
445 /* Get the corresponding struct mem ... */
446 /* cast through void* to get rid of alignment warnings */
447 mem = (struct mem *)(void *)((u8_t *)rmem - SIZEOF_STRUCT_MEM);
448 /* ... which has to be in a used state ... */
449 LWIP_ASSERT("mem_free: mem->used", mem->used);
450 /* ... and is now unused. */
454 /* the newly freed struct is now the lowest */
458 MEM_STATS_DEC_USED(used, mem->next - (mem_size_t)(((u8_t *)mem - ram)));
460 /* finally, see if prev or next are free also */
462 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
464 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
465 LWIP_MEM_FREE_UNPROTECT();
469 * Shrink memory returned by mem_malloc().
471 * @param rmem pointer to memory allocated by mem_malloc the is to be shrinked
472 * @param new_size required size after shrinking (needs to be smaller than or
473 * equal to the previous size)
474 * @return for compatibility reasons: is always == rmem, at the moment
475 * or NULL if newsize is > old size, in which case rmem is NOT touched
479 mem_trim(void *rmem, mem_size_t new_size)
481 mem_size_t size, newsize;
482 mem_size_t ptr, ptr2;
483 struct mem *mem, *mem2;
484 /* use the FREE_PROTECT here: it protects with sem OR SYS_ARCH_PROTECT */
485 LWIP_MEM_FREE_DECL_PROTECT();
487 /* Expand the size of the allocated memory region so that we can
488 adjust for alignment. */
489 newsize = (mem_size_t)LWIP_MEM_ALIGN_SIZE(new_size);
490 if ((newsize > MEM_SIZE_ALIGNED) || (newsize < new_size)) {
494 if (newsize < MIN_SIZE_ALIGNED) {
495 /* every data block must be at least MIN_SIZE_ALIGNED long */
496 newsize = MIN_SIZE_ALIGNED;
499 LWIP_ASSERT("mem_trim: legal memory", (u8_t *)rmem >= (u8_t *)ram &&
500 (u8_t *)rmem < (u8_t *)ram_end);
502 if ((u8_t *) rmem < ram || (u8_t *) rmem >= (u8_t *) ram_end)
504 SYS_ARCH_DECL_PROTECT(lev);
505 LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_trim: illegal memory\n"));
506 /* protect mem stats from concurrent access */
507 SYS_ARCH_PROTECT(lev);
508 MEM_STATS_INC(illegal);
509 SYS_ARCH_UNPROTECT(lev);
512 /* Get the corresponding struct mem ... */
513 /* cast through void* to get rid of alignment warnings */
514 mem = (struct mem *)(void *)((u8_t *)rmem - SIZEOF_STRUCT_MEM);
515 /* ... and its offset pointer */
516 ptr = (mem_size_t)((u8_t *)mem - ram);
518 size = (mem_size_t)((mem_size_t)(mem->next - ptr) - SIZEOF_STRUCT_MEM);
519 LWIP_ASSERT("mem_trim can only shrink memory", newsize <= size);
520 if (newsize > size) {
524 if (newsize == size) {
525 /* No change in size, simply return */
529 /* protect the heap from concurrent access */
530 LWIP_MEM_FREE_PROTECT();
532 mem2 = (struct mem *)(void *)&ram[mem->next];
533 if (mem2->used == 0) {
534 /* The next struct is unused, we can simply move it at little */
536 /* remember the old next pointer */
538 /* create new struct mem which is moved directly after the shrinked mem */
539 ptr2 = (mem_size_t)(ptr + SIZEOF_STRUCT_MEM + newsize);
541 lfree = (struct mem *)(void *)&ram[ptr2];
543 mem2 = (struct mem *)(void *)&ram[ptr2];
545 /* restore the next pointer */
547 /* link it back to mem */
551 /* last thing to restore linked list: as we have moved mem2,
552 * let 'mem2->next->prev' point to mem2 again. but only if mem2->next is not
553 * the end of the heap */
554 if (mem2->next != MEM_SIZE_ALIGNED) {
555 ((struct mem *)(void *)&ram[mem2->next])->prev = ptr2;
557 MEM_STATS_DEC_USED(used, (size - newsize));
558 /* no need to plug holes, we've already done that */
559 } else if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED <= size) {
560 /* Next struct is used but there's room for another struct mem with
561 * at least MIN_SIZE_ALIGNED of data.
562 * Old size ('size') must be big enough to contain at least 'newsize' plus a struct mem
563 * ('SIZEOF_STRUCT_MEM') with some data ('MIN_SIZE_ALIGNED').
564 * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
565 * region that couldn't hold data, but when mem->next gets freed,
566 * the 2 regions would be combined, resulting in more free memory */
567 ptr2 = (mem_size_t)(ptr + SIZEOF_STRUCT_MEM + newsize);
568 mem2 = (struct mem *)(void *)&ram[ptr2];
573 mem2->next = mem->next;
576 if (mem2->next != MEM_SIZE_ALIGNED) {
577 ((struct mem *)(void *)&ram[mem2->next])->prev = ptr2;
579 MEM_STATS_DEC_USED(used, (size - newsize));
580 /* the original mem->next is used, so no need to plug holes! */
583 next struct mem is used but size between mem and mem2 is not big enough
584 to create another struct mem
585 -> don't do anyhting.
586 -> the remaining space stays unused since it is too small
588 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
590 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
591 LWIP_MEM_FREE_UNPROTECT();
596 * Allocate a block of memory with a minimum of 'size' bytes.
598 * @param size_in is the minimum size of the requested block in bytes.
599 * @return pointer to allocated memory or NULL if no free memory was found.
601 * Note that the returned value will always be aligned (as defined by MEM_ALIGNMENT).
604 mem_malloc(mem_size_t size_in)
606 mem_size_t ptr, ptr2, size;
607 struct mem *mem, *mem2;
608 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
609 u8_t local_mem_free_count = 0;
610 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
611 LWIP_MEM_ALLOC_DECL_PROTECT();
617 /* Expand the size of the allocated memory region so that we can
618 adjust for alignment. */
619 size = (mem_size_t)LWIP_MEM_ALIGN_SIZE(size_in);
620 if ((size > MEM_SIZE_ALIGNED) ||
625 if (size < MIN_SIZE_ALIGNED) {
626 /* every data block must be at least MIN_SIZE_ALIGNED long */
627 size = MIN_SIZE_ALIGNED;
630 /* protect the heap from concurrent access */
631 sys_mutex_lock(&mem_mutex);
632 LWIP_MEM_ALLOC_PROTECT();
633 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
634 /* run as long as a mem_free disturbed mem_malloc or mem_trim */
636 local_mem_free_count = 0;
637 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
639 /* Scan through the heap searching for a free block that is big enough,
640 * beginning with the lowest free block.
642 for (ptr = (mem_size_t)((u8_t *)lfree - ram); ptr < MEM_SIZE_ALIGNED - size;
643 ptr = ((struct mem *)(void *)&ram[ptr])->next) {
644 mem = (struct mem *)(void *)&ram[ptr];
645 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
647 LWIP_MEM_ALLOC_UNPROTECT();
648 /* allow mem_free or mem_trim to run */
649 LWIP_MEM_ALLOC_PROTECT();
650 if (mem_free_count != 0) {
651 /* If mem_free or mem_trim have run, we have to restart since they
652 could have altered our current struct mem. */
653 local_mem_free_count = 1;
656 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
659 (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size) {
660 /* mem is not used and at least perfect fit is possible:
661 * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
663 if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >= (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED)) {
664 /* (in addition to the above, we test if another struct mem (SIZEOF_STRUCT_MEM) containing
665 * at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
666 * -> split large block, create empty remainder,
667 * remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
668 * mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
669 * struct mem would fit in but no data between mem2 and mem2->next
670 * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
671 * region that couldn't hold data, but when mem->next gets freed,
672 * the 2 regions would be combined, resulting in more free memory
674 ptr2 = (mem_size_t)(ptr + SIZEOF_STRUCT_MEM + size);
675 /* create mem2 struct */
676 mem2 = (struct mem *)(void *)&ram[ptr2];
678 mem2->next = mem->next;
680 /* and insert it between mem and mem->next */
684 if (mem2->next != MEM_SIZE_ALIGNED) {
685 ((struct mem *)(void *)&ram[mem2->next])->prev = ptr2;
687 MEM_STATS_INC_USED(used, (size + SIZEOF_STRUCT_MEM));
689 /* (a mem2 struct does no fit into the user data space of mem and mem->next will always
690 * be used at this point: if not we have 2 unused structs in a row, plug_holes should have
691 * take care of this).
692 * -> near fit or exact fit: do not split, no mem2 creation
693 * also can't move mem->next directly behind mem, since mem->next
694 * will always be used at this point!
697 MEM_STATS_INC_USED(used, mem->next - (mem_size_t)((u8_t *)mem - ram));
699 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
700 mem_malloc_adjust_lfree:
701 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
703 struct mem *cur = lfree;
704 /* Find next free block after mem and update lowest free pointer */
705 while (cur->used && cur != ram_end) {
706 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
708 LWIP_MEM_ALLOC_UNPROTECT();
709 /* prevent high interrupt latency... */
710 LWIP_MEM_ALLOC_PROTECT();
711 if (mem_free_count != 0) {
712 /* If mem_free or mem_trim have run, we have to restart since they
713 could have altered our current struct mem or lfree. */
714 goto mem_malloc_adjust_lfree;
716 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
717 cur = (struct mem *)(void *)&ram[cur->next];
720 LWIP_ASSERT("mem_malloc: !lfree->used", ((lfree == ram_end) || (!lfree->used)));
722 LWIP_MEM_ALLOC_UNPROTECT();
723 sys_mutex_unlock(&mem_mutex);
724 LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.",
725 (mem_ptr_t)mem + SIZEOF_STRUCT_MEM + size <= (mem_ptr_t)ram_end);
726 LWIP_ASSERT("mem_malloc: allocated memory properly aligned.",
727 ((mem_ptr_t)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0);
728 LWIP_ASSERT("mem_malloc: sanity check alignment",
729 (((mem_ptr_t)mem) & (MEM_ALIGNMENT-1)) == 0);
731 return (u8_t *)mem + SIZEOF_STRUCT_MEM;
734 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT
735 /* if we got interrupted by a mem_free, try again */
736 } while (local_mem_free_count != 0);
737 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */
738 LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("mem_malloc: could not allocate %"S16_F" bytes\n", (s16_t)size));
740 LWIP_MEM_ALLOC_UNPROTECT();
741 sys_mutex_unlock(&mem_mutex);
745 #endif /* MEM_USE_POOLS */
747 #if MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS)
749 mem_calloc(mem_size_t count, mem_size_t size)
751 return mem_clib_calloc(count, size);
754 #else /* MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS) */
756 * Contiguously allocates enough space for count objects that are size bytes
757 * of memory each and returns a pointer to the allocated memory.
759 * The allocated memory is filled with bytes of value zero.
761 * @param count number of objects to allocate
762 * @param size size of the objects to allocate
763 * @return pointer to allocated memory / NULL pointer if there is an error
766 mem_calloc(mem_size_t count, mem_size_t size)
770 /* allocate 'count' objects of size 'size' */
771 p = mem_malloc(count * size);
773 /* zero the memory */
774 memset(p, 0, (size_t)count * (size_t)size);
778 #endif /* MEM_LIBC_MALLOC && (!LWIP_STATS || !MEM_STATS) */