SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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