Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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