Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

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