Library for Bert van Dam's book "ARM MICROCONTROLLERS" For all chapters with internet.

Dependencies:   mbed

Committer:
ICTFBI
Date:
Fri Oct 16 14:28:26 2015 +0000
Revision:
0:4edb816d21e1
Pre-update 16-10-15

Who changed what in which revision?

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