MeterLogger
Data Structures | Macros | Functions | Variables
memp.c File Reference
#include "lwip/opt.h"
#include "lwip/memp.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#include "lwip/raw.h"
#include "lwip/tcp_impl.h"
#include "lwip/igmp.h"
#include "lwip/api.h"
#include "lwip/api_msg.h"
#include "lwip/tcpip.h"
#include "lwip/sys.h"
#include "lwip/timers.h"
#include "lwip/stats.h"
#include "netif/etharp.h"
#include "lwip/ip_frag.h"
#include "lwip/snmp_structs.h"
#include "lwip/snmp_msg.h"
#include "lwip/dns.h"
#include "netif/ppp_oe.h"
#include <string.h>
#include "lwip/memp_std.h"
Include dependency graph for memp.c:

Go to the source code of this file.

Data Structures

struct  memp
 

Macros

#define MEMP_SIZE   0
 
#define MEMP_ALIGN_SIZE(x)   (LWIP_MEM_ALIGN_SIZE(x))
 
#define LWIP_MEMPOOL(name, num, size, desc, attr)   LWIP_MEM_ALIGN_SIZE(size),
 
#define LWIP_MEMPOOL(name, num, size, desc, attr)   (num),
 
#define LWIP_MEMPOOL(name, num, size, desc, attr)   (desc),
 
#define LWIP_MEMPOOL(name, num, size, desc, attr)   + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
 

Functions

void memp_init (void)
 
void * memp_malloc (memp_t type)
 
void memp_free (memp_t type, void *mem)
 

Variables

static struct mempmemp_tab [MEMP_MAX]
 
static const u32_t memp_sizes [MEMP_MAXICACHE_RODATA_ATTR
 
u16_t memp_sizes_test [1] = {PBUF_POOL_BUFSIZE,}
 
static const u16_t memp_num [MEMP_MAX]
 
const char * memp_desc [MEMP_MAX]
 
static u8_t memp_memory [MEM_ALIGNMENT - 1 #define LWIP_MEMPOOL(name, num, size, desc, attr)]
 

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.

Definition in file memp.c.

Macro Definition Documentation

◆ LWIP_MEMPOOL [1/4]

#define LWIP_MEMPOOL (   name,
  num,
  size,
  desc,
  attr 
)    LWIP_MEM_ALIGN_SIZE(size),

◆ LWIP_MEMPOOL [2/4]

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

◆ LWIP_MEMPOOL [3/4]

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

◆ LWIP_MEMPOOL [4/4]

#define LWIP_MEMPOOL (   name,
  num,
  size,
  desc,
  attr 
)    + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )

◆ MEMP_ALIGN_SIZE

#define MEMP_ALIGN_SIZE (   x)    (LWIP_MEM_ALIGN_SIZE(x))

Definition at line 111 of file memp.c.

◆ MEMP_SIZE

#define MEMP_SIZE   0

Definition at line 110 of file memp.c.

Referenced by memp_free(), memp_init(), and memp_malloc().

Function Documentation

◆ 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

Definition at line 438 of file memp.c.

References ETH_PAD_SIZE, LWIP_ASSERT, MEM_ALIGNMENT, memp_desc, memp_memory_PBUF_POOL_base, MEMP_SIZE, memp_sizes_test, MEMP_STATS_DEC, memp::next, NULL, PBUF_LINK_HLEN, PBUF_POOL_BUFSIZE, printf, SYS_ARCH_DECL_PROTECT, SYS_ARCH_PROTECT, SYS_ARCH_UNPROTECT, and TCP_MSS.

Referenced by espconn_kill_oldest(), espconn_kill_pcb(), espconn_tcp_client(), espconn_tcp_disconnect_successful(), espconn_tcp_server(), pbuf_free(), tcpip_callback_with_block(), tcpip_input(), and tcpip_thread().

439 {
440  struct memp *memp;
441  SYS_ARCH_DECL_PROTECT(old_level);
442 
443  if (mem == NULL) {
444  return;
445  }
446  LWIP_ASSERT("memp_free: mem properly aligned",
447  ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
448 
449  memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
450 
451  SYS_ARCH_PROTECT(old_level);
452 #if MEMP_OVERFLOW_CHECK
453 #if MEMP_OVERFLOW_CHECK >= 2
454  memp_overflow_check_all();
455 #else
456  memp_overflow_check_element_overflow(memp, type);
457  memp_overflow_check_element_underflow(memp, type);
458 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
459 #endif /* MEMP_OVERFLOW_CHECK */
460 
461  MEMP_STATS_DEC(used, type);
462 
463  memp->next = memp_tab[type];
464  memp_tab[type] = memp;
465 
466 #if MEMP_SANITY_CHECK
467  LWIP_ASSERT("memp sanity", memp_sanity());
468 #endif /* MEMP_SANITY_CHECK */
469 
470  SYS_ARCH_UNPROTECT(old_level);
471 }
#define MEMP_SIZE
Definition: memp.c:110
#define NULL
Definition: def.h:47
#define MEM_ALIGNMENT
Definition: opt.h:124
#define SYS_ARCH_DECL_PROTECT(x)
Definition: cc.h:86
struct memp * next
Definition: memp.c:67
unsigned long mem_ptr_t
Definition: cc.h:58
Definition: memp.c:66
static struct memp * memp_tab[MEMP_MAX]
Definition: memp.c:117
#define SYS_ARCH_PROTECT(x)
Definition: cc.h:87
unsigned char u8_t
Definition: cc.h:52
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:65
Definition: mem.c:155
#define MEMP_STATS_DEC(x, i)
Definition: stats.h:254
#define SYS_ARCH_UNPROTECT(x)
Definition: cc.h:88
Here is the caller graph for this function:

◆ memp_init()

void memp_init ( void  )

Initialize this module.

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

Definition at line 341 of file memp.c.

References LWIP_MEM_ALIGN, MEMP_MAX, memp_memory, memp_num, MEMP_OVERFLOW_CHECK, MEMP_SIZE, MEMP_STATS_AVAIL, memp::next, and NULL.

Referenced by lwip_init().

342 {
343  struct memp *memp;
344  u16_t i, j;
345 
346  for (i = 0; i < MEMP_MAX; ++i) {
347  MEMP_STATS_AVAIL(used, i, 0);
348  MEMP_STATS_AVAIL(max, i, 0);
349  MEMP_STATS_AVAIL(err, i, 0);
350  MEMP_STATS_AVAIL(avail, i, memp_num[i]);
351  }
352 
353 #if !MEMP_SEPARATE_POOLS
354  memp = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
355 #endif /* !MEMP_SEPARATE_POOLS */
356  /* for every pool: */
357  for (i = 0; i < MEMP_MAX; ++i) {
358  memp_tab[i] = NULL;
359 #if MEMP_SEPARATE_POOLS
360  memp = (struct memp*)memp_bases[i];
361 #endif /* MEMP_SEPARATE_POOLS */
362  /* create a linked list of memp elements */
363  for (j = 0; j < memp_num[i]; ++j) {
364  memp->next = (struct memp *)memp_tab[i];
365  memp_tab[i] = memp;
366  memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]
368  + MEMP_SANITY_REGION_AFTER_ALIGNED
369 #endif
370  );
371  }
372  }
373 #if MEMP_OVERFLOW_CHECK
374  memp_overflow_init();
375  /* check everything a first time to see if it worked */
376  memp_overflow_check_all();
377 #endif /* MEMP_OVERFLOW_CHECK */
378 }
Definition: memp.h:46
#define MEMP_STATS_AVAIL(x, i, y)
Definition: stats.h:252
static u8_t memp_memory[MEM_ALIGNMENT - 1 #define LWIP_MEMPOOL(name, num, size, desc, attr)]
Definition: memp.c:176
#define MEMP_SIZE
Definition: memp.c:110
#define NULL
Definition: def.h:47
static const u16_t memp_num[MEMP_MAX]
Definition: memp.c:139
#define MEMP_OVERFLOW_CHECK
Definition: opt.h:154
struct memp * next
Definition: memp.c:67
Definition: memp.c:66
static struct memp * memp_tab[MEMP_MAX]
Definition: memp.c:117
unsigned char u8_t
Definition: cc.h:52
#define LWIP_MEM_ALIGN(addr)
Definition: mem.h:159
unsigned short u16_t
Definition: cc.h:54
Here is the caller 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

the debug version has two more parameters:

Parameters
filefile name calling this function
linenumber of line where this function is called
Returns
a pointer to the allocated memory or a NULL pointer on error

Definition at line 393 of file memp.c.

References LWIP_ASSERT, LWIP_DBG_LEVEL_SERIOUS, LWIP_DEBUGF, LWIP_ERROR, MEM_ALIGNMENT, MEMP_DEBUG, memp_desc, MEMP_MAX, MEMP_SIZE, MEMP_STATS_INC, MEMP_STATS_INC_USED, memp::next, NULL, SYS_ARCH_DECL_PROTECT, SYS_ARCH_PROTECT, and SYS_ARCH_UNPROTECT.

Referenced by pbuf_alloc(), tcpip_callback_with_block(), and tcpip_input().

397 {
398  struct memp *memp;
399  SYS_ARCH_DECL_PROTECT(old_level);
400 
401  LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
402 
403  SYS_ARCH_PROTECT(old_level);
404 #if MEMP_OVERFLOW_CHECK >= 2
405  memp_overflow_check_all();
406 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
407 
408  memp = memp_tab[type];
409 
410  if (memp != NULL) {
411  memp_tab[type] = memp->next;
412 #if MEMP_OVERFLOW_CHECK
413  memp->next = NULL;
414  memp->file = file;
415  memp->line = line;
416 #endif /* MEMP_OVERFLOW_CHECK */
417  MEMP_STATS_INC_USED(used, type);
418  LWIP_ASSERT("memp_malloc: memp properly aligned",
419  ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
420  memp = (struct memp*)(void *)((u8_t*)memp + MEMP_SIZE);
421  } else {
422  LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
423  MEMP_STATS_INC(err, type);
424  }
425 
426  SYS_ARCH_UNPROTECT(old_level);
427 
428  return memp;
429 }
Definition: memp.h:46
#define MEMP_SIZE
Definition: memp.c:110
#define NULL
Definition: def.h:47
#define MEM_ALIGNMENT
Definition: opt.h:124
#define MEMP_DEBUG
Definition: opt.h:1903
#define SYS_ARCH_DECL_PROTECT(x)
Definition: cc.h:86
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:73
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:94
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:46
#define MEMP_STATS_INC_USED(x, i)
Definition: stats.h:255
struct memp * next
Definition: memp.c:67
unsigned long mem_ptr_t
Definition: cc.h:58
const char * memp_desc[MEMP_MAX]
Definition: memp.c:147
Definition: memp.c:66
static struct memp * memp_tab[MEMP_MAX]
Definition: memp.c:117
#define SYS_ARCH_PROTECT(x)
Definition: cc.h:87
unsigned char u8_t
Definition: cc.h:52
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:65
#define MEMP_STATS_INC(x, i)
Definition: stats.h:253
#define SYS_ARCH_UNPROTECT(x)
Definition: cc.h:88
Here is the caller graph for this function:

Variable Documentation

◆ ICACHE_RODATA_ATTR

const u32_t memp_sizes [MEMP_MAX] ICACHE_RODATA_ATTR
static
Initial value:
= {
#define LWIP_MEMPOOL(name,num,size,desc,attr)
}

This array holds the element sizes of each pool.

Definition at line 129 of file memp.c.

◆ memp_desc

const char* memp_desc[MEMP_MAX]
Initial value:
= {
#define LWIP_MEMPOOL(name,num,size,desc,attr)
}

This array holds a textual description of each pool.

Definition at line 147 of file memp.c.

Referenced by memp_free(), and memp_malloc().

◆ memp_memory

u8_t memp_memory[MEM_ALIGNMENT - 1 #define LWIP_MEMPOOL(name, num, size, desc, attr)]
static

This is the actual memory used by the pools (all pools in one big block).

Definition at line 176 of file memp.c.

Referenced by memp_init().

◆ memp_num

const u16_t memp_num[MEMP_MAX]
static
Initial value:
= {
#define LWIP_MEMPOOL(name,num,size,desc,attr)
}

This array holds the number of elements in each pool.

Definition at line 139 of file memp.c.

Referenced by memp_init().

◆ memp_sizes_test

u16_t memp_sizes_test[1] = {PBUF_POOL_BUFSIZE,}

Definition at line 134 of file memp.c.

Referenced by memp_free().

◆ memp_tab

struct memp* memp_tab[MEMP_MAX]
static

This array holds the first free element of each pool. Elements form a linked list.

Definition at line 117 of file memp.c.