Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Thu Feb 23 21:38:39 2012 +0000
Revision:
0:616601bde9fb

        

Who changed what in which revision?

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