2007-08-09 22:21:44 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Dynamic pool memory manager
|
|
|
|
*
|
2007-11-25 10:43:28 +00:00
|
|
|
* lwIP has dedicated pools for many structures (netconn, protocol control blocks,
|
|
|
|
* packet buffers, ...). All these pools are managed here.
|
2016-08-07 08:05:34 +00:00
|
|
|
*
|
|
|
|
* @defgroup mempool Memory pools
|
|
|
|
* @ingroup infrastructure
|
|
|
|
* Custom memory pools
|
|
|
|
|
2007-08-09 22:21:44 +00:00
|
|
|
*/
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
/*
|
2004-02-07 00:30:03 +00:00
|
|
|
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
2015-10-06 19:13:15 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
2002-10-19 12:59:30 +00:00
|
|
|
* are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
2015-10-06 19:13:15 +00:00
|
|
|
* derived from this software without specific prior written permission.
|
2002-10-19 12:59:30 +00:00
|
|
|
*
|
2015-10-06 19:13:15 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
|
|
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
2002-10-19 12:59:30 +00:00
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
2015-10-06 19:13:15 +00:00
|
|
|
*
|
2002-10-19 12:59:30 +00:00
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-05-21 16:55:57 +00:00
|
|
|
#include "lwip/opt.h"
|
2007-09-07 23:01:59 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/memp.h"
|
2016-07-21 20:17:32 +00:00
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/stats.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* Make sure we include everything we need for size calculation required by memp_std.h */
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/pbuf.h"
|
2003-11-14 13:17:23 +00:00
|
|
|
#include "lwip/raw.h"
|
2016-07-21 20:17:32 +00:00
|
|
|
#include "lwip/udp.h"
|
|
|
|
#include "lwip/tcp.h"
|
|
|
|
#include "lwip/priv/tcp_priv.h"
|
|
|
|
#include "lwip/ip4_frag.h"
|
|
|
|
#include "lwip/netbuf.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/api.h"
|
2016-07-21 20:17:32 +00:00
|
|
|
#include "lwip/priv/tcpip_priv.h"
|
2015-10-09 19:58:58 +00:00
|
|
|
#include "lwip/priv/api_msg.h"
|
2014-02-20 18:50:17 +00:00
|
|
|
#include "lwip/sockets.h"
|
2016-07-21 20:17:32 +00:00
|
|
|
#include "lwip/netifapi.h"
|
2016-07-19 07:29:51 +00:00
|
|
|
#include "lwip/etharp.h"
|
2016-07-21 20:17:32 +00:00
|
|
|
#include "lwip/igmp.h"
|
|
|
|
#include "lwip/timeouts.h"
|
|
|
|
/* needed by default MEMP_NUM_SYS_TIMEOUT */
|
|
|
|
#include "netif/ppp/ppp_opts.h"
|
2014-09-02 19:27:24 +00:00
|
|
|
#include "lwip/netdb.h"
|
2016-07-21 20:17:32 +00:00
|
|
|
#include "lwip/dns.h"
|
2016-12-14 20:04:53 +00:00
|
|
|
#include "lwip/priv/nd6_priv.h"
|
2011-05-17 19:35:14 +00:00
|
|
|
#include "lwip/ip6_frag.h"
|
|
|
|
#include "lwip/mld6.h"
|
2007-09-07 23:01:59 +00:00
|
|
|
|
2007-03-04 12:12:42 +00:00
|
|
|
|
2015-11-12 07:45:04 +00:00
|
|
|
#define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
|
2015-10-09 19:58:58 +00:00
|
|
|
#include "lwip/priv/memp_std.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2016-02-16 21:31:59 +00:00
|
|
|
const struct memp_desc* const memp_pools[MEMP_MAX] = {
|
2015-11-12 07:45:04 +00:00
|
|
|
#define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name,
|
2015-10-09 19:58:58 +00:00
|
|
|
#include "lwip/priv/memp_std.h"
|
2007-09-21 12:36:35 +00:00
|
|
|
};
|
|
|
|
|
2016-07-21 20:17:32 +00:00
|
|
|
#if MEMP_MEM_MALLOC && MEMP_OVERFLOW_CHECK >= 2
|
|
|
|
#undef MEMP_OVERFLOW_CHECK
|
|
|
|
/* MEMP_OVERFLOW_CHECK >= 2 does not work with MEMP_MEM_MALLOC, use 1 instead */
|
|
|
|
#define MEMP_OVERFLOW_CHECK 1
|
|
|
|
#endif
|
2010-01-10 12:44:09 +00:00
|
|
|
|
2016-07-21 20:17:32 +00:00
|
|
|
#if MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC
|
2007-06-07 21:36:20 +00:00
|
|
|
/**
|
2012-08-22 19:59:02 +00:00
|
|
|
* Check that memp-lists don't form a circle, using "Floyd's cycle-finding algorithm".
|
2007-06-07 21:36:20 +00:00
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static int
|
2015-11-12 07:45:04 +00:00
|
|
|
memp_sanity(const struct memp_desc *desc)
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2012-08-22 19:59:02 +00:00
|
|
|
struct memp *t, *h;
|
2002-11-11 14:31:00 +00:00
|
|
|
|
2015-11-12 07:45:04 +00:00
|
|
|
t = *desc->tab;
|
|
|
|
if (t != NULL) {
|
|
|
|
for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
|
2015-11-17 02:08:37 +00:00
|
|
|
h = ((h->next != NULL) ? h->next->next : NULL)) {
|
2015-11-12 07:45:04 +00:00
|
|
|
if (t == h) {
|
|
|
|
return 0;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-12 07:45:04 +00:00
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2016-07-21 20:17:32 +00:00
|
|
|
#endif /* MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC */
|
2015-11-12 07:45:04 +00:00
|
|
|
|
2007-05-18 19:48:44 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK
|
2007-06-07 21:36:20 +00:00
|
|
|
/**
|
|
|
|
* Check if a memp element was victim of an overflow
|
|
|
|
* (e.g. the restricted area after it has been altered)
|
|
|
|
*
|
|
|
|
* @param p the memp element to check
|
2016-08-26 15:13:05 +00:00
|
|
|
* @param desc the pool p comes from
|
2007-06-07 21:36:20 +00:00
|
|
|
*/
|
2007-05-18 19:48:44 +00:00
|
|
|
static void
|
2015-11-12 07:45:04 +00:00
|
|
|
memp_overflow_check_element_overflow(struct memp *p, const struct memp_desc *desc)
|
2007-05-18 19:48:44 +00:00
|
|
|
{
|
2016-08-19 08:34:12 +00:00
|
|
|
#if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
|
2007-05-18 19:48:44 +00:00
|
|
|
u16_t k;
|
|
|
|
u8_t *m;
|
2015-11-12 07:45:04 +00:00
|
|
|
m = (u8_t*)p + MEMP_SIZE + desc->size;
|
2010-04-12 11:08:11 +00:00
|
|
|
for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
|
2007-05-18 19:48:44 +00:00
|
|
|
if (m[k] != 0xcd) {
|
2010-04-12 11:08:11 +00:00
|
|
|
char errstr[128] = "detected memp overflow in pool ";
|
2015-11-12 07:45:04 +00:00
|
|
|
strcat(errstr, desc->desc);
|
2010-04-12 11:08:11 +00:00
|
|
|
LWIP_ASSERT(errstr, 0);
|
2007-05-18 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-19 08:34:12 +00:00
|
|
|
#else /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
|
|
|
|
LWIP_UNUSED_ARG(p);
|
|
|
|
LWIP_UNUSED_ARG(desc);
|
|
|
|
#endif /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
|
2010-04-12 11:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a memp element was victim of an underflow
|
|
|
|
* (e.g. the restricted area before it has been altered)
|
|
|
|
*
|
|
|
|
* @param p the memp element to check
|
2016-08-26 15:13:05 +00:00
|
|
|
* @param desc the pool p comes from
|
2010-04-12 11:08:11 +00:00
|
|
|
*/
|
|
|
|
static void
|
2015-11-12 07:45:04 +00:00
|
|
|
memp_overflow_check_element_underflow(struct memp *p, const struct memp_desc *desc)
|
2010-04-12 11:08:11 +00:00
|
|
|
{
|
2016-08-19 08:34:12 +00:00
|
|
|
#if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
|
2010-04-12 11:08:11 +00:00
|
|
|
u16_t k;
|
|
|
|
u8_t *m;
|
|
|
|
m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
|
|
|
|
for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
|
2007-05-18 19:48:44 +00:00
|
|
|
if (m[k] != 0xcd) {
|
2010-04-12 11:08:11 +00:00
|
|
|
char errstr[128] = "detected memp underflow in pool ";
|
2015-11-12 07:45:04 +00:00
|
|
|
strcat(errstr, desc->desc);
|
2010-04-12 11:08:11 +00:00
|
|
|
LWIP_ASSERT(errstr, 0);
|
2007-05-18 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-19 08:34:12 +00:00
|
|
|
#else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
|
|
|
|
LWIP_UNUSED_ARG(p);
|
|
|
|
LWIP_UNUSED_ARG(desc);
|
|
|
|
#endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
|
2007-05-18 19:48:44 +00:00
|
|
|
}
|
2007-11-25 10:43:28 +00:00
|
|
|
|
2016-07-21 20:17:32 +00:00
|
|
|
/**
|
|
|
|
* Initialize the restricted area of on memp element.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
memp_overflow_init_element(struct memp *p, const struct memp_desc *desc)
|
|
|
|
{
|
2016-08-19 08:34:12 +00:00
|
|
|
#if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0
|
2016-07-21 20:17:32 +00:00
|
|
|
u8_t *m;
|
|
|
|
#if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
|
|
|
|
m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
|
|
|
|
memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
|
|
|
|
#endif
|
|
|
|
#if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
|
|
|
|
m = (u8_t*)p + MEMP_SIZE + desc->size;
|
|
|
|
memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
|
|
|
|
#endif
|
2016-08-19 08:34:12 +00:00
|
|
|
#else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
|
|
|
|
LWIP_UNUSED_ARG(p);
|
|
|
|
LWIP_UNUSED_ARG(desc);
|
|
|
|
#endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
|
2016-07-21 20:17:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if MEMP_OVERFLOW_CHECK >= 2
|
2007-06-07 21:36:20 +00:00
|
|
|
/**
|
|
|
|
* Do an overflow check for all elements in every pool.
|
|
|
|
*
|
|
|
|
* @see memp_overflow_check_element for a description of the check
|
|
|
|
*/
|
2007-05-18 19:48:44 +00:00
|
|
|
static void
|
2007-05-18 21:10:33 +00:00
|
|
|
memp_overflow_check_all(void)
|
2007-05-18 19:48:44 +00:00
|
|
|
{
|
|
|
|
u16_t i, j;
|
|
|
|
struct memp *p;
|
2016-07-21 20:17:32 +00:00
|
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
|
|
|
SYS_ARCH_PROTECT(old_level);
|
2007-05-18 19:48:44 +00:00
|
|
|
|
2010-04-12 11:08:11 +00:00
|
|
|
for (i = 0; i < MEMP_MAX; ++i) {
|
2016-11-04 01:01:58 +00:00
|
|
|
p = (struct memp*)LWIP_MEM_ALIGN(memp_pools[i]->base);
|
2015-11-12 07:45:04 +00:00
|
|
|
for (j = 0; j < memp_pools[i]->num; ++j) {
|
|
|
|
memp_overflow_check_element_overflow(p, memp_pools[i]);
|
|
|
|
memp_overflow_check_element_underflow(p, memp_pools[i]);
|
2016-12-12 09:17:33 +00:00
|
|
|
p = LWIP_ALIGNMENT_CAST(struct memp*, ((u8_t*)p + MEMP_SIZE + memp_pools[i]->size + MEMP_SANITY_REGION_AFTER_ALIGNED));
|
2007-05-18 19:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-21 20:17:32 +00:00
|
|
|
SYS_ARCH_UNPROTECT(old_level);
|
2007-05-18 19:48:44 +00:00
|
|
|
}
|
2016-07-21 20:17:32 +00:00
|
|
|
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2016-07-16 15:56:29 +00:00
|
|
|
/**
|
|
|
|
* Initialize custom memory pool.
|
|
|
|
* Related functions: memp_malloc_pool, memp_free_pool
|
|
|
|
*
|
|
|
|
* @param desc pool to initialize
|
|
|
|
*/
|
2015-11-12 07:45:04 +00:00
|
|
|
void
|
|
|
|
memp_init_pool(const struct memp_desc *desc)
|
|
|
|
{
|
2016-07-26 13:01:39 +00:00
|
|
|
#if MEMP_MEM_MALLOC
|
|
|
|
LWIP_UNUSED_ARG(desc);
|
|
|
|
#else
|
2015-11-12 07:45:04 +00:00
|
|
|
int i;
|
|
|
|
struct memp *memp;
|
2016-07-21 20:17:32 +00:00
|
|
|
|
2015-11-12 07:45:04 +00:00
|
|
|
*desc->tab = NULL;
|
|
|
|
memp = (struct memp*)LWIP_MEM_ALIGN(desc->base);
|
|
|
|
/* create a linked list of memp elements */
|
|
|
|
for (i = 0; i < desc->num; ++i) {
|
|
|
|
memp->next = *desc->tab;
|
|
|
|
*desc->tab = memp;
|
2016-08-18 23:34:33 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK
|
|
|
|
memp_overflow_init_element(memp, desc);
|
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
2016-07-07 19:55:51 +00:00
|
|
|
/* cast through void* to get rid of alignment warnings */
|
|
|
|
memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
|
2015-11-12 07:45:04 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK
|
|
|
|
+ MEMP_SANITY_REGION_AFTER_ALIGNED
|
|
|
|
#endif
|
|
|
|
);
|
2016-07-21 20:17:32 +00:00
|
|
|
}
|
2016-07-08 08:06:01 +00:00
|
|
|
#if MEMP_STATS
|
2016-07-08 09:44:28 +00:00
|
|
|
desc->stats->avail = desc->num;
|
2016-08-19 10:24:26 +00:00
|
|
|
#endif /* MEMP_STATS */
|
2016-07-21 20:17:32 +00:00
|
|
|
#endif /* !MEMP_MEM_MALLOC */
|
2016-07-08 08:06:01 +00:00
|
|
|
|
2016-08-19 10:24:26 +00:00
|
|
|
#if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
|
2016-07-08 09:44:28 +00:00
|
|
|
desc->stats->name = desc->desc;
|
2016-08-19 10:24:26 +00:00
|
|
|
#endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
|
2015-11-12 07:45:04 +00:00
|
|
|
}
|
|
|
|
|
2007-06-07 21:36:20 +00:00
|
|
|
/**
|
2016-07-16 15:56:29 +00:00
|
|
|
* Initializes lwIP built-in pools.
|
|
|
|
* Related functions: memp_malloc, memp_free
|
2015-10-06 19:08:28 +00:00
|
|
|
*
|
2007-07-14 00:07:43 +00:00
|
|
|
* Carves out memp_memory into linked lists for each pool-type.
|
2007-06-07 21:36:20 +00:00
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
memp_init(void)
|
|
|
|
{
|
2015-11-12 07:45:04 +00:00
|
|
|
u16_t i;
|
2006-04-12 21:07:44 +00:00
|
|
|
|
2007-06-07 21:36:20 +00:00
|
|
|
/* for every pool: */
|
2016-07-07 19:55:51 +00:00
|
|
|
for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
|
2015-11-12 07:45:04 +00:00
|
|
|
memp_init_pool(memp_pools[i]);
|
2016-07-07 19:55:51 +00:00
|
|
|
|
2016-07-26 13:01:39 +00:00
|
|
|
#if LWIP_STATS && MEMP_STATS
|
2016-07-07 19:55:51 +00:00
|
|
|
lwip_stats.memp[i] = memp_pools[i]->stats;
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2015-11-12 07:45:04 +00:00
|
|
|
|
2016-07-21 20:17:32 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK >= 2
|
2007-05-18 19:48:44 +00:00
|
|
|
/* check everything a first time to see if it worked */
|
2007-05-18 21:10:33 +00:00
|
|
|
memp_overflow_check_all();
|
2016-07-21 20:17:32 +00:00
|
|
|
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2016-07-21 20:17:32 +00:00
|
|
|
static void*
|
2015-11-12 07:45:04 +00:00
|
|
|
#if !MEMP_OVERFLOW_CHECK
|
2016-07-21 20:17:32 +00:00
|
|
|
do_memp_malloc_pool(const struct memp_desc *desc)
|
2015-11-12 07:45:04 +00:00
|
|
|
#else
|
2016-07-21 20:17:32 +00:00
|
|
|
do_memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
|
2015-11-12 07:45:04 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
struct memp *memp;
|
|
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
|
|
|
|
2016-07-21 20:17:32 +00:00
|
|
|
#if MEMP_MEM_MALLOC
|
|
|
|
memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
|
|
|
|
SYS_ARCH_PROTECT(old_level);
|
|
|
|
#else /* MEMP_MEM_MALLOC */
|
2015-11-12 07:45:04 +00:00
|
|
|
SYS_ARCH_PROTECT(old_level);
|
|
|
|
|
|
|
|
memp = *desc->tab;
|
2016-07-21 20:17:32 +00:00
|
|
|
#endif /* MEMP_MEM_MALLOC */
|
2015-11-12 07:45:04 +00:00
|
|
|
|
|
|
|
if (memp != NULL) {
|
2016-07-21 20:17:32 +00:00
|
|
|
#if !MEMP_MEM_MALLOC
|
2016-11-16 19:15:51 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK == 1
|
|
|
|
memp_overflow_check_element_overflow(memp, desc);
|
|
|
|
memp_overflow_check_element_underflow(memp, desc);
|
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
|
|
|
|
2015-11-12 07:45:04 +00:00
|
|
|
*desc->tab = memp->next;
|
|
|
|
#if MEMP_OVERFLOW_CHECK
|
|
|
|
memp->next = NULL;
|
2016-07-21 20:17:32 +00:00
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
|
|
|
#endif /* !MEMP_MEM_MALLOC */
|
|
|
|
#if MEMP_OVERFLOW_CHECK
|
2015-11-12 07:45:04 +00:00
|
|
|
memp->file = file;
|
|
|
|
memp->line = line;
|
2016-07-21 20:17:32 +00:00
|
|
|
#if MEMP_MEM_MALLOC
|
|
|
|
memp_overflow_init_element(memp, desc);
|
|
|
|
#endif /* MEMP_MEM_MALLOC */
|
2015-11-12 07:45:04 +00:00
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
|
|
|
LWIP_ASSERT("memp_malloc: memp properly aligned",
|
|
|
|
((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
|
2016-07-07 19:55:51 +00:00
|
|
|
#if MEMP_STATS
|
|
|
|
desc->stats->used++;
|
|
|
|
if (desc->stats->used > desc->stats->max) {
|
|
|
|
desc->stats->max = desc->stats->used;
|
|
|
|
}
|
|
|
|
#endif
|
2016-07-21 20:17:32 +00:00
|
|
|
SYS_ARCH_UNPROTECT(old_level);
|
|
|
|
/* cast through u8_t* to get rid of alignment warnings */
|
|
|
|
return ((u8_t*)memp + MEMP_SIZE);
|
2016-07-07 19:55:51 +00:00
|
|
|
} else {
|
|
|
|
LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
|
|
|
|
#if MEMP_STATS
|
|
|
|
desc->stats->err++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-11-12 07:45:04 +00:00
|
|
|
SYS_ARCH_UNPROTECT(old_level);
|
2016-07-21 20:17:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-12 07:45:04 +00:00
|
|
|
|
2016-07-21 20:17:32 +00:00
|
|
|
/**
|
|
|
|
* Get an element from a custom pool.
|
|
|
|
*
|
|
|
|
* @param desc the pool to get an element from
|
|
|
|
*
|
|
|
|
* @return a pointer to the allocated memory or a NULL pointer on error
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
#if !MEMP_OVERFLOW_CHECK
|
|
|
|
memp_malloc_pool(const struct memp_desc *desc)
|
|
|
|
#else
|
|
|
|
memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
LWIP_ASSERT("invalid pool desc", desc != NULL);
|
|
|
|
if (desc == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if !MEMP_OVERFLOW_CHECK
|
2016-07-26 10:26:52 +00:00
|
|
|
return do_memp_malloc_pool(desc);
|
2016-07-21 20:17:32 +00:00
|
|
|
#else
|
2016-07-26 10:26:52 +00:00
|
|
|
return do_memp_malloc_pool_fn(desc, file, line);
|
2016-07-21 20:17:32 +00:00
|
|
|
#endif
|
2015-11-12 07:45:04 +00:00
|
|
|
}
|
|
|
|
|
2007-06-07 21:36:20 +00:00
|
|
|
/**
|
|
|
|
* Get an element from a specific pool.
|
|
|
|
*
|
|
|
|
* @param type the pool to get an element from
|
2007-11-25 10:43:28 +00:00
|
|
|
*
|
|
|
|
* @return a pointer to the allocated memory or a NULL pointer on error
|
2007-06-07 21:36:20 +00:00
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
void *
|
2007-11-25 10:43:28 +00:00
|
|
|
#if !MEMP_OVERFLOW_CHECK
|
2002-10-19 12:59:30 +00:00
|
|
|
memp_malloc(memp_t type)
|
2007-11-25 10:43:28 +00:00
|
|
|
#else
|
|
|
|
memp_malloc_fn(memp_t type, const char* file, const int line)
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
2015-11-12 07:45:04 +00:00
|
|
|
void *memp;
|
2007-06-22 20:50:21 +00:00
|
|
|
LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-05-18 19:48:44 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK >= 2
|
2007-05-18 21:10:33 +00:00
|
|
|
memp_overflow_check_all();
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
|
2003-06-27 20:46:11 +00:00
|
|
|
|
2015-11-12 07:45:04 +00:00
|
|
|
#if !MEMP_OVERFLOW_CHECK
|
2016-07-21 20:17:32 +00:00
|
|
|
memp = do_memp_malloc_pool(memp_pools[type]);
|
2015-11-12 07:45:04 +00:00
|
|
|
#else
|
2016-07-21 20:17:32 +00:00
|
|
|
memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
|
2015-11-12 07:45:04 +00:00
|
|
|
#endif
|
2015-10-06 19:08:28 +00:00
|
|
|
|
2007-07-15 10:54:24 +00:00
|
|
|
return memp;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2015-12-04 14:58:32 +00:00
|
|
|
static void
|
|
|
|
do_memp_free_pool(const struct memp_desc* desc, void *mem)
|
|
|
|
{
|
2016-07-21 20:17:32 +00:00
|
|
|
struct memp *memp;
|
2016-07-27 06:26:42 +00:00
|
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-05-04 15:52:11 +00:00
|
|
|
LWIP_ASSERT("memp_free: mem properly aligned",
|
|
|
|
((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
|
2006-04-12 21:07:44 +00:00
|
|
|
|
2016-07-07 19:55:51 +00:00
|
|
|
/* cast through void* to get rid of alignment warnings */
|
2010-03-15 09:57:03 +00:00
|
|
|
memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2006-04-12 21:07:44 +00:00
|
|
|
SYS_ARCH_PROTECT(old_level);
|
2003-06-27 20:46:11 +00:00
|
|
|
|
2015-11-19 11:09:04 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK == 1
|
2015-11-12 07:45:04 +00:00
|
|
|
memp_overflow_check_element_overflow(memp, desc);
|
|
|
|
memp_overflow_check_element_underflow(memp, desc);
|
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
2015-02-13 20:53:55 +00:00
|
|
|
|
2016-07-07 19:55:51 +00:00
|
|
|
#if MEMP_STATS
|
|
|
|
desc->stats->used--;
|
|
|
|
#endif
|
|
|
|
|
2016-07-21 20:17:32 +00:00
|
|
|
#if MEMP_MEM_MALLOC
|
2016-07-26 13:01:39 +00:00
|
|
|
LWIP_UNUSED_ARG(desc);
|
2016-07-21 20:17:32 +00:00
|
|
|
SYS_ARCH_UNPROTECT(old_level);
|
|
|
|
mem_free(memp);
|
|
|
|
#else /* MEMP_MEM_MALLOC */
|
2015-11-12 07:45:04 +00:00
|
|
|
memp->next = *desc->tab;
|
|
|
|
*desc->tab = memp;
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2004-02-26 10:43:09 +00:00
|
|
|
#if MEMP_SANITY_CHECK
|
2015-11-12 07:45:04 +00:00
|
|
|
LWIP_ASSERT("memp sanity", memp_sanity(desc));
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif /* MEMP_SANITY_CHECK */
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-02-12 22:00:18 +00:00
|
|
|
SYS_ARCH_UNPROTECT(old_level);
|
2016-07-21 20:17:32 +00:00
|
|
|
#endif /* !MEMP_MEM_MALLOC */
|
2015-11-12 07:45:04 +00:00
|
|
|
}
|
|
|
|
|
2016-07-16 15:56:29 +00:00
|
|
|
/**
|
|
|
|
* Put a custom pool element back into its pool.
|
|
|
|
*
|
|
|
|
* @param desc the pool where to put mem
|
|
|
|
* @param mem the memp element to free
|
|
|
|
*/
|
2015-12-04 14:58:32 +00:00
|
|
|
void
|
|
|
|
memp_free_pool(const struct memp_desc* desc, void *mem)
|
|
|
|
{
|
2016-07-21 20:17:32 +00:00
|
|
|
LWIP_ASSERT("invalid pool desc", desc != NULL);
|
2015-12-04 14:58:32 +00:00
|
|
|
if ((desc == NULL) || (mem == NULL)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do_memp_free_pool(desc, mem);
|
|
|
|
}
|
|
|
|
|
2015-11-12 07:45:04 +00:00
|
|
|
/**
|
|
|
|
* Put an element back into its pool.
|
|
|
|
*
|
|
|
|
* @param type the pool where to put mem
|
|
|
|
* @param mem the memp element to free
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
memp_free(memp_t type, void *mem)
|
|
|
|
{
|
|
|
|
#ifdef LWIP_HOOK_MEMP_AVAILABLE
|
|
|
|
struct memp *old_first;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
|
|
|
|
|
2016-11-03 11:13:05 +00:00
|
|
|
if (mem == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-12 07:45:04 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK >= 2
|
|
|
|
memp_overflow_check_all();
|
|
|
|
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
|
|
|
|
|
2015-12-04 14:58:32 +00:00
|
|
|
#ifdef LWIP_HOOK_MEMP_AVAILABLE
|
2016-08-18 12:53:57 +00:00
|
|
|
old_first = *memp_pools[type]->tab;
|
2015-12-04 14:58:32 +00:00
|
|
|
#endif
|
2015-11-12 07:45:04 +00:00
|
|
|
|
2016-07-21 20:17:32 +00:00
|
|
|
do_memp_free_pool(memp_pools[type], mem);
|
2015-11-12 07:45:04 +00:00
|
|
|
|
2015-02-13 20:53:55 +00:00
|
|
|
#ifdef LWIP_HOOK_MEMP_AVAILABLE
|
|
|
|
if (old_first == NULL) {
|
|
|
|
LWIP_HOOK_MEMP_AVAILABLE(type);
|
|
|
|
}
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|