Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
dcoban
Date:
Tue Apr 03 18:43:13 2012 +0000
Revision:
0:1a61c61d0845

        

Who changed what in which revision?

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