123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- /**
- * @file
- * Dynamic pool memory manager
- *
- * lwIP has dedicated pools for many structures (netconn, protocol control blocks,
- * packet buffers, ...). All these pools are managed here.
- */
- /*
- * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
- * 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"
- #include "lwip/memp.h"
- #include "lwip/pbuf.h"
- #include "lwip/udp.h"
- #include "lwip/raw.h"
- #include "lwip/tcp.h"
- #include "lwip/igmp.h"
- #include "lwip/api.h"
- #include "lwip/api_msg.h"
- #include "lwip/tcpip.h"
- #include "lwip/sys.h"
- #include "lwip/stats.h"
- #include "netif/etharp.h"
- #include "lwip/ip_frag.h"
- #include <string.h>
- #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
- struct memp {
- struct memp *next;
- #if MEMP_OVERFLOW_CHECK
- const char *file;
- int line;
- #endif /* MEMP_OVERFLOW_CHECK */
- };
- #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 LWIP_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 LWIP_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 (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)
- #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) (LWIP_MEM_ALIGN_SIZE(x))
- #endif /* MEMP_OVERFLOW_CHECK */
- /** This array holds the first free element of each pool.
- * Elements form a linked list. */
- static struct memp *memp_tab[MEMP_MAX];
- #else /* MEMP_MEM_MALLOC */
- #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
- #endif /* MEMP_MEM_MALLOC */
- /** This array holds the element sizes of each pool. */
- #if !MEM_USE_POOLS && !MEMP_MEM_MALLOC
- static
- #endif
- const u16_t memp_sizes[MEMP_MAX] = {
- #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEM_ALIGN_SIZE(size),
- #include "lwip/memp_std.h"
- };
- /** This array holds a textual description of each pool. */
- #ifdef LWIP_DEBUG
- static const char *memp_desc[MEMP_MAX] = {
- #define LWIP_MEMPOOL(name,num,size,desc) (desc),
- #include "lwip/memp_std.h"
- };
- #endif /* LWIP_DEBUG */
- #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
- /** This array holds the number of elements in each pool. */
- static const u16_t memp_num[MEMP_MAX] = {
- #define LWIP_MEMPOOL(name,num,size,desc) (num),
- #include "lwip/memp_std.h"
- };
- /** This is the actual memory used by the pools. */
- static u8_t memp_memory[MEM_ALIGNMENT - 1
- #define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
- #include "lwip/memp_std.h"
- ];
- #if MEMP_SANITY_CHECK
- /**
- * Check that memp-lists don't form a circle
- */
- static int
- memp_sanity(void)
- {
- s16_t i, c;
- struct memp *m, *n;
- for (i = 0; i < MEMP_MAX; i++) {
- for (m = memp_tab[i]; m != NULL; m = m->next) {
- c = 1;
- for (n = memp_tab[i]; n != NULL; n = n->next) {
- if (n == m && --c < 0) {
- return 0;
- }
- }
- }
- }
- return 1;
- }
- #endif /* MEMP_SANITY_CHECK*/
- #if MEMP_OVERFLOW_CHECK
- /**
- * 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
- */
- static void
- memp_overflow_check_element(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;
- for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
- if (m[k] != 0xcd) {
- LWIP_ASSERT("detected memp overflow!", 0);
- }
- }
- #endif
- }
- /**
- * Do an overflow check for all elements in every pool.
- *
- * @see memp_overflow_check_element for a description of the check
- */
- static void
- memp_overflow_check_all(void)
- {
- u16_t i, j;
- struct memp *p;
- p = LWIP_MEM_ALIGN(memp_memory);
- for (i = 0; i < MEMP_MAX; ++i) {
- p = p;
- for (j = 0; j < memp_num[i]; ++j) {
- memp_overflow_check_element(p, memp_sizes[i]);
- p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
- }
- }
- }
- /**
- * Initialize the restricted areas of all memp elements in every pool.
- */
- static void
- memp_overflow_init(void)
- {
- u16_t i, j;
- struct memp *p;
- u8_t *m;
- p = LWIP_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];
- memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
- #endif
- p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
- }
- }
- }
- #endif /* MEMP_OVERFLOW_CHECK */
- /**
- * Initialize this module.
- *
- * Carves out memp_memory into linked lists for each pool-type.
- */
- void
- memp_init(void)
- {
- struct memp *memp;
- u16_t i, j;
- for (i = 0; i < MEMP_MAX; ++i) {
- MEMP_STATS_AVAIL(used, i, 0);
- MEMP_STATS_AVAIL(max, i, 0);
- MEMP_STATS_AVAIL(err, i, 0);
- MEMP_STATS_AVAIL(avail, i, memp_num[i]);
- }
- memp = LWIP_MEM_ALIGN(memp_memory);
- /* for every pool: */
- for (i = 0; i < MEMP_MAX; ++i) {
- memp_tab[i] = NULL;
- /* create a linked list of memp elements */
- for (j = 0; j < memp_num[i]; ++j) {
- memp->next = memp_tab[i];
- memp_tab[i] = memp;
- memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]
- #if MEMP_OVERFLOW_CHECK
- + MEMP_SANITY_REGION_AFTER_ALIGNED
- #endif
- );
- }
- }
- #if MEMP_OVERFLOW_CHECK
- memp_overflow_init();
- /* check everything a first time to see if it worked */
- memp_overflow_check_all();
- #endif /* MEMP_OVERFLOW_CHECK */
- }
- #else
- /* fix time-wait tcp pcb issue */
- static rt_uint16_t tcp_pcbs = 0;
- #endif /* MEMP_MEM_MALLOC */
- /**
- * Get an element from a specific pool.
- *
- * @param type the pool to get an element from
- *
- * the debug version has two more parameters:
- * @param file file name calling this function
- * @param line number of line where this function is called
- *
- * @return a pointer to the allocated memory or a NULL pointer on error
- */
- void *
- #if !MEMP_OVERFLOW_CHECK
- memp_malloc(memp_t type)
- #else
- memp_malloc_fn(memp_t type, const char* file, const int line)
- #endif
- {
- #if !MEMP_MEM_MALLOC
- struct memp *memp;
- SYS_ARCH_DECL_PROTECT(old_level);
-
- LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
- SYS_ARCH_PROTECT(old_level);
- #if MEMP_OVERFLOW_CHECK >= 2
- memp_overflow_check_all();
- #endif /* MEMP_OVERFLOW_CHECK >= 2 */
- memp = memp_tab[type];
-
- if (memp != NULL) {
- memp_tab[type] = memp->next;
- #if MEMP_OVERFLOW_CHECK
- memp->next = NULL;
- memp->file = file;
- memp->line = line;
- #endif /* MEMP_OVERFLOW_CHECK */
- MEMP_STATS_INC_USED(used, type);
- LWIP_ASSERT("memp_malloc: memp properly aligned",
- ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
- memp = (struct memp*)((u8_t*)memp + MEMP_SIZE);
- } else {
- LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
- MEMP_STATS_INC(err, type);
- }
- SYS_ARCH_UNPROTECT(old_level);
- return memp;
- #else
- void* ptr;
- rt_uint32_t size;
- SYS_ARCH_DECL_PROTECT(old_level);
- size = memp_sizes[type];
- LWIP_DEBUGF(MEMP_DEBUG, ("memp malloc %s, size %d, ", memp_desc[type], memp_sizes[type]));
- SYS_ARCH_PROTECT(old_level);
- if (type == MEMP_TCP_PCB)
- {
- if (tcp_pcbs >= MEMP_NUM_TCP_PCB)
- {
- SYS_ARCH_UNPROTECT(old_level);
- return NULL;
- }
- else
- {
- /* increased tcp pcb allocated number */
- tcp_pcbs ++;
- }
- }
- SYS_ARCH_UNPROTECT(old_level);
- ptr = mem_malloc(size);
- LWIP_DEBUGF(MEMP_DEBUG, ("mem 0x%x\n", ptr));
- return ptr;
- #endif /* MEMP_MEM_MALLOC */
- }
- /**
- * 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)
- {
- #if !MEMP_MEM_MALLOC
- struct memp *memp;
- SYS_ARCH_DECL_PROTECT(old_level);
- if (mem == NULL) {
- return;
- }
- LWIP_ASSERT("memp_free: mem properly aligned",
- ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
- memp = (struct memp *)((u8_t*)mem - MEMP_SIZE);
- SYS_ARCH_PROTECT(old_level);
- #if MEMP_OVERFLOW_CHECK
- #if MEMP_OVERFLOW_CHECK >= 2
- memp_overflow_check_all();
- #else
- memp_overflow_check_element(memp, memp_sizes[type]);
- #endif /* MEMP_OVERFLOW_CHECK >= 2 */
- #endif /* MEMP_OVERFLOW_CHECK */
- MEMP_STATS_DEC(used, type);
-
- memp->next = memp_tab[type];
- memp_tab[type] = memp;
- #if MEMP_SANITY_CHECK
- LWIP_ASSERT("memp sanity", memp_sanity());
- #endif /* MEMP_SANITY_CHECK */
- SYS_ARCH_UNPROTECT(old_level);
- #else
- SYS_ARCH_DECL_PROTECT(old_level);
- SYS_ARCH_PROTECT(old_level);
- if (type == MEMP_TCP_PCB && mem)
- {
- tcp_pcbs --;
- }
- SYS_ARCH_UNPROTECT(old_level);
- LWIP_DEBUGF(MEMP_DEBUG, ("memp free %s, mem 0x%x\n", memp_desc[type], mem));
- /* release this memory */
- mem_free(mem);
- #endif /* MEMP_MEM_MALLOC */
- }
|