Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

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