BadVPN – Blame information for rev 1

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