Prusa MINI Firmware overview
memp.c File Reference
#include "lwip/opt.h"
#include "lwip/memp.h"
#include "lwip/sys.h"
#include "lwip/stats.h"
#include <string.h>
#include "lwip/pbuf.h"
#include "lwip/raw.h"
#include "lwip/udp.h"
#include "lwip/tcp.h"
#include "lwip/priv/tcp_priv.h"
#include "lwip/ip4_frag.h"
#include "lwip/netbuf.h"
#include "lwip/api.h"
#include "lwip/priv/tcpip_priv.h"
#include "lwip/priv/api_msg.h"
#include "lwip/sockets.h"
#include "lwip/netifapi.h"
#include "lwip/etharp.h"
#include "lwip/igmp.h"
#include "lwip/timeouts.h"
#include "netif/ppp/ppp_opts.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "lwip/priv/nd6_priv.h"
#include "lwip/ip6_frag.h"
#include "lwip/mld6.h"
#include "lwip/priv/memp_std.h"

Macros

#define LWIP_MEMPOOL(name, num, size, desc)   LWIP_MEMPOOL_DECLARE(name,num,size,desc)
 
#define LWIP_MEMPOOL(name, num, size, desc)   &memp_ ## name,
 

Functions

void memp_init_pool (const struct memp_desc *desc)
 
void memp_init (void)
 
static voiddo_memp_malloc_pool (const struct memp_desc *desc)
 
voidmemp_malloc_pool (const struct memp_desc *desc)
 
voidmemp_malloc (memp_t type)
 
static void do_memp_free_pool (const struct memp_desc *desc, void *mem)
 
void memp_free_pool (const struct memp_desc *desc, void *mem)
 
void memp_free (memp_t type, void *mem)
 

Variables

const struct memp_desc *const memp_pools [MEMP_MAX]
 

Detailed Description

Dynamic pool memory manager

lwIP has dedicated pools for many structures (netconn, protocol control blocks, packet buffers, ...). All these pools are managed here.

Macro Definition Documentation

◆ LWIP_MEMPOOL [1/2]

#define LWIP_MEMPOOL (   name,
  num,
  size,
  desc 
)    LWIP_MEMPOOL_DECLARE(name,num,size,desc)

◆ LWIP_MEMPOOL [2/2]

#define LWIP_MEMPOOL (   name,
  num,
  size,
  desc 
)    &memp_ ## name,

Function Documentation

◆ memp_init_pool()

void memp_init_pool ( const struct memp_desc desc)

Initialize custom memory pool. Related functions: memp_malloc_pool, memp_free_pool

Parameters
descpool to initialize
231 {
232 #if MEMP_MEM_MALLOC
233  LWIP_UNUSED_ARG(desc);
234 #else
235  int i;
236  struct memp *memp;
237 
238  *desc->tab = NULL;
239  memp = (struct memp*)LWIP_MEM_ALIGN(desc->base);
240  /* create a linked list of memp elements */
241  for (i = 0; i < desc->num; ++i) {
242  memp->next = *desc->tab;
243  *desc->tab = memp;
244 #if MEMP_OVERFLOW_CHECK
245  memp_overflow_init_element(memp, desc);
246 #endif /* MEMP_OVERFLOW_CHECK */
247  /* cast through void* to get rid of alignment warnings */
248  memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
250  + MEMP_SANITY_REGION_AFTER_ALIGNED
251 #endif
252  );
253  }
254 #if MEMP_STATS
255  desc->stats->avail = desc->num;
256 #endif /* MEMP_STATS */
257 #endif /* !MEMP_MEM_MALLOC */
258 
259 #if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
260  desc->stats->name = desc->desc;
261 #endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
262 }
Here is the caller graph for this function:

◆ memp_init()

void memp_init ( void  )

Initializes lwIP built-in pools. Related functions: memp_malloc, memp_free

Carves out memp_memory into linked lists for each pool-type.

272 {
273  u16_t i;
274 
275  /* for every pool: */
276  for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
278 
279 #if LWIP_STATS && MEMP_STATS
280  lwip_stats.memp[i] = memp_pools[i]->stats;
281 #endif
282  }
283 
284 #if MEMP_OVERFLOW_CHECK >= 2
285  /* check everything a first time to see if it worked */
286  memp_overflow_check_all();
287 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
288 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_memp_malloc_pool()

static void* do_memp_malloc_pool ( const struct memp_desc desc)
static
296 {
297  struct memp *memp;
298  SYS_ARCH_DECL_PROTECT(old_level);
299 
300 #if MEMP_MEM_MALLOC
301  memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
302  SYS_ARCH_PROTECT(old_level);
303 #else /* MEMP_MEM_MALLOC */
304  SYS_ARCH_PROTECT(old_level);
305 
306  memp = *desc->tab;
307 #endif /* MEMP_MEM_MALLOC */
308 
309  if (memp != NULL) {
310 #if !MEMP_MEM_MALLOC
311 #if MEMP_OVERFLOW_CHECK == 1
312  memp_overflow_check_element_overflow(memp, desc);
313  memp_overflow_check_element_underflow(memp, desc);
314 #endif /* MEMP_OVERFLOW_CHECK */
315 
316  *desc->tab = memp->next;
317 #if MEMP_OVERFLOW_CHECK
318  memp->next = NULL;
319 #endif /* MEMP_OVERFLOW_CHECK */
320 #endif /* !MEMP_MEM_MALLOC */
321 #if MEMP_OVERFLOW_CHECK
322  memp->file = file;
323  memp->line = line;
324 #if MEMP_MEM_MALLOC
325  memp_overflow_init_element(memp, desc);
326 #endif /* MEMP_MEM_MALLOC */
327 #endif /* MEMP_OVERFLOW_CHECK */
328  LWIP_ASSERT("memp_malloc: memp properly aligned",
329  ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
330 #if MEMP_STATS
331  desc->stats->used++;
332  if (desc->stats->used > desc->stats->max) {
333  desc->stats->max = desc->stats->used;
334  }
335 #endif
336  SYS_ARCH_UNPROTECT(old_level);
337  /* cast through u8_t* to get rid of alignment warnings */
338  return ((u8_t*)memp + MEMP_SIZE);
339  } else {
340  LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
341 #if MEMP_STATS
342  desc->stats->err++;
343 #endif
344  }
345 
346  SYS_ARCH_UNPROTECT(old_level);
347  return NULL;
348 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ memp_malloc_pool()

void* memp_malloc_pool ( const struct memp_desc desc)

Get an element from a custom pool.

Parameters
descthe pool to get an element from
Returns
a pointer to the allocated memory or a NULL pointer on error
363 {
364  LWIP_ASSERT("invalid pool desc", desc != NULL);
365  if (desc == NULL) {
366  return NULL;
367  }
368 
369 #if !MEMP_OVERFLOW_CHECK
370  return do_memp_malloc_pool(desc);
371 #else
372  return do_memp_malloc_pool_fn(desc, file, line);
373 #endif
374 }
Here is the call graph for this function:

◆ memp_malloc()

void* memp_malloc ( memp_t  type)

Get an element from a specific pool.

Parameters
typethe pool to get an element from
Returns
a pointer to the allocated memory or a NULL pointer on error
389 {
390  void *memp;
391  LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
392 
393 #if MEMP_OVERFLOW_CHECK >= 2
394  memp_overflow_check_all();
395 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
396 
397 #if !MEMP_OVERFLOW_CHECK
399 #else
400  memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
401 #endif
402 
403  return memp;
404 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_memp_free_pool()

static void do_memp_free_pool ( const struct memp_desc desc,
void mem 
)
static
408 {
409  struct memp *memp;
410  SYS_ARCH_DECL_PROTECT(old_level);
411 
412  LWIP_ASSERT("memp_free: mem properly aligned",
413  ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
414 
415  /* cast through void* to get rid of alignment warnings */
416  memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
417 
418  SYS_ARCH_PROTECT(old_level);
419 
420 #if MEMP_OVERFLOW_CHECK == 1
421  memp_overflow_check_element_overflow(memp, desc);
422  memp_overflow_check_element_underflow(memp, desc);
423 #endif /* MEMP_OVERFLOW_CHECK */
424 
425 #if MEMP_STATS
426  desc->stats->used--;
427 #endif
428 
429 #if MEMP_MEM_MALLOC
430  LWIP_UNUSED_ARG(desc);
431  SYS_ARCH_UNPROTECT(old_level);
432  mem_free(memp);
433 #else /* MEMP_MEM_MALLOC */
434  memp->next = *desc->tab;
435  *desc->tab = memp;
436 
437 #if MEMP_SANITY_CHECK
438  LWIP_ASSERT("memp sanity", memp_sanity(desc));
439 #endif /* MEMP_SANITY_CHECK */
440 
441  SYS_ARCH_UNPROTECT(old_level);
442 #endif /* !MEMP_MEM_MALLOC */
443 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ memp_free_pool()

void memp_free_pool ( const struct memp_desc desc,
void mem 
)

Put a custom pool element back into its pool.

Parameters
descthe pool where to put mem
memthe memp element to free
453 {
454  LWIP_ASSERT("invalid pool desc", desc != NULL);
455  if ((desc == NULL) || (mem == NULL)) {
456  return;
457  }
458 
459  do_memp_free_pool(desc, mem);
460 }
Here is the call graph for this function:

◆ memp_free()

void memp_free ( memp_t  type,
void mem 
)

Put an element back into its pool.

Parameters
typethe pool where to put mem
memthe memp element to free
470 {
471 #ifdef LWIP_HOOK_MEMP_AVAILABLE
472  struct memp *old_first;
473 #endif
474 
475  LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
476 
477  if (mem == NULL) {
478  return;
479  }
480 
481 #if MEMP_OVERFLOW_CHECK >= 2
482  memp_overflow_check_all();
483 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
484 
485 #ifdef LWIP_HOOK_MEMP_AVAILABLE
486  old_first = *memp_pools[type]->tab;
487 #endif
488 
490 
491 #ifdef LWIP_HOOK_MEMP_AVAILABLE
492  if (old_first == NULL) {
493  LWIP_HOOK_MEMP_AVAILABLE(type);
494  }
495 #endif
496 }
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ memp_pools

const struct memp_desc* const memp_pools[MEMP_MAX]
Initial value:
= {
#define LWIP_MEMPOOL(name,num,size,desc)
}
SYS_ARCH_UNPROTECT
#define SYS_ARCH_UNPROTECT(lev)
Definition: sys.h:403
do_memp_malloc_pool
static void * do_memp_malloc_pool(const struct memp_desc *desc)
Definition: memp.c:292
LWIP_ASSERT
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
mem
Definition: mem.c:264
memp_init_pool
void memp_init_pool(const struct memp_desc *desc)
Definition: memp.c:230
u16_t
uint16_t u16_t
Definition: arch.h:121
LWIP_ARRAYSIZE
#define LWIP_ARRAYSIZE(x)
Definition: def.h:58
MEMP_MAX
Definition: memp.h:55
type
uint8_t type
Definition: UsbCore.h:184
memp_desc::base
u8_t * base
Definition: memp_priv.h:148
i
uint8_t i
Definition: screen_test_graph.c:72
SYS_ARCH_PROTECT
#define SYS_ARCH_PROTECT(lev)
Definition: sys.h:402
MEMP_ALIGN_SIZE
#define MEMP_ALIGN_SIZE(x)
Definition: memp_priv.h:86
MEMP_SIZE
#define MEMP_SIZE
Definition: memp_priv.h:85
MEM_ALIGNMENT
#define MEM_ALIGNMENT
Definition: opt.h:248
NULL
#define NULL
Definition: usbd_def.h:53
memp::next
struct memp * next
Definition: memp_priv.h:92
LWIP_DBG_LEVEL_SERIOUS
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:57
LWIP_ERROR
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:135
MEMP_DEBUG
#define MEMP_DEBUG
Definition: opt.h:2722
u8_t
uint8_t u8_t
Definition: arch.h:119
memp_desc::num
u16_t num
Definition: memp_priv.h:145
MEMP_OVERFLOW_CHECK
#define MEMP_OVERFLOW_CHECK
Definition: opt.h:269
memp_desc::size
u16_t size
Definition: memp_priv.h:141
LWIP_UNUSED_ARG
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:308
do_memp_free_pool
static void do_memp_free_pool(const struct memp_desc *desc, void *mem)
Definition: memp.c:407
LWIP_MEM_ALIGN
#define LWIP_MEM_ALIGN(addr)
Definition: arch.h:229
mem_ptr_t
uintptr_t mem_ptr_t
Definition: arch.h:125
SYS_ARCH_DECL_PROTECT
#define SYS_ARCH_DECL_PROTECT(lev)
Definition: sys.h:401
mem_malloc
void * mem_malloc(mem_size_t size)
Definition: mem.c:603
memp_desc::tab
struct memp ** tab
Definition: memp_priv.h:151
memp
Definition: memp_priv.h:91
mem_free
void mem_free(void *rmem)
Definition: mem.c:419
LWIP_DEBUGF
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:164
memp_pools
const struct memp_desc *const memp_pools[MEMP_MAX]
Definition: memp.c:81