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"
59 #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
63 #if MEMP_OVERFLOW_CHECK
66 #endif /* MEMP_OVERFLOW_CHECK */
69 #if MEMP_OVERFLOW_CHECK
70 /* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
71 * and at the end of each element, initialize them as 0xcd and check
73 /* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
74 * every single element in each pool is checked!
75 * This is VERY SLOW but also very helpful. */
76 /* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
77 * lwipopts.h to change the amount reserved for checking. */
78 #ifndef MEMP_SANITY_REGION_BEFORE
79 #define MEMP_SANITY_REGION_BEFORE 16
80 #endif /* MEMP_SANITY_REGION_BEFORE*/
81 #if MEMP_SANITY_REGION_BEFORE > 0
82 #define MEMP_SANITY_REGION_BEFORE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
84 #define MEMP_SANITY_REGION_BEFORE_ALIGNED 0
85 #endif /* MEMP_SANITY_REGION_BEFORE*/
86 #ifndef MEMP_SANITY_REGION_AFTER
87 #define MEMP_SANITY_REGION_AFTER 16
88 #endif /* MEMP_SANITY_REGION_AFTER*/
89 #if MEMP_SANITY_REGION_AFTER > 0
90 #define MEMP_SANITY_REGION_AFTER_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
92 #define MEMP_SANITY_REGION_AFTER_ALIGNED 0
93 #endif /* MEMP_SANITY_REGION_AFTER*/
95 /* MEMP_SIZE: save space for struct memp and for sanity check */
96 #define MEMP_SIZE (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
97 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
99 #else /* MEMP_OVERFLOW_CHECK */
102 * We don't need to preserve the struct memp while not allocated, so we
103 * can save a little space and set MEMP_SIZE to 0.
106 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
108 #endif /* MEMP_OVERFLOW_CHECK */
110 /** This array holds the first free element of each pool.
111 * Elements form a linked list. */
112 static struct memp *memp_tab[MEMP_MAX];
114 #else /* MEMP_MEM_MALLOC */
116 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
118 #endif /* MEMP_MEM_MALLOC */
120 /** This array holds the element sizes of each pool. */
121 #if !MEM_USE_POOLS && !MEMP_MEM_MALLOC
124 const u16_t memp_sizes[MEMP_MAX] = {
125 #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEM_ALIGN_SIZE(size),
126 #include "lwip/memp_std.h"
129 #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
131 /** This array holds the number of elements in each pool. */
132 static const u16_t memp_num[MEMP_MAX] = {
133 #define LWIP_MEMPOOL(name,num,size,desc) (num),
134 #include "lwip/memp_std.h"
137 /** This array holds a textual description of each pool. */
139 static const char *memp_desc[MEMP_MAX] = {
140 #define LWIP_MEMPOOL(name,num,size,desc) (desc),
141 #include "lwip/memp_std.h"
143 #endif /* LWIP_DEBUG */
145 /** This is the actual memory used by the pools. */
146 static u8_t memp_memory[MEM_ALIGNMENT - 1
147 #define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
148 #include "lwip/memp_std.h"
151 #if MEMP_SANITY_CHECK
153 * Check that memp-lists don't form a circle
161 for (i = 0; i < MEMP_MAX; i++) {
162 for (m = memp_tab[i]; m != NULL; m = m->next) {
164 for (n = memp_tab[i]; n != NULL; n = n->next) {
165 if (n == m && --c < 0) {
173 #endif /* MEMP_SANITY_CHECK*/
174 #if MEMP_OVERFLOW_CHECK
176 * Check if a memp element was victim of an overflow
177 * (e.g. the restricted area after it has been altered)
179 * @param p the memp element to check
180 * @param memp_size the element size of the pool p comes from
183 memp_overflow_check_element(struct memp *p, u16_t memp_size)
187 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
188 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
189 for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
191 LWIP_ASSERT("detected memp underflow!", 0);
195 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
196 m = (u8_t*)p + MEMP_SIZE + memp_size;
197 for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
199 LWIP_ASSERT("detected memp overflow!", 0);
206 * Do an overflow check for all elements in every pool.
208 * @see memp_overflow_check_element for a description of the check
211 memp_overflow_check_all(void)
216 p = LWIP_MEM_ALIGN(memp_memory);
217 for (i = 0; i < MEMP_MAX; ++i) {
219 for (j = 0; j < memp_num[i]; ++j) {
220 memp_overflow_check_element(p, memp_sizes[i]);
221 p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
227 * Initialize the restricted areas of all memp elements in every pool.
230 memp_overflow_init(void)
236 p = LWIP_MEM_ALIGN(memp_memory);
237 for (i = 0; i < MEMP_MAX; ++i) {
239 for (j = 0; j < memp_num[i]; ++j) {
240 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
241 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
242 memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
244 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
245 m = (u8_t*)p + MEMP_SIZE + memp_sizes[i];
246 memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
248 p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
252 #endif /* MEMP_OVERFLOW_CHECK */
255 * Initialize this module.
257 * Carves out memp_memory into linked lists for each pool-type.
265 for (i = 0; i < MEMP_MAX; ++i) {
266 MEMP_STATS_AVAIL(used, i, 0);
267 MEMP_STATS_AVAIL(max, i, 0);
268 MEMP_STATS_AVAIL(err, i, 0);
269 MEMP_STATS_AVAIL(avail, i, memp_num[i]);
272 memp = LWIP_MEM_ALIGN(memp_memory);
273 /* for every pool: */
274 for (i = 0; i < MEMP_MAX; ++i) {
276 /* create a linked list of memp elements */
277 for (j = 0; j < memp_num[i]; ++j) {
278 memp->next = memp_tab[i];
280 memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]
281 #if MEMP_OVERFLOW_CHECK
282 + MEMP_SANITY_REGION_AFTER_ALIGNED
287 #if MEMP_OVERFLOW_CHECK
288 memp_overflow_init();
289 /* check everything a first time to see if it worked */
290 memp_overflow_check_all();
291 #endif /* MEMP_OVERFLOW_CHECK */
295 * Get an element from a specific pool.
297 * @param type the pool to get an element from
299 * the debug version has two more parameters:
300 * @param file file name calling this function
301 * @param line number of line where this function is called
303 * @return a pointer to the allocated memory or a NULL pointer on error
306 #if !MEMP_OVERFLOW_CHECK
307 memp_malloc(memp_t type)
309 memp_malloc_fn(memp_t type, const char* file, const int line)
313 SYS_ARCH_DECL_PROTECT(old_level);
315 LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
317 SYS_ARCH_PROTECT(old_level);
318 #if MEMP_OVERFLOW_CHECK >= 2
319 memp_overflow_check_all();
320 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
322 memp = memp_tab[type];
325 memp_tab[type] = memp->next;
326 #if MEMP_OVERFLOW_CHECK
330 #endif /* MEMP_OVERFLOW_CHECK */
331 MEMP_STATS_INC_USED(used, type);
332 LWIP_ASSERT("memp_malloc: memp properly aligned",
333 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
334 memp = (struct memp*)((u8_t*)memp + MEMP_SIZE);
336 LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
337 MEMP_STATS_INC(err, type);
340 SYS_ARCH_UNPROTECT(old_level);
346 * Put an element back into its pool.
348 * @param type the pool where to put mem
349 * @param mem the memp element to free
352 memp_free(memp_t type, void *mem)
355 SYS_ARCH_DECL_PROTECT(old_level);
360 LWIP_ASSERT("memp_free: mem properly aligned",
361 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
363 memp = (struct memp *)((u8_t*)mem - MEMP_SIZE);
365 SYS_ARCH_PROTECT(old_level);
366 #if MEMP_OVERFLOW_CHECK
367 #if MEMP_OVERFLOW_CHECK >= 2
368 memp_overflow_check_all();
370 memp_overflow_check_element(memp, memp_sizes[type]);
371 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
372 #endif /* MEMP_OVERFLOW_CHECK */
374 MEMP_STATS_DEC(used, type);
376 memp->next = memp_tab[type];
377 memp_tab[type] = memp;
379 #if MEMP_SANITY_CHECK
380 LWIP_ASSERT("memp sanity", memp_sanity());
381 #endif /* MEMP_SANITY_CHECK */
383 SYS_ARCH_UNPROTECT(old_level);
386 #endif /* MEMP_MEM_MALLOC */