lwip/src/core/memp.c

352 lines
10 KiB
C
Raw Normal View History

2002-10-19 12:59:30 +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>
*
*/
#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"
#if ARP_QUEUEING
#include "netif/etharp.h"
#endif
2002-10-19 12:59:30 +00:00
struct memp {
struct memp *next;
#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];
#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. */
/* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
* lwipopts.h to change the amount reserved for checking. */
#ifndef MEMP_SANITY_REGION_BEFORE
#define MEMP_SANITY_REGION_BEFORE 16
#endif /* MEMP_SANITY_REGION_BEFORE*/
#if MEMP_SANITY_REGION_BEFORE > 0
#define MEMP_SANITY_REGION_BEFORE_ALIGNED MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
#else
#define MEMP_SANITY_REGION_BEFORE_ALIGNED 0
#endif /* MEMP_SANITY_REGION_BEFORE*/
#ifndef MEMP_SANITY_REGION_AFTER
#define MEMP_SANITY_REGION_AFTER 16
#endif /* MEMP_SANITY_REGION_AFTER*/
#if MEMP_SANITY_REGION_AFTER > 0
#define MEMP_SANITY_REGION_AFTER_ALIGNED MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
#else
#define MEMP_SANITY_REGION_AFTER_ALIGNED 0
#endif /* MEMP_SANITY_REGION_AFTER*/
/* MEMP_SIZE: save space for struct memp and for sanity check */
#define MEMP_SIZE (MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
#define MEMP_ALIGN_SIZE(x) (MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
#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
#define MEMP_ALIGN_SIZE(x) (MEM_ALIGN_SIZE(x))
#endif /* MEMP_OVERFLOW_CHECK */
2002-10-19 12:59:30 +00:00
static const u16_t memp_sizes[MEMP_MAX] = {
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)),
#if ARP_QUEUEING
MEMP_ALIGN_SIZE(sizeof(struct etharp_q_entry)),
#endif
MEMP_ALIGN_SIZE(sizeof(struct pbuf)) + MEMP_ALIGN_SIZE(PBUF_POOL_BUFSIZE),
MEMP_ALIGN_SIZE(sizeof(struct sys_timeo))
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,
#if ARP_QUEUEING
MEMP_NUM_ARP_QUEUE,
#endif
PBUF_POOL_SIZE,
2002-10-19 12:59:30 +00:00
MEMP_NUM_SYS_TIMEOUT
};
#define MEMP_TYPE_SIZE(qty, type) \
((qty) * (MEMP_SIZE + MEMP_ALIGN_SIZE(sizeof(type))))
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) +
#if ARP_QUEUEING
MEMP_TYPE_SIZE(MEMP_NUM_ARP_QUEUE, struct etharp_q_entry) +
#endif
MEMP_TYPE_SIZE(PBUF_POOL_SIZE, struct pbuf) +
PBUF_POOL_SIZE * MEMP_ALIGN_SIZE(PBUF_POOL_BUFSIZE) +
MEMP_TYPE_SIZE(MEMP_NUM_SYS_TIMEOUT, struct sys_timeo)];
2003-11-14 13:17:23 +00:00
#if MEMP_SANITY_CHECK
2002-10-19 12:59:30 +00:00
static int
memp_sanity(void)
{
s16_t i, c;
2002-10-19 12:59:30 +00:00
struct memp *m, *n;
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;
for (n = memp_tab[i]; n != NULL; n = n->next) {
2006-11-17 10:51:13 +00:00
if (n == m && --c < 0) {
return 0;
}
2002-10-19 12:59:30 +00:00
}
}
}
return 1;
}
#endif /* MEMP_SANITY_CHECK*/
#if MEMP_OVERFLOW_CHECK
static void
memp_overflow_check_single(struct memp *p, u16_t memp_size)
{
u16_t k;
u8_t *m;
#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++) {
if (m[k] != 0xcd) {
LWIP_ASSERT("detected memp underflow!", 0);
}
}
#endif
#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++) {
if (m[k] != 0xcd) {
LWIP_ASSERT("detected memp overflow!", 0);
}
}
#endif
}
static void
memp_overflow_check(void)
{
u16_t i, j;
struct memp *p;
p = MEM_ALIGN(memp_memory);
for (i = 0; i < MEMP_MAX; ++i) {
p = p;
for (j = 0; j < memp_num[i]; ++j) {
memp_overflow_check_single(p, memp_sizes[i]);
p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i]);
}
}
}
static void
memp_overflow_init(void)
{
u16_t i, j;
struct memp *p;
u8_t *m;
p = MEM_ALIGN(memp_memory);
for (i = 0; i < MEMP_MAX; ++i) {
p = p;
for (j = 0; j < memp_num[i]; ++j) {
#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 + memp_sizes[i] - MEMP_SANITY_REGION_AFTER_ALIGNED;
memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
#endif
p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i]);
}
}
}
#endif /* MEMP_OVERFLOW_CHECK */
2003-11-14 13:17:23 +00:00
2002-10-19 12:59:30 +00:00
void
memp_init(void)
{
struct memp *memp;
2002-10-19 12:59:30 +00:00
u16_t i, j;
2003-11-14 13:17:23 +00:00
#if MEMP_STATS
for (i = 0; i < MEMP_MAX; ++i) {
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 */
memp = MEM_ALIGN(memp_memory);
for (i = 0; i < MEMP_MAX; ++i) {
memp_tab[i] = NULL;
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;
memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]);
2002-10-19 12:59:30 +00:00
}
}
#if MEMP_OVERFLOW_CHECK
memp_overflow_init();
/* check everything a first time to see if it worked */
memp_overflow_check();
#endif /* MEMP_OVERFLOW_CHECK */
2002-10-19 12:59:30 +00:00
}
2003-11-14 13:17:23 +00:00
2002-10-19 12:59:30 +00:00
void *
#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)
#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);
Add the following features and bugfixes: Added select() functionality to sockets library. Support for errno in sockets library. Byte ordering fixes. basic lwip_ioctl(), FIONREAD, get/setsockopt() etc. support - added additional argument to netif_add to pass state pointer so that the if_init function has access to context information before the interface is added, without accessing globals. - added netif_remove() - to conserve cpu load the tcpip_tcp_timer should only be active when tcbs that need it exist. - pass length of available data to callbacks for NETCONN_EVT_RCV events - added tcpip_link_input(), a hack to allow processing of PPP packets in tcpip_thread() context. This saves threads and context switches. - renamed incompatible ASSERT() macro to LWIP_ASSERT() to avoid name collision. - changed a bunch of %d's to %u's in format strings for unsigned values. - added ip_frag to lwip_stats. - changed IP_REASS_MAXAGE and IP_REASS_TMO defaults to more realistic values. - added sys_timeout_remove() function to cancel timeouts (needed by PPP amongst other things). - tolerate NULL returns from sys_arch_timeouts() since some threads might not need to use or have timeouts. - added sys_sem_wait_timeout() - moved mem_malloc() function to end of mem.c to work around tasking compiler bug. - automatically bind to local tcp port if 0. - allow customization of port ranges for automatic local bindings. - corrected various typos, spelling errors, etc.. Thanks to Marc Boucher for many of these changes.
2003-02-06 22:18:56 +00:00
LWIP_ASSERT("memp_malloc: type < MEMP_MAX", type < MEMP_MAX);
2002-10-19 12:59:30 +00:00
2003-06-27 20:46:11 +00:00
SYS_ARCH_PROTECT(old_level);
#if MEMP_OVERFLOW_CHECK >= 2
memp_overflow_check();
#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];
if (memp != NULL) {
2002-10-19 12:59:30 +00:00
memp_tab[type] = memp->next;
memp->next = NULL;
#if MEMP_OVERFLOW_CHECK
memp->file = file;
memp->line = line;
#endif /* MEMP_OVERFLOW_CHECK */
2003-11-14 13:17:23 +00:00
#if MEMP_STATS
++lwip_stats.memp[type].used;
if (lwip_stats.memp[type].used > lwip_stats.memp[type].max) {
lwip_stats.memp[type].max = lwip_stats.memp[type].used;
2002-10-19 12:59:30 +00:00
}
#endif /* MEMP_STATS */
Add the following features and bugfixes: Added select() functionality to sockets library. Support for errno in sockets library. Byte ordering fixes. basic lwip_ioctl(), FIONREAD, get/setsockopt() etc. support - added additional argument to netif_add to pass state pointer so that the if_init function has access to context information before the interface is added, without accessing globals. - added netif_remove() - to conserve cpu load the tcpip_tcp_timer should only be active when tcbs that need it exist. - pass length of available data to callbacks for NETCONN_EVT_RCV events - added tcpip_link_input(), a hack to allow processing of PPP packets in tcpip_thread() context. This saves threads and context switches. - renamed incompatible ASSERT() macro to LWIP_ASSERT() to avoid name collision. - changed a bunch of %d's to %u's in format strings for unsigned values. - added ip_frag to lwip_stats. - changed IP_REASS_MAXAGE and IP_REASS_TMO defaults to more realistic values. - added sys_timeout_remove() function to cancel timeouts (needed by PPP amongst other things). - tolerate NULL returns from sys_arch_timeouts() since some threads might not need to use or have timeouts. - added sys_sem_wait_timeout() - moved mem_malloc() function to end of mem.c to work around tasking compiler bug. - automatically bind to local tcp port if 0. - allow customization of port ranges for automatic local bindings. - corrected various typos, spelling errors, etc.. Thanks to Marc Boucher for many of these changes.
2003-02-06 22:18:56 +00:00
LWIP_ASSERT("memp_malloc: memp properly aligned",
((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
2002-10-19 12:59:30 +00:00
} else {
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
++lwip_stats.memp[type].err;
2002-10-19 12:59:30 +00:00
#endif /* MEMP_STATS */
}
SYS_ARCH_UNPROTECT(old_level);
return (void*)((u8_t*)memp + MEMP_SIZE);
2002-10-19 12:59:30 +00:00
}
2003-11-14 13:17:23 +00:00
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
if (mem == NULL) {
2002-10-19 12:59:30 +00:00
return;
}
LWIP_ASSERT("memp_free: mem properly aligned",
((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
memp = (struct memp *)((u8_t*)mem - MEMP_SIZE);
2002-10-19 12:59:30 +00:00
SYS_ARCH_PROTECT(old_level);
#if MEMP_OVERFLOW_CHECK
#if MEMP_OVERFLOW_CHECK >= 2
memp_overflow_check();
#else
memp_overflow_check_single(memp, memp_sizes[type]);
#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
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;
#if MEMP_SANITY_CHECK
Add the following features and bugfixes: Added select() functionality to sockets library. Support for errno in sockets library. Byte ordering fixes. basic lwip_ioctl(), FIONREAD, get/setsockopt() etc. support - added additional argument to netif_add to pass state pointer so that the if_init function has access to context information before the interface is added, without accessing globals. - added netif_remove() - to conserve cpu load the tcpip_tcp_timer should only be active when tcbs that need it exist. - pass length of available data to callbacks for NETCONN_EVT_RCV events - added tcpip_link_input(), a hack to allow processing of PPP packets in tcpip_thread() context. This saves threads and context switches. - renamed incompatible ASSERT() macro to LWIP_ASSERT() to avoid name collision. - changed a bunch of %d's to %u's in format strings for unsigned values. - added ip_frag to lwip_stats. - changed IP_REASS_MAXAGE and IP_REASS_TMO defaults to more realistic values. - added sys_timeout_remove() function to cancel timeouts (needed by PPP amongst other things). - tolerate NULL returns from sys_arch_timeouts() since some threads might not need to use or have timeouts. - added sys_sem_wait_timeout() - moved mem_malloc() function to end of mem.c to work around tasking compiler bug. - automatically bind to local tcp port if 0. - allow customization of port ranges for automatic local bindings. - corrected various typos, spelling errors, etc.. Thanks to Marc Boucher for many of these changes.
2003-02-06 22:18:56 +00:00
LWIP_ASSERT("memp sanity", memp_sanity());
#endif /* MEMP_SANITY_CHECK */
2002-10-19 12:59:30 +00:00
SYS_ARCH_UNPROTECT(old_level);
2002-10-19 12:59:30 +00:00
}