Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

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