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 #if defined (__cplusplus) && __cplusplus 44 extern "C" { 45 #endif 46 47 #include "lwip/mem.h" 48 #include "lwip/priv/mem_priv.h" 49 50 #if MEMP_OVERFLOW_CHECK 51 52 /* MEMP_SIZE: save space for struct memp and for sanity check */ 53 #define MEMP_SIZE (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEM_SANITY_REGION_BEFORE_ALIGNED) 54 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEM_SANITY_REGION_AFTER_ALIGNED) 55 56 #else /* MEMP_OVERFLOW_CHECK */ 57 /* 58 * No sanity checks 59 * We don't need to preserve the struct memp while not allocated, so we 60 * can save a little space and set MEMP_SIZE to 0. 61 */ 62 #define MEMP_SIZE 0 63 #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x)) 64 65 #endif /* MEMP_OVERFLOW_CHECK */ 66 67 #if !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK 68 struct memp { 69 struct memp *next; 70 #if MEMP_OVERFLOW_CHECK 71 const char *file; 72 int line; 73 #endif /* MEMP_OVERFLOW_CHECK */ 74 }; 75 #endif /* !MEMP_MEM_MALLOC || MEMP_OVERFLOW_CHECK */ 76 77 #if MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS 78 /* Use a helper type to get the start and end of the user "memory pools" for mem_malloc */ 79 typedef enum { 80 /* Get the first (via: MEMP_POOL_HELPER_START = ((u8_t) 1*MEMP_POOL_A + 0*MEMP_POOL_B + 0*MEMP_POOL_C + 0) */ 81 MEMP_POOL_HELPER_FIRST = ((u8_t) 82 #define LWIP_MEMPOOL(name, num, size, desc) 83 #define LWIP_MALLOC_MEMPOOL_START 1 84 #define LWIP_MALLOC_MEMPOOL(num, size) * MEMP_POOL_##size + 0 85 #define LWIP_MALLOC_MEMPOOL_END 86 #include "lwip/priv/memp_std.h" 87 ), 88 /* Get the last (via: 89 MEMP_POOL_HELPER_END = ((u8_t) 0 + MEMP_POOL_A*0 + MEMP_POOL_B*0 + MEMP_POOL_C*1) */ 90 MEMP_POOL_HELPER_LAST = ((u8_t) 91 #define LWIP_MEMPOOL(name, num, size, desc) 92 #define LWIP_MALLOC_MEMPOOL_START 93 #define LWIP_MALLOC_MEMPOOL(num, size) 0 + MEMP_POOL_##size * 94 #define LWIP_MALLOC_MEMPOOL_END 1 95 #include "lwip/priv/memp_std.h" 96 ) 97 } memp_pool_helper_t; 98 99 /* The actual start and stop values are here (cast them over) 100 We use this helper type and these defines so we can avoid using const memp_t values */ 101 #define MEMP_POOL_FIRST ((memp_t) MEMP_POOL_HELPER_FIRST) 102 #define MEMP_POOL_LAST ((memp_t) MEMP_POOL_HELPER_LAST) 103 #endif /* MEM_USE_POOLS && MEMP_USE_CUSTOM_POOLS */ 104 105 /** Memory pool descriptor */ 106 struct memp_desc { 107 #if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY 108 /** Textual description */ 109 const char *desc; 110 #endif /* LWIP_DEBUG || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY */ 111 #if MEMP_STATS 112 /** Statistics */ 113 struct stats_mem *stats; 114 #endif 115 116 /** Element size */ 117 u16_t size; 118 119 #if !MEMP_MEM_MALLOC 120 /** Number of elements */ 121 u16_t num; 122 123 /** Base address */ 124 u8_t *base; 125 126 /** First free element of each pool. Elements form a linked list. */ 127 struct memp **tab; 128 #if LWIP_ALLOW_SOCKET_CONFIG 129 struct memp *tab_tail; 130 #endif 131 #endif /* MEMP_MEM_MALLOC */ 132 }; 133 134 #if defined(LWIP_DEBUG) || MEMP_OVERFLOW_CHECK || LWIP_STATS_DISPLAY 135 #define DECLARE_LWIP_MEMPOOL_DESC(desc) (desc), 136 #else 137 #define DECLARE_LWIP_MEMPOOL_DESC(desc) 138 #endif 139 140 #if MEMP_STATS 141 #define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name) static struct stats_mem name; 142 #define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name) &name, 143 #else 144 #define LWIP_MEMPOOL_DECLARE_STATS_INSTANCE(name) 145 #define LWIP_MEMPOOL_DECLARE_STATS_REFERENCE(name) 146 #endif 147 148 #if LWIP_ALLOW_SOCKET_CONFIG 149 void memp_init_pool(const char *szdesc, u32_t num, u16_t size, u8_t type); 150 #else 151 void memp_init_pool(const struct memp_desc *desc); 152 #endif 153 154 #if MEMP_OVERFLOW_CHECK 155 void *memp_malloc_pool_fn(const struct memp_desc *desc, const char *file, const int line); 156 #define memp_malloc_pool(d) memp_malloc_pool_fn((d), __FILE__, __LINE__) 157 #else 158 void *memp_malloc_pool(const struct memp_desc *desc); 159 #endif 160 void memp_free_pool(const struct memp_desc *desc, void *mem); 161 162 #if defined (__cplusplus) && __cplusplus 163 } 164 #endif 165 166 #endif /* LWIP_HDR_MEMP_PRIV_H */ 167