Ian Craggs / Mbed 2 deprecated IBMIoTClientEthernetExample-MACfix-Debug

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample-MACfix-DebugFix by James Sutton

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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