I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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