2007-08-09 22:21:44 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Dynamic pool memory manager
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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.
|
2002-10-19 12:59:30 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
* 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
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the lwIP TCP/IP stack.
|
|
|
|
*
|
|
|
|
* Author: Adam Dunkels <adam@sics.se>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2007-05-21 16:55:57 +00:00
|
|
|
#include <string.h>
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2007-05-21 16:55:57 +00:00
|
|
|
#include "lwip/opt.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/memp.h"
|
|
|
|
#include "lwip/pbuf.h"
|
|
|
|
#include "lwip/udp.h"
|
2003-11-14 13:17:23 +00:00
|
|
|
#include "lwip/raw.h"
|
2002-10-19 12:59:30 +00:00
|
|
|
#include "lwip/tcp.h"
|
|
|
|
#include "lwip/api.h"
|
|
|
|
#include "lwip/api_msg.h"
|
|
|
|
#include "lwip/tcpip.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/stats.h"
|
|
|
|
|
2007-03-04 12:12:42 +00:00
|
|
|
#if ARP_QUEUEING
|
|
|
|
#include "netif/etharp.h"
|
|
|
|
#endif
|
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
struct memp {
|
|
|
|
struct memp *next;
|
2007-05-18 19:48:44 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK
|
|
|
|
const char *file;
|
|
|
|
int line;
|
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
2002-10-19 12:59:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct memp *memp_tab[MEMP_MAX];
|
|
|
|
|
2007-05-18 19:48:44 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK
|
|
|
|
/* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
|
|
|
|
* and at the end of each element, initialize them as 0xcd and check
|
|
|
|
* them later. */
|
|
|
|
/* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
|
|
|
|
* every single element in each pool is checked!
|
|
|
|
* This is VERY SLOW but also very helpful. */
|
2007-05-18 20:25:27 +00:00
|
|
|
/* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
|
|
|
|
* lwipopts.h to change the amount reserved for checking. */
|
2007-05-18 19:48:44 +00:00
|
|
|
#ifndef MEMP_SANITY_REGION_BEFORE
|
2007-05-18 20:25:27 +00:00
|
|
|
#define MEMP_SANITY_REGION_BEFORE 16
|
|
|
|
#endif /* MEMP_SANITY_REGION_BEFORE*/
|
|
|
|
#if MEMP_SANITY_REGION_BEFORE > 0
|
2007-06-13 17:17:26 +00:00
|
|
|
#define MEMP_SANITY_REGION_BEFORE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
|
2007-05-18 20:25:27 +00:00
|
|
|
#else
|
|
|
|
#define MEMP_SANITY_REGION_BEFORE_ALIGNED 0
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif /* MEMP_SANITY_REGION_BEFORE*/
|
|
|
|
#ifndef MEMP_SANITY_REGION_AFTER
|
2007-05-18 20:25:27 +00:00
|
|
|
#define MEMP_SANITY_REGION_AFTER 16
|
|
|
|
#endif /* MEMP_SANITY_REGION_AFTER*/
|
|
|
|
#if MEMP_SANITY_REGION_AFTER > 0
|
2007-06-13 17:17:26 +00:00
|
|
|
#define MEMP_SANITY_REGION_AFTER_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
|
2007-05-18 20:25:27 +00:00
|
|
|
#else
|
|
|
|
#define MEMP_SANITY_REGION_AFTER_ALIGNED 0
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif /* MEMP_SANITY_REGION_AFTER*/
|
|
|
|
|
|
|
|
/* MEMP_SIZE: save space for struct memp and for sanity check */
|
2007-06-13 17:17:26 +00:00
|
|
|
#define MEMP_SIZE (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
|
|
|
|
#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
|
2007-05-18 19:48:44 +00:00
|
|
|
|
|
|
|
#else /* MEMP_OVERFLOW_CHECK */
|
|
|
|
|
|
|
|
/* No sanity checks
|
|
|
|
* We don't need to preserve the struct memp while not allocated, so we
|
|
|
|
* can save a little space and set MEMP_SIZE to 0.
|
|
|
|
*/
|
|
|
|
#define MEMP_SIZE 0
|
2007-06-13 17:17:26 +00:00
|
|
|
#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
|
2007-05-18 19:48:44 +00:00
|
|
|
|
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
|
|
|
|
2007-06-21 20:12:11 +00:00
|
|
|
#if !MEM_USE_POOLS
|
|
|
|
static
|
|
|
|
#endif
|
|
|
|
const u16_t memp_sizes[MEMP_MAX] = {
|
2007-05-18 19:48:44 +00:00
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct pbuf)),
|
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct raw_pcb)),
|
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct udp_pcb)),
|
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct tcp_pcb)),
|
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct tcp_pcb_listen)),
|
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct tcp_seg)),
|
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct netbuf)),
|
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct netconn)),
|
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct tcpip_msg)),
|
2007-08-16 19:49:08 +00:00
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct tcpip_msg)),
|
2007-03-04 12:12:42 +00:00
|
|
|
#if ARP_QUEUEING
|
2007-05-18 19:48:44 +00:00
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct etharp_q_entry)),
|
2007-05-13 16:16:03 +00:00
|
|
|
#endif
|
2007-05-18 19:48:44 +00:00
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct pbuf)) + MEMP_ALIGN_SIZE(PBUF_POOL_BUFSIZE),
|
2007-06-21 20:12:11 +00:00
|
|
|
MEMP_ALIGN_SIZE(sizeof(struct sys_timeo)),
|
|
|
|
#if MEM_USE_POOLS
|
|
|
|
MEMP_ALIGN_SIZE(MEM_POOL_SIZE_1),
|
|
|
|
MEMP_ALIGN_SIZE(MEM_POOL_SIZE_2),
|
|
|
|
MEMP_ALIGN_SIZE(MEM_POOL_SIZE_3),
|
|
|
|
MEMP_ALIGN_SIZE(MEM_POOL_SIZE_4),
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const u16_t memp_num[MEMP_MAX] = {
|
|
|
|
MEMP_NUM_PBUF,
|
2003-11-14 13:17:23 +00:00
|
|
|
MEMP_NUM_RAW_PCB,
|
2002-10-19 12:59:30 +00:00
|
|
|
MEMP_NUM_UDP_PCB,
|
|
|
|
MEMP_NUM_TCP_PCB,
|
|
|
|
MEMP_NUM_TCP_PCB_LISTEN,
|
|
|
|
MEMP_NUM_TCP_SEG,
|
|
|
|
MEMP_NUM_NETBUF,
|
|
|
|
MEMP_NUM_NETCONN,
|
|
|
|
MEMP_NUM_TCPIP_MSG,
|
2007-08-16 19:49:08 +00:00
|
|
|
MEMP_NUM_TCPIP_MSG_INPUT,
|
2007-03-04 12:12:42 +00:00
|
|
|
#if ARP_QUEUEING
|
|
|
|
MEMP_NUM_ARP_QUEUE,
|
2007-05-13 16:16:03 +00:00
|
|
|
#endif
|
|
|
|
PBUF_POOL_SIZE,
|
2007-06-21 20:12:11 +00:00
|
|
|
MEMP_NUM_SYS_TIMEOUT,
|
|
|
|
#if MEM_USE_POOLS
|
|
|
|
MEM_POOL_NUM_1,
|
|
|
|
MEM_POOL_NUM_2,
|
|
|
|
MEM_POOL_NUM_3,
|
|
|
|
MEM_POOL_NUM_4,
|
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
};
|
|
|
|
|
2006-04-12 21:07:44 +00:00
|
|
|
#define MEMP_TYPE_SIZE(qty, type) \
|
2007-05-18 19:48:44 +00:00
|
|
|
((qty) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(type))))
|
2006-04-12 21:07:44 +00:00
|
|
|
|
|
|
|
static u8_t memp_memory[MEM_ALIGNMENT - 1 +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_PBUF, struct pbuf) +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_RAW_PCB, struct raw_pcb) +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_UDP_PCB, struct udp_pcb) +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_TCP_PCB, struct tcp_pcb) +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_TCP_PCB_LISTEN, struct tcp_pcb_listen) +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_TCP_SEG, struct tcp_seg) +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_NETBUF, struct netbuf) +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_NETCONN, struct netconn) +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_TCPIP_MSG, struct tcpip_msg) +
|
2007-08-16 19:49:08 +00:00
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_TCPIP_MSG_INPUT, struct tcpip_msg) +
|
2007-03-04 12:12:42 +00:00
|
|
|
#if ARP_QUEUEING
|
2007-03-09 07:15:04 +00:00
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_ARP_QUEUE, struct etharp_q_entry) +
|
2007-05-13 16:16:03 +00:00
|
|
|
#endif
|
|
|
|
MEMP_TYPE_SIZE(PBUF_POOL_SIZE, struct pbuf) +
|
2007-06-21 20:12:11 +00:00
|
|
|
((PBUF_POOL_SIZE) * MEMP_ALIGN_SIZE(PBUF_POOL_BUFSIZE)) +
|
|
|
|
MEMP_TYPE_SIZE(MEMP_NUM_SYS_TIMEOUT, struct sys_timeo)
|
|
|
|
#if MEM_USE_POOLS
|
|
|
|
+ ((MEM_POOL_NUM_1) * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_1))
|
|
|
|
+ ((MEM_POOL_NUM_2) * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_2))
|
|
|
|
+ ((MEM_POOL_NUM_3) * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_3))
|
|
|
|
+ ((MEM_POOL_NUM_4) * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_4))
|
|
|
|
#endif
|
|
|
|
];
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2004-02-26 10:43:09 +00:00
|
|
|
#if MEMP_SANITY_CHECK
|
2007-06-07 21:36:20 +00:00
|
|
|
/**
|
|
|
|
* Check that memp-lists don't form a circle
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
static int
|
|
|
|
memp_sanity(void)
|
|
|
|
{
|
2005-11-25 12:03:38 +00:00
|
|
|
s16_t i, c;
|
2002-10-19 12:59:30 +00:00
|
|
|
struct memp *m, *n;
|
2002-11-11 14:31:00 +00:00
|
|
|
|
2006-04-12 21:07:44 +00:00
|
|
|
for (i = 0; i < MEMP_MAX; i++) {
|
|
|
|
for (m = memp_tab[i]; m != NULL; m = m->next) {
|
2002-10-19 12:59:30 +00:00
|
|
|
c = 1;
|
2006-04-12 21:07:44 +00:00
|
|
|
for (n = memp_tab[i]; n != NULL; n = n->next) {
|
2006-11-17 10:51:13 +00:00
|
|
|
if (n == m && --c < 0) {
|
2007-05-18 19:48:44 +00:00
|
|
|
return 0;
|
2002-11-11 14:31:00 +00:00
|
|
|
}
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2004-02-26 10:43:09 +00:00
|
|
|
#endif /* MEMP_SANITY_CHECK*/
|
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
|
|
|
|
* @param memp_size the element size of the pool p comes from
|
|
|
|
*/
|
2007-05-18 19:48:44 +00:00
|
|
|
static void
|
2007-05-18 21:10:33 +00:00
|
|
|
memp_overflow_check_element(struct memp *p, u16_t memp_size)
|
2007-05-18 19:48:44 +00:00
|
|
|
{
|
|
|
|
u16_t k;
|
|
|
|
u8_t *m;
|
2007-05-18 20:25:27 +00:00
|
|
|
#if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
|
|
|
|
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) {
|
|
|
|
LWIP_ASSERT("detected memp underflow!", 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2007-05-18 20:25:27 +00:00
|
|
|
#if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
|
|
|
|
m = (u8_t*)p + MEMP_SIZE + memp_size - MEMP_SANITY_REGION_AFTER_ALIGNED;
|
|
|
|
for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
|
2007-05-18 19:48:44 +00:00
|
|
|
if (m[k] != 0xcd) {
|
|
|
|
LWIP_ASSERT("detected memp overflow!", 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
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;
|
|
|
|
|
2007-06-13 17:17:26 +00:00
|
|
|
p = LWIP_MEM_ALIGN(memp_memory);
|
2007-05-18 19:48:44 +00:00
|
|
|
for (i = 0; i < MEMP_MAX; ++i) {
|
|
|
|
p = p;
|
|
|
|
for (j = 0; j < memp_num[i]; ++j) {
|
2007-05-18 21:10:33 +00:00
|
|
|
memp_overflow_check_element(p, memp_sizes[i]);
|
2007-05-18 19:48:44 +00:00
|
|
|
p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-06-07 21:36:20 +00:00
|
|
|
/**
|
|
|
|
* Initialize the restricted areas of all memp elements in every pool.
|
|
|
|
*/
|
2007-05-18 19:48:44 +00:00
|
|
|
static void
|
|
|
|
memp_overflow_init(void)
|
|
|
|
{
|
|
|
|
u16_t i, j;
|
|
|
|
struct memp *p;
|
|
|
|
u8_t *m;
|
|
|
|
|
2007-06-13 17:17:26 +00:00
|
|
|
p = LWIP_MEM_ALIGN(memp_memory);
|
2007-05-18 19:48:44 +00:00
|
|
|
for (i = 0; i < MEMP_MAX; ++i) {
|
|
|
|
p = p;
|
|
|
|
for (j = 0; j < memp_num[i]; ++j) {
|
2007-05-18 20:25:27 +00:00
|
|
|
#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);
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif
|
2007-05-18 20:25:27 +00:00
|
|
|
#if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
|
|
|
|
m = (u8_t*)p + MEMP_SIZE + memp_sizes[i] - MEMP_SANITY_REGION_AFTER_ALIGNED;
|
|
|
|
memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif
|
|
|
|
p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
2003-11-14 13:17:23 +00:00
|
|
|
|
2007-06-07 21:36:20 +00:00
|
|
|
/**
|
|
|
|
* Initialize this module.
|
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)
|
|
|
|
{
|
2006-04-12 21:07:44 +00:00
|
|
|
struct memp *memp;
|
2002-10-19 12:59:30 +00:00
|
|
|
u16_t i, j;
|
2006-04-12 21:07:44 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
#if MEMP_STATS
|
2006-04-12 21:07:44 +00:00
|
|
|
for (i = 0; i < MEMP_MAX; ++i) {
|
2002-12-18 12:49:01 +00:00
|
|
|
lwip_stats.memp[i].used = lwip_stats.memp[i].max =
|
|
|
|
lwip_stats.memp[i].err = 0;
|
|
|
|
lwip_stats.memp[i].avail = memp_num[i];
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
#endif /* MEMP_STATS */
|
|
|
|
|
2007-06-13 17:17:26 +00:00
|
|
|
memp = LWIP_MEM_ALIGN(memp_memory);
|
2007-06-07 21:36:20 +00:00
|
|
|
/* for every pool: */
|
2006-04-12 21:07:44 +00:00
|
|
|
for (i = 0; i < MEMP_MAX; ++i) {
|
|
|
|
memp_tab[i] = NULL;
|
2007-06-07 21:36:20 +00:00
|
|
|
/* create a linked list of memp elements */
|
2006-04-12 21:07:44 +00:00
|
|
|
for (j = 0; j < memp_num[i]; ++j) {
|
|
|
|
memp->next = memp_tab[i];
|
2002-10-19 12:59:30 +00:00
|
|
|
memp_tab[i] = memp;
|
2006-04-12 21:07:44 +00:00
|
|
|
memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-18 19:48:44 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK
|
|
|
|
memp_overflow_init();
|
|
|
|
/* check everything a first time to see if it worked */
|
2007-05-18 21:10:33 +00:00
|
|
|
memp_overflow_check_all();
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
2003-11-14 13:17:23 +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-08-09 22:21:44 +00:00
|
|
|
* @param file file name calling this function
|
|
|
|
* @param line number of line where this function is called
|
2007-06-07 21:36:20 +00:00
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
void *
|
2007-05-18 19:48:44 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK
|
|
|
|
memp_malloc_fn(memp_t type, const char* file, const int line)
|
|
|
|
#else
|
2002-10-19 12:59:30 +00:00
|
|
|
memp_malloc(memp_t type)
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif
|
2002-10-19 12:59:30 +00:00
|
|
|
{
|
|
|
|
struct memp *memp;
|
2003-06-27 20:46:11 +00:00
|
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
2002-11-11 14:31:00 +00:00
|
|
|
|
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
|
|
|
|
2003-06-27 20:46:11 +00:00
|
|
|
SYS_ARCH_PROTECT(old_level);
|
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
|
|
|
|
2002-10-19 12:59:30 +00:00
|
|
|
memp = memp_tab[type];
|
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (memp != NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
memp_tab[type] = memp->next;
|
2007-05-18 19:48:44 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK
|
2007-07-15 10:54:24 +00:00
|
|
|
memp->next = NULL;
|
2007-05-18 19:48:44 +00:00
|
|
|
memp->file = file;
|
|
|
|
memp->line = line;
|
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
2003-11-14 13:17:23 +00:00
|
|
|
#if MEMP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.memp[type].used;
|
2003-05-01 13:24:01 +00:00
|
|
|
if (lwip_stats.memp[type].used > lwip_stats.memp[type].max) {
|
2002-12-18 12:49:01 +00:00
|
|
|
lwip_stats.memp[type].max = lwip_stats.memp[type].used;
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|
|
|
|
#endif /* MEMP_STATS */
|
2003-02-06 22:18:56 +00:00
|
|
|
LWIP_ASSERT("memp_malloc: memp properly aligned",
|
2006-04-12 21:07:44 +00:00
|
|
|
((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
|
2007-07-15 10:54:24 +00:00
|
|
|
memp = (struct memp*)((u8_t*)memp + MEMP_SIZE);
|
2002-10-19 12:59:30 +00:00
|
|
|
} else {
|
2005-11-25 12:03:38 +00:00
|
|
|
LWIP_DEBUGF(MEMP_DEBUG | 2, ("memp_malloc: out of memory in pool %"S16_F"\n", type));
|
2003-11-14 13:17:23 +00:00
|
|
|
#if MEMP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
++lwip_stats.memp[type].err;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* MEMP_STATS */
|
2006-04-12 21:07:44 +00:00
|
|
|
}
|
|
|
|
|
2003-02-12 22:00:18 +00:00
|
|
|
SYS_ARCH_UNPROTECT(old_level);
|
2006-04-12 21:07:44 +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
|
|
|
|
2007-06-07 21:36:20 +00:00
|
|
|
/**
|
|
|
|
* Put an element back into its pool.
|
|
|
|
*
|
|
|
|
* @param type the pool where to put mem
|
|
|
|
* @param mem the memp element to free
|
|
|
|
*/
|
2002-10-19 12:59:30 +00:00
|
|
|
void
|
|
|
|
memp_free(memp_t type, void *mem)
|
|
|
|
{
|
|
|
|
struct memp *memp;
|
2003-06-27 20:46:11 +00:00
|
|
|
SYS_ARCH_DECL_PROTECT(old_level);
|
2002-10-19 12:59:30 +00:00
|
|
|
|
2003-05-01 13:24:01 +00:00
|
|
|
if (mem == NULL) {
|
2002-10-19 12:59:30 +00:00
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
2007-05-18 19:48:44 +00:00
|
|
|
memp = (struct memp *)((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);
|
2007-05-18 19:48:44 +00:00
|
|
|
#if MEMP_OVERFLOW_CHECK
|
|
|
|
#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
|
|
|
#else
|
2007-05-18 21:10:33 +00:00
|
|
|
memp_overflow_check_element(memp, memp_sizes[type]);
|
2007-05-18 19:48:44 +00:00
|
|
|
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
|
|
|
|
#endif /* MEMP_OVERFLOW_CHECK */
|
2003-06-27 20:46:11 +00:00
|
|
|
|
2003-11-14 13:17:23 +00:00
|
|
|
#if MEMP_STATS
|
2002-12-18 12:49:01 +00:00
|
|
|
lwip_stats.memp[type].used--;
|
2002-10-19 12:59:30 +00:00
|
|
|
#endif /* MEMP_STATS */
|
|
|
|
|
|
|
|
memp->next = memp_tab[type];
|
|
|
|
memp_tab[type] = memp;
|
|
|
|
|
2004-02-26 10:43:09 +00:00
|
|
|
#if MEMP_SANITY_CHECK
|
2003-02-06 22:18:56 +00:00
|
|
|
LWIP_ASSERT("memp sanity", memp_sanity());
|
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);
|
2002-10-19 12:59:30 +00:00
|
|
|
}
|