Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

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