HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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