Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Mon Jun 16 07:40:08 2014 +0000
Revision:
1:809b59c7a800
Parent:
0:51f1ef89ec7b
mbed-lib and other libs are a based on a project, published in a Elektor-book "ARM-microkontroller Part II".

Who changed what in which revision?

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