ReactOS 0.4.16-dev-311-g9382aa2
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/altcp.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/priv/sockets_priv.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"
Include dependency graph for memp.c:

Go to the source code of this file.

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.

Definition in file memp.c.

Macro Definition Documentation

◆ LWIP_MEMPOOL [1/2]

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

Definition at line 78 of file memp.c.

◆ LWIP_MEMPOOL [2/2]

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

Definition at line 78 of file memp.c.

Function Documentation

◆ do_memp_free_pool()

static void do_memp_free_pool ( const struct memp_desc desc,
void mem 
)
static

Definition at line 359 of file memp.c.

360{
361 struct memp *memp;
362 SYS_ARCH_DECL_PROTECT(old_level);
363
364 LWIP_ASSERT("memp_free: mem properly aligned",
365 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
366
367 /* cast through void* to get rid of alignment warnings */
368 memp = (struct memp *)(void *)((u8_t *)mem - MEMP_SIZE);
369
370 SYS_ARCH_PROTECT(old_level);
371
372#if MEMP_OVERFLOW_CHECK == 1
373 memp_overflow_check_element(memp, desc);
374#endif /* MEMP_OVERFLOW_CHECK */
375
376#if MEMP_STATS
377 desc->stats->used--;
378#endif
379
380#if MEMP_MEM_MALLOC
382 SYS_ARCH_UNPROTECT(old_level);
383 mem_free(memp);
384#else /* MEMP_MEM_MALLOC */
385 memp->next = *desc->tab;
386 *desc->tab = memp;
387
388#if MEMP_SANITY_CHECK
389 LWIP_ASSERT("memp sanity", memp_sanity(desc));
390#endif /* MEMP_SANITY_CHECK */
391
392 SYS_ARCH_UNPROTECT(old_level);
393#endif /* !MEMP_MEM_MALLOC */
394}
#define MEM_ALIGNMENT
Definition: d3d9_helpers.c:15
#define mem_free(ptr, bsize)
Definition: types.h:124
#define SYS_ARCH_UNPROTECT(lev)
Definition: cc.h:39
#define SYS_ARCH_PROTECT(lev)
Definition: cc.h:38
#define SYS_ARCH_DECL_PROTECT(lev)
Definition: cc.h:37
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
uint8_t u8_t
Definition: arch.h:125
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:373
uintptr_t mem_ptr_t
Definition: arch.h:135
#define MEMP_SIZE
Definition: memp_priv.h:63
static const WCHAR desc[]
Definition: protectdata.c:36
Definition: mem.c:349
Definition: memp_priv.h:69
struct memp * next
Definition: memp_priv.h:70

Referenced by memp_free(), and memp_free_pool().

◆ do_memp_malloc_pool()

static void * do_memp_malloc_pool ( const struct memp_desc desc)
static

Definition at line 245 of file memp.c.

249{
250 struct memp *memp;
251 SYS_ARCH_DECL_PROTECT(old_level);
252
253#if MEMP_MEM_MALLOC
254 memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
255 SYS_ARCH_PROTECT(old_level);
256#else /* MEMP_MEM_MALLOC */
257 SYS_ARCH_PROTECT(old_level);
258
259 memp = *desc->tab;
260#endif /* MEMP_MEM_MALLOC */
261
262 if (memp != NULL) {
263#if !MEMP_MEM_MALLOC
264#if MEMP_OVERFLOW_CHECK == 1
265 memp_overflow_check_element(memp, desc);
266#endif /* MEMP_OVERFLOW_CHECK */
267
268 *desc->tab = memp->next;
269#if MEMP_OVERFLOW_CHECK
270 memp->next = NULL;
271#endif /* MEMP_OVERFLOW_CHECK */
272#endif /* !MEMP_MEM_MALLOC */
273#if MEMP_OVERFLOW_CHECK
274 memp->file = file;
275 memp->line = line;
276#if MEMP_MEM_MALLOC
277 memp_overflow_init_element(memp, desc);
278#endif /* MEMP_MEM_MALLOC */
279#endif /* MEMP_OVERFLOW_CHECK */
280 LWIP_ASSERT("memp_malloc: memp properly aligned",
281 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
282#if MEMP_STATS
283 desc->stats->used++;
284 if (desc->stats->used > desc->stats->max) {
285 desc->stats->max = desc->stats->used;
286 }
287#endif
288 SYS_ARCH_UNPROTECT(old_level);
289 /* cast through u8_t* to get rid of alignment warnings */
290 return ((u8_t *)memp + MEMP_SIZE);
291 } else {
292#if MEMP_STATS
293 desc->stats->err++;
294#endif
295 SYS_ARCH_UNPROTECT(old_level);
296 LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
297 }
298
299 return NULL;
300}
#define NULL
Definition: types.h:112
void * mem_malloc(mem_size_t size_in)
Definition: mem.c:831
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:158
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:57
#define MEMP_DEBUG
Definition: opt.h:3420
#define MEMP_ALIGN_SIZE(x)
Definition: memp_priv.h:64
Definition: fci.c:127
Definition: parser.c:49

Referenced by memp_malloc(), and memp_malloc_pool().

◆ 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 420 of file memp.c.

421{
422#ifdef LWIP_HOOK_MEMP_AVAILABLE
423 struct memp *old_first;
424#endif
425
426 LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
427
428 if (mem == NULL) {
429 return;
430 }
431
432#if MEMP_OVERFLOW_CHECK >= 2
433 memp_overflow_check_all();
434#endif /* MEMP_OVERFLOW_CHECK >= 2 */
435
436#ifdef LWIP_HOOK_MEMP_AVAILABLE
437 old_first = *memp_pools[type]->tab;
438#endif
439
441
442#ifdef LWIP_HOOK_MEMP_AVAILABLE
443 if (old_first == NULL) {
444 LWIP_HOOK_MEMP_AVAILABLE(type);
445 }
446#endif
447}
#define LWIP_ERROR(message, expression, handler)
Definition: debug.h:130
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
static void do_memp_free_pool(const struct memp_desc *desc, void *mem)
Definition: memp.c:359
const struct memp_desc *const memp_pools[MEMP_MAX]
Definition: memp.c:81
@ MEMP_MAX
Definition: memp.h:55

Referenced by pbuf_free(), tcpip_callbackmsg_delete(), tcpip_inpkt(), tcpip_thread_handle_msg(), and tcpip_try_callback().

◆ 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

Definition at line 403 of file memp.c.

404{
405 LWIP_ASSERT("invalid pool desc", desc != NULL);
406 if ((desc == NULL) || (mem == NULL)) {
407 return;
408 }
409
411}

◆ 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.

Definition at line 224 of file memp.c.

225{
226 u16_t i;
227
228 /* for every pool: */
229 for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
231
232#if LWIP_STATS && MEMP_STATS
233 lwip_stats.memp[i] = memp_pools[i]->stats;
234#endif
235 }
236
237#if MEMP_OVERFLOW_CHECK >= 2
238 /* check everything a first time to see if it worked */
239 memp_overflow_check_all();
240#endif /* MEMP_OVERFLOW_CHECK >= 2 */
241}
#define LWIP_ARRAYSIZE(x)
Definition: def.h:69
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
uint16_t u16_t
Definition: arch.h:127
void memp_init_pool(const struct memp_desc *desc)
Definition: memp.c:175

Referenced by lwip_init().

◆ 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

Definition at line 175 of file memp.c.

176{
177#if MEMP_MEM_MALLOC
179#else
180 int i;
181 struct memp *memp;
182
183 *desc->tab = NULL;
184 memp = (struct memp *)LWIP_MEM_ALIGN(desc->base);
185#if MEMP_MEM_INIT
186 /* force memset on pool memory */
187 memset(memp, 0, (size_t)desc->num * (MEMP_SIZE + desc->size
189 + MEM_SANITY_REGION_AFTER_ALIGNED
190#endif
191 ));
192#endif
193 /* create a linked list of memp elements */
194 for (i = 0; i < desc->num; ++i) {
195 memp->next = *desc->tab;
196 *desc->tab = memp;
197#if MEMP_OVERFLOW_CHECK
198 memp_overflow_init_element(memp, desc);
199#endif /* MEMP_OVERFLOW_CHECK */
200 /* cast through void* to get rid of alignment warnings */
201 memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
203 + MEM_SANITY_REGION_AFTER_ALIGNED
204#endif
205 );
206 }
207#if MEMP_STATS
208 desc->stats->avail = desc->num;
209#endif /* MEMP_STATS */
210#endif /* !MEMP_MEM_MALLOC */
211
212#if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
213 desc->stats->name = desc->desc;
214#endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
215}
#define LWIP_MEM_ALIGN(addr)
Definition: arch.h:294
#define MEMP_OVERFLOW_CHECK
Definition: opt.h:303
#define memset(x, y, z)
Definition: compat.h:39

Referenced by memp_init().

◆ 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

Definition at line 337 of file memp.c.

341{
342 void *memp;
343 LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
344
345#if MEMP_OVERFLOW_CHECK >= 2
346 memp_overflow_check_all();
347#endif /* MEMP_OVERFLOW_CHECK >= 2 */
348
349#if !MEMP_OVERFLOW_CHECK
351#else
352 memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
353#endif
354
355 return memp;
356}
static void * do_memp_malloc_pool(const struct memp_desc *desc)
Definition: memp.c:245

Referenced by pbuf_alloc(), pbuf_alloc_reference(), tcpip_callback(), tcpip_callbackmsg_new(), tcpip_inpkt(), and tcpip_try_callback().

◆ 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

Definition at line 311 of file memp.c.

315{
316 LWIP_ASSERT("invalid pool desc", desc != NULL);
317 if (desc == NULL) {
318 return NULL;
319 }
320
321#if !MEMP_OVERFLOW_CHECK
323#else
324 return do_memp_malloc_pool_fn(desc, file, line);
325#endif
326}

Variable Documentation

◆ memp_pools

Initial value:
= {
#define LWIP_MEMPOOL(name,num,size,desc)
}

Definition at line 81 of file memp.c.

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