1 /**
2 * @file
3 * Dynamic pool memory manager
4 *
5 * lwIP has dedicated pools for many structures (netconn, protocol control blocks,
6 * packet buffers, ...). All these pools are managed here.
7 *
8 * @defgroup mempool Memory pools
9 * @ingroup infrastructure
10 * Custom memory pools
11
12 */
13
14 /*
15 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without modification,
19 * are permitted provided that the following conditions are met:
20 *
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.
28 *
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
38 * OF SUCH DAMAGE.
39 *
40 * This file is part of the lwIP TCP/IP stack.
41 *
42 * Author: Adam Dunkels <adam@sics.se>
43 *
44 */
45
46 #include "lwip/opt.h"
47
48 #include "lwip/memp.h"
49 #include "lwip/sys.h"
50 #include "lwip/stats.h"
51
52 #include <string.h>
53
54 /* Make sure we include everything we need for size calculation required by memp_std.h */
55 #include "lwip/pbuf.h"
56 #include "lwip/raw.h"
57 #include "lwip/udp.h"
58 #include "lwip/tcp.h"
59 #include "lwip/priv/tcp_priv.h"
60 #include "lwip/ip4_frag.h"
61 #include "lwip/netbuf.h"
62 #include "lwip/api.h"
63 #include "lwip/priv/tcpip_priv.h"
64 #include "lwip/priv/api_msg.h"
65 #include "lwip/sockets.h"
66 #include "lwip/priv/sockets_priv.h"
67 #include "lwip/netifapi.h"
68 #include "lwip/etharp.h"
69 #include "lwip/igmp.h"
70 #include "lwip/timeouts.h"
71 /* needed by default MEMP_NUM_SYS_TIMEOUT */
72 #include "netif/ppp/ppp_opts.h"
73 #include "lwip/netdb.h"
74 #include "lwip/dns.h"
75 #include "lwip/priv/nd6_priv.h"
76 #include "lwip/ip6_frag.h"
77 #include "lwip/mld6.h"
78
79 #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
80 #include "lwip/priv/memp_std.h"
81
82 const struct memp_desc* const memp_pools[MEMP_MAX] = {
83 #define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name,
84 #include "lwip/priv/memp_std.h"
85 };
86
87 #ifdef LWIP_HOOK_FILENAME
88 #include LWIP_HOOK_FILENAME
89 #endif
90
91 #if MEMP_MEM_MALLOC && MEMP_OVERFLOW_CHECK >= 2
92 #undef MEMP_OVERFLOW_CHECK
93 /* MEMP_OVERFLOW_CHECK >= 2 does not work with MEMP_MEM_MALLOC, use 1 instead */
94 #define MEMP_OVERFLOW_CHECK 1
95 #endif
96
97 #if MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC
98 /**
99 * Check that memp-lists don't form a circle, using "Floyd's cycle-finding algorithm".
100 */
101 static int
memp_sanity(const struct memp_desc * desc)102 memp_sanity(const struct memp_desc *desc)
103 {
104 struct memp *t, *h;
105
106 t = *desc->tab;
107 if (t != NULL) {
108 for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
109 h = ((h->next != NULL) ? h->next->next : NULL)) {
110 if (t == h) {
111 return 0;
112 }
113 }
114 }
115
116 return 1;
117 }
118 #endif /* MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC */
119
120 #if MEMP_OVERFLOW_CHECK
121 /**
122 * Check if a memp element was victim of an overflow
123 * (e.g. the restricted area after it has been altered)
124 *
125 * @param p the memp element to check
126 * @param desc the pool p comes from
127 */
128 static void
memp_overflow_check_element_overflow(struct memp * p,const struct memp_desc * desc)129 memp_overflow_check_element_overflow(struct memp *p, const struct memp_desc *desc)
130 {
131 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
132 u16_t k;
133 u8_t *m;
134 m = (u8_t*)p + MEMP_SIZE + desc->size;
135 for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
136 if (m[k] != 0xcd) {
137 char errstr[128] = "detected memp overflow in pool ";
138 strcat(errstr, desc->desc);
139 LWIP_ASSERT(errstr, 0);
140 }
141 }
142 #else /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
143 LWIP_UNUSED_ARG(p);
144 LWIP_UNUSED_ARG(desc);
145 #endif /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
146 }
147
148 /**
149 * Check if a memp element was victim of an underflow
150 * (e.g. the restricted area before it has been altered)
151 *
152 * @param p the memp element to check
153 * @param desc the pool p comes from
154 */
155 static void
memp_overflow_check_element_underflow(struct memp * p,const struct memp_desc * desc)156 memp_overflow_check_element_underflow(struct memp *p, const struct memp_desc *desc)
157 {
158 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
159 u16_t k;
160 u8_t *m;
161 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
162 for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
163 if (m[k] != 0xcd) {
164 char errstr[128] = "detected memp underflow in pool ";
165 strcat(errstr, desc->desc);
166 LWIP_ASSERT(errstr, 0);
167 }
168 }
169 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
170 LWIP_UNUSED_ARG(p);
171 LWIP_UNUSED_ARG(desc);
172 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
173 }
174
175 /**
176 * Initialize the restricted area of on memp element.
177 */
178 static void
memp_overflow_init_element(struct memp * p,const struct memp_desc * desc)179 memp_overflow_init_element(struct memp *p, const struct memp_desc *desc)
180 {
181 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0
182 u8_t *m;
183 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
184 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
185 memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
186 #endif
187 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
188 m = (u8_t*)p + MEMP_SIZE + desc->size;
189 memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
190 #endif
191 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
192 LWIP_UNUSED_ARG(p);
193 LWIP_UNUSED_ARG(desc);
194 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
195 }
196
197 #if MEMP_OVERFLOW_CHECK >= 2
198 /**
199 * Do an overflow check for all elements in every pool.
200 *
201 * @see memp_overflow_check_element for a description of the check
202 */
203 static void
memp_overflow_check_all(void)204 memp_overflow_check_all(void)
205 {
206 u16_t i, j;
207 struct memp *p;
208 SYS_ARCH_DECL_PROTECT(old_level);
209 SYS_ARCH_PROTECT(old_level);
210
211 for (i = 0; i < MEMP_MAX; ++i) {
212 p = (struct memp*)LWIP_MEM_ALIGN(memp_pools[i]->base);
213 for (j = 0; j < memp_pools[i]->num; ++j) {
214 memp_overflow_check_element_overflow(p, memp_pools[i]);
215 memp_overflow_check_element_underflow(p, memp_pools[i]);
216 p = LWIP_ALIGNMENT_CAST(struct memp*, ((u8_t*)p + MEMP_SIZE + memp_pools[i]->size + MEMP_SANITY_REGION_AFTER_ALIGNED));
217 }
218 }
219 SYS_ARCH_UNPROTECT(old_level);
220 }
221 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
222 #endif /* MEMP_OVERFLOW_CHECK */
223
224 /**
225 * Initialize custom memory pool.
226 * Related functions: memp_malloc_pool, memp_free_pool
227 *
228 * @param desc pool to initialize
229 */
230 void
memp_init_pool(const struct memp_desc * desc)231 memp_init_pool(const struct memp_desc *desc)
232 {
233 #if MEMP_MEM_MALLOC
234 LWIP_UNUSED_ARG(desc);
235 #else
236 int i;
237 struct memp *memp;
238
239 *desc->tab = NULL;
240 memp = (struct memp*)LWIP_MEM_ALIGN(desc->base);
241 /* create a linked list of memp elements */
242 for (i = 0; i < desc->num; ++i) {
243 memp->next = *desc->tab;
244 *desc->tab = memp;
245 #if MEMP_OVERFLOW_CHECK
246 memp_overflow_init_element(memp, desc);
247 #endif /* MEMP_OVERFLOW_CHECK */
248 /* cast through void* to get rid of alignment warnings */
249 memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
250 #if MEMP_OVERFLOW_CHECK
251 + MEMP_SANITY_REGION_AFTER_ALIGNED
252 #endif
253 );
254 }
255 #if MEMP_STATS
256 desc->stats->avail = desc->num;
257 #endif /* MEMP_STATS */
258 #endif /* !MEMP_MEM_MALLOC */
259
260 #if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
261 desc->stats->name = desc->desc;
262 #endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
263 }
264
265 /**
266 * Initializes lwIP built-in pools.
267 * Related functions: memp_malloc, memp_free
268 *
269 * Carves out memp_memory into linked lists for each pool-type.
270 */
271 void
memp_init(void)272 memp_init(void)
273 {
274 u16_t i;
275
276 /* for every pool: */
277 for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
278 memp_init_pool(memp_pools[i]);
279
280 #if LWIP_STATS && MEMP_STATS
281 lwip_stats.memp[i] = memp_pools[i]->stats;
282 #endif
283 }
284
285 #if MEMP_OVERFLOW_CHECK >= 2
286 /* check everything a first time to see if it worked */
287 memp_overflow_check_all();
288 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
289 }
290
291 static void*
292 #if !MEMP_OVERFLOW_CHECK
do_memp_malloc_pool(const struct memp_desc * desc)293 do_memp_malloc_pool(const struct memp_desc *desc)
294 #else
295 do_memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
296 #endif
297 {
298 struct memp *memp;
299 SYS_ARCH_DECL_PROTECT(old_level);
300
301 #if MEMP_MEM_MALLOC
302 memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
303 SYS_ARCH_PROTECT(old_level);
304 #else /* MEMP_MEM_MALLOC */
305 SYS_ARCH_PROTECT(old_level);
306
307 memp = *desc->tab;
308 #endif /* MEMP_MEM_MALLOC */
309
310 if (memp != NULL) {
311 #if !MEMP_MEM_MALLOC
312 #if MEMP_OVERFLOW_CHECK == 1
313 memp_overflow_check_element_overflow(memp, desc);
314 memp_overflow_check_element_underflow(memp, desc);
315 #endif /* MEMP_OVERFLOW_CHECK */
316
317 *desc->tab = memp->next;
318 #if MEMP_OVERFLOW_CHECK
319 memp->next = NULL;
320 #endif /* MEMP_OVERFLOW_CHECK */
321 #endif /* !MEMP_MEM_MALLOC */
322 #if MEMP_OVERFLOW_CHECK
323 memp->file = file;
324 memp->line = line;
325 #if MEMP_MEM_MALLOC
326 memp_overflow_init_element(memp, desc);
327 #endif /* MEMP_MEM_MALLOC */
328 #endif /* MEMP_OVERFLOW_CHECK */
329 LWIP_ASSERT("memp_malloc: memp properly aligned",
330 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
331 #if MEMP_STATS
332 desc->stats->used++;
333 if (desc->stats->used > desc->stats->max) {
334 desc->stats->max = desc->stats->used;
335 }
336 #endif
337 SYS_ARCH_UNPROTECT(old_level);
338 /* cast through u8_t* to get rid of alignment warnings */
339 return ((u8_t*)memp + MEMP_SIZE);
340 } else {
341 LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
342 #if MEMP_STATS
343 desc->stats->err++;
344 #endif
345 }
346
347 SYS_ARCH_UNPROTECT(old_level);
348 return NULL;
349 }
350
351 /**
352 * Get an element from a custom pool.
353 *
354 * @param desc the pool to get an element from
355 *
356 * @return a pointer to the allocated memory or a NULL pointer on error
357 */
358 void *
359 #if !MEMP_OVERFLOW_CHECK
memp_malloc_pool(const struct memp_desc * desc)360 memp_malloc_pool(const struct memp_desc *desc)
361 #else
362 memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
363 #endif
364 {
365 LWIP_ASSERT("invalid pool desc", desc != NULL);
366 if (desc == NULL) {
367 return NULL;
368 }
369
370 #if !MEMP_OVERFLOW_CHECK
371 return do_memp_malloc_pool(desc);
372 #else
373 return do_memp_malloc_pool_fn(desc, file, line);
374 #endif
375 }
376
377 /**
378 * Get an element from a specific pool.
379 *
380 * @param type the pool to get an element from
381 *
382 * @return a pointer to the allocated memory or a NULL pointer on error
383 */
384 void *
385 #if !MEMP_OVERFLOW_CHECK
memp_malloc(memp_t type)386 memp_malloc(memp_t type)
387 #else
388 memp_malloc_fn(memp_t type, const char* file, const int line)
389 #endif
390 {
391 void *memp;
392 LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
393
394 #if MEMP_OVERFLOW_CHECK >= 2
395 memp_overflow_check_all();
396 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
397
398 #if !MEMP_OVERFLOW_CHECK
399 memp = do_memp_malloc_pool(memp_pools[type]);
400 #else
401 memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
402 #endif
403
404 return memp;
405 }
406
407 static void
do_memp_free_pool(const struct memp_desc * desc,void * mem)408 do_memp_free_pool(const struct memp_desc* desc, void *mem)
409 {
410 struct memp *memp;
411 SYS_ARCH_DECL_PROTECT(old_level);
412
413 LWIP_ASSERT("memp_free: mem properly aligned",
414 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
415
416 /* cast through void* to get rid of alignment warnings */
417 memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
418
419 SYS_ARCH_PROTECT(old_level);
420
421 #if MEMP_OVERFLOW_CHECK == 1
422 memp_overflow_check_element_overflow(memp, desc);
423 memp_overflow_check_element_underflow(memp, desc);
424 #endif /* MEMP_OVERFLOW_CHECK */
425
426 #if MEMP_STATS
427 desc->stats->used--;
428 #endif
429
430 #if MEMP_MEM_MALLOC
431 LWIP_UNUSED_ARG(desc);
432 SYS_ARCH_UNPROTECT(old_level);
433 mem_free(memp);
434 #else /* MEMP_MEM_MALLOC */
435 memp->next = *desc->tab;
436 *desc->tab = memp;
437
438 #if MEMP_SANITY_CHECK
439 LWIP_ASSERT("memp sanity", memp_sanity(desc));
440 #endif /* MEMP_SANITY_CHECK */
441
442 SYS_ARCH_UNPROTECT(old_level);
443 #endif /* !MEMP_MEM_MALLOC */
444 }
445
446 /**
447 * Put a custom pool element back into its pool.
448 *
449 * @param desc the pool where to put mem
450 * @param mem the memp element to free
451 */
452 void
memp_free_pool(const struct memp_desc * desc,void * mem)453 memp_free_pool(const struct memp_desc* desc, void *mem)
454 {
455 LWIP_ASSERT("invalid pool desc", desc != NULL);
456 if ((desc == NULL) || (mem == NULL)) {
457 return;
458 }
459
460 do_memp_free_pool(desc, mem);
461 }
462
463 /**
464 * Put an element back into its pool.
465 *
466 * @param type the pool where to put mem
467 * @param mem the memp element to free
468 */
469 void
memp_free(memp_t type,void * mem)470 memp_free(memp_t type, void *mem)
471 {
472 #ifdef LWIP_HOOK_MEMP_AVAILABLE
473 struct memp *old_first;
474 #endif
475
476 LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
477
478 if (mem == NULL) {
479 return;
480 }
481
482 #if MEMP_OVERFLOW_CHECK >= 2
483 memp_overflow_check_all();
484 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
485
486 #ifdef LWIP_HOOK_MEMP_AVAILABLE
487 old_first = *memp_pools[type]->tab;
488 #endif
489
490 do_memp_free_pool(memp_pools[type], mem);
491
492 #ifdef LWIP_HOOK_MEMP_AVAILABLE
493 if (old_first == NULL) {
494 LWIP_HOOK_MEMP_AVAILABLE(type);
495 }
496 #endif
497 }
498