A modified version of http://mbed.org/users/segundo/libraries/NetServices/ljhqix to add HTTP proxy feature (based on http://mbed.org/users/igorsk/programs/NetServicesSource/ltjpag)

Dependents:   SenseClient

Committer:
mimil
Date:
Tue Sep 06 13:26:45 2011 +0000
Revision:
0:308f83189a3f

        

Who changed what in which revision?

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