Net stack with AutoIP enabled

Dependencies:   mbed

Committer:
darran
Date:
Fri Jul 02 17:21:58 2010 +0000
Revision:
0:ac21159e27f4

        

Who changed what in which revision?

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