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.
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 * This file is part of the lwIP TCP/IP stack.
37 * Author: Adam Dunkels <adam@sics.se>
43 #include "lwip/memp.h"
44 #include "lwip/pbuf.h"
48 #include "lwip/igmp.h"
50 #include "lwip/api_msg.h"
51 #include "lwip/tcpip.h"
53 #include "lwip/stats.h"
54 #include "netif/etharp.h"
55 #include "lwip/ip_frag.h"
61 #if MEMP_OVERFLOW_CHECK
64 #endif /* MEMP_OVERFLOW_CHECK */
67 #if MEMP_OVERFLOW_CHECK
68 /* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
69 * and at the end of each element, initialize them as 0xcd and check
71 /* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
72 * every single element in each pool is checked!
73 * This is VERY SLOW but also very helpful. */
74 /* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
75 * lwipopts.h to change the amount reserved for checking. */
76 #ifndef MEMP_SANITY_REGION_BEFORE
77 #define MEMP_SANITY_REGION_BEFORE 16
78 #endif /* MEMP_SANITY_REGION_BEFORE*/
79 #if MEMP_SANITY_REGION_BEFORE > 0
80 #define MEMP_SANITY_REGION_BEFORE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
82 #define MEMP_SANITY_REGION_BEFORE_ALIGNED 0
83 #endif /* MEMP_SANITY_REGION_BEFORE*/
84 #ifndef MEMP_SANITY_REGION_AFTER
85 #define MEMP_SANITY_REGION_AFTER 16
86 #endif /* MEMP_SANITY_REGION_AFTER*/
87 #if MEMP_SANITY_REGION_AFTER > 0
88 #define MEMP_SANITY_REGION_AFTER_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
90 #define MEMP_SANITY_REGION_AFTER_ALIGNED 0
91 #endif /* MEMP_SANITY_REGION_AFTER*/
93 /* MEMP_SIZE: save space for struct memp and for sanity check */
94 #define MEMP_SIZE (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
95 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
97 #else /* MEMP_OVERFLOW_CHECK */
100 * We don't need to preserve the struct memp while not allocated, so we
101 * can save a little space and set MEMP_SIZE to 0.
104 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
106 #endif /* MEMP_OVERFLOW_CHECK */
108 /** This array holds the first free element of each pool.
109 * Elements form a linked list. */
110 static struct memp *memp_tab[MEMP_MAX];
112 /** This array holds the element sizes of each pool. */
116 const u16_t memp_sizes[MEMP_MAX] = {
117 #define LWIP_MEMPOOL(name,num,size,desc) MEMP_ALIGN_SIZE(size),
118 #include "lwip/memp_std.h"
121 /** This array holds the number of elements in each pool. */
122 static const u16_t memp_num[MEMP_MAX] = {
123 #define LWIP_MEMPOOL(name,num,size,desc) (num),
124 #include "lwip/memp_std.h"
127 /** This array holds a textual description of each pool. */
129 static const char *memp_desc[MEMP_MAX] = {
130 #define LWIP_MEMPOOL(name,num,size,desc) (desc),
131 #include "lwip/memp_std.h"
133 #endif /* LWIP_DEBUG */
135 /** This is the actual memory used by the pools. */
136 static u8_t memp_memory[MEM_ALIGNMENT - 1
137 #define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
138 #include "lwip/memp_std.h"
141 #if MEMP_SANITY_CHECK
143 * Check that memp-lists don't form a circle
151 for (i = 0; i < MEMP_MAX; i++) {
152 for (m = memp_tab[i]; m != NULL; m = m->next) {
154 for (n = memp_tab[i]; n != NULL; n = n->next) {
155 if (n == m && --c < 0) {
163 #endif /* MEMP_SANITY_CHECK*/
164 #if MEMP_OVERFLOW_CHECK
166 * Check if a memp element was victim of an overflow
167 * (e.g. the restricted area after it has been altered)
169 * @param p the memp element to check
170 * @param memp_size the element size of the pool p comes from
173 memp_overflow_check_element(struct memp *p, u16_t memp_size)
177 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
178 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
179 for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
181 LWIP_ASSERT("detected memp underflow!", 0);
185 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
186 m = (u8_t*)p + MEMP_SIZE + memp_size - MEMP_SANITY_REGION_AFTER_ALIGNED;
187 for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
189 LWIP_ASSERT("detected memp overflow!", 0);
196 * Do an overflow check for all elements in every pool.
198 * @see memp_overflow_check_element for a description of the check
201 memp_overflow_check_all(void)
206 p = LWIP_MEM_ALIGN(memp_memory);
207 for (i = 0; i < MEMP_MAX; ++i) {
209 for (j = 0; j < memp_num[i]; ++j) {
210 memp_overflow_check_element(p, memp_sizes[i]);
211 p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i]);
217 * Initialize the restricted areas of all memp elements in every pool.
220 memp_overflow_init(void)
226 p = LWIP_MEM_ALIGN(memp_memory);
227 for (i = 0; i < MEMP_MAX; ++i) {
229 for (j = 0; j < memp_num[i]; ++j) {
230 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
231 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
232 memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
234 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
235 m = (u8_t*)p + MEMP_SIZE + memp_sizes[i] - MEMP_SANITY_REGION_AFTER_ALIGNED;
236 memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
238 p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i]);
242 #endif /* MEMP_OVERFLOW_CHECK */
245 * Initialize this module.
247 * Carves out memp_memory into linked lists for each pool-type.
256 for (i = 0; i < MEMP_MAX; ++i) {
257 lwip_stats.memp[i].used = lwip_stats.memp[i].max =
258 lwip_stats.memp[i].err = 0;
259 lwip_stats.memp[i].avail = memp_num[i];
261 #endif /* MEMP_STATS */
263 memp = LWIP_MEM_ALIGN(memp_memory);
264 /* for every pool: */
265 for (i = 0; i < MEMP_MAX; ++i) {
267 /* create a linked list of memp elements */
268 for (j = 0; j < memp_num[i]; ++j) {
269 memp->next = memp_tab[i];
271 memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]);
274 #if MEMP_OVERFLOW_CHECK
275 memp_overflow_init();
276 /* check everything a first time to see if it worked */
277 memp_overflow_check_all();
278 #endif /* MEMP_OVERFLOW_CHECK */
282 * Get an element from a specific pool.
284 * @param type the pool to get an element from
286 * the debug version has two more parameters:
287 * @param file file name calling this function
288 * @param line number of line where this function is called
290 * @return a pointer to the allocated memory or a NULL pointer on error
293 #if !MEMP_OVERFLOW_CHECK
294 memp_malloc(memp_t type)
296 memp_malloc_fn(memp_t type, const char* file, const int line)
300 SYS_ARCH_DECL_PROTECT(old_level);
302 LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
304 SYS_ARCH_PROTECT(old_level);
305 #if MEMP_OVERFLOW_CHECK >= 2
306 memp_overflow_check_all();
307 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
309 memp = memp_tab[type];
312 memp_tab[type] = memp->next;
313 #if MEMP_OVERFLOW_CHECK
317 #endif /* MEMP_OVERFLOW_CHECK */
319 ++lwip_stats.memp[type].used;
320 if (lwip_stats.memp[type].used > lwip_stats.memp[type].max) {
321 lwip_stats.memp[type].max = lwip_stats.memp[type].used;
323 #endif /* MEMP_STATS */
324 LWIP_ASSERT("memp_malloc: memp properly aligned",
325 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
326 memp = (struct memp*)((u8_t*)memp + MEMP_SIZE);
328 LWIP_DEBUGF(MEMP_DEBUG | 2, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
330 ++lwip_stats.memp[type].err;
331 #endif /* MEMP_STATS */
334 SYS_ARCH_UNPROTECT(old_level);
340 * Put an element back into its pool.
342 * @param type the pool where to put mem
343 * @param mem the memp element to free
346 memp_free(memp_t type, void *mem)
349 SYS_ARCH_DECL_PROTECT(old_level);
354 LWIP_ASSERT("memp_free: mem properly aligned",
355 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
357 memp = (struct memp *)((u8_t*)mem - MEMP_SIZE);
359 SYS_ARCH_PROTECT(old_level);
360 #if MEMP_OVERFLOW_CHECK
361 #if MEMP_OVERFLOW_CHECK >= 2
362 memp_overflow_check_all();
364 memp_overflow_check_element(memp, memp_sizes[type]);
365 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
366 #endif /* MEMP_OVERFLOW_CHECK */
369 lwip_stats.memp[type].used--;
370 #endif /* MEMP_STATS */
372 memp->next = memp_tab[type];
373 memp_tab[type] = memp;
375 #if MEMP_SANITY_CHECK
376 LWIP_ASSERT("memp sanity", memp_sanity());
377 #endif /* MEMP_SANITY_CHECK */
379 SYS_ARCH_UNPROTECT(old_level);