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