1/** 2 * @file 3 * memory pools lwIP internal implementations (do not use in application code) 4 */ 5 6/* 7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without modification, 11 * are permitted provided that the following conditions are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 3. The name of the author may not be used to endorse or promote products 19 * derived from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30 * OF SUCH DAMAGE. 31 * 32 * This file is part of the lwIP TCP/IP stack. 33 * 34 * Author: Adam Dunkels <adam@sics.se> 35 * 36 */ 37 38#ifndef LWIP_HDR_MEMP_PRIV_H 39#define LWIP_HDR_MEMP_PRIV_H 40 41#include "lwip/opt.h" 42 43#ifdef __cplusplus 44extern "C" { 45#endif 46 47#include "lwip/mem.h" 48 49#if MEMP_OVERFLOW_CHECK 50/* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning 51 * and at the end of each element, initialize them as 0xcd and check 52 * them later. */ 53/* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free, 54 * every single element in each pool is checked! 55 * This is VERY SLOW but also very helpful. */ 56/* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in 57 * lwipopts.h to change the amount reserved for checking. */ 58#ifndef MEMP_SANITY_REGION_BEFORE 59#define MEMP_SANITY_REGION_BEFORE 16 60#endif /* MEMP_SANITY_REGION_BEFORE*/ 61#if MEMP_SANITY_REGION_BEFORE > 0 62#define MEMP_SANITY_REGION_BEFORE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE) 63#else 64#define MEMP_SANITY_REGION_BEFORE_ALIGNED 0 65#endif /* MEMP_SANITY_REGION_BEFORE*/ 66#ifndef MEMP_SANITY_REGION_AFTER 67#define MEMP_SANITY_REGION_AFTER 16 68#endif /* MEMP_SANITY_REGION_AFTER*/ 69#if MEMP_SANITY_REGION_AFTER > 0 70#define MEMP_SANITY_REGION_AFTER_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER) 71#else 72#define MEMP_SANITY_REGION_AFTER_ALIGNED 0 73#endif /* MEMP_SANITY_REGION_AFTER*/ 74 75/* MEMP_SIZE: save space for struct memp and for sanity check */ 76#define MEMP_SIZE (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED) 77#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED) 78 79#else /* MEMP_OVERFLOW_CHECK */ 80 81/* No sanity checks 82 * We don't need to preserve the struct memp while not allocated, so we 83 * can save a little space and set MEMP_SIZE to 0. 84 */ 85#define MEMP_SIZE 0 86#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x)) 87 88#endif /* MEMP_OVERFLOW_CHECK */ 89 90#if !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK 91struct memp { 92 struct memp *next; 93#if MEMP_OVERFLOW_CHECK 94 const char *file; 95 int line; 96#endif /* MEMP_OVERFLOW_CHECK */ 97}; 98#endif /* !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK */ 99 100#if MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS 101/* Use a helper type to get the start and end of the user "memory pools" for mem_malloc */ 102typedef enum { 103 /* Get the first (via: 104 MEMP_POOL_HELPER_START = ((u8_t) 1*MEMP_POOL_A + 0*MEMP_POOL_B + 0*MEMP_POOL_C + 0)*/ 105 MEMP_POOL_HELPER_FIRST = ((u8_t) 106#define LWIP_MEMPOOL(name,num,size,desc) 107#define LWIP_MALLOC_MEMPOOL_START 1 108#define LWIP_MALLOC_MEMPOOL(num, size) * MEMP_POOL_##size + 0 109#define LWIP_MALLOC_MEMPOOL_END 110#include "lwip/priv/memp_std.h" 111 ) , 112 /* Get the last (via: 113 MEMP_POOL_HELPER_END = ((u8_t) 0 + MEMP_POOL_A*0 + MEMP_POOL_B*0 + MEMP_POOL_C*1) */ 114 MEMP_POOL_HELPER_LAST = ((u8_t) 115#define LWIP_MEMPOOL(name,num,size,desc) 116#define LWIP_MALLOC_MEMPOOL_START 117#define LWIP_MALLOC_MEMPOOL(num, size) 0 + MEMP_POOL_##size * 118#define LWIP_MALLOC_MEMPOOL_END 1 119#include "lwip/priv/memp_std.h" 120 ) 121} memp_pool_helper_t; 122 123/* The actual start and stop values are here (cast them over) 124 We use this helper type and these defines so we can avoid using const memp_t values */ 125#define MEMP_POOL_FIRST ((memp_t) MEMP_POOL_HELPER_FIRST) 126#define MEMP_POOL_LAST ((memp_t) MEMP_POOL_HELPER_LAST) 127#endif /* MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS */ 128 129/** Memory pool descriptor */ 130struct memp_desc { 131#if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY 132 /** Textual description */ 133 const char *desc; 134#endif /* LWIP_DEBUG || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY */ 135#if MEMP_STATS 136 /** Statistics */ 137 struct stats_mem *stats; 138#endif 139 140 /** Element size */ 141 u16_t size; 142 143#if !MEMP_MEM_MALLOC 144 /** Number of elements */ 145 u16_t num; 146 147 /** Base address */ 148 u8_t *base; 149 150 /** First free element of each pool. Elements form a linked list. */ 151 struct memp **tab; 152#endif /* MEMP_MEM_MALLOC */ 153}; 154 155#if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY 156#define DECLARE_LWIP_MEMPOOL_DESC(desc) (desc), 157#else 158#define DECLARE_LWIP_MEMPOOL_DESC(desc) 159#endif 160 161#if MEMP_STATS 162#define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name) static struct stats_mem name; 163#define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name) &name, 164#else 165#define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name) 166#define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name) 167#endif 168 169void memp_init_pool(const struct memp_desc *desc); 170 171#if MEMP_OVERFLOW_CHECK 172void *memp_malloc_pool_fn(const struct memp_desc* desc, const char* file, const int line); 173#define memp_malloc_pool(d) memp_malloc_pool_fn((d), __FILE__, __LINE__) 174#else 175void *memp_malloc_pool(const struct memp_desc *desc); 176#endif 177void memp_free_pool(const struct memp_desc* desc, void *mem); 178 179#ifdef __cplusplus 180} 181#endif 182 183#endif /* LWIP_HDR_MEMP_PRIV_H */ 184