Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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