ZG2100 Network interface source
lwip/core/memp.c@0:b802fc31f1db, 2010-07-09 (annotated)
- Committer:
- donatien
- Date:
- Fri Jul 09 15:37:23 2010 +0000
- Revision:
- 0:b802fc31f1db
- Child:
- 1:3a7c15057192
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:b802fc31f1db | 1 | /** |
donatien | 0:b802fc31f1db | 2 | * @file |
donatien | 0:b802fc31f1db | 3 | * Dynamic pool memory manager |
donatien | 0:b802fc31f1db | 4 | * |
donatien | 0:b802fc31f1db | 5 | * lwIP has dedicated pools for many structures (netconn, protocol control blocks, |
donatien | 0:b802fc31f1db | 6 | * packet buffers, ...). All these pools are managed here. |
donatien | 0:b802fc31f1db | 7 | */ |
donatien | 0:b802fc31f1db | 8 | |
donatien | 0:b802fc31f1db | 9 | /* |
donatien | 0:b802fc31f1db | 10 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
donatien | 0:b802fc31f1db | 11 | * All rights reserved. |
donatien | 0:b802fc31f1db | 12 | * |
donatien | 0:b802fc31f1db | 13 | * Redistribution and use in source and binary forms, with or without modification, |
donatien | 0:b802fc31f1db | 14 | * are permitted provided that the following conditions are met: |
donatien | 0:b802fc31f1db | 15 | * |
donatien | 0:b802fc31f1db | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
donatien | 0:b802fc31f1db | 17 | * this list of conditions and the following disclaimer. |
donatien | 0:b802fc31f1db | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
donatien | 0:b802fc31f1db | 19 | * this list of conditions and the following disclaimer in the documentation |
donatien | 0:b802fc31f1db | 20 | * and/or other materials provided with the distribution. |
donatien | 0:b802fc31f1db | 21 | * 3. The name of the author may not be used to endorse or promote products |
donatien | 0:b802fc31f1db | 22 | * derived from this software without specific prior written permission. |
donatien | 0:b802fc31f1db | 23 | * |
donatien | 0:b802fc31f1db | 24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
donatien | 0:b802fc31f1db | 25 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
donatien | 0:b802fc31f1db | 26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
donatien | 0:b802fc31f1db | 27 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
donatien | 0:b802fc31f1db | 28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
donatien | 0:b802fc31f1db | 29 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
donatien | 0:b802fc31f1db | 30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
donatien | 0:b802fc31f1db | 31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
donatien | 0:b802fc31f1db | 32 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
donatien | 0:b802fc31f1db | 33 | * OF SUCH DAMAGE. |
donatien | 0:b802fc31f1db | 34 | * |
donatien | 0:b802fc31f1db | 35 | * This file is part of the lwIP TCP/IP stack. |
donatien | 0:b802fc31f1db | 36 | * |
donatien | 0:b802fc31f1db | 37 | * Author: Adam Dunkels <adam@sics.se> |
donatien | 0:b802fc31f1db | 38 | * |
donatien | 0:b802fc31f1db | 39 | */ |
donatien | 0:b802fc31f1db | 40 | |
donatien | 0:b802fc31f1db | 41 | #include "lwip/opt.h" |
donatien | 0:b802fc31f1db | 42 | |
donatien | 0:b802fc31f1db | 43 | #include "lwip/memp.h" |
donatien | 0:b802fc31f1db | 44 | #include "lwip/pbuf.h" |
donatien | 0:b802fc31f1db | 45 | #include "lwip/udp.h" |
donatien | 0:b802fc31f1db | 46 | #include "lwip/raw.h" |
donatien | 0:b802fc31f1db | 47 | #include "lwip/tcp_impl.h" |
donatien | 0:b802fc31f1db | 48 | #include "lwip/igmp.h" |
donatien | 0:b802fc31f1db | 49 | #include "lwip/api.h" |
donatien | 0:b802fc31f1db | 50 | #include "lwip/api_msg.h" |
donatien | 0:b802fc31f1db | 51 | #include "lwip/tcpip.h" |
donatien | 0:b802fc31f1db | 52 | #include "lwip/sys.h" |
donatien | 0:b802fc31f1db | 53 | #include "lwip/timers.h" |
donatien | 0:b802fc31f1db | 54 | #include "lwip/stats.h" |
donatien | 0:b802fc31f1db | 55 | #include "netif/etharp.h" |
donatien | 0:b802fc31f1db | 56 | #include "lwip/ip_frag.h" |
donatien | 0:b802fc31f1db | 57 | #include "lwip/snmp_structs.h" |
donatien | 0:b802fc31f1db | 58 | #include "lwip/snmp_msg.h" |
donatien | 0:b802fc31f1db | 59 | #include "lwip/dns.h" |
donatien | 0:b802fc31f1db | 60 | |
donatien | 0:b802fc31f1db | 61 | #include <string.h> |
donatien | 0:b802fc31f1db | 62 | |
donatien | 0:b802fc31f1db | 63 | #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */ |
donatien | 0:b802fc31f1db | 64 | |
donatien | 0:b802fc31f1db | 65 | struct memp { |
donatien | 0:b802fc31f1db | 66 | struct memp *next; |
donatien | 0:b802fc31f1db | 67 | #if MEMP_OVERFLOW_CHECK |
donatien | 0:b802fc31f1db | 68 | const char *file; |
donatien | 0:b802fc31f1db | 69 | int line; |
donatien | 0:b802fc31f1db | 70 | #endif /* MEMP_OVERFLOW_CHECK */ |
donatien | 0:b802fc31f1db | 71 | }; |
donatien | 0:b802fc31f1db | 72 | |
donatien | 0:b802fc31f1db | 73 | #if MEMP_OVERFLOW_CHECK |
donatien | 0:b802fc31f1db | 74 | /* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning |
donatien | 0:b802fc31f1db | 75 | * and at the end of each element, initialize them as 0xcd and check |
donatien | 0:b802fc31f1db | 76 | * them later. */ |
donatien | 0:b802fc31f1db | 77 | /* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free, |
donatien | 0:b802fc31f1db | 78 | * every single element in each pool is checked! |
donatien | 0:b802fc31f1db | 79 | * This is VERY SLOW but also very helpful. */ |
donatien | 0:b802fc31f1db | 80 | /* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in |
donatien | 0:b802fc31f1db | 81 | * lwipopts.h to change the amount reserved for checking. */ |
donatien | 0:b802fc31f1db | 82 | #ifndef MEMP_SANITY_REGION_BEFORE |
donatien | 0:b802fc31f1db | 83 | #define MEMP_SANITY_REGION_BEFORE 16 |
donatien | 0:b802fc31f1db | 84 | #endif /* MEMP_SANITY_REGION_BEFORE*/ |
donatien | 0:b802fc31f1db | 85 | #if MEMP_SANITY_REGION_BEFORE > 0 |
donatien | 0:b802fc31f1db | 86 | #define MEMP_SANITY_REGION_BEFORE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE) |
donatien | 0:b802fc31f1db | 87 | #else |
donatien | 0:b802fc31f1db | 88 | #define MEMP_SANITY_REGION_BEFORE_ALIGNED 0 |
donatien | 0:b802fc31f1db | 89 | #endif /* MEMP_SANITY_REGION_BEFORE*/ |
donatien | 0:b802fc31f1db | 90 | #ifndef MEMP_SANITY_REGION_AFTER |
donatien | 0:b802fc31f1db | 91 | #define MEMP_SANITY_REGION_AFTER 16 |
donatien | 0:b802fc31f1db | 92 | #endif /* MEMP_SANITY_REGION_AFTER*/ |
donatien | 0:b802fc31f1db | 93 | #if MEMP_SANITY_REGION_AFTER > 0 |
donatien | 0:b802fc31f1db | 94 | #define MEMP_SANITY_REGION_AFTER_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER) |
donatien | 0:b802fc31f1db | 95 | #else |
donatien | 0:b802fc31f1db | 96 | #define MEMP_SANITY_REGION_AFTER_ALIGNED 0 |
donatien | 0:b802fc31f1db | 97 | #endif /* MEMP_SANITY_REGION_AFTER*/ |
donatien | 0:b802fc31f1db | 98 | |
donatien | 0:b802fc31f1db | 99 | /* MEMP_SIZE: save space for struct memp and for sanity check */ |
donatien | 0:b802fc31f1db | 100 | #define MEMP_SIZE (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED) |
donatien | 0:b802fc31f1db | 101 | #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED) |
donatien | 0:b802fc31f1db | 102 | |
donatien | 0:b802fc31f1db | 103 | #else /* MEMP_OVERFLOW_CHECK */ |
donatien | 0:b802fc31f1db | 104 | |
donatien | 0:b802fc31f1db | 105 | /* No sanity checks |
donatien | 0:b802fc31f1db | 106 | * We don't need to preserve the struct memp while not allocated, so we |
donatien | 0:b802fc31f1db | 107 | * can save a little space and set MEMP_SIZE to 0. |
donatien | 0:b802fc31f1db | 108 | */ |
donatien | 0:b802fc31f1db | 109 | #define MEMP_SIZE 0 |
donatien | 0:b802fc31f1db | 110 | #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x)) |
donatien | 0:b802fc31f1db | 111 | |
donatien | 0:b802fc31f1db | 112 | #endif /* MEMP_OVERFLOW_CHECK */ |
donatien | 0:b802fc31f1db | 113 | |
donatien | 0:b802fc31f1db | 114 | /** This array holds the first free element of each pool. |
donatien | 0:b802fc31f1db | 115 | * Elements form a linked list. */ |
donatien | 0:b802fc31f1db | 116 | static struct memp *memp_tab[MEMP_MAX] MEM_POSITION; |
donatien | 0:b802fc31f1db | 117 | |
donatien | 0:b802fc31f1db | 118 | #else /* MEMP_MEM_MALLOC */ |
donatien | 0:b802fc31f1db | 119 | |
donatien | 0:b802fc31f1db | 120 | #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x)) |
donatien | 0:b802fc31f1db | 121 | |
donatien | 0:b802fc31f1db | 122 | #endif /* MEMP_MEM_MALLOC */ |
donatien | 0:b802fc31f1db | 123 | |
donatien | 0:b802fc31f1db | 124 | /** This array holds the element sizes of each pool. */ |
donatien | 0:b802fc31f1db | 125 | #if !MEM_USE_POOLS && !MEMP_MEM_MALLOC |
donatien | 0:b802fc31f1db | 126 | static |
donatien | 0:b802fc31f1db | 127 | #endif |
donatien | 0:b802fc31f1db | 128 | const u16_t memp_sizes[MEMP_MAX] = { |
donatien | 0:b802fc31f1db | 129 | #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEM_ALIGN_SIZE(size), |
donatien | 0:b802fc31f1db | 130 | #include "lwip/memp_std.h" |
donatien | 0:b802fc31f1db | 131 | }; |
donatien | 0:b802fc31f1db | 132 | |
donatien | 0:b802fc31f1db | 133 | #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */ |
donatien | 0:b802fc31f1db | 134 | |
donatien | 0:b802fc31f1db | 135 | /** This array holds the number of elements in each pool. */ |
donatien | 0:b802fc31f1db | 136 | static const u16_t memp_num[MEMP_MAX] = { |
donatien | 0:b802fc31f1db | 137 | #define LWIP_MEMPOOL(name,num,size,desc) (num), |
donatien | 0:b802fc31f1db | 138 | #include "lwip/memp_std.h" |
donatien | 0:b802fc31f1db | 139 | }; |
donatien | 0:b802fc31f1db | 140 | |
donatien | 0:b802fc31f1db | 141 | /** This array holds a textual description of each pool. */ |
donatien | 0:b802fc31f1db | 142 | #ifdef LWIP_DEBUG |
donatien | 0:b802fc31f1db | 143 | static const char *memp_desc[MEMP_MAX] = { |
donatien | 0:b802fc31f1db | 144 | #define LWIP_MEMPOOL(name,num,size,desc) (desc), |
donatien | 0:b802fc31f1db | 145 | #include "lwip/memp_std.h" |
donatien | 0:b802fc31f1db | 146 | }; |
donatien | 0:b802fc31f1db | 147 | #endif /* LWIP_DEBUG */ |
donatien | 0:b802fc31f1db | 148 | |
donatien | 0:b802fc31f1db | 149 | #if MEMP_SEPARATE_POOLS |
donatien | 0:b802fc31f1db | 150 | |
donatien | 0:b802fc31f1db | 151 | /** This creates each memory pool. These are named memp_memory_XXX_base (where |
donatien | 0:b802fc31f1db | 152 | * XXX is the name of the pool defined in memp_std.h). |
donatien | 0:b802fc31f1db | 153 | * To relocate a pool, declare it as extern in cc.h. Example for GCC: |
donatien | 0:b802fc31f1db | 154 | * extern u8_t __attribute__((section(".onchip_mem"))) memp_memory_UDP_PCB_base[]; |
donatien | 0:b802fc31f1db | 155 | */ |
donatien | 0:b802fc31f1db | 156 | #define LWIP_MEMPOOL(name,num,size,desc) u8_t memp_memory_ ## name ## _base \ |
donatien | 0:b802fc31f1db | 157 | [((num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size)))]; |
donatien | 0:b802fc31f1db | 158 | #include "lwip/memp_std.h" |
donatien | 0:b802fc31f1db | 159 | |
donatien | 0:b802fc31f1db | 160 | /** This array holds the base of each memory pool. */ |
donatien | 0:b802fc31f1db | 161 | static u8_t *const memp_bases[] = { |
donatien | 0:b802fc31f1db | 162 | #define LWIP_MEMPOOL(name,num,size,desc) memp_memory_ ## name ## _base, |
donatien | 0:b802fc31f1db | 163 | #include "lwip/memp_std.h" |
donatien | 0:b802fc31f1db | 164 | } MEM_POSITION; |
donatien | 0:b802fc31f1db | 165 | |
donatien | 0:b802fc31f1db | 166 | #else /* MEMP_SEPARATE_POOLS */ |
donatien | 0:b802fc31f1db | 167 | |
donatien | 0:b802fc31f1db | 168 | /** This is the actual memory used by the pools (all pools in one big block). */ |
donatien | 0:b802fc31f1db | 169 | static u8_t memp_memory[MEM_ALIGNMENT - 1 |
donatien | 0:b802fc31f1db | 170 | #define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) ) |
donatien | 0:b802fc31f1db | 171 | #include "lwip/memp_std.h" |
donatien | 0:b802fc31f1db | 172 | ] MEM_POSITION; |
donatien | 0:b802fc31f1db | 173 | |
donatien | 0:b802fc31f1db | 174 | #endif /* MEMP_SEPARATE_POOLS */ |
donatien | 0:b802fc31f1db | 175 | |
donatien | 0:b802fc31f1db | 176 | #if MEMP_SANITY_CHECK |
donatien | 0:b802fc31f1db | 177 | /** |
donatien | 0:b802fc31f1db | 178 | * Check that memp-lists don't form a circle |
donatien | 0:b802fc31f1db | 179 | */ |
donatien | 0:b802fc31f1db | 180 | static int |
donatien | 0:b802fc31f1db | 181 | memp_sanity(void) |
donatien | 0:b802fc31f1db | 182 | { |
donatien | 0:b802fc31f1db | 183 | s16_t i, c; |
donatien | 0:b802fc31f1db | 184 | struct memp *m, *n; |
donatien | 0:b802fc31f1db | 185 | |
donatien | 0:b802fc31f1db | 186 | for (i = 0; i < MEMP_MAX; i++) { |
donatien | 0:b802fc31f1db | 187 | for (m = memp_tab[i]; m != NULL; m = m->next) { |
donatien | 0:b802fc31f1db | 188 | c = 1; |
donatien | 0:b802fc31f1db | 189 | for (n = memp_tab[i]; n != NULL; n = n->next) { |
donatien | 0:b802fc31f1db | 190 | if (n == m && --c < 0) { |
donatien | 0:b802fc31f1db | 191 | return 0; |
donatien | 0:b802fc31f1db | 192 | } |
donatien | 0:b802fc31f1db | 193 | } |
donatien | 0:b802fc31f1db | 194 | } |
donatien | 0:b802fc31f1db | 195 | } |
donatien | 0:b802fc31f1db | 196 | return 1; |
donatien | 0:b802fc31f1db | 197 | } |
donatien | 0:b802fc31f1db | 198 | #endif /* MEMP_SANITY_CHECK*/ |
donatien | 0:b802fc31f1db | 199 | #if MEMP_OVERFLOW_CHECK |
donatien | 0:b802fc31f1db | 200 | /** |
donatien | 0:b802fc31f1db | 201 | * Check if a memp element was victim of an overflow |
donatien | 0:b802fc31f1db | 202 | * (e.g. the restricted area after it has been altered) |
donatien | 0:b802fc31f1db | 203 | * |
donatien | 0:b802fc31f1db | 204 | * @param p the memp element to check |
donatien | 0:b802fc31f1db | 205 | * @param memp_size the element size of the pool p comes from |
donatien | 0:b802fc31f1db | 206 | */ |
donatien | 0:b802fc31f1db | 207 | static void |
donatien | 0:b802fc31f1db | 208 | memp_overflow_check_element(struct memp *p, u16_t memp_size) |
donatien | 0:b802fc31f1db | 209 | { |
donatien | 0:b802fc31f1db | 210 | u16_t k; |
donatien | 0:b802fc31f1db | 211 | u8_t *m; |
donatien | 0:b802fc31f1db | 212 | #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 |
donatien | 0:b802fc31f1db | 213 | m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED; |
donatien | 0:b802fc31f1db | 214 | for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) { |
donatien | 0:b802fc31f1db | 215 | if (m[k] != 0xcd) { |
donatien | 0:b802fc31f1db | 216 | LWIP_ASSERT("detected memp underflow!", 0); |
donatien | 0:b802fc31f1db | 217 | } |
donatien | 0:b802fc31f1db | 218 | } |
donatien | 0:b802fc31f1db | 219 | #endif |
donatien | 0:b802fc31f1db | 220 | #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0 |
donatien | 0:b802fc31f1db | 221 | m = (u8_t*)p + MEMP_SIZE + memp_size; |
donatien | 0:b802fc31f1db | 222 | for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) { |
donatien | 0:b802fc31f1db | 223 | if (m[k] != 0xcd) { |
donatien | 0:b802fc31f1db | 224 | LWIP_ASSERT("detected memp overflow!", 0); |
donatien | 0:b802fc31f1db | 225 | } |
donatien | 0:b802fc31f1db | 226 | } |
donatien | 0:b802fc31f1db | 227 | #endif |
donatien | 0:b802fc31f1db | 228 | } |
donatien | 0:b802fc31f1db | 229 | |
donatien | 0:b802fc31f1db | 230 | /** |
donatien | 0:b802fc31f1db | 231 | * Do an overflow check for all elements in every pool. |
donatien | 0:b802fc31f1db | 232 | * |
donatien | 0:b802fc31f1db | 233 | * @see memp_overflow_check_element for a description of the check |
donatien | 0:b802fc31f1db | 234 | */ |
donatien | 0:b802fc31f1db | 235 | static void |
donatien | 0:b802fc31f1db | 236 | memp_overflow_check_all(void) |
donatien | 0:b802fc31f1db | 237 | { |
donatien | 0:b802fc31f1db | 238 | u16_t i, j; |
donatien | 0:b802fc31f1db | 239 | struct memp *p; |
donatien | 0:b802fc31f1db | 240 | |
donatien | 0:b802fc31f1db | 241 | p = LWIP_MEM_ALIGN(memp_memory); |
donatien | 0:b802fc31f1db | 242 | for (i = 0; i < MEMP_MAX; ++i) { |
donatien | 0:b802fc31f1db | 243 | p = p; |
donatien | 0:b802fc31f1db | 244 | for (j = 0; j < memp_num[i]; ++j) { |
donatien | 0:b802fc31f1db | 245 | memp_overflow_check_element(p, memp_sizes[i]); |
donatien | 0:b802fc31f1db | 246 | p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED); |
donatien | 0:b802fc31f1db | 247 | } |
donatien | 0:b802fc31f1db | 248 | } |
donatien | 0:b802fc31f1db | 249 | } |
donatien | 0:b802fc31f1db | 250 | |
donatien | 0:b802fc31f1db | 251 | /** |
donatien | 0:b802fc31f1db | 252 | * Initialize the restricted areas of all memp elements in every pool. |
donatien | 0:b802fc31f1db | 253 | */ |
donatien | 0:b802fc31f1db | 254 | static void |
donatien | 0:b802fc31f1db | 255 | memp_overflow_init(void) |
donatien | 0:b802fc31f1db | 256 | { |
donatien | 0:b802fc31f1db | 257 | u16_t i, j; |
donatien | 0:b802fc31f1db | 258 | struct memp *p; |
donatien | 0:b802fc31f1db | 259 | u8_t *m; |
donatien | 0:b802fc31f1db | 260 | |
donatien | 0:b802fc31f1db | 261 | p = LWIP_MEM_ALIGN(memp_memory); |
donatien | 0:b802fc31f1db | 262 | for (i = 0; i < MEMP_MAX; ++i) { |
donatien | 0:b802fc31f1db | 263 | p = p; |
donatien | 0:b802fc31f1db | 264 | for (j = 0; j < memp_num[i]; ++j) { |
donatien | 0:b802fc31f1db | 265 | #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 |
donatien | 0:b802fc31f1db | 266 | m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED; |
donatien | 0:b802fc31f1db | 267 | memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED); |
donatien | 0:b802fc31f1db | 268 | #endif |
donatien | 0:b802fc31f1db | 269 | #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0 |
donatien | 0:b802fc31f1db | 270 | m = (u8_t*)p + MEMP_SIZE + memp_sizes[i]; |
donatien | 0:b802fc31f1db | 271 | memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED); |
donatien | 0:b802fc31f1db | 272 | #endif |
donatien | 0:b802fc31f1db | 273 | p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED); |
donatien | 0:b802fc31f1db | 274 | } |
donatien | 0:b802fc31f1db | 275 | } |
donatien | 0:b802fc31f1db | 276 | } |
donatien | 0:b802fc31f1db | 277 | #endif /* MEMP_OVERFLOW_CHECK */ |
donatien | 0:b802fc31f1db | 278 | |
donatien | 0:b802fc31f1db | 279 | /** |
donatien | 0:b802fc31f1db | 280 | * Initialize this module. |
donatien | 0:b802fc31f1db | 281 | * |
donatien | 0:b802fc31f1db | 282 | * Carves out memp_memory into linked lists for each pool-type. |
donatien | 0:b802fc31f1db | 283 | */ |
donatien | 0:b802fc31f1db | 284 | void |
donatien | 0:b802fc31f1db | 285 | memp_init(void) |
donatien | 0:b802fc31f1db | 286 | { |
donatien | 0:b802fc31f1db | 287 | struct memp *memp; |
donatien | 0:b802fc31f1db | 288 | u16_t i, j; |
donatien | 0:b802fc31f1db | 289 | |
donatien | 0:b802fc31f1db | 290 | for (i = 0; i < MEMP_MAX; ++i) { |
donatien | 0:b802fc31f1db | 291 | MEMP_STATS_AVAIL(used, i, 0); |
donatien | 0:b802fc31f1db | 292 | MEMP_STATS_AVAIL(max, i, 0); |
donatien | 0:b802fc31f1db | 293 | MEMP_STATS_AVAIL(err, i, 0); |
donatien | 0:b802fc31f1db | 294 | MEMP_STATS_AVAIL(avail, i, memp_num[i]); |
donatien | 0:b802fc31f1db | 295 | } |
donatien | 0:b802fc31f1db | 296 | |
donatien | 0:b802fc31f1db | 297 | #if !MEMP_SEPARATE_POOLS |
donatien | 0:b802fc31f1db | 298 | memp = (struct memp *)LWIP_MEM_ALIGN(memp_memory); |
donatien | 0:b802fc31f1db | 299 | #endif /* !MEMP_SEPARATE_POOLS */ |
donatien | 0:b802fc31f1db | 300 | /* for every pool: */ |
donatien | 0:b802fc31f1db | 301 | for (i = 0; i < MEMP_MAX; ++i) { |
donatien | 0:b802fc31f1db | 302 | memp_tab[i] = NULL; |
donatien | 0:b802fc31f1db | 303 | #if MEMP_SEPARATE_POOLS |
donatien | 0:b802fc31f1db | 304 | memp = (struct memp*)memp_bases[i]; |
donatien | 0:b802fc31f1db | 305 | #endif /* MEMP_SEPARATE_POOLS */ |
donatien | 0:b802fc31f1db | 306 | /* create a linked list of memp elements */ |
donatien | 0:b802fc31f1db | 307 | for (j = 0; j < memp_num[i]; ++j) { |
donatien | 0:b802fc31f1db | 308 | memp->next = memp_tab[i]; |
donatien | 0:b802fc31f1db | 309 | memp_tab[i] = memp; |
donatien | 0:b802fc31f1db | 310 | memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i] |
donatien | 0:b802fc31f1db | 311 | #if MEMP_OVERFLOW_CHECK |
donatien | 0:b802fc31f1db | 312 | + MEMP_SANITY_REGION_AFTER_ALIGNED |
donatien | 0:b802fc31f1db | 313 | #endif |
donatien | 0:b802fc31f1db | 314 | ); |
donatien | 0:b802fc31f1db | 315 | } |
donatien | 0:b802fc31f1db | 316 | } |
donatien | 0:b802fc31f1db | 317 | #if MEMP_OVERFLOW_CHECK |
donatien | 0:b802fc31f1db | 318 | memp_overflow_init(); |
donatien | 0:b802fc31f1db | 319 | /* check everything a first time to see if it worked */ |
donatien | 0:b802fc31f1db | 320 | memp_overflow_check_all(); |
donatien | 0:b802fc31f1db | 321 | #endif /* MEMP_OVERFLOW_CHECK */ |
donatien | 0:b802fc31f1db | 322 | } |
donatien | 0:b802fc31f1db | 323 | |
donatien | 0:b802fc31f1db | 324 | /** |
donatien | 0:b802fc31f1db | 325 | * Get an element from a specific pool. |
donatien | 0:b802fc31f1db | 326 | * |
donatien | 0:b802fc31f1db | 327 | * @param type the pool to get an element from |
donatien | 0:b802fc31f1db | 328 | * |
donatien | 0:b802fc31f1db | 329 | * the debug version has two more parameters: |
donatien | 0:b802fc31f1db | 330 | * @param file file name calling this function |
donatien | 0:b802fc31f1db | 331 | * @param line number of line where this function is called |
donatien | 0:b802fc31f1db | 332 | * |
donatien | 0:b802fc31f1db | 333 | * @return a pointer to the allocated memory or a NULL pointer on error |
donatien | 0:b802fc31f1db | 334 | */ |
donatien | 0:b802fc31f1db | 335 | void * |
donatien | 0:b802fc31f1db | 336 | #if !MEMP_OVERFLOW_CHECK |
donatien | 0:b802fc31f1db | 337 | memp_malloc(memp_t type) |
donatien | 0:b802fc31f1db | 338 | #else |
donatien | 0:b802fc31f1db | 339 | memp_malloc_fn(memp_t type, const char* file, const int line) |
donatien | 0:b802fc31f1db | 340 | #endif |
donatien | 0:b802fc31f1db | 341 | { |
donatien | 0:b802fc31f1db | 342 | struct memp *memp; |
donatien | 0:b802fc31f1db | 343 | SYS_ARCH_DECL_PROTECT(old_level); |
donatien | 0:b802fc31f1db | 344 | |
donatien | 0:b802fc31f1db | 345 | LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;); |
donatien | 0:b802fc31f1db | 346 | |
donatien | 0:b802fc31f1db | 347 | SYS_ARCH_PROTECT(old_level); |
donatien | 0:b802fc31f1db | 348 | #if MEMP_OVERFLOW_CHECK >= 2 |
donatien | 0:b802fc31f1db | 349 | memp_overflow_check_all(); |
donatien | 0:b802fc31f1db | 350 | #endif /* MEMP_OVERFLOW_CHECK >= 2 */ |
donatien | 0:b802fc31f1db | 351 | |
donatien | 0:b802fc31f1db | 352 | memp = memp_tab[type]; |
donatien | 0:b802fc31f1db | 353 | |
donatien | 0:b802fc31f1db | 354 | if (memp != NULL) { |
donatien | 0:b802fc31f1db | 355 | memp_tab[type] = memp->next; |
donatien | 0:b802fc31f1db | 356 | #if MEMP_OVERFLOW_CHECK |
donatien | 0:b802fc31f1db | 357 | memp->next = NULL; |
donatien | 0:b802fc31f1db | 358 | memp->file = file; |
donatien | 0:b802fc31f1db | 359 | memp->line = line; |
donatien | 0:b802fc31f1db | 360 | #endif /* MEMP_OVERFLOW_CHECK */ |
donatien | 0:b802fc31f1db | 361 | MEMP_STATS_INC_USED(used, type); |
donatien | 0:b802fc31f1db | 362 | LWIP_ASSERT("memp_malloc: memp properly aligned", |
donatien | 0:b802fc31f1db | 363 | ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0); |
donatien | 0:b802fc31f1db | 364 | memp = (struct memp*)(void *)((u8_t*)memp + MEMP_SIZE); |
donatien | 0:b802fc31f1db | 365 | } else { |
donatien | 0:b802fc31f1db | 366 | LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type])); |
donatien | 0:b802fc31f1db | 367 | MEMP_STATS_INC(err, type); |
donatien | 0:b802fc31f1db | 368 | } |
donatien | 0:b802fc31f1db | 369 | |
donatien | 0:b802fc31f1db | 370 | SYS_ARCH_UNPROTECT(old_level); |
donatien | 0:b802fc31f1db | 371 | |
donatien | 0:b802fc31f1db | 372 | return memp; |
donatien | 0:b802fc31f1db | 373 | } |
donatien | 0:b802fc31f1db | 374 | |
donatien | 0:b802fc31f1db | 375 | /** |
donatien | 0:b802fc31f1db | 376 | * Put an element back into its pool. |
donatien | 0:b802fc31f1db | 377 | * |
donatien | 0:b802fc31f1db | 378 | * @param type the pool where to put mem |
donatien | 0:b802fc31f1db | 379 | * @param mem the memp element to free |
donatien | 0:b802fc31f1db | 380 | */ |
donatien | 0:b802fc31f1db | 381 | void |
donatien | 0:b802fc31f1db | 382 | memp_free(memp_t type, void *mem) |
donatien | 0:b802fc31f1db | 383 | { |
donatien | 0:b802fc31f1db | 384 | struct memp *memp; |
donatien | 0:b802fc31f1db | 385 | SYS_ARCH_DECL_PROTECT(old_level); |
donatien | 0:b802fc31f1db | 386 | |
donatien | 0:b802fc31f1db | 387 | if (mem == NULL) { |
donatien | 0:b802fc31f1db | 388 | return; |
donatien | 0:b802fc31f1db | 389 | } |
donatien | 0:b802fc31f1db | 390 | LWIP_ASSERT("memp_free: mem properly aligned", |
donatien | 0:b802fc31f1db | 391 | ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0); |
donatien | 0:b802fc31f1db | 392 | |
donatien | 0:b802fc31f1db | 393 | memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE); |
donatien | 0:b802fc31f1db | 394 | |
donatien | 0:b802fc31f1db | 395 | SYS_ARCH_PROTECT(old_level); |
donatien | 0:b802fc31f1db | 396 | #if MEMP_OVERFLOW_CHECK |
donatien | 0:b802fc31f1db | 397 | #if MEMP_OVERFLOW_CHECK >= 2 |
donatien | 0:b802fc31f1db | 398 | memp_overflow_check_all(); |
donatien | 0:b802fc31f1db | 399 | #else |
donatien | 0:b802fc31f1db | 400 | memp_overflow_check_element(memp, memp_sizes[type]); |
donatien | 0:b802fc31f1db | 401 | #endif /* MEMP_OVERFLOW_CHECK >= 2 */ |
donatien | 0:b802fc31f1db | 402 | #endif /* MEMP_OVERFLOW_CHECK */ |
donatien | 0:b802fc31f1db | 403 | |
donatien | 0:b802fc31f1db | 404 | MEMP_STATS_DEC(used, type); |
donatien | 0:b802fc31f1db | 405 | |
donatien | 0:b802fc31f1db | 406 | memp->next = memp_tab[type]; |
donatien | 0:b802fc31f1db | 407 | memp_tab[type] = memp; |
donatien | 0:b802fc31f1db | 408 | |
donatien | 0:b802fc31f1db | 409 | #if MEMP_SANITY_CHECK |
donatien | 0:b802fc31f1db | 410 | LWIP_ASSERT("memp sanity", memp_sanity()); |
donatien | 0:b802fc31f1db | 411 | #endif /* MEMP_SANITY_CHECK */ |
donatien | 0:b802fc31f1db | 412 | |
donatien | 0:b802fc31f1db | 413 | SYS_ARCH_UNPROTECT(old_level); |
donatien | 0:b802fc31f1db | 414 | } |
donatien | 0:b802fc31f1db | 415 | |
donatien | 0:b802fc31f1db | 416 | #endif /* MEMP_MEM_MALLOC */ |