3 * Dynamic pool memory manager
5 * lwIP has dedicated pools for many structures (netconn, protocol control blocks,
6 * packet buffers, ...). All these pools are managed here.
8 * @defgroup mempool Memory pools
9 * @ingroup infrastructure
15 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
16 * All rights reserved.
18 * Redistribution and use in source and binary forms, with or without modification,
19 * are permitted provided that the following conditions are met:
21 * 1. Redistributions of source code must retain the above copyright notice,
22 * this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright notice,
24 * this list of conditions and the following disclaimer in the documentation
25 * and/or other materials provided with the distribution.
26 * 3. The name of the author may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
32 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
34 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
40 * This file is part of the lwIP TCP/IP stack.
42 * Author: Adam Dunkels <adam@sics.se>
48 #include "lwip/memp.h"
50 #include "lwip/stats.h"
54 /* Make sure we include everything we need for size calculation required by memp_std.h */
55 #include "lwip/pbuf.h"
59 #include "lwip/priv/tcp_priv.h"
60 #include "lwip/ip4_frag.h"
61 #include "lwip/netbuf.h"
63 #include "lwip/priv/tcpip_priv.h"
64 #include "lwip/priv/api_msg.h"
65 #include "lwip/sockets.h"
66 #include "lwip/netifapi.h"
67 #include "lwip/etharp.h"
68 #include "lwip/igmp.h"
69 #include "lwip/timeouts.h"
70 /* needed by default MEMP_NUM_SYS_TIMEOUT */
71 #include "netif/ppp/ppp_opts.h"
72 #include "lwip/netdb.h"
74 #include "lwip/priv/nd6_priv.h"
75 #include "lwip/ip6_frag.h"
76 #include "lwip/mld6.h"
78 #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
79 #include "lwip/priv/memp_std.h"
81 const struct memp_desc* const memp_pools[MEMP_MAX] = {
82 #define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name,
83 #include "lwip/priv/memp_std.h"
86 #ifdef LWIP_HOOK_FILENAME
87 #include LWIP_HOOK_FILENAME
90 #if MEMP_MEM_MALLOC && MEMP_OVERFLOW_CHECK >= 2
91 #undef MEMP_OVERFLOW_CHECK
92 /* MEMP_OVERFLOW_CHECK >= 2 does not work with MEMP_MEM_MALLOC, use 1 instead */
93 #define MEMP_OVERFLOW_CHECK 1
96 /** This array holds the element sizes of each pool. */
97 #if !MEM_USE_POOLS && !MEMP_MEM_MALLOC && !LWIP_PBUF_FROM_CUSTOM_POOLS
100 const u16_t memp_sizes[MEMP_MAX] = {
101 #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEM_ALIGN_SIZE(size),
102 #include "lwip/priv/memp_std.h"
105 #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
107 /** When using variable pbuf pools, pbuf_alloc will start scanning for pbufs at the
108 * highest indexed pbuf pool (PBUF_CUSTOM_POOL_IDX_START), which holds the smallest pbufs.
109 * It will look through the list of pbuf pools for a suitable pool to draw from that has a free
110 * pbuf until it passes MEMP_PBUF_POOL, which holds the largest pbufs.
112 #if LWIP_PBUF_FROM_CUSTOM_POOLS
113 _Static_assert ((PBUF_CUSTOM_POOL_IDX_START > PBUF_CUSTOM_POOL_IDX_END), "PBUF_CUSTOM_POOL_IDX_START must be greater than PBUF_CUSTOM_POOL_IDX_END");
114 #define MEMP_IS_PBUF_POOL(type) (type <= PBUF_CUSTOM_POOL_IDX_START && type >= PBUF_CUSTOM_POOL_IDX_END)
115 #define MEMP_PBUF_POOL_HIGHWATERMARK(type) (PBUF_CUSTOM_POOL_IDX_START - type)
117 #define MEMP_IS_PBUF_POOL(type) (type == MEMP_PBUF_POOL)
118 #define MEMP_PBUF_POOL_HIGHWATERMARK(type) (type)
121 #define sys_profile_interval_set_pbuf_highwatermark(...)
123 #if MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC
125 * Check that memp-lists don't form a circle, using "Floyd's cycle-finding algorithm".
128 memp_sanity(const struct memp_desc *desc)
134 for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
135 h = ((h->next != NULL) ? h->next->next : NULL)) {
144 #endif /* MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC */
146 #if MEMP_OVERFLOW_CHECK
148 * Check if a memp element was victim of an overflow
149 * (e.g. the restricted area after it has been altered)
151 * @param p the memp element to check
152 * @param desc the pool p comes from
155 memp_overflow_check_element_overflow(struct memp *p, const struct memp_desc *desc)
157 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
160 m = (u8_t*)p + MEMP_SIZE + desc->size;
161 for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
163 char errstr[128] = "detected memp overflow in pool ";
164 strcat(errstr, desc->desc);
165 LWIP_ASSERT(errstr, 0);
168 #else /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
170 LWIP_UNUSED_ARG(desc);
171 #endif /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
175 * Check if a memp element was victim of an underflow
176 * (e.g. the restricted area before it has been altered)
178 * @param p the memp element to check
179 * @param desc the pool p comes from
182 memp_overflow_check_element_underflow(struct memp *p, const struct memp_desc *desc)
184 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
187 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
188 for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
190 char errstr[128] = "detected memp underflow in pool ";
191 strcat(errstr, desc->desc);
192 LWIP_ASSERT(errstr, 0);
195 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
197 LWIP_UNUSED_ARG(desc);
198 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
202 * Initialize the restricted area of on memp element.
205 memp_overflow_init_element(struct memp *p, const struct memp_desc *desc)
207 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0
209 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
210 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
211 memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
213 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
214 m = (u8_t*)p + MEMP_SIZE + desc->size;
215 memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
217 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
219 LWIP_UNUSED_ARG(desc);
220 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
223 #if MEMP_OVERFLOW_CHECK >= 2
225 * Do an overflow check for all elements in every pool.
227 * @see memp_overflow_check_element for a description of the check
230 memp_overflow_check_all(void)
234 SYS_ARCH_DECL_PROTECT(old_level);
235 SYS_ARCH_PROTECT(old_level);
237 for (i = 0; i < MEMP_MAX; ++i) {
238 p = (struct memp*)LWIP_MEM_ALIGN(memp_pools[i]->base);
239 for (j = 0; j < memp_pools[i]->num; ++j) {
240 memp_overflow_check_element_overflow(p, memp_pools[i]);
241 memp_overflow_check_element_underflow(p, memp_pools[i]);
242 p = LWIP_ALIGNMENT_CAST(struct memp*, ((u8_t*)p + MEMP_SIZE + memp_pools[i]->size + MEMP_SANITY_REGION_AFTER_ALIGNED));
245 SYS_ARCH_UNPROTECT(old_level);
247 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
248 #endif /* MEMP_OVERFLOW_CHECK */
251 * Initialize custom memory pool.
252 * Related functions: memp_malloc_pool, memp_free_pool
254 * @param desc pool to initialize
257 memp_init_pool(const struct memp_desc *desc)
260 LWIP_UNUSED_ARG(desc);
266 memp = (struct memp*)LWIP_MEM_ALIGN(desc->base);
267 /* create a linked list of memp elements */
268 for (i = 0; i < desc->num; ++i) {
269 memp->next = *desc->tab;
271 #if MEMP_OVERFLOW_CHECK
272 memp_overflow_init_element(memp, desc);
273 #endif /* MEMP_OVERFLOW_CHECK */
274 /* cast through void* to get rid of alignment warnings */
275 memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
276 #if MEMP_OVERFLOW_CHECK
277 + MEMP_SANITY_REGION_AFTER_ALIGNED
282 desc->stats->avail = desc->num;
283 #endif /* MEMP_STATS */
284 #endif /* !MEMP_MEM_MALLOC */
286 #if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
287 desc->stats->name = desc->desc;
288 #endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
292 * Check if the pool is not empty
294 u8_t memp_is_not_empty (memp_t type)
296 return (memp_pools[type]->tab != NULL);
300 * Initializes lwIP built-in pools.
301 * Related functions: memp_malloc, memp_free
303 * Carves out memp_memory into linked lists for each pool-type.
309 #if LWIP_PBUF_FROM_CUSTOM_POOLS
311 #endif // LWIP_PBUF_FROM_CUSTOM_POOLS
313 /* for every pool: */
314 for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
315 memp_init_pool(memp_pools[i]);
317 #if LWIP_STATS && MEMP_STATS
318 lwip_stats.memp[i] = memp_pools[i]->stats;
322 #if MEMP_OVERFLOW_CHECK >= 2
323 /* check everything a first time to see if it worked */
324 memp_overflow_check_all();
325 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
327 #if LWIP_PBUF_FROM_CUSTOM_POOLS
328 /* Verify that custom pools that contain PBUFs are laid out
329 * in decreasing order by size */
331 i = PBUF_CUSTOM_POOL_IDX_START;
333 LWIP_ASSERT("memp_init: PBUF pool size ordering incorrect",
336 if (i == PBUF_CUSTOM_POOL_IDX_END) {
343 #if MEMP_OVERFLOW_CHECK >= 2
344 /* check everything a first time to see if it worked */
345 memp_overflow_check_all();
346 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
350 #if !MEMP_OVERFLOW_CHECK
351 do_memp_malloc_pool(const struct memp_desc *desc)
353 do_memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
357 SYS_ARCH_DECL_PROTECT(old_level);
360 memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
361 SYS_ARCH_PROTECT(old_level);
362 #else /* MEMP_MEM_MALLOC */
363 SYS_ARCH_PROTECT(old_level);
366 #endif /* MEMP_MEM_MALLOC */
370 #if MEMP_OVERFLOW_CHECK == 1
371 memp_overflow_check_element_overflow(memp, desc);
372 memp_overflow_check_element_underflow(memp, desc);
373 #endif /* MEMP_OVERFLOW_CHECK */
375 *desc->tab = memp->next;
376 #if MEMP_OVERFLOW_CHECK
378 #endif /* MEMP_OVERFLOW_CHECK */
379 #endif /* !MEMP_MEM_MALLOC */
380 #if MEMP_OVERFLOW_CHECK
384 memp_overflow_init_element(memp, desc);
385 #endif /* MEMP_MEM_MALLOC */
386 #endif /* MEMP_OVERFLOW_CHECK */
387 LWIP_ASSERT("memp_malloc: memp properly aligned",
388 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
391 if (desc->stats->used > desc->stats->max) {
392 desc->stats->max = desc->stats->used;
396 SYS_ARCH_UNPROTECT(old_level);
397 /* cast through u8_t* to get rid of alignment warnings */
398 return ((u8_t*)memp + MEMP_SIZE);
400 LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
406 SYS_ARCH_UNPROTECT(old_level);
411 * Get an element from a custom pool.
413 * @param desc the pool to get an element from
415 * @return a pointer to the allocated memory or a NULL pointer on error
418 #if !MEMP_OVERFLOW_CHECK
419 memp_malloc_pool(const struct memp_desc *desc)
421 memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
424 LWIP_ASSERT("invalid pool desc", desc != NULL);
429 #if !MEMP_OVERFLOW_CHECK
430 return do_memp_malloc_pool(desc);
432 return do_memp_malloc_pool_fn(desc, file, line);
436 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
437 #if LWIP_PBUF_FROM_CUSTOM_POOLS
438 int num_used_pool[PBUF_CUSTOM_POOL_IDX_START - PBUF_CUSTOM_POOL_IDX_END + 1] = {0};
440 int num_used_pool = 0;
443 static int *get_num_used_pool_ptr(memp_t type)
445 if (MEMP_IS_PBUF_POOL(type)) {
446 #if LWIP_PBUF_FROM_CUSTOM_POOLS
447 return &num_used_pool[PBUF_CUSTOM_POOL_IDX_START - type];
449 return &num_used_pool;
454 #endif /* (MEMP_DEBUG | LWIP_DBG_TRACE) */
457 * Get an element from a specific pool.
459 * @param type the pool to get an element from
461 * @return a pointer to the allocated memory or a NULL pointer on error
464 #if !MEMP_OVERFLOW_CHECK
465 memp_malloc(memp_t type)
467 memp_malloc_fn(memp_t type, const char* file, const int line)
471 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
472 int *num_used_pool_ptr = get_num_used_pool_ptr(type);
475 LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
477 #if MEMP_OVERFLOW_CHECK >= 2
478 memp_overflow_check_all();
479 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
481 #if !MEMP_OVERFLOW_CHECK
482 memp = do_memp_malloc_pool(memp_pools[type]);
484 memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
487 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
489 LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_TRACE, ("mm: out-of-mem in %s\n", memp_pools[type]->desc));
490 if (LWIP_PERF && MEMP_IS_PBUF_POOL(type)) {
491 sys_profile_interval_set_pbuf_highwatermark((u32_t)(*num_used_pool_ptr), MEMP_PBUF_POOL_HIGHWATERMARK(type));
495 if (MEMP_IS_PBUF_POOL(type)) {
496 (*num_used_pool_ptr)++;
499 LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_TRACE, ("mm:p++ = %d (%p) (%s)\n", *num_used_pool_ptr, memp, memp_pools[type]->desc));
500 sys_profile_interval_set_pbuf_highwatermark((u32_t)(*num_used_pool_ptr), MEMP_PBUF_POOL_HIGHWATERMARK(type));
510 do_memp_free_pool(const struct memp_desc* desc, void *mem)
513 SYS_ARCH_DECL_PROTECT(old_level);
515 LWIP_ASSERT("memp_free: mem properly aligned",
516 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
518 /* cast through void* to get rid of alignment warnings */
519 memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
521 SYS_ARCH_PROTECT(old_level);
523 #if MEMP_OVERFLOW_CHECK == 1
524 memp_overflow_check_element_overflow(memp, desc);
525 memp_overflow_check_element_underflow(memp, desc);
526 #endif /* MEMP_OVERFLOW_CHECK */
533 LWIP_UNUSED_ARG(desc);
534 SYS_ARCH_UNPROTECT(old_level);
536 #else /* MEMP_MEM_MALLOC */
537 memp->next = *desc->tab;
540 #if MEMP_SANITY_CHECK
541 LWIP_ASSERT("memp sanity", memp_sanity(desc));
542 #endif /* MEMP_SANITY_CHECK */
544 SYS_ARCH_UNPROTECT(old_level);
545 #endif /* !MEMP_MEM_MALLOC */
549 * Put a custom pool element back into its pool.
551 * @param desc the pool where to put mem
552 * @param mem the memp element to free
555 memp_free_pool(const struct memp_desc* desc, void *mem)
557 LWIP_ASSERT("invalid pool desc", desc != NULL);
558 if ((desc == NULL) || (mem == NULL)) {
562 do_memp_free_pool(desc, mem);
566 * Put an element back into its pool.
568 * @param type the pool where to put mem
569 * @param mem the memp element to free
572 memp_free(memp_t type, void *mem)
574 #ifdef LWIP_HOOK_MEMP_AVAILABLE
575 struct memp *old_first;
578 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
579 int *num_used_pool_ptr = get_num_used_pool_ptr(type);
582 LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
588 #if MEMP_OVERFLOW_CHECK >= 2
589 memp_overflow_check_all();
590 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
592 #ifdef LWIP_HOOK_MEMP_AVAILABLE
593 old_first = *memp_pools[type]->tab;
596 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
597 if (MEMP_IS_PBUF_POOL(type)) {
598 (*num_used_pool_ptr)--;
602 do_memp_free_pool(memp_pools[type], mem);
604 #ifdef LWIP_HOOK_MEMP_AVAILABLE
605 if (old_first == NULL) {
606 LWIP_HOOK_MEMP_AVAILABLE(type);
610 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
611 if (LWIP_PERF && MEMP_IS_PBUF_POOL(type)) {
612 LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_TRACE, ("mf:p-- = %d (%p) (%s)\n", *num_used_pool_ptr, mem, memp_pools[type]->desc));
613 sys_profile_interval_set_pbuf_highwatermark((u32_t)(*num_used_pool_ptr), MEMP_PBUF_POOL_HIGHWATERMARK(type));
618 #if MEMP_SEPARATE_POOLS
620 memp_pbuf_index(memp_t type, const void *mem)
622 if (MEMP_IS_PBUF_POOL(type)) {
623 size_t buf_size = MEMP_SIZE + MEMP_ALIGN_SIZE(memp_sizes[type]);
624 return ((u8_t *)mem - (u8_t *)LWIP_MEM_ALIGN(memp_pools[type]->base)) / buf_size;
630 memp_num_pbufs(memp_t type)
632 return memp_pools[type]->num;
634 #endif /* MEMP_SEPARATE_POOLS */
636 #endif /* MEMP_MEM_MALLOC */