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