Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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