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:
Mon Mar 14 16:15:36 2016 +0000
Revision:
20:08f08bfc3f3d
Parent:
19:1046f8be4d44
Synchronized with git revision fec574a5ed6db26aca1b13992ff271bf527d4a0d

Full URL: https://github.com/mbedmicro/mbed/commit/fec574a5ed6db26aca1b13992ff271bf527d4a0d/

Increased allocated netbufs to handle DTLS handshakes

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
emilmont 10:42a34d63b218 169 #if defined(TARGET_LPC1768)
emilmont 10:42a34d63b218 170 # define ETHMEM_SECTION __attribute((section("AHBSRAM1")))
mbed_official 19:1046f8be4d44 171 #else
emilmont 10:42a34d63b218 172 # define ETHMEM_SECTION
emilmont 10:42a34d63b218 173 #endif
emilmont 10:42a34d63b218 174
mbed_official 0:51ac1d130fd4 175 /** This is the actual memory used by the pools (all pools in one big block). */
mbed_official 0:51ac1d130fd4 176 static u8_t memp_memory[MEM_ALIGNMENT - 1
mbed_official 0:51ac1d130fd4 177 #define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
mbed_official 0:51ac1d130fd4 178 #include "lwip/memp_std.h"
emilmont 10:42a34d63b218 179 ] ETHMEM_SECTION;
mbed_official 0:51ac1d130fd4 180
mbed_official 0:51ac1d130fd4 181 #endif /* MEMP_SEPARATE_POOLS */
mbed_official 0:51ac1d130fd4 182
mbed_official 0:51ac1d130fd4 183 #if MEMP_SANITY_CHECK
mbed_official 0:51ac1d130fd4 184 /**
mbed_official 0:51ac1d130fd4 185 * Check that memp-lists don't form a circle
mbed_official 0:51ac1d130fd4 186 */
mbed_official 0:51ac1d130fd4 187 static int
mbed_official 0:51ac1d130fd4 188 memp_sanity(void)
mbed_official 0:51ac1d130fd4 189 {
mbed_official 0:51ac1d130fd4 190 s16_t i, c;
mbed_official 0:51ac1d130fd4 191 struct memp *m, *n;
mbed_official 0:51ac1d130fd4 192
mbed_official 0:51ac1d130fd4 193 for (i = 0; i < MEMP_MAX; i++) {
mbed_official 0:51ac1d130fd4 194 for (m = memp_tab[i]; m != NULL; m = m->next) {
mbed_official 0:51ac1d130fd4 195 c = 1;
mbed_official 0:51ac1d130fd4 196 for (n = memp_tab[i]; n != NULL; n = n->next) {
mbed_official 0:51ac1d130fd4 197 if (n == m && --c < 0) {
mbed_official 0:51ac1d130fd4 198 return 0;
mbed_official 0:51ac1d130fd4 199 }
mbed_official 0:51ac1d130fd4 200 }
mbed_official 0:51ac1d130fd4 201 }
mbed_official 0:51ac1d130fd4 202 }
mbed_official 0:51ac1d130fd4 203 return 1;
mbed_official 0:51ac1d130fd4 204 }
mbed_official 0:51ac1d130fd4 205 #endif /* MEMP_SANITY_CHECK*/
mbed_official 0:51ac1d130fd4 206 #if MEMP_OVERFLOW_CHECK
mbed_official 0:51ac1d130fd4 207 #if defined(LWIP_DEBUG) && MEMP_STATS
mbed_official 0:51ac1d130fd4 208 static const char * memp_overflow_names[] = {
mbed_official 0:51ac1d130fd4 209 #define LWIP_MEMPOOL(name,num,size,desc) "/"desc,
mbed_official 0:51ac1d130fd4 210 #include "lwip/memp_std.h"
mbed_official 0:51ac1d130fd4 211 };
mbed_official 0:51ac1d130fd4 212 #endif
mbed_official 0:51ac1d130fd4 213
mbed_official 0:51ac1d130fd4 214 /**
mbed_official 0:51ac1d130fd4 215 * Check if a memp element was victim of an overflow
mbed_official 0:51ac1d130fd4 216 * (e.g. the restricted area after it has been altered)
mbed_official 0:51ac1d130fd4 217 *
mbed_official 0:51ac1d130fd4 218 * @param p the memp element to check
mbed_official 0:51ac1d130fd4 219 * @param memp_type the pool p comes from
mbed_official 0:51ac1d130fd4 220 */
mbed_official 0:51ac1d130fd4 221 static void
mbed_official 0:51ac1d130fd4 222 memp_overflow_check_element_overflow(struct memp *p, u16_t memp_type)
mbed_official 0:51ac1d130fd4 223 {
mbed_official 0:51ac1d130fd4 224 u16_t k;
mbed_official 0:51ac1d130fd4 225 u8_t *m;
mbed_official 0:51ac1d130fd4 226 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
mbed_official 0:51ac1d130fd4 227 m = (u8_t*)p + MEMP_SIZE + memp_sizes[memp_type];
mbed_official 0:51ac1d130fd4 228 for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
mbed_official 0:51ac1d130fd4 229 if (m[k] != 0xcd) {
mbed_official 0:51ac1d130fd4 230 char errstr[128] = "detected memp overflow in pool ";
mbed_official 0:51ac1d130fd4 231 char digit[] = "0";
mbed_official 0:51ac1d130fd4 232 if(memp_type >= 10) {
mbed_official 0:51ac1d130fd4 233 digit[0] = '0' + (memp_type/10);
mbed_official 0:51ac1d130fd4 234 strcat(errstr, digit);
mbed_official 0:51ac1d130fd4 235 }
mbed_official 0:51ac1d130fd4 236 digit[0] = '0' + (memp_type%10);
mbed_official 0:51ac1d130fd4 237 strcat(errstr, digit);
mbed_official 0:51ac1d130fd4 238 #if defined(LWIP_DEBUG) && MEMP_STATS
mbed_official 0:51ac1d130fd4 239 strcat(errstr, memp_overflow_names[memp_type]);
mbed_official 0:51ac1d130fd4 240 #endif
mbed_official 0:51ac1d130fd4 241 LWIP_ASSERT(errstr, 0);
mbed_official 0:51ac1d130fd4 242 }
mbed_official 0:51ac1d130fd4 243 }
mbed_official 0:51ac1d130fd4 244 #endif
mbed_official 0:51ac1d130fd4 245 }
mbed_official 0:51ac1d130fd4 246
mbed_official 0:51ac1d130fd4 247 /**
mbed_official 0:51ac1d130fd4 248 * Check if a memp element was victim of an underflow
mbed_official 0:51ac1d130fd4 249 * (e.g. the restricted area before it has been altered)
mbed_official 0:51ac1d130fd4 250 *
mbed_official 0:51ac1d130fd4 251 * @param p the memp element to check
mbed_official 0:51ac1d130fd4 252 * @param memp_type the pool p comes from
mbed_official 0:51ac1d130fd4 253 */
mbed_official 0:51ac1d130fd4 254 static void
mbed_official 0:51ac1d130fd4 255 memp_overflow_check_element_underflow(struct memp *p, u16_t memp_type)
mbed_official 0:51ac1d130fd4 256 {
mbed_official 0:51ac1d130fd4 257 u16_t k;
mbed_official 0:51ac1d130fd4 258 u8_t *m;
mbed_official 0:51ac1d130fd4 259 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
mbed_official 0:51ac1d130fd4 260 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
mbed_official 0:51ac1d130fd4 261 for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
mbed_official 0:51ac1d130fd4 262 if (m[k] != 0xcd) {
mbed_official 0:51ac1d130fd4 263 char errstr[128] = "detected memp underflow in pool ";
mbed_official 0:51ac1d130fd4 264 char digit[] = "0";
mbed_official 0:51ac1d130fd4 265 if(memp_type >= 10) {
mbed_official 0:51ac1d130fd4 266 digit[0] = '0' + (memp_type/10);
mbed_official 0:51ac1d130fd4 267 strcat(errstr, digit);
mbed_official 0:51ac1d130fd4 268 }
mbed_official 0:51ac1d130fd4 269 digit[0] = '0' + (memp_type%10);
mbed_official 0:51ac1d130fd4 270 strcat(errstr, digit);
mbed_official 0:51ac1d130fd4 271 #if defined(LWIP_DEBUG) && MEMP_STATS
mbed_official 0:51ac1d130fd4 272 strcat(errstr, memp_overflow_names[memp_type]);
mbed_official 0:51ac1d130fd4 273 #endif
mbed_official 0:51ac1d130fd4 274 LWIP_ASSERT(errstr, 0);
mbed_official 0:51ac1d130fd4 275 }
mbed_official 0:51ac1d130fd4 276 }
mbed_official 0:51ac1d130fd4 277 #endif
mbed_official 0:51ac1d130fd4 278 }
mbed_official 0:51ac1d130fd4 279
mbed_official 0:51ac1d130fd4 280 /**
mbed_official 0:51ac1d130fd4 281 * Do an overflow check for all elements in every pool.
mbed_official 0:51ac1d130fd4 282 *
mbed_official 0:51ac1d130fd4 283 * @see memp_overflow_check_element for a description of the check
mbed_official 0:51ac1d130fd4 284 */
mbed_official 0:51ac1d130fd4 285 static void
mbed_official 0:51ac1d130fd4 286 memp_overflow_check_all(void)
mbed_official 0:51ac1d130fd4 287 {
mbed_official 0:51ac1d130fd4 288 u16_t i, j;
mbed_official 0:51ac1d130fd4 289 struct memp *p;
mbed_official 0:51ac1d130fd4 290
mbed_official 0:51ac1d130fd4 291 p = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
mbed_official 0:51ac1d130fd4 292 for (i = 0; i < MEMP_MAX; ++i) {
mbed_official 0:51ac1d130fd4 293 p = p;
mbed_official 0:51ac1d130fd4 294 for (j = 0; j < memp_num[i]; ++j) {
mbed_official 0:51ac1d130fd4 295 memp_overflow_check_element_overflow(p, i);
mbed_official 0:51ac1d130fd4 296 p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
mbed_official 0:51ac1d130fd4 297 }
mbed_official 0:51ac1d130fd4 298 }
mbed_official 0:51ac1d130fd4 299 p = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
mbed_official 0:51ac1d130fd4 300 for (i = 0; i < MEMP_MAX; ++i) {
mbed_official 0:51ac1d130fd4 301 p = p;
mbed_official 0:51ac1d130fd4 302 for (j = 0; j < memp_num[i]; ++j) {
mbed_official 0:51ac1d130fd4 303 memp_overflow_check_element_underflow(p, i);
mbed_official 0:51ac1d130fd4 304 p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
mbed_official 0:51ac1d130fd4 305 }
mbed_official 0:51ac1d130fd4 306 }
mbed_official 0:51ac1d130fd4 307 }
mbed_official 0:51ac1d130fd4 308
mbed_official 0:51ac1d130fd4 309 /**
mbed_official 0:51ac1d130fd4 310 * Initialize the restricted areas of all memp elements in every pool.
mbed_official 0:51ac1d130fd4 311 */
mbed_official 0:51ac1d130fd4 312 static void
mbed_official 0:51ac1d130fd4 313 memp_overflow_init(void)
mbed_official 0:51ac1d130fd4 314 {
mbed_official 0:51ac1d130fd4 315 u16_t i, j;
mbed_official 0:51ac1d130fd4 316 struct memp *p;
mbed_official 0:51ac1d130fd4 317 u8_t *m;
mbed_official 0:51ac1d130fd4 318
mbed_official 0:51ac1d130fd4 319 p = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
mbed_official 0:51ac1d130fd4 320 for (i = 0; i < MEMP_MAX; ++i) {
mbed_official 0:51ac1d130fd4 321 p = p;
mbed_official 0:51ac1d130fd4 322 for (j = 0; j < memp_num[i]; ++j) {
mbed_official 0:51ac1d130fd4 323 #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
mbed_official 0:51ac1d130fd4 324 m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
mbed_official 0:51ac1d130fd4 325 memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
mbed_official 0:51ac1d130fd4 326 #endif
mbed_official 0:51ac1d130fd4 327 #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
mbed_official 0:51ac1d130fd4 328 m = (u8_t*)p + MEMP_SIZE + memp_sizes[i];
mbed_official 0:51ac1d130fd4 329 memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
mbed_official 0:51ac1d130fd4 330 #endif
mbed_official 0:51ac1d130fd4 331 p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
mbed_official 0:51ac1d130fd4 332 }
mbed_official 0:51ac1d130fd4 333 }
mbed_official 0:51ac1d130fd4 334 }
mbed_official 0:51ac1d130fd4 335 #endif /* MEMP_OVERFLOW_CHECK */
mbed_official 0:51ac1d130fd4 336
mbed_official 0:51ac1d130fd4 337 /**
mbed_official 0:51ac1d130fd4 338 * Initialize this module.
mbed_official 0:51ac1d130fd4 339 *
mbed_official 0:51ac1d130fd4 340 * Carves out memp_memory into linked lists for each pool-type.
mbed_official 0:51ac1d130fd4 341 */
mbed_official 0:51ac1d130fd4 342 void
mbed_official 0:51ac1d130fd4 343 memp_init(void)
mbed_official 0:51ac1d130fd4 344 {
mbed_official 0:51ac1d130fd4 345 struct memp *memp;
mbed_official 0:51ac1d130fd4 346 u16_t i, j;
mbed_official 0:51ac1d130fd4 347
mbed_official 0:51ac1d130fd4 348 for (i = 0; i < MEMP_MAX; ++i) {
mbed_official 0:51ac1d130fd4 349 MEMP_STATS_AVAIL(used, i, 0);
mbed_official 0:51ac1d130fd4 350 MEMP_STATS_AVAIL(max, i, 0);
mbed_official 0:51ac1d130fd4 351 MEMP_STATS_AVAIL(err, i, 0);
mbed_official 0:51ac1d130fd4 352 MEMP_STATS_AVAIL(avail, i, memp_num[i]);
mbed_official 0:51ac1d130fd4 353 }
mbed_official 0:51ac1d130fd4 354
mbed_official 0:51ac1d130fd4 355 #if !MEMP_SEPARATE_POOLS
mbed_official 0:51ac1d130fd4 356 memp = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
mbed_official 0:51ac1d130fd4 357 #endif /* !MEMP_SEPARATE_POOLS */
mbed_official 0:51ac1d130fd4 358 /* for every pool: */
mbed_official 0:51ac1d130fd4 359 for (i = 0; i < MEMP_MAX; ++i) {
mbed_official 0:51ac1d130fd4 360 memp_tab[i] = NULL;
mbed_official 0:51ac1d130fd4 361 #if MEMP_SEPARATE_POOLS
mbed_official 0:51ac1d130fd4 362 memp = (struct memp*)memp_bases[i];
mbed_official 0:51ac1d130fd4 363 #endif /* MEMP_SEPARATE_POOLS */
mbed_official 0:51ac1d130fd4 364 /* create a linked list of memp elements */
mbed_official 0:51ac1d130fd4 365 for (j = 0; j < memp_num[i]; ++j) {
mbed_official 0:51ac1d130fd4 366 memp->next = memp_tab[i];
mbed_official 0:51ac1d130fd4 367 memp_tab[i] = memp;
mbed_official 0:51ac1d130fd4 368 memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]
mbed_official 0:51ac1d130fd4 369 #if MEMP_OVERFLOW_CHECK
mbed_official 0:51ac1d130fd4 370 + MEMP_SANITY_REGION_AFTER_ALIGNED
mbed_official 0:51ac1d130fd4 371 #endif
mbed_official 0:51ac1d130fd4 372 );
mbed_official 0:51ac1d130fd4 373 }
mbed_official 0:51ac1d130fd4 374 }
mbed_official 0:51ac1d130fd4 375 #if MEMP_OVERFLOW_CHECK
mbed_official 0:51ac1d130fd4 376 memp_overflow_init();
mbed_official 0:51ac1d130fd4 377 /* check everything a first time to see if it worked */
mbed_official 0:51ac1d130fd4 378 memp_overflow_check_all();
mbed_official 0:51ac1d130fd4 379 #endif /* MEMP_OVERFLOW_CHECK */
mbed_official 0:51ac1d130fd4 380 }
mbed_official 0:51ac1d130fd4 381
mbed_official 0:51ac1d130fd4 382 /**
mbed_official 0:51ac1d130fd4 383 * Get an element from a specific pool.
mbed_official 0:51ac1d130fd4 384 *
mbed_official 0:51ac1d130fd4 385 * @param type the pool to get an element from
mbed_official 0:51ac1d130fd4 386 *
mbed_official 0:51ac1d130fd4 387 * the debug version has two more parameters:
mbed_official 0:51ac1d130fd4 388 * @param file file name calling this function
mbed_official 0:51ac1d130fd4 389 * @param line number of line where this function is called
mbed_official 0:51ac1d130fd4 390 *
mbed_official 0:51ac1d130fd4 391 * @return a pointer to the allocated memory or a NULL pointer on error
mbed_official 0:51ac1d130fd4 392 */
mbed_official 0:51ac1d130fd4 393 void *
mbed_official 0:51ac1d130fd4 394 #if !MEMP_OVERFLOW_CHECK
mbed_official 0:51ac1d130fd4 395 memp_malloc(memp_t type)
mbed_official 0:51ac1d130fd4 396 #else
mbed_official 0:51ac1d130fd4 397 memp_malloc_fn(memp_t type, const char* file, const int line)
mbed_official 0:51ac1d130fd4 398 #endif
mbed_official 0:51ac1d130fd4 399 {
mbed_official 0:51ac1d130fd4 400 struct memp *memp;
mbed_official 0:51ac1d130fd4 401 SYS_ARCH_DECL_PROTECT(old_level);
mbed_official 0:51ac1d130fd4 402
mbed_official 0:51ac1d130fd4 403 LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
mbed_official 0:51ac1d130fd4 404
mbed_official 0:51ac1d130fd4 405 SYS_ARCH_PROTECT(old_level);
mbed_official 0:51ac1d130fd4 406 #if MEMP_OVERFLOW_CHECK >= 2
mbed_official 0:51ac1d130fd4 407 memp_overflow_check_all();
mbed_official 0:51ac1d130fd4 408 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
mbed_official 0:51ac1d130fd4 409
mbed_official 0:51ac1d130fd4 410 memp = memp_tab[type];
mbed_official 0:51ac1d130fd4 411
mbed_official 0:51ac1d130fd4 412 if (memp != NULL) {
mbed_official 0:51ac1d130fd4 413 memp_tab[type] = memp->next;
mbed_official 0:51ac1d130fd4 414 #if MEMP_OVERFLOW_CHECK
mbed_official 0:51ac1d130fd4 415 memp->next = NULL;
mbed_official 0:51ac1d130fd4 416 memp->file = file;
mbed_official 0:51ac1d130fd4 417 memp->line = line;
mbed_official 0:51ac1d130fd4 418 #endif /* MEMP_OVERFLOW_CHECK */
mbed_official 0:51ac1d130fd4 419 MEMP_STATS_INC_USED(used, type);
mbed_official 0:51ac1d130fd4 420 LWIP_ASSERT("memp_malloc: memp properly aligned",
mbed_official 0:51ac1d130fd4 421 ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
mbed_official 0:51ac1d130fd4 422 memp = (struct memp*)(void *)((u8_t*)memp + MEMP_SIZE);
mbed_official 0:51ac1d130fd4 423 } else {
mbed_official 0:51ac1d130fd4 424 LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
mbed_official 0:51ac1d130fd4 425 MEMP_STATS_INC(err, type);
mbed_official 0:51ac1d130fd4 426 }
mbed_official 0:51ac1d130fd4 427
mbed_official 0:51ac1d130fd4 428 SYS_ARCH_UNPROTECT(old_level);
mbed_official 0:51ac1d130fd4 429
mbed_official 0:51ac1d130fd4 430 return memp;
mbed_official 0:51ac1d130fd4 431 }
mbed_official 0:51ac1d130fd4 432
mbed_official 0:51ac1d130fd4 433 /**
mbed_official 0:51ac1d130fd4 434 * Put an element back into its pool.
mbed_official 0:51ac1d130fd4 435 *
mbed_official 0:51ac1d130fd4 436 * @param type the pool where to put mem
mbed_official 0:51ac1d130fd4 437 * @param mem the memp element to free
mbed_official 0:51ac1d130fd4 438 */
mbed_official 0:51ac1d130fd4 439 void
mbed_official 0:51ac1d130fd4 440 memp_free(memp_t type, void *mem)
mbed_official 0:51ac1d130fd4 441 {
mbed_official 0:51ac1d130fd4 442 struct memp *memp;
mbed_official 0:51ac1d130fd4 443 SYS_ARCH_DECL_PROTECT(old_level);
mbed_official 0:51ac1d130fd4 444
mbed_official 0:51ac1d130fd4 445 if (mem == NULL) {
mbed_official 0:51ac1d130fd4 446 return;
mbed_official 0:51ac1d130fd4 447 }
mbed_official 0:51ac1d130fd4 448 LWIP_ASSERT("memp_free: mem properly aligned",
mbed_official 0:51ac1d130fd4 449 ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
mbed_official 0:51ac1d130fd4 450
mbed_official 0:51ac1d130fd4 451 memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
mbed_official 0:51ac1d130fd4 452
mbed_official 0:51ac1d130fd4 453 SYS_ARCH_PROTECT(old_level);
mbed_official 0:51ac1d130fd4 454 #if MEMP_OVERFLOW_CHECK
mbed_official 0:51ac1d130fd4 455 #if MEMP_OVERFLOW_CHECK >= 2
mbed_official 0:51ac1d130fd4 456 memp_overflow_check_all();
mbed_official 0:51ac1d130fd4 457 #else
mbed_official 0:51ac1d130fd4 458 memp_overflow_check_element_overflow(memp, type);
mbed_official 0:51ac1d130fd4 459 memp_overflow_check_element_underflow(memp, type);
mbed_official 0:51ac1d130fd4 460 #endif /* MEMP_OVERFLOW_CHECK >= 2 */
mbed_official 0:51ac1d130fd4 461 #endif /* MEMP_OVERFLOW_CHECK */
mbed_official 0:51ac1d130fd4 462
mbed_official 0:51ac1d130fd4 463 MEMP_STATS_DEC(used, type);
mbed_official 0:51ac1d130fd4 464
mbed_official 0:51ac1d130fd4 465 memp->next = memp_tab[type];
mbed_official 0:51ac1d130fd4 466 memp_tab[type] = memp;
mbed_official 0:51ac1d130fd4 467
mbed_official 0:51ac1d130fd4 468 #if MEMP_SANITY_CHECK
mbed_official 0:51ac1d130fd4 469 LWIP_ASSERT("memp sanity", memp_sanity());
mbed_official 0:51ac1d130fd4 470 #endif /* MEMP_SANITY_CHECK */
mbed_official 0:51ac1d130fd4 471
mbed_official 0:51ac1d130fd4 472 SYS_ARCH_UNPROTECT(old_level);
mbed_official 0:51ac1d130fd4 473 }
mbed_official 0:51ac1d130fd4 474
mbed_official 0:51ac1d130fd4 475 #endif /* MEMP_MEM_MALLOC */