Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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