Official mbed lwIP library (version 1.4.0)

Dependents:   LwIPNetworking NetServicesMin EthernetInterface EthernetInterface_RSF ... more

Legacy Networking Libraries

This is an mbed 2 networking library. For mbed OS 5, lwip has been integrated with built-in networking interfaces. The networking libraries have been revised to better support additional network stacks and thread safety here.

This library is based on the code of lwIP v1.4.0

Copyright (c) 2001, 2002 Swedish Institute of Computer Science.
All rights reserved. 

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
Committer:
mbed_official
Date:
Fri Jun 22 09:25:39 2012 +0000
Revision:
0:51ac1d130fd4
Child:
2:fcd6ac34b3f8
Initial import from lwip-1.4.0: http://download.savannah.gnu.org/releases/lwip/lwip-1.4.0.zip

Who changed what in which revision?

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