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