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