90 #include "netif/ppp/ppp_opts.h" 97 #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc) 100 const struct memp_desc*
const memp_pools[MEMP_MAX] = {
101 #define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name, 105 #ifdef LWIP_HOOK_FILENAME 106 #include LWIP_HOOK_FILENAME 109 #if MEMP_MEM_MALLOC && MEMP_OVERFLOW_CHECK >= 2 110 #undef MEMP_OVERFLOW_CHECK 112 #define MEMP_OVERFLOW_CHECK 1 115 #if MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC 120 memp_sanity(
const struct memp_desc *desc)
126 for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
127 h = ((h->next != NULL) ? h->next->next : NULL)) {
138 #if MEMP_OVERFLOW_CHECK 147 memp_overflow_check_element_overflow(
struct memp *p,
const struct memp_desc *desc)
149 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0 152 m = (u8_t*)p + MEMP_SIZE + desc->
size;
153 for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
155 char errstr[128] =
"detected memp overflow in pool ";
156 strcat(errstr, desc->desc);
157 LWIP_ASSERT(errstr, 0);
174 memp_overflow_check_element_underflow(
struct memp *p,
const struct memp_desc *desc)
176 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 179 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
180 for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
182 char errstr[128] =
"detected memp underflow in pool ";
183 strcat(errstr, desc->desc);
184 LWIP_ASSERT(errstr, 0);
197 memp_overflow_init_element(
struct memp *p,
const struct memp_desc *desc)
199 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 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);
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);
215 #if MEMP_OVERFLOW_CHECK >= 2 222 memp_overflow_check_all(
void)
227 SYS_ARCH_PROTECT(old_level);
229 for (i = 0; i < MEMP_MAX; ++i) {
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));
237 SYS_ARCH_UNPROTECT(old_level);
260 for (i = 0; i < desc->
num; ++i) {
261 memp->next = *desc->
tab;
263 #if MEMP_OVERFLOW_CHECK 264 memp_overflow_init_element(memp, desc);
267 memp = (
struct memp *)(
void *)((u8_t *)memp + MEMP_SIZE + desc->
size 269 + MEMP_SANITY_REGION_AFTER_ALIGNED
274 desc->stats->avail = desc->
num;
278 #if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) 279 desc->stats->name = desc->desc;
295 for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
298 #if LWIP_STATS && MEMP_STATS 299 lwip_stats.memp[i] = memp_pools[i]->stats;
303 #if MEMP_OVERFLOW_CHECK >= 2 305 memp_overflow_check_all();
310 #if !MEMP_OVERFLOW_CHECK 311 do_memp_malloc_pool(
const struct memp_desc *desc)
313 do_memp_malloc_pool_fn(
const struct memp_desc *desc,
const char* file,
const int line)
320 memp = (
struct memp *)
mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->
size));
321 SYS_ARCH_PROTECT(old_level);
323 SYS_ARCH_PROTECT(old_level);
330 #if MEMP_OVERFLOW_CHECK == 1 331 memp_overflow_check_element_overflow(memp, desc);
332 memp_overflow_check_element_underflow(memp, desc);
335 *desc->
tab = memp->next;
336 #if MEMP_OVERFLOW_CHECK 340 #if MEMP_OVERFLOW_CHECK 344 memp_overflow_init_element(memp, desc);
347 LWIP_ASSERT(
"memp_malloc: memp properly aligned",
348 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
351 if (desc->stats->used > desc->stats->max) {
352 desc->stats->max = desc->stats->used;
355 SYS_ARCH_UNPROTECT(old_level);
357 return ((u8_t*)memp + MEMP_SIZE);
365 SYS_ARCH_UNPROTECT(old_level);
377 #if !MEMP_OVERFLOW_CHECK 380 memp_malloc_pool_fn(
const struct memp_desc *desc,
const char* file,
const int line)
383 LWIP_ASSERT(
"invalid pool desc", desc != NULL);
388 #if !MEMP_OVERFLOW_CHECK 389 return do_memp_malloc_pool(desc);
391 return do_memp_malloc_pool_fn(desc, file, line);
403 #if !MEMP_OVERFLOW_CHECK 406 memp_malloc_fn(
memp_t type,
const char* file,
const int line)
410 LWIP_ERROR(
"memp_malloc: type < MEMP_MAX", (type < MEMP_MAX),
return NULL;);
412 #if MEMP_OVERFLOW_CHECK >= 2 413 memp_overflow_check_all();
416 #if !MEMP_OVERFLOW_CHECK 417 memp = do_memp_malloc_pool(memp_pools[type]);
419 memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
426 do_memp_free_pool(
const struct memp_desc* desc,
void *
mem)
431 LWIP_ASSERT(
"memp_free: mem properly aligned",
432 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
435 memp = (
struct memp *)(
void *)((u8_t*)mem - MEMP_SIZE);
437 SYS_ARCH_PROTECT(old_level);
439 #if MEMP_OVERFLOW_CHECK == 1 440 memp_overflow_check_element_overflow(memp, desc);
441 memp_overflow_check_element_underflow(memp, desc);
450 SYS_ARCH_UNPROTECT(old_level);
453 memp->next = *desc->
tab;
456 #if MEMP_SANITY_CHECK 457 LWIP_ASSERT(
"memp sanity", memp_sanity(desc));
460 SYS_ARCH_UNPROTECT(old_level);
473 LWIP_ASSERT(
"invalid pool desc", desc != NULL);
474 if ((desc == NULL) || (mem == NULL)) {
478 do_memp_free_pool(desc, mem);
490 #ifdef LWIP_HOOK_MEMP_AVAILABLE 491 struct memp *old_first;
494 LWIP_ERROR(
"memp_free: type < MEMP_MAX", (type < MEMP_MAX),
return;);
500 #if MEMP_OVERFLOW_CHECK >= 2 501 memp_overflow_check_all();
504 #ifdef LWIP_HOOK_MEMP_AVAILABLE 505 old_first = *memp_pools[type]->
tab;
508 do_memp_free_pool(memp_pools[type], mem);
510 #ifdef LWIP_HOOK_MEMP_AVAILABLE 511 if (old_first == NULL) {
512 LWIP_HOOK_MEMP_AVAILABLE(type);
void memp_free_pool(const struct memp_desc *desc, void *mem)
void mem_free(void *rmem)
#define LWIP_ALIGNMENT_CAST(target_type, val)
void memp_free(memp_t type, void *mem)
#define LWIP_DBG_LEVEL_SERIOUS
#define SYS_ARCH_DECL_PROTECT(lev)
#define MEMP_OVERFLOW_CHECK
void memp_init_pool(const struct memp_desc *desc)
#define LWIP_DEBUGF(debug, message)
#define LWIP_MEM_ALIGN(addr)
#define LWIP_UNUSED_ARG(x)
void * memp_malloc_pool(const struct memp_desc *desc)
void * mem_malloc(mem_size_t size)
void * memp_malloc(memp_t type)