The Pedigree Project  0.1
memp.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
33 /*
34  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without modification,
38  * are permitted provided that the following conditions are met:
39  *
40  * 1. Redistributions of source code must retain the above copyright notice,
41  * this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright notice,
43  * this list of conditions and the following disclaimer in the documentation
44  * and/or other materials provided with the distribution.
45  * 3. The name of the author may not be used to endorse or promote products
46  * derived from this software without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
49  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
51  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
52  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
53  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
56  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
57  * OF SUCH DAMAGE.
58  *
59  * This file is part of the lwIP TCP/IP stack.
60  *
61  * Author: Adam Dunkels <adam@sics.se>
62  *
63  */
64 
65 #include "lwip/opt.h"
66 
67 #include "lwip/memp.h"
68 #include "lwip/sys.h"
69 #include "lwip/stats.h"
70 
71 #include <string.h>
72 
73 /* Make sure we include everything we need for size calculation required by memp_std.h */
74 #include "lwip/pbuf.h"
75 #include "lwip/raw.h"
76 #include "lwip/udp.h"
77 #include "lwip/tcp.h"
78 #include "lwip/priv/tcp_priv.h"
79 #include "lwip/ip4_frag.h"
80 #include "lwip/netbuf.h"
81 #include "lwip/api.h"
82 #include "lwip/priv/tcpip_priv.h"
83 #include "lwip/priv/api_msg.h"
84 #include "lwip/sockets.h"
85 #include "lwip/netifapi.h"
86 #include "lwip/etharp.h"
87 #include "lwip/igmp.h"
88 #include "lwip/timeouts.h"
89 /* needed by default MEMP_NUM_SYS_TIMEOUT */
90 #include "netif/ppp/ppp_opts.h"
91 #include "lwip/netdb.h"
92 #include "lwip/dns.h"
93 #include "lwip/priv/nd6_priv.h"
94 #include "lwip/ip6_frag.h"
95 #include "lwip/mld6.h"
96 
97 #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
98 #include "lwip/priv/memp_std.h"
99 
100 const struct memp_desc* const memp_pools[MEMP_MAX] = {
101 #define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name,
102 #include "lwip/priv/memp_std.h"
103 };
104 
105 #ifdef LWIP_HOOK_FILENAME
106 #include LWIP_HOOK_FILENAME
107 #endif
108 
109 #if MEMP_MEM_MALLOC && MEMP_OVERFLOW_CHECK >= 2
110 #undef MEMP_OVERFLOW_CHECK
111 /* MEMP_OVERFLOW_CHECK >= 2 does not work with MEMP_MEM_MALLOC, use 1 instead */
112 #define MEMP_OVERFLOW_CHECK 1
113 #endif
114 
115 #if MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC
116 
119 static int
120 memp_sanity(const struct memp_desc *desc)
121 {
122  struct memp *t, *h;
123 
124  t = *desc->tab;
125  if (t != NULL) {
126  for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
127  h = ((h->next != NULL) ? h->next->next : NULL)) {
128  if (t == h) {
129  return 0;
130  }
131  }
132  }
133 
134  return 1;
135 }
136 #endif /* MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC */
137 
138 #if MEMP_OVERFLOW_CHECK
139 
146 static void
147 memp_overflow_check_element_overflow(struct memp *p, const struct memp_desc *desc)
148 {
149 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
150  u16_t k;
151  u8_t *m;
152  m = (u8_t*)p + MEMP_SIZE + desc->size;
153  for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
154  if (m[k] != 0xcd) {
155  char errstr[128] = "detected memp overflow in pool ";
156  strcat(errstr, desc->desc);
157  LWIP_ASSERT(errstr, 0);
158  }
159  }
160 #else /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
161  LWIP_UNUSED_ARG(p);
162  LWIP_UNUSED_ARG(desc);
163 #endif /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
164 }
165 
173 static void
174 memp_overflow_check_element_underflow(struct memp *p, const struct memp_desc *desc)
175 {
176 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
177  u16_t k;
178  u8_t *m;
179  m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
180  for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
181  if (m[k] != 0xcd) {
182  char errstr[128] = "detected memp underflow in pool ";
183  strcat(errstr, desc->desc);
184  LWIP_ASSERT(errstr, 0);
185  }
186  }
187 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
188  LWIP_UNUSED_ARG(p);
189  LWIP_UNUSED_ARG(desc);
190 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
191 }
192 
196 static void
197 memp_overflow_init_element(struct memp *p, const struct memp_desc *desc)
198 {
199 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0
200  u8_t *m;
201 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
202  m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
203  memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
204 #endif
205 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
206  m = (u8_t*)p + MEMP_SIZE + desc->size;
207  memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
208 #endif
209 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
210  LWIP_UNUSED_ARG(p);
211  LWIP_UNUSED_ARG(desc);
212 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
213 }
214 
215 #if MEMP_OVERFLOW_CHECK >= 2
216 
221 static void
222 memp_overflow_check_all(void)
223 {
224  u16_t i, j;
225  struct memp *p;
226  SYS_ARCH_DECL_PROTECT(old_level);
227  SYS_ARCH_PROTECT(old_level);
228 
229  for (i = 0; i < MEMP_MAX; ++i) {
230  p = (struct memp*)LWIP_MEM_ALIGN(memp_pools[i]->base);
231  for (j = 0; j < memp_pools[i]->num; ++j) {
232  memp_overflow_check_element_overflow(p, memp_pools[i]);
233  memp_overflow_check_element_underflow(p, memp_pools[i]);
234  p = LWIP_ALIGNMENT_CAST(struct memp*, ((u8_t*)p + MEMP_SIZE + memp_pools[i]->size + MEMP_SANITY_REGION_AFTER_ALIGNED));
235  }
236  }
237  SYS_ARCH_UNPROTECT(old_level);
238 }
239 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
240 #endif /* MEMP_OVERFLOW_CHECK */
241 
248 void
249 memp_init_pool(const struct memp_desc *desc)
250 {
251 #if MEMP_MEM_MALLOC
252  LWIP_UNUSED_ARG(desc);
253 #else
254  int i;
255  struct memp *memp;
256 
257  *desc->tab = NULL;
258  memp = (struct memp*)LWIP_MEM_ALIGN(desc->base);
259  /* create a linked list of memp elements */
260  for (i = 0; i < desc->num; ++i) {
261  memp->next = *desc->tab;
262  *desc->tab = memp;
263 #if MEMP_OVERFLOW_CHECK
264  memp_overflow_init_element(memp, desc);
265 #endif /* MEMP_OVERFLOW_CHECK */
266  /* cast through void* to get rid of alignment warnings */
267  memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
269  + MEMP_SANITY_REGION_AFTER_ALIGNED
270 #endif
271  );
272  }
273 #if MEMP_STATS
274  desc->stats->avail = desc->num;
275 #endif /* MEMP_STATS */
276 #endif /* !MEMP_MEM_MALLOC */
277 
278 #if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
279  desc->stats->name = desc->desc;
280 #endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
281 }
282 
289 void
291 {
292  u16_t i;
293 
294  /* for every pool: */
295  for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
296  memp_init_pool(memp_pools[i]);
297 
298 #if LWIP_STATS && MEMP_STATS
299  lwip_stats.memp[i] = memp_pools[i]->stats;
300 #endif
301  }
302 
303 #if MEMP_OVERFLOW_CHECK >= 2
304  /* check everything a first time to see if it worked */
305  memp_overflow_check_all();
306 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
307 }
308 
309 static void*
310 #if !MEMP_OVERFLOW_CHECK
311 do_memp_malloc_pool(const struct memp_desc *desc)
312 #else
313 do_memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
314 #endif
315 {
316  struct memp *memp;
317  SYS_ARCH_DECL_PROTECT(old_level);
318 
319 #if MEMP_MEM_MALLOC
320  memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
321  SYS_ARCH_PROTECT(old_level);
322 #else /* MEMP_MEM_MALLOC */
323  SYS_ARCH_PROTECT(old_level);
324 
325  memp = *desc->tab;
326 #endif /* MEMP_MEM_MALLOC */
327 
328  if (memp != NULL) {
329 #if !MEMP_MEM_MALLOC
330 #if MEMP_OVERFLOW_CHECK == 1
331  memp_overflow_check_element_overflow(memp, desc);
332  memp_overflow_check_element_underflow(memp, desc);
333 #endif /* MEMP_OVERFLOW_CHECK */
334 
335  *desc->tab = memp->next;
336 #if MEMP_OVERFLOW_CHECK
337  memp->next = NULL;
338 #endif /* MEMP_OVERFLOW_CHECK */
339 #endif /* !MEMP_MEM_MALLOC */
340 #if MEMP_OVERFLOW_CHECK
341  memp->file = file;
342  memp->line = line;
343 #if MEMP_MEM_MALLOC
344  memp_overflow_init_element(memp, desc);
345 #endif /* MEMP_MEM_MALLOC */
346 #endif /* MEMP_OVERFLOW_CHECK */
347  LWIP_ASSERT("memp_malloc: memp properly aligned",
348  ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
349 #if MEMP_STATS
350  desc->stats->used++;
351  if (desc->stats->used > desc->stats->max) {
352  desc->stats->max = desc->stats->used;
353  }
354 #endif
355  SYS_ARCH_UNPROTECT(old_level);
356  /* cast through u8_t* to get rid of alignment warnings */
357  return ((u8_t*)memp + MEMP_SIZE);
358  } else {
359  LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
360 #if MEMP_STATS
361  desc->stats->err++;
362 #endif
363  }
364 
365  SYS_ARCH_UNPROTECT(old_level);
366  return NULL;
367 }
368 
376 void *
377 #if !MEMP_OVERFLOW_CHECK
378 memp_malloc_pool(const struct memp_desc *desc)
379 #else
380 memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
381 #endif
382 {
383  LWIP_ASSERT("invalid pool desc", desc != NULL);
384  if (desc == NULL) {
385  return NULL;
386  }
387 
388 #if !MEMP_OVERFLOW_CHECK
389  return do_memp_malloc_pool(desc);
390 #else
391  return do_memp_malloc_pool_fn(desc, file, line);
392 #endif
393 }
394 
402 void *
403 #if !MEMP_OVERFLOW_CHECK
405 #else
406 memp_malloc_fn(memp_t type, const char* file, const int line)
407 #endif
408 {
409  void *memp;
410  LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
411 
412 #if MEMP_OVERFLOW_CHECK >= 2
413  memp_overflow_check_all();
414 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
415 
416 #if !MEMP_OVERFLOW_CHECK
417  memp = do_memp_malloc_pool(memp_pools[type]);
418 #else
419  memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
420 #endif
421 
422  return memp;
423 }
424 
425 static void
426 do_memp_free_pool(const struct memp_desc* desc, void *mem)
427 {
428  struct memp *memp;
429  SYS_ARCH_DECL_PROTECT(old_level);
430 
431  LWIP_ASSERT("memp_free: mem properly aligned",
432  ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
433 
434  /* cast through void* to get rid of alignment warnings */
435  memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
436 
437  SYS_ARCH_PROTECT(old_level);
438 
439 #if MEMP_OVERFLOW_CHECK == 1
440  memp_overflow_check_element_overflow(memp, desc);
441  memp_overflow_check_element_underflow(memp, desc);
442 #endif /* MEMP_OVERFLOW_CHECK */
443 
444 #if MEMP_STATS
445  desc->stats->used--;
446 #endif
447 
448 #if MEMP_MEM_MALLOC
449  LWIP_UNUSED_ARG(desc);
450  SYS_ARCH_UNPROTECT(old_level);
451  mem_free(memp);
452 #else /* MEMP_MEM_MALLOC */
453  memp->next = *desc->tab;
454  *desc->tab = memp;
455 
456 #if MEMP_SANITY_CHECK
457  LWIP_ASSERT("memp sanity", memp_sanity(desc));
458 #endif /* MEMP_SANITY_CHECK */
459 
460  SYS_ARCH_UNPROTECT(old_level);
461 #endif /* !MEMP_MEM_MALLOC */
462 }
463 
470 void
471 memp_free_pool(const struct memp_desc* desc, void *mem)
472 {
473  LWIP_ASSERT("invalid pool desc", desc != NULL);
474  if ((desc == NULL) || (mem == NULL)) {
475  return;
476  }
477 
478  do_memp_free_pool(desc, mem);
479 }
480 
487 void
488 memp_free(memp_t type, void *mem)
489 {
490 #ifdef LWIP_HOOK_MEMP_AVAILABLE
491  struct memp *old_first;
492 #endif
493 
494  LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
495 
496  if (mem == NULL) {
497  return;
498  }
499 
500 #if MEMP_OVERFLOW_CHECK >= 2
501  memp_overflow_check_all();
502 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
503 
504 #ifdef LWIP_HOOK_MEMP_AVAILABLE
505  old_first = *memp_pools[type]->tab;
506 #endif
507 
508  do_memp_free_pool(memp_pools[type], mem);
509 
510 #ifdef LWIP_HOOK_MEMP_AVAILABLE
511  if (old_first == NULL) {
512  LWIP_HOOK_MEMP_AVAILABLE(type);
513  }
514 #endif
515 }
void memp_free_pool(const struct memp_desc *desc, void *mem)
Definition: memp.c:471
void mem_free(void *rmem)
Definition: mem.c:438
memp_t
Definition: memp.h:69
u8_t * base
Definition: memp_priv.h:167
#define LWIP_ALIGNMENT_CAST(target_type, val)
Definition: arch.h:204
void memp_free(memp_t type, void *mem)
Definition: memp.c:488
#define SYS_ARCH_DECL_PROTECT(lev)
Definition: sys.h:420
u16_t size
Definition: memp_priv.h:160
void memp_init(void)
Definition: memp.c:290
u16_t num
Definition: memp_priv.h:164
#define MEMP_OVERFLOW_CHECK
Definition: opt.h:288
void memp_init_pool(const struct memp_desc *desc)
Definition: memp.c:249
#define MEMP_DEBUG
Definition: opt.h:2741
struct memp ** tab
Definition: memp_priv.h:170
#define LWIP_DEBUGF(debug, message)
#define LWIP_MEM_ALIGN(addr)
Definition: arch.h:248
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:327
void * memp_malloc_pool(const struct memp_desc *desc)
Definition: memp.c:378
Definition: mem.c:283
void * mem_malloc(mem_size_t size)
Definition: mem.c:622
void * memp_malloc(memp_t type)
Definition: memp.c:404