STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
memp.c
Go to the documentation of this file.
1 
9 /*
10  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  *
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.
23  *
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
33  * OF SUCH DAMAGE.
34  *
35  * This file is part of the lwIP TCP/IP stack.
36  *
37  * Author: Adam Dunkels <adam@sics.se>
38  *
39  */
40 
41 #include "lwip/opt.h"
42 
43 #include "lwip/memp.h"
44 #include "lwip/pbuf.h"
45 #include "lwip/udp.h"
46 #include "lwip/raw.h"
47 #include "lwip/igmp.h"
48 #include "lwip/api.h"
49 #include "lwip/priv/api_msg.h"
50 #include "lwip/sockets.h"
51 #include "lwip/sys.h"
52 #include "lwip/lwip_timers.h"
53 #include "lwip/stats.h"
54 #include "netif/etharp.h"
55 #include "lwip/ip_frag.h"
56 #include "lwip/dns.h"
57 #include "lwip/netdb.h"
58 #include "netif/ppp/ppp.h"
59 #include "netif/ppp/pppos.h"
60 #include "netif/ppp/pppoe.h"
61 #include "netif/ppp/pppol2tp.h"
62 #include "lwip/nd6.h"
63 #include "lwip/ip6_frag.h"
64 #include "lwip/mld6.h"
65 #include "lwip/tcp.h"
66 #include "lwip/tcpip.h"
67 #include "lwip/priv/tcp_priv.h"
68 #include "lwip/priv/tcpip_priv.h"
69 
70 #include <string.h>
71 
72 #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
73 #include "lwip/priv/memp_std.h"
74 
75 const struct memp_desc *memp_pools[MEMP_MAX] = {
76 #define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name,
77 #include "lwip/priv/memp_std.h"
78 };
79 
80 #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
81 
82 #if MEMP_SANITY_CHECK
83 
86 static int
87 memp_sanity(const struct memp_desc *desc)
88 {
89  struct memp *t, *h;
90 
91  t = *desc->tab;
92  if (t != NULL) {
93  for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
94  h = ((h->next != NULL) ? h->next->next : NULL)) {
95  if (t == h) {
96  return 0;
97  }
98  }
99  }
100 
101  return 1;
102 }
103 #endif /* MEMP_SANITY_CHECK*/
104 
105 #if MEMP_OVERFLOW_CHECK
106 
114 static void
115 memp_overflow_check_element_overflow(struct memp *p, const struct memp_desc *desc)
116 {
117  u16_t k;
118  u8_t *m;
119 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
120  m = (u8_t*)p + MEMP_SIZE + desc->size;
121  for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
122  if (m[k] != 0xcd) {
123  char errstr[128] = "detected memp overflow in pool ";
124  strcat(errstr, desc->desc);
125  LWIP_ASSERT(errstr, 0);
126  }
127  }
128 #endif
129 }
130 
138 static void
139 memp_overflow_check_element_underflow(struct memp *p, const struct memp_desc *desc)
140 {
141  u16_t k;
142  u8_t *m;
143 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
144  m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
145  for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
146  if (m[k] != 0xcd) {
147  char errstr[128] = "detected memp underflow in pool ";
148  strcat(errstr, desc->desc);
149  LWIP_ASSERT(errstr, 0);
150  }
151  }
152 #endif
153 }
154 
160 static void
161 memp_overflow_check_all(void)
162 {
163  u16_t i, j;
164  struct memp *p;
165 
166  for (i = 0; i < MEMP_MAX; ++i) {
167  p = (struct memp *)(size_t)(memp_pools[i]->base);
168  for (j = 0; j < memp_pools[i]->num; ++j) {
169  memp_overflow_check_element_overflow(p, memp_pools[i]);
170  memp_overflow_check_element_underflow(p, memp_pools[i]);
171  p = (struct memp*)(size_t)((u8_t*)p + MEMP_SIZE + memp_pools[i]->size + MEMP_SANITY_REGION_AFTER_ALIGNED);
172  }
173  }
174 }
175 
179 static void
180 memp_overflow_init(const struct memp_desc *desc)
181 {
182  u16_t i;
183  struct memp *p;
184  u8_t *m;
185 
186  p = (struct memp *)(size_t)(desc->base);
187  for (i = 0; i < desc->num; ++i) {
188 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
189  m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
190  memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
191 #endif
192 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
193  m = (u8_t*)p + MEMP_SIZE + desc->size;
194  memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
195 #endif
196  p = (struct memp*)(size_t)((u8_t*)p + MEMP_SIZE + desc->size + MEMP_SANITY_REGION_AFTER_ALIGNED);
197  }
198 }
199 #endif /* MEMP_OVERFLOW_CHECK */
200 
201 
202 void
203 memp_init_pool(const struct memp_desc *desc)
204 {
205  int i;
206  struct memp *memp;
207 
208  *desc->tab = NULL;
209  memp = (struct memp*)LWIP_MEM_ALIGN(desc->base);
210  /* create a linked list of memp elements */
211  for (i = 0; i < desc->num; ++i) {
212  memp->next = *desc->tab;
213  *desc->tab = memp;
214  memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
216  + MEMP_SANITY_REGION_AFTER_ALIGNED
217 #endif
218  );
219  }
220 
221 #if MEMP_OVERFLOW_CHECK
222  memp_overflow_init(desc);
223 #endif /* MEMP_OVERFLOW_CHECK */
224 }
225 
231 void
233 {
234  u16_t i;
235 
236  for (i = 0; i < MEMP_MAX; ++i) {
237  MEMP_STATS_AVAIL(used, i, 0);
238  MEMP_STATS_AVAIL(max, i, 0);
239  MEMP_STATS_AVAIL(err, i, 0);
240  MEMP_STATS_AVAIL(avail, i, memp_pools[i]->num);
241  }
242 
243  /* for every pool: */
244  for (i = 0; i < MEMP_MAX; ++i) {
245  memp_init_pool(memp_pools[i]);
246  }
247 
248 #if MEMP_OVERFLOW_CHECK
249  /* check everything a first time to see if it worked */
250  memp_overflow_check_all();
251 #endif /* MEMP_OVERFLOW_CHECK */
252 }
253 
254 void *
255 #if !MEMP_OVERFLOW_CHECK
256 memp_malloc_pool(const struct memp_desc *desc)
257 #else
258 memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
259 #endif
260 {
261  struct memp *memp;
262  SYS_ARCH_DECL_PROTECT(old_level);
263 
264  SYS_ARCH_PROTECT(old_level);
265 
266  memp = *desc->tab;
267 
268 #if MEMP_OVERFLOW_CHECK == 1
269  memp_overflow_check_element_overflow(memp, desc);
270  memp_overflow_check_element_underflow(memp, desc);
271 #endif /* MEMP_OVERFLOW_CHECK */
272 
273  if (memp != NULL) {
274  *desc->tab = memp->next;
275 #if MEMP_OVERFLOW_CHECK
276  memp->next = NULL;
277  memp->file = file;
278  memp->line = line;
279 #endif /* MEMP_OVERFLOW_CHECK */
280  LWIP_ASSERT("memp_malloc: memp properly aligned",
281  ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
282  memp = (struct memp*)(void *)((u8_t*)memp + MEMP_SIZE);
283  }
284 
285  SYS_ARCH_UNPROTECT(old_level);
286 
287  return memp;
288 }
289 
301 void *
302 #if !MEMP_OVERFLOW_CHECK
304 #else
305 memp_malloc_fn(memp_t type, const char* file, const int line)
306 #endif
307 {
308  void *memp;
309  SYS_ARCH_DECL_PROTECT(old_level);
310 
311  LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
312 
313  SYS_ARCH_PROTECT(old_level);
314 
315 #if MEMP_OVERFLOW_CHECK >= 2
316  memp_overflow_check_all();
317 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
318 
319 #if !MEMP_OVERFLOW_CHECK
320  memp = memp_malloc_pool(memp_pools[type]);
321 #else
322  memp = memp_malloc_pool_fn(memp_pools[type], file, line);
323 #endif
324 
325  if (memp != NULL) {
326  MEMP_STATS_INC(used, type);
327  if(MEMP_STATS_GET(used, type) > MEMP_STATS_GET(max, type)) {
328  MEMP_STATS_AVAIL(max, type, MEMP_STATS_GET(used, type));
329  }
330  } else {
331  LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_pools[type]->desc));
332  MEMP_STATS_INC(err, type);
333  }
334 
335  SYS_ARCH_UNPROTECT(old_level);
336 
337  return memp;
338 }
339 
340 static void
341 #ifdef LWIP_HOOK_MEMP_AVAILABLE
342 do_memp_free_pool(const struct memp_desc* desc, void *mem, struct memp **old_first)
343 #else
344 do_memp_free_pool(const struct memp_desc* desc, void *mem)
345 #endif
346 {
347  struct memp *memp;
348  SYS_ARCH_DECL_PROTECT(old_level);
349 
350  LWIP_ASSERT("memp_free: mem properly aligned",
351  ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
352 
353  memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
354 
355  SYS_ARCH_PROTECT(old_level);
356 
357 #if MEMP_OVERFLOW_CHECK == 1
358  memp_overflow_check_element_overflow(memp, desc);
359  memp_overflow_check_element_underflow(memp, desc);
360 #endif /* MEMP_OVERFLOW_CHECK */
361 
362  memp->next = *desc->tab;
363 
364 #ifdef LWIP_HOOK_MEMP_AVAILABLE
365  if (old_first)
366  *old_first = *desc->tab;
367 #endif
368 
369  *desc->tab = memp;
370 
371 #if MEMP_SANITY_CHECK
372  LWIP_ASSERT("memp sanity", memp_sanity(desc));
373 #endif /* MEMP_SANITY_CHECK */
374 
375  SYS_ARCH_UNPROTECT(old_level);
376 }
377 
378 void
379 memp_free_pool(const struct memp_desc* desc, void *mem)
380 {
381  if ((desc == NULL) || (mem == NULL)) {
382  return;
383  }
384 
385 #ifdef LWIP_HOOK_MEMP_AVAILABLE
386  do_memp_free_pool(desc, mem, NULL);
387 #else
388  do_memp_free_pool(desc, mem);
389 #endif
390 }
391 
398 void
399 memp_free(memp_t type, void *mem)
400 {
401 #ifdef LWIP_HOOK_MEMP_AVAILABLE
402  struct memp *old_first;
403 #endif
404  SYS_ARCH_DECL_PROTECT(old_level);
405 
406  LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
407 
408  SYS_ARCH_PROTECT(old_level);
409 
410 #if MEMP_OVERFLOW_CHECK >= 2
411  memp_overflow_check_all();
412 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
413 
414  MEMP_STATS_DEC(used, type);
415 
416 #ifdef LWIP_HOOK_MEMP_AVAILABLE
417  do_memp_free_pool(memp_pools[type], mem, &old_first);
418 #else
419  do_memp_free_pool(memp_pools[type], mem);
420 #endif
421 
422  SYS_ARCH_UNPROTECT(old_level);
423 
424 #ifdef LWIP_HOOK_MEMP_AVAILABLE
425  if (old_first == NULL) {
426  LWIP_HOOK_MEMP_AVAILABLE(type);
427  }
428 #endif
429 }
430 
431 #endif /* MEMP_MEM_MALLOC */
432 
Definition: memp.h:48
#define MEMP_STATS_AVAIL(x, i, y)
Definition: stats.h:344
u8_t * base
Definition: memp_priv.h:136
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:47
void memp_free_pool(const struct memp_desc *desc, void *mem)
Definition: memp.c:379
memp_t
Definition: memp.h:45
#define MEMP_STATS_GET(x, i)
Definition: stats.h:349
#define MEMP_DEBUG
Definition: opt.h:2865
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
u32_t mem_ptr_t
Definition: cc.h:44
#define MEM_ALIGNMENT
Definition: lwipopts.h:73
#define SYS_ARCH_DECL_PROTECT(lev)
Definition: sys.h:304
#define NULL
Definition: usbd_def.h:53
void memp_init(void)
Definition: memp.c:232
const struct memp_desc * memp_pools[MEMP_MAX]
Definition: memp.c:75
#define MEMP_OVERFLOW_CHECK
Definition: opt.h:155
struct memp ** tab
Definition: memp_priv.h:139
void memp_init_pool(const struct memp_desc *desc)
Definition: memp.c:203
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
u16_t num
Definition: memp_priv.h:128
Definition: memp_priv.h:85
struct memp * next
Definition: memp_priv.h:86
#define MEMP_SIZE
Definition: memp_priv.h:80
u16_t size
Definition: memp_priv.h:124
unsigned char u8_t
Definition: cc.h:38
#define SYS_ARCH_PROTECT(lev)
Definition: sys.h:305
void * memp_malloc_pool(const struct memp_desc *desc)
Definition: memp.c:256
#define LWIP_MEM_ALIGN(addr)
Definition: mem.h:116
#define SYS_ARCH_UNPROTECT(lev)
Definition: sys.h:306
Definition: mem.c:179
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:89
#define MEMP_STATS_DEC(x, i)
Definition: stats.h:346
#define MEMP_STATS_INC(x, i)
Definition: stats.h:345
void * memp_malloc(memp_t type)
Definition: memp.c:303
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
unsigned short u16_t
Definition: cc.h:40