Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / lwip / repo / lwip / src / core / memp.c
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/netifapi.h"
67 #include "lwip/etharp.h"
68 #include "lwip/igmp.h"
69 #include "lwip/timeouts.h"
70 /* needed by default MEMP_NUM_SYS_TIMEOUT */
71 #include "netif/ppp/ppp_opts.h"
72 #include "lwip/netdb.h"
73 #include "lwip/dns.h"
74 #include "lwip/priv/nd6_priv.h"
75 #include "lwip/ip6_frag.h"
76 #include "lwip/mld6.h"
77
78 #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
79 #include "lwip/priv/memp_std.h"
80
81 const struct memp_desc* const memp_pools[MEMP_MAX] = {
82 #define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name,
83 #include "lwip/priv/memp_std.h"
84 };
85
86 #ifdef LWIP_HOOK_FILENAME
87 #include LWIP_HOOK_FILENAME
88 #endif
89
90 #if MEMP_MEM_MALLOC && MEMP_OVERFLOW_CHECK >= 2
91 #undef MEMP_OVERFLOW_CHECK
92 /* MEMP_OVERFLOW_CHECK >= 2 does not work with MEMP_MEM_MALLOC, use 1 instead */
93 #define MEMP_OVERFLOW_CHECK 1
94 #endif
95
96 /** This array holds the element sizes of each pool. */
97 #if !MEM_USE_POOLS && !MEMP_MEM_MALLOC && !LWIP_PBUF_FROM_CUSTOM_POOLS
98 static
99 #endif
100 const u16_t memp_sizes[MEMP_MAX] = {
101 #define LWIP_MEMPOOL(name,num,size,desc)  LWIP_MEM_ALIGN_SIZE(size),
102 #include "lwip/priv/memp_std.h"
103 };
104
105 #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
106
107 /** When using variable pbuf pools, pbuf_alloc will start scanning for pbufs at the
108  * highest indexed pbuf pool (PBUF_CUSTOM_POOL_IDX_START), which holds the smallest pbufs.
109  * It will look through the list of pbuf pools for a suitable pool to draw from that has a free
110  * pbuf until it passes MEMP_PBUF_POOL, which holds the largest pbufs.
111  **/
112 #if LWIP_PBUF_FROM_CUSTOM_POOLS
113 _Static_assert ((PBUF_CUSTOM_POOL_IDX_START > PBUF_CUSTOM_POOL_IDX_END), "PBUF_CUSTOM_POOL_IDX_START must be greater than PBUF_CUSTOM_POOL_IDX_END");
114 #define MEMP_IS_PBUF_POOL(type) (type <= PBUF_CUSTOM_POOL_IDX_START && type >= PBUF_CUSTOM_POOL_IDX_END)
115 #define MEMP_PBUF_POOL_HIGHWATERMARK(type) (PBUF_CUSTOM_POOL_IDX_START - type)
116 #else
117 #define MEMP_IS_PBUF_POOL(type) (type == MEMP_PBUF_POOL)
118 #define MEMP_PBUF_POOL_HIGHWATERMARK(type) (type)
119 #endif
120
121 #define sys_profile_interval_set_pbuf_highwatermark(...)
122
123 #if MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC
124 /**
125  * Check that memp-lists don't form a circle, using "Floyd's cycle-finding algorithm".
126  */
127 static int
128 memp_sanity(const struct memp_desc *desc)
129 {
130   struct memp *t, *h;
131
132   t = *desc->tab;
133   if (t != NULL) {
134     for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
135       h = ((h->next != NULL) ? h->next->next : NULL)) {
136       if (t == h) {
137         return 0;
138       }
139     }
140   }
141
142   return 1;
143 }
144 #endif /* MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC */
145
146 #if MEMP_OVERFLOW_CHECK
147 /**
148  * Check if a memp element was victim of an overflow
149  * (e.g. the restricted area after it has been altered)
150  *
151  * @param p the memp element to check
152  * @param desc the pool p comes from
153  */
154 static void
155 memp_overflow_check_element_overflow(struct memp *p, const struct memp_desc *desc)
156 {
157 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
158   u16_t k;
159   u8_t *m;
160   m = (u8_t*)p + MEMP_SIZE + desc->size;
161   for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
162     if (m[k] != 0xcd) {
163       char errstr[128] = "detected memp overflow in pool ";
164       strcat(errstr, desc->desc);
165       LWIP_ASSERT(errstr, 0);
166     }
167   }
168 #else /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
169   LWIP_UNUSED_ARG(p);
170   LWIP_UNUSED_ARG(desc);
171 #endif /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
172 }
173
174 /**
175  * Check if a memp element was victim of an underflow
176  * (e.g. the restricted area before it has been altered)
177  *
178  * @param p the memp element to check
179  * @param desc the pool p comes from
180  */
181 static void
182 memp_overflow_check_element_underflow(struct memp *p, const struct memp_desc *desc)
183 {
184 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
185   u16_t k;
186   u8_t *m;
187   m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
188   for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
189     if (m[k] != 0xcd) {
190       char errstr[128] = "detected memp underflow in pool ";
191       strcat(errstr, desc->desc);
192       LWIP_ASSERT(errstr, 0);
193     }
194   }
195 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
196   LWIP_UNUSED_ARG(p);
197   LWIP_UNUSED_ARG(desc);
198 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
199 }
200
201 /**
202  * Initialize the restricted area of on memp element.
203  */
204 static void
205 memp_overflow_init_element(struct memp *p, const struct memp_desc *desc)
206 {
207 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0
208   u8_t *m;
209 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
210   m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
211   memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
212 #endif
213 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
214   m = (u8_t*)p + MEMP_SIZE + desc->size;
215   memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
216 #endif
217 #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
218   LWIP_UNUSED_ARG(p);
219   LWIP_UNUSED_ARG(desc);
220 #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
221 }
222
223 #if MEMP_OVERFLOW_CHECK >= 2
224 /**
225  * Do an overflow check for all elements in every pool.
226  *
227  * @see memp_overflow_check_element for a description of the check
228  */
229 static void
230 memp_overflow_check_all(void)
231 {
232   u16_t i, j;
233   struct memp *p;
234   SYS_ARCH_DECL_PROTECT(old_level);
235   SYS_ARCH_PROTECT(old_level);
236
237   for (i = 0; i < MEMP_MAX; ++i) {
238     p = (struct memp*)LWIP_MEM_ALIGN(memp_pools[i]->base);
239     for (j = 0; j < memp_pools[i]->num; ++j) {
240       memp_overflow_check_element_overflow(p, memp_pools[i]);
241       memp_overflow_check_element_underflow(p, memp_pools[i]);
242       p = LWIP_ALIGNMENT_CAST(struct memp*, ((u8_t*)p + MEMP_SIZE + memp_pools[i]->size + MEMP_SANITY_REGION_AFTER_ALIGNED));
243     }
244   }
245   SYS_ARCH_UNPROTECT(old_level);
246 }
247 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
248 #endif /* MEMP_OVERFLOW_CHECK */
249
250 /**
251  * Initialize custom memory pool.
252  * Related functions: memp_malloc_pool, memp_free_pool
253  *
254  * @param desc pool to initialize
255  */
256 void
257 memp_init_pool(const struct memp_desc *desc)
258 {
259 #if MEMP_MEM_MALLOC
260   LWIP_UNUSED_ARG(desc);
261 #else
262   int i;
263   struct memp *memp;
264
265   *desc->tab = NULL;
266   memp = (struct memp*)LWIP_MEM_ALIGN(desc->base);
267   /* create a linked list of memp elements */
268   for (i = 0; i < desc->num; ++i) {
269     memp->next = *desc->tab;
270     *desc->tab = memp;
271 #if MEMP_OVERFLOW_CHECK
272     memp_overflow_init_element(memp, desc);
273 #endif /* MEMP_OVERFLOW_CHECK */
274    /* cast through void* to get rid of alignment warnings */
275    memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
276 #if MEMP_OVERFLOW_CHECK
277       + MEMP_SANITY_REGION_AFTER_ALIGNED
278 #endif
279     );
280   }
281 #if MEMP_STATS
282   desc->stats->avail = desc->num;
283 #endif /* MEMP_STATS */
284 #endif /* !MEMP_MEM_MALLOC */
285
286 #if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
287   desc->stats->name  = desc->desc;
288 #endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
289 }
290
291 /**
292  * Check if the pool is not empty
293  */
294 u8_t memp_is_not_empty (memp_t type)
295 {
296     return (memp_pools[type]->tab != NULL);
297 }
298
299 /**
300  * Initializes lwIP built-in pools.
301  * Related functions: memp_malloc, memp_free
302  *
303  * Carves out memp_memory into linked lists for each pool-type.
304  */
305 void
306 memp_init(void)
307 {
308   u16_t i;
309 #if LWIP_PBUF_FROM_CUSTOM_POOLS
310   u16_t j;
311 #endif // LWIP_PBUF_FROM_CUSTOM_POOLS
312
313   /* for every pool: */
314   for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
315     memp_init_pool(memp_pools[i]);
316
317 #if LWIP_STATS && MEMP_STATS
318     lwip_stats.memp[i] = memp_pools[i]->stats;
319 #endif
320   }
321
322 #if MEMP_OVERFLOW_CHECK >= 2
323   /* check everything a first time to see if it worked */
324   memp_overflow_check_all();
325 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
326
327 #if LWIP_PBUF_FROM_CUSTOM_POOLS
328   /* Verify that custom pools that contain PBUFs are laid out
329    * in decreasing order by size */
330   j = 0;
331   i = PBUF_CUSTOM_POOL_IDX_START;
332   while (1) {
333     LWIP_ASSERT("memp_init: PBUF pool size ordering incorrect",
334                  memp_sizes[i] > j);
335     j = memp_sizes[i];
336     if (i == PBUF_CUSTOM_POOL_IDX_END) {
337       break;
338     }
339     i--;
340   }
341 #endif
342
343 #if MEMP_OVERFLOW_CHECK >= 2
344   /* check everything a first time to see if it worked */
345   memp_overflow_check_all();
346 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
347 }
348
349 static void*
350 #if !MEMP_OVERFLOW_CHECK
351 do_memp_malloc_pool(const struct memp_desc *desc)
352 #else
353 do_memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
354 #endif
355 {
356   struct memp *memp;
357   SYS_ARCH_DECL_PROTECT(old_level);
358
359 #if MEMP_MEM_MALLOC
360   memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
361   SYS_ARCH_PROTECT(old_level);
362 #else /* MEMP_MEM_MALLOC */
363   SYS_ARCH_PROTECT(old_level);
364
365   memp = *desc->tab;
366 #endif /* MEMP_MEM_MALLOC */
367
368   if (memp != NULL) {
369 #if !MEMP_MEM_MALLOC
370 #if MEMP_OVERFLOW_CHECK == 1
371     memp_overflow_check_element_overflow(memp, desc);
372     memp_overflow_check_element_underflow(memp, desc);
373 #endif /* MEMP_OVERFLOW_CHECK */
374
375     *desc->tab = memp->next;
376 #if MEMP_OVERFLOW_CHECK
377     memp->next = NULL;
378 #endif /* MEMP_OVERFLOW_CHECK */
379 #endif /* !MEMP_MEM_MALLOC */
380 #if MEMP_OVERFLOW_CHECK
381     memp->file = file;
382     memp->line = line;
383 #if MEMP_MEM_MALLOC
384     memp_overflow_init_element(memp, desc);
385 #endif /* MEMP_MEM_MALLOC */
386 #endif /* MEMP_OVERFLOW_CHECK */
387     LWIP_ASSERT("memp_malloc: memp properly aligned",
388                 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
389 #if MEMP_STATS
390     desc->stats->used++;
391     if (desc->stats->used > desc->stats->max) {
392       desc->stats->max = desc->stats->used;
393     }
394 #endif
395
396     SYS_ARCH_UNPROTECT(old_level);
397     /* cast through u8_t* to get rid of alignment warnings */
398     return ((u8_t*)memp + MEMP_SIZE);
399   } else {
400     LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
401 #if MEMP_STATS
402     desc->stats->err++;
403 #endif
404   }
405
406   SYS_ARCH_UNPROTECT(old_level);
407   return NULL;
408 }
409
410 /**
411  * Get an element from a custom pool.
412  *
413  * @param desc the pool to get an element from
414  *
415  * @return a pointer to the allocated memory or a NULL pointer on error
416  */
417 void *
418 #if !MEMP_OVERFLOW_CHECK
419 memp_malloc_pool(const struct memp_desc *desc)
420 #else
421 memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
422 #endif
423 {
424   LWIP_ASSERT("invalid pool desc", desc != NULL);
425   if (desc == NULL) {
426     return NULL;
427   }
428
429 #if !MEMP_OVERFLOW_CHECK
430   return do_memp_malloc_pool(desc);
431 #else
432   return do_memp_malloc_pool_fn(desc, file, line);
433 #endif
434 }
435
436 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
437 #if LWIP_PBUF_FROM_CUSTOM_POOLS
438 int num_used_pool[PBUF_CUSTOM_POOL_IDX_START - PBUF_CUSTOM_POOL_IDX_END + 1] = {0};
439 #else
440 int num_used_pool = 0;
441 #endif
442
443 static int *get_num_used_pool_ptr(memp_t type)
444 {
445   if (MEMP_IS_PBUF_POOL(type)) {
446 #if LWIP_PBUF_FROM_CUSTOM_POOLS
447     return &num_used_pool[PBUF_CUSTOM_POOL_IDX_START - type]; 
448 #else
449     return &num_used_pool;
450 #endif
451   }
452   return NULL;
453 }
454 #endif /* (MEMP_DEBUG | LWIP_DBG_TRACE) */
455
456 /**
457  * Get an element from a specific pool.
458  *
459  * @param type the pool to get an element from
460  *
461  * @return a pointer to the allocated memory or a NULL pointer on error
462  */
463 void *
464 #if !MEMP_OVERFLOW_CHECK
465 memp_malloc(memp_t type)
466 #else
467 memp_malloc_fn(memp_t type, const char* file, const int line)
468 #endif
469 {
470   void *memp;
471 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
472   int *num_used_pool_ptr = get_num_used_pool_ptr(type);
473 #endif
474
475   LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
476
477 #if MEMP_OVERFLOW_CHECK >= 2
478   memp_overflow_check_all();
479 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
480
481 #if !MEMP_OVERFLOW_CHECK
482   memp = do_memp_malloc_pool(memp_pools[type]);
483 #else
484   memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
485 #endif
486
487 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
488   if (!memp) {
489     LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_TRACE, ("mm: out-of-mem in %s\n", memp_pools[type]->desc));
490     if (LWIP_PERF && MEMP_IS_PBUF_POOL(type)) {
491       sys_profile_interval_set_pbuf_highwatermark((u32_t)(*num_used_pool_ptr), MEMP_PBUF_POOL_HIGHWATERMARK(type));
492     }
493   }
494   else {
495     if (MEMP_IS_PBUF_POOL(type)) {
496       (*num_used_pool_ptr)++;
497
498       if (LWIP_PERF) {
499           LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_TRACE, ("mm:p++ = %d (%p) (%s)\n", *num_used_pool_ptr, memp, memp_pools[type]->desc));
500           sys_profile_interval_set_pbuf_highwatermark((u32_t)(*num_used_pool_ptr), MEMP_PBUF_POOL_HIGHWATERMARK(type));
501       }
502     }
503   }
504 #endif
505
506   return memp;
507 }
508
509 static void
510 do_memp_free_pool(const struct memp_desc* desc, void *mem)
511 {
512   struct memp *memp;
513   SYS_ARCH_DECL_PROTECT(old_level);
514
515   LWIP_ASSERT("memp_free: mem properly aligned",
516                 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
517
518   /* cast through void* to get rid of alignment warnings */
519   memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
520
521   SYS_ARCH_PROTECT(old_level);
522
523 #if MEMP_OVERFLOW_CHECK == 1
524   memp_overflow_check_element_overflow(memp, desc);
525   memp_overflow_check_element_underflow(memp, desc);
526 #endif /* MEMP_OVERFLOW_CHECK */
527
528 #if MEMP_STATS
529   desc->stats->used--;
530 #endif
531
532 #if MEMP_MEM_MALLOC
533   LWIP_UNUSED_ARG(desc);
534   SYS_ARCH_UNPROTECT(old_level);
535   mem_free(memp);
536 #else /* MEMP_MEM_MALLOC */
537   memp->next = *desc->tab;
538   *desc->tab = memp;
539
540 #if MEMP_SANITY_CHECK
541   LWIP_ASSERT("memp sanity", memp_sanity(desc));
542 #endif /* MEMP_SANITY_CHECK */
543
544   SYS_ARCH_UNPROTECT(old_level);
545 #endif /* !MEMP_MEM_MALLOC */
546 }
547
548 /**
549  * Put a custom pool element back into its pool.
550  *
551  * @param desc the pool where to put mem
552  * @param mem the memp element to free
553  */
554 void
555 memp_free_pool(const struct memp_desc* desc, void *mem)
556 {
557   LWIP_ASSERT("invalid pool desc", desc != NULL);
558   if ((desc == NULL) || (mem == NULL)) {
559     return;
560   }
561
562   do_memp_free_pool(desc, mem);
563 }
564
565 /**
566  * Put an element back into its pool.
567  *
568  * @param type the pool where to put mem
569  * @param mem the memp element to free
570  */
571 void
572 memp_free(memp_t type, void *mem)
573 {
574 #ifdef LWIP_HOOK_MEMP_AVAILABLE
575   struct memp *old_first;
576 #endif
577
578 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
579   int *num_used_pool_ptr = get_num_used_pool_ptr(type);
580 #endif
581
582   LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
583
584   if (mem == NULL) {
585     return;
586   }
587
588 #if MEMP_OVERFLOW_CHECK >= 2
589   memp_overflow_check_all();
590 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
591
592 #ifdef LWIP_HOOK_MEMP_AVAILABLE
593   old_first = *memp_pools[type]->tab;
594 #endif
595
596 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
597   if (MEMP_IS_PBUF_POOL(type)) {
598     (*num_used_pool_ptr)--;
599   }
600 #endif
601
602   do_memp_free_pool(memp_pools[type], mem);
603
604 #ifdef LWIP_HOOK_MEMP_AVAILABLE
605   if (old_first == NULL) {
606     LWIP_HOOK_MEMP_AVAILABLE(type);
607   }
608 #endif
609
610 #if (MEMP_DEBUG | LWIP_DBG_TRACE)
611   if (LWIP_PERF && MEMP_IS_PBUF_POOL(type)) {
612     LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_TRACE, ("mf:p-- = %d (%p) (%s)\n", *num_used_pool_ptr, mem, memp_pools[type]->desc));
613     sys_profile_interval_set_pbuf_highwatermark((u32_t)(*num_used_pool_ptr), MEMP_PBUF_POOL_HIGHWATERMARK(type));
614   }
615 #endif
616 }
617
618 #if MEMP_SEPARATE_POOLS
619 u16_t
620 memp_pbuf_index(memp_t type, const void *mem)
621 {
622   if (MEMP_IS_PBUF_POOL(type)) {
623     size_t buf_size = MEMP_SIZE + MEMP_ALIGN_SIZE(memp_sizes[type]);
624     return ((u8_t *)mem - (u8_t *)LWIP_MEM_ALIGN(memp_pools[type]->base)) / buf_size;
625   }
626   return ~0;
627 }
628
629 u16_t
630 memp_num_pbufs(memp_t type)
631 {
632   return memp_pools[type]->num;
633 }
634 #endif /* MEMP_SEPARATE_POOLS */
635
636 #endif /* MEMP_MEM_MALLOC */