EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Tue Jul 27 15:59:42 2010 +0000
Revision:
5:dd63a1e02b1b
Parent:
0:632c9925f013

        

Who changed what in which revision?

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