Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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