A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

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