Onenet
Dependents: K64F_eCompass_OneNET_JW
lwip/core/mem.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 memory manager |
robert_jw | 0:b2805b6888dc | 4 | * |
robert_jw | 0:b2805b6888dc | 5 | * This is a lightweight replacement for the standard C library malloc(). |
robert_jw | 0:b2805b6888dc | 6 | * |
robert_jw | 0:b2805b6888dc | 7 | * If you want to use the standard C library malloc() instead, define |
robert_jw | 0:b2805b6888dc | 8 | * MEM_LIBC_MALLOC to 1 in your lwipopts.h |
robert_jw | 0:b2805b6888dc | 9 | * |
robert_jw | 0:b2805b6888dc | 10 | * To let mem_malloc() use pools (prevents fragmentation and is much faster than |
robert_jw | 0:b2805b6888dc | 11 | * a heap but might waste some memory), define MEM_USE_POOLS to 1, define |
robert_jw | 0:b2805b6888dc | 12 | * MEM_USE_CUSTOM_POOLS to 1 and create a file "lwippools.h" that includes a list |
robert_jw | 0:b2805b6888dc | 13 | * of pools like this (more pools can be added between _START and _END): |
robert_jw | 0:b2805b6888dc | 14 | * |
robert_jw | 0:b2805b6888dc | 15 | * Define three pools with sizes 256, 512, and 1512 bytes |
robert_jw | 0:b2805b6888dc | 16 | * LWIP_MALLOC_MEMPOOL_START |
robert_jw | 0:b2805b6888dc | 17 | * LWIP_MALLOC_MEMPOOL(20, 256) |
robert_jw | 0:b2805b6888dc | 18 | * LWIP_MALLOC_MEMPOOL(10, 512) |
robert_jw | 0:b2805b6888dc | 19 | * LWIP_MALLOC_MEMPOOL(5, 1512) |
robert_jw | 0:b2805b6888dc | 20 | * LWIP_MALLOC_MEMPOOL_END |
robert_jw | 0:b2805b6888dc | 21 | */ |
robert_jw | 0:b2805b6888dc | 22 | |
robert_jw | 0:b2805b6888dc | 23 | /* |
robert_jw | 0:b2805b6888dc | 24 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
robert_jw | 0:b2805b6888dc | 25 | * All rights reserved. |
robert_jw | 0:b2805b6888dc | 26 | * |
robert_jw | 0:b2805b6888dc | 27 | * Redistribution and use in source and binary forms, with or without modification, |
robert_jw | 0:b2805b6888dc | 28 | * are permitted provided that the following conditions are met: |
robert_jw | 0:b2805b6888dc | 29 | * |
robert_jw | 0:b2805b6888dc | 30 | * 1. Redistributions of source code must retain the above copyright notice, |
robert_jw | 0:b2805b6888dc | 31 | * this list of conditions and the following disclaimer. |
robert_jw | 0:b2805b6888dc | 32 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
robert_jw | 0:b2805b6888dc | 33 | * this list of conditions and the following disclaimer in the documentation |
robert_jw | 0:b2805b6888dc | 34 | * and/or other materials provided with the distribution. |
robert_jw | 0:b2805b6888dc | 35 | * 3. The name of the author may not be used to endorse or promote products |
robert_jw | 0:b2805b6888dc | 36 | * derived from this software without specific prior written permission. |
robert_jw | 0:b2805b6888dc | 37 | * |
robert_jw | 0:b2805b6888dc | 38 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
robert_jw | 0:b2805b6888dc | 39 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
robert_jw | 0:b2805b6888dc | 40 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
robert_jw | 0:b2805b6888dc | 41 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
robert_jw | 0:b2805b6888dc | 42 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
robert_jw | 0:b2805b6888dc | 43 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
robert_jw | 0:b2805b6888dc | 44 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
robert_jw | 0:b2805b6888dc | 45 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
robert_jw | 0:b2805b6888dc | 46 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
robert_jw | 0:b2805b6888dc | 47 | * OF SUCH DAMAGE. |
robert_jw | 0:b2805b6888dc | 48 | * |
robert_jw | 0:b2805b6888dc | 49 | * This file is part of the lwIP TCP/IP stack. |
robert_jw | 0:b2805b6888dc | 50 | * |
robert_jw | 0:b2805b6888dc | 51 | * Author: Adam Dunkels <adam@sics.se> |
robert_jw | 0:b2805b6888dc | 52 | * Simon Goldschmidt |
robert_jw | 0:b2805b6888dc | 53 | * |
robert_jw | 0:b2805b6888dc | 54 | */ |
robert_jw | 0:b2805b6888dc | 55 | |
robert_jw | 0:b2805b6888dc | 56 | #include "lwip/opt.h" |
robert_jw | 0:b2805b6888dc | 57 | |
robert_jw | 0:b2805b6888dc | 58 | #if !MEM_LIBC_MALLOC /* don't build if not configured for use in lwipopts.h */ |
robert_jw | 0:b2805b6888dc | 59 | |
robert_jw | 0:b2805b6888dc | 60 | #include "lwip/def.h" |
robert_jw | 0:b2805b6888dc | 61 | #include "lwip/mem.h" |
robert_jw | 0:b2805b6888dc | 62 | #include "lwip/sys.h" |
robert_jw | 0:b2805b6888dc | 63 | #include "lwip/stats.h" |
robert_jw | 0:b2805b6888dc | 64 | #include "lwip/err.h" |
robert_jw | 0:b2805b6888dc | 65 | |
robert_jw | 0:b2805b6888dc | 66 | #include <string.h> |
robert_jw | 0:b2805b6888dc | 67 | |
robert_jw | 0:b2805b6888dc | 68 | #if MEM_USE_POOLS |
robert_jw | 0:b2805b6888dc | 69 | /* lwIP head implemented with different sized pools */ |
robert_jw | 0:b2805b6888dc | 70 | |
robert_jw | 0:b2805b6888dc | 71 | /** |
robert_jw | 0:b2805b6888dc | 72 | * Allocate memory: determine the smallest pool that is big enough |
robert_jw | 0:b2805b6888dc | 73 | * to contain an element of 'size' and get an element from that pool. |
robert_jw | 0:b2805b6888dc | 74 | * |
robert_jw | 0:b2805b6888dc | 75 | * @param size the size in bytes of the memory needed |
robert_jw | 0:b2805b6888dc | 76 | * @return a pointer to the allocated memory or NULL if the pool is empty |
robert_jw | 0:b2805b6888dc | 77 | */ |
robert_jw | 0:b2805b6888dc | 78 | void * |
robert_jw | 0:b2805b6888dc | 79 | mem_malloc(mem_size_t size) |
robert_jw | 0:b2805b6888dc | 80 | { |
robert_jw | 0:b2805b6888dc | 81 | struct memp_malloc_helper *element; |
robert_jw | 0:b2805b6888dc | 82 | memp_t poolnr; |
robert_jw | 0:b2805b6888dc | 83 | mem_size_t required_size = size + sizeof(struct memp_malloc_helper); |
robert_jw | 0:b2805b6888dc | 84 | |
robert_jw | 0:b2805b6888dc | 85 | for (poolnr = MEMP_POOL_FIRST; poolnr <= MEMP_POOL_LAST; poolnr = (memp_t)(poolnr + 1)) { |
robert_jw | 0:b2805b6888dc | 86 | #if MEM_USE_POOLS_TRY_BIGGER_POOL |
robert_jw | 0:b2805b6888dc | 87 | again: |
robert_jw | 0:b2805b6888dc | 88 | #endif /* MEM_USE_POOLS_TRY_BIGGER_POOL */ |
robert_jw | 0:b2805b6888dc | 89 | /* is this pool big enough to hold an element of the required size |
robert_jw | 0:b2805b6888dc | 90 | plus a struct memp_malloc_helper that saves the pool this element came from? */ |
robert_jw | 0:b2805b6888dc | 91 | if (required_size <= memp_sizes[poolnr]) { |
robert_jw | 0:b2805b6888dc | 92 | break; |
robert_jw | 0:b2805b6888dc | 93 | } |
robert_jw | 0:b2805b6888dc | 94 | } |
robert_jw | 0:b2805b6888dc | 95 | if (poolnr > MEMP_POOL_LAST) { |
robert_jw | 0:b2805b6888dc | 96 | LWIP_ASSERT("mem_malloc(): no pool is that big!", 0); |
robert_jw | 0:b2805b6888dc | 97 | return NULL; |
robert_jw | 0:b2805b6888dc | 98 | } |
robert_jw | 0:b2805b6888dc | 99 | element = (struct memp_malloc_helper*)memp_malloc(poolnr); |
robert_jw | 0:b2805b6888dc | 100 | if (element == NULL) { |
robert_jw | 0:b2805b6888dc | 101 | /* No need to DEBUGF or ASSERT: This error is already |
robert_jw | 0:b2805b6888dc | 102 | taken care of in memp.c */ |
robert_jw | 0:b2805b6888dc | 103 | #if MEM_USE_POOLS_TRY_BIGGER_POOL |
robert_jw | 0:b2805b6888dc | 104 | /** Try a bigger pool if this one is empty! */ |
robert_jw | 0:b2805b6888dc | 105 | if (poolnr < MEMP_POOL_LAST) { |
robert_jw | 0:b2805b6888dc | 106 | poolnr++; |
robert_jw | 0:b2805b6888dc | 107 | goto again; |
robert_jw | 0:b2805b6888dc | 108 | } |
robert_jw | 0:b2805b6888dc | 109 | #endif /* MEM_USE_POOLS_TRY_BIGGER_POOL */ |
robert_jw | 0:b2805b6888dc | 110 | return NULL; |
robert_jw | 0:b2805b6888dc | 111 | } |
robert_jw | 0:b2805b6888dc | 112 | |
robert_jw | 0:b2805b6888dc | 113 | /* save the pool number this element came from */ |
robert_jw | 0:b2805b6888dc | 114 | element->poolnr = poolnr; |
robert_jw | 0:b2805b6888dc | 115 | /* and return a pointer to the memory directly after the struct memp_malloc_helper */ |
robert_jw | 0:b2805b6888dc | 116 | element++; |
robert_jw | 0:b2805b6888dc | 117 | |
robert_jw | 0:b2805b6888dc | 118 | return element; |
robert_jw | 0:b2805b6888dc | 119 | } |
robert_jw | 0:b2805b6888dc | 120 | |
robert_jw | 0:b2805b6888dc | 121 | /** |
robert_jw | 0:b2805b6888dc | 122 | * Free memory previously allocated by mem_malloc. Loads the pool number |
robert_jw | 0:b2805b6888dc | 123 | * and calls memp_free with that pool number to put the element back into |
robert_jw | 0:b2805b6888dc | 124 | * its pool |
robert_jw | 0:b2805b6888dc | 125 | * |
robert_jw | 0:b2805b6888dc | 126 | * @param rmem the memory element to free |
robert_jw | 0:b2805b6888dc | 127 | */ |
robert_jw | 0:b2805b6888dc | 128 | void |
robert_jw | 0:b2805b6888dc | 129 | mem_free(void *rmem) |
robert_jw | 0:b2805b6888dc | 130 | { |
robert_jw | 0:b2805b6888dc | 131 | struct memp_malloc_helper *hmem = (struct memp_malloc_helper*)rmem; |
robert_jw | 0:b2805b6888dc | 132 | |
robert_jw | 0:b2805b6888dc | 133 | LWIP_ASSERT("rmem != NULL", (rmem != NULL)); |
robert_jw | 0:b2805b6888dc | 134 | LWIP_ASSERT("rmem == MEM_ALIGN(rmem)", (rmem == LWIP_MEM_ALIGN(rmem))); |
robert_jw | 0:b2805b6888dc | 135 | |
robert_jw | 0:b2805b6888dc | 136 | /* get the original struct memp_malloc_helper */ |
robert_jw | 0:b2805b6888dc | 137 | hmem--; |
robert_jw | 0:b2805b6888dc | 138 | |
robert_jw | 0:b2805b6888dc | 139 | LWIP_ASSERT("hmem != NULL", (hmem != NULL)); |
robert_jw | 0:b2805b6888dc | 140 | LWIP_ASSERT("hmem == MEM_ALIGN(hmem)", (hmem == LWIP_MEM_ALIGN(hmem))); |
robert_jw | 0:b2805b6888dc | 141 | LWIP_ASSERT("hmem->poolnr < MEMP_MAX", (hmem->poolnr < MEMP_MAX)); |
robert_jw | 0:b2805b6888dc | 142 | |
robert_jw | 0:b2805b6888dc | 143 | /* and put it in the pool we saved earlier */ |
robert_jw | 0:b2805b6888dc | 144 | memp_free(hmem->poolnr, hmem); |
robert_jw | 0:b2805b6888dc | 145 | } |
robert_jw | 0:b2805b6888dc | 146 | |
robert_jw | 0:b2805b6888dc | 147 | #else /* MEM_USE_POOLS */ |
robert_jw | 0:b2805b6888dc | 148 | /* lwIP replacement for your libc malloc() */ |
robert_jw | 0:b2805b6888dc | 149 | |
robert_jw | 0:b2805b6888dc | 150 | /** |
robert_jw | 0:b2805b6888dc | 151 | * The heap is made up as a list of structs of this type. |
robert_jw | 0:b2805b6888dc | 152 | * This does not have to be aligned since for getting its size, |
robert_jw | 0:b2805b6888dc | 153 | * we only use the macro SIZEOF_STRUCT_MEM, which automatically alignes. |
robert_jw | 0:b2805b6888dc | 154 | */ |
robert_jw | 0:b2805b6888dc | 155 | struct mem { |
robert_jw | 0:b2805b6888dc | 156 | /** index (-> ram[next]) of the next struct */ |
robert_jw | 0:b2805b6888dc | 157 | mem_size_t next; |
robert_jw | 0:b2805b6888dc | 158 | /** index (-> ram[prev]) of the previous struct */ |
robert_jw | 0:b2805b6888dc | 159 | mem_size_t prev; |
robert_jw | 0:b2805b6888dc | 160 | /** 1: this area is used; 0: this area is unused */ |
robert_jw | 0:b2805b6888dc | 161 | u8_t used; |
robert_jw | 0:b2805b6888dc | 162 | }; |
robert_jw | 0:b2805b6888dc | 163 | |
robert_jw | 0:b2805b6888dc | 164 | /** All allocated blocks will be MIN_SIZE bytes big, at least! |
robert_jw | 0:b2805b6888dc | 165 | * MIN_SIZE can be overridden to suit your needs. Smaller values save space, |
robert_jw | 0:b2805b6888dc | 166 | * larger values could prevent too small blocks to fragment the RAM too much. */ |
robert_jw | 0:b2805b6888dc | 167 | #ifndef MIN_SIZE |
robert_jw | 0:b2805b6888dc | 168 | #define MIN_SIZE 12 |
robert_jw | 0:b2805b6888dc | 169 | #endif /* MIN_SIZE */ |
robert_jw | 0:b2805b6888dc | 170 | /* some alignment macros: we define them here for better source code layout */ |
robert_jw | 0:b2805b6888dc | 171 | #define MIN_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MIN_SIZE) |
robert_jw | 0:b2805b6888dc | 172 | #define SIZEOF_STRUCT_MEM LWIP_MEM_ALIGN_SIZE(sizeof(struct mem)) |
robert_jw | 0:b2805b6888dc | 173 | #define MEM_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEM_SIZE) |
robert_jw | 0:b2805b6888dc | 174 | |
robert_jw | 0:b2805b6888dc | 175 | /** If you want to relocate the heap to external memory, simply define |
robert_jw | 0:b2805b6888dc | 176 | * LWIP_RAM_HEAP_POINTER as a void-pointer to that location. |
robert_jw | 0:b2805b6888dc | 177 | * If so, make sure the memory at that location is big enough (see below on |
robert_jw | 0:b2805b6888dc | 178 | * how that space is calculated). */ |
robert_jw | 0:b2805b6888dc | 179 | #ifndef LWIP_RAM_HEAP_POINTER |
robert_jw | 0:b2805b6888dc | 180 | |
robert_jw | 0:b2805b6888dc | 181 | #if defined(TARGET_LPC4088) || defined(TARGET_LPC4088_DM) |
robert_jw | 0:b2805b6888dc | 182 | # if defined (__ICCARM__) |
robert_jw | 0:b2805b6888dc | 183 | # define ETHMEM_SECTION |
robert_jw | 0:b2805b6888dc | 184 | # elif defined(TOOLCHAIN_GCC_CR) |
robert_jw | 0:b2805b6888dc | 185 | # define ETHMEM_SECTION __attribute__((section(".data.$RamPeriph32"))) |
robert_jw | 0:b2805b6888dc | 186 | # else |
robert_jw | 0:b2805b6888dc | 187 | # define ETHMEM_SECTION __attribute__((section("AHBSRAM1"),aligned)) |
robert_jw | 0:b2805b6888dc | 188 | # endif |
robert_jw | 0:b2805b6888dc | 189 | #elif defined(TARGET_LPC1768) |
robert_jw | 0:b2805b6888dc | 190 | # define ETHMEM_SECTION __attribute((section("AHBSRAM0"))) |
robert_jw | 0:b2805b6888dc | 191 | #else |
robert_jw | 0:b2805b6888dc | 192 | # define ETHMEM_SECTION |
robert_jw | 0:b2805b6888dc | 193 | #endif |
robert_jw | 0:b2805b6888dc | 194 | |
robert_jw | 0:b2805b6888dc | 195 | /** the heap. we need one struct mem at the end and some room for alignment */ |
robert_jw | 0:b2805b6888dc | 196 | u8_t ram_heap[MEM_SIZE_ALIGNED + (2*SIZEOF_STRUCT_MEM) + MEM_ALIGNMENT] ETHMEM_SECTION; |
robert_jw | 0:b2805b6888dc | 197 | #define LWIP_RAM_HEAP_POINTER ram_heap |
robert_jw | 0:b2805b6888dc | 198 | #endif /* LWIP_RAM_HEAP_POINTER */ |
robert_jw | 0:b2805b6888dc | 199 | |
robert_jw | 0:b2805b6888dc | 200 | /** pointer to the heap (ram_heap): for alignment, ram is now a pointer instead of an array */ |
robert_jw | 0:b2805b6888dc | 201 | static u8_t *ram; |
robert_jw | 0:b2805b6888dc | 202 | /** the last entry, always unused! */ |
robert_jw | 0:b2805b6888dc | 203 | static struct mem *ram_end; |
robert_jw | 0:b2805b6888dc | 204 | /** pointer to the lowest free block, this is used for faster search */ |
robert_jw | 0:b2805b6888dc | 205 | static struct mem *lfree; |
robert_jw | 0:b2805b6888dc | 206 | |
robert_jw | 0:b2805b6888dc | 207 | /** concurrent access protection */ |
robert_jw | 0:b2805b6888dc | 208 | static sys_mutex_t mem_mutex; |
robert_jw | 0:b2805b6888dc | 209 | |
robert_jw | 0:b2805b6888dc | 210 | #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT |
robert_jw | 0:b2805b6888dc | 211 | |
robert_jw | 0:b2805b6888dc | 212 | static volatile u8_t mem_free_count; |
robert_jw | 0:b2805b6888dc | 213 | |
robert_jw | 0:b2805b6888dc | 214 | /* Allow mem_free from other (e.g. interrupt) context */ |
robert_jw | 0:b2805b6888dc | 215 | #define LWIP_MEM_FREE_DECL_PROTECT() SYS_ARCH_DECL_PROTECT(lev_free) |
robert_jw | 0:b2805b6888dc | 216 | #define LWIP_MEM_FREE_PROTECT() SYS_ARCH_PROTECT(lev_free) |
robert_jw | 0:b2805b6888dc | 217 | #define LWIP_MEM_FREE_UNPROTECT() SYS_ARCH_UNPROTECT(lev_free) |
robert_jw | 0:b2805b6888dc | 218 | #define LWIP_MEM_ALLOC_DECL_PROTECT() SYS_ARCH_DECL_PROTECT(lev_alloc) |
robert_jw | 0:b2805b6888dc | 219 | #define LWIP_MEM_ALLOC_PROTECT() SYS_ARCH_PROTECT(lev_alloc) |
robert_jw | 0:b2805b6888dc | 220 | #define LWIP_MEM_ALLOC_UNPROTECT() SYS_ARCH_UNPROTECT(lev_alloc) |
robert_jw | 0:b2805b6888dc | 221 | |
robert_jw | 0:b2805b6888dc | 222 | #else /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ |
robert_jw | 0:b2805b6888dc | 223 | |
robert_jw | 0:b2805b6888dc | 224 | /* Protect the heap only by using a semaphore */ |
robert_jw | 0:b2805b6888dc | 225 | #define LWIP_MEM_FREE_DECL_PROTECT() |
robert_jw | 0:b2805b6888dc | 226 | #define LWIP_MEM_FREE_PROTECT() sys_mutex_lock(&mem_mutex) |
robert_jw | 0:b2805b6888dc | 227 | #define LWIP_MEM_FREE_UNPROTECT() sys_mutex_unlock(&mem_mutex) |
robert_jw | 0:b2805b6888dc | 228 | /* mem_malloc is protected using semaphore AND LWIP_MEM_ALLOC_PROTECT */ |
robert_jw | 0:b2805b6888dc | 229 | #define LWIP_MEM_ALLOC_DECL_PROTECT() |
robert_jw | 0:b2805b6888dc | 230 | #define LWIP_MEM_ALLOC_PROTECT() |
robert_jw | 0:b2805b6888dc | 231 | #define LWIP_MEM_ALLOC_UNPROTECT() |
robert_jw | 0:b2805b6888dc | 232 | |
robert_jw | 0:b2805b6888dc | 233 | #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ |
robert_jw | 0:b2805b6888dc | 234 | |
robert_jw | 0:b2805b6888dc | 235 | |
robert_jw | 0:b2805b6888dc | 236 | /** |
robert_jw | 0:b2805b6888dc | 237 | * "Plug holes" by combining adjacent empty struct mems. |
robert_jw | 0:b2805b6888dc | 238 | * After this function is through, there should not exist |
robert_jw | 0:b2805b6888dc | 239 | * one empty struct mem pointing to another empty struct mem. |
robert_jw | 0:b2805b6888dc | 240 | * |
robert_jw | 0:b2805b6888dc | 241 | * @param mem this points to a struct mem which just has been freed |
robert_jw | 0:b2805b6888dc | 242 | * @internal this function is only called by mem_free() and mem_trim() |
robert_jw | 0:b2805b6888dc | 243 | * |
robert_jw | 0:b2805b6888dc | 244 | * This assumes access to the heap is protected by the calling function |
robert_jw | 0:b2805b6888dc | 245 | * already. |
robert_jw | 0:b2805b6888dc | 246 | */ |
robert_jw | 0:b2805b6888dc | 247 | static void |
robert_jw | 0:b2805b6888dc | 248 | plug_holes(struct mem *mem) |
robert_jw | 0:b2805b6888dc | 249 | { |
robert_jw | 0:b2805b6888dc | 250 | struct mem *nmem; |
robert_jw | 0:b2805b6888dc | 251 | struct mem *pmem; |
robert_jw | 0:b2805b6888dc | 252 | |
robert_jw | 0:b2805b6888dc | 253 | LWIP_ASSERT("plug_holes: mem >= ram", (u8_t *)mem >= ram); |
robert_jw | 0:b2805b6888dc | 254 | LWIP_ASSERT("plug_holes: mem < ram_end", (u8_t *)mem < (u8_t *)ram_end); |
robert_jw | 0:b2805b6888dc | 255 | LWIP_ASSERT("plug_holes: mem->used == 0", mem->used == 0); |
robert_jw | 0:b2805b6888dc | 256 | |
robert_jw | 0:b2805b6888dc | 257 | /* plug hole forward */ |
robert_jw | 0:b2805b6888dc | 258 | LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE_ALIGNED", mem->next <= MEM_SIZE_ALIGNED); |
robert_jw | 0:b2805b6888dc | 259 | |
robert_jw | 0:b2805b6888dc | 260 | nmem = (struct mem *)(void *)&ram[mem->next]; |
robert_jw | 0:b2805b6888dc | 261 | if (mem != nmem && nmem->used == 0 && (u8_t *)nmem != (u8_t *)ram_end) { |
robert_jw | 0:b2805b6888dc | 262 | /* if mem->next is unused and not end of ram, combine mem and mem->next */ |
robert_jw | 0:b2805b6888dc | 263 | if (lfree == nmem) { |
robert_jw | 0:b2805b6888dc | 264 | lfree = mem; |
robert_jw | 0:b2805b6888dc | 265 | } |
robert_jw | 0:b2805b6888dc | 266 | mem->next = nmem->next; |
robert_jw | 0:b2805b6888dc | 267 | ((struct mem *)(void *)&ram[nmem->next])->prev = (mem_size_t)((u8_t *)mem - ram); |
robert_jw | 0:b2805b6888dc | 268 | } |
robert_jw | 0:b2805b6888dc | 269 | |
robert_jw | 0:b2805b6888dc | 270 | /* plug hole backward */ |
robert_jw | 0:b2805b6888dc | 271 | pmem = (struct mem *)(void *)&ram[mem->prev]; |
robert_jw | 0:b2805b6888dc | 272 | if (pmem != mem && pmem->used == 0) { |
robert_jw | 0:b2805b6888dc | 273 | /* if mem->prev is unused, combine mem and mem->prev */ |
robert_jw | 0:b2805b6888dc | 274 | if (lfree == mem) { |
robert_jw | 0:b2805b6888dc | 275 | lfree = pmem; |
robert_jw | 0:b2805b6888dc | 276 | } |
robert_jw | 0:b2805b6888dc | 277 | pmem->next = mem->next; |
robert_jw | 0:b2805b6888dc | 278 | ((struct mem *)(void *)&ram[mem->next])->prev = (mem_size_t)((u8_t *)pmem - ram); |
robert_jw | 0:b2805b6888dc | 279 | } |
robert_jw | 0:b2805b6888dc | 280 | } |
robert_jw | 0:b2805b6888dc | 281 | |
robert_jw | 0:b2805b6888dc | 282 | /** |
robert_jw | 0:b2805b6888dc | 283 | * Zero the heap and initialize start, end and lowest-free |
robert_jw | 0:b2805b6888dc | 284 | */ |
robert_jw | 0:b2805b6888dc | 285 | void |
robert_jw | 0:b2805b6888dc | 286 | mem_init(void) |
robert_jw | 0:b2805b6888dc | 287 | { |
robert_jw | 0:b2805b6888dc | 288 | struct mem *mem; |
robert_jw | 0:b2805b6888dc | 289 | |
robert_jw | 0:b2805b6888dc | 290 | LWIP_ASSERT("Sanity check alignment", |
robert_jw | 0:b2805b6888dc | 291 | (SIZEOF_STRUCT_MEM & (MEM_ALIGNMENT-1)) == 0); |
robert_jw | 0:b2805b6888dc | 292 | |
robert_jw | 0:b2805b6888dc | 293 | /* align the heap */ |
robert_jw | 0:b2805b6888dc | 294 | ram = (u8_t *)LWIP_MEM_ALIGN(LWIP_RAM_HEAP_POINTER); |
robert_jw | 0:b2805b6888dc | 295 | /* initialize the start of the heap */ |
robert_jw | 0:b2805b6888dc | 296 | mem = (struct mem *)(void *)ram; |
robert_jw | 0:b2805b6888dc | 297 | mem->next = MEM_SIZE_ALIGNED; |
robert_jw | 0:b2805b6888dc | 298 | mem->prev = 0; |
robert_jw | 0:b2805b6888dc | 299 | mem->used = 0; |
robert_jw | 0:b2805b6888dc | 300 | /* initialize the end of the heap */ |
robert_jw | 0:b2805b6888dc | 301 | ram_end = (struct mem *)(void *)&ram[MEM_SIZE_ALIGNED]; |
robert_jw | 0:b2805b6888dc | 302 | ram_end->used = 1; |
robert_jw | 0:b2805b6888dc | 303 | ram_end->next = MEM_SIZE_ALIGNED; |
robert_jw | 0:b2805b6888dc | 304 | ram_end->prev = MEM_SIZE_ALIGNED; |
robert_jw | 0:b2805b6888dc | 305 | |
robert_jw | 0:b2805b6888dc | 306 | /* initialize the lowest-free pointer to the start of the heap */ |
robert_jw | 0:b2805b6888dc | 307 | lfree = (struct mem *)(void *)ram; |
robert_jw | 0:b2805b6888dc | 308 | |
robert_jw | 0:b2805b6888dc | 309 | MEM_STATS_AVAIL(avail, MEM_SIZE_ALIGNED); |
robert_jw | 0:b2805b6888dc | 310 | |
robert_jw | 0:b2805b6888dc | 311 | if(sys_mutex_new(&mem_mutex) != ERR_OK) { |
robert_jw | 0:b2805b6888dc | 312 | LWIP_ASSERT("failed to create mem_mutex", 0); |
robert_jw | 0:b2805b6888dc | 313 | } |
robert_jw | 0:b2805b6888dc | 314 | } |
robert_jw | 0:b2805b6888dc | 315 | |
robert_jw | 0:b2805b6888dc | 316 | /** |
robert_jw | 0:b2805b6888dc | 317 | * Put a struct mem back on the heap |
robert_jw | 0:b2805b6888dc | 318 | * |
robert_jw | 0:b2805b6888dc | 319 | * @param rmem is the data portion of a struct mem as returned by a previous |
robert_jw | 0:b2805b6888dc | 320 | * call to mem_malloc() |
robert_jw | 0:b2805b6888dc | 321 | */ |
robert_jw | 0:b2805b6888dc | 322 | void |
robert_jw | 0:b2805b6888dc | 323 | mem_free(void *rmem) |
robert_jw | 0:b2805b6888dc | 324 | { |
robert_jw | 0:b2805b6888dc | 325 | struct mem *mem; |
robert_jw | 0:b2805b6888dc | 326 | LWIP_MEM_FREE_DECL_PROTECT(); |
robert_jw | 0:b2805b6888dc | 327 | |
robert_jw | 0:b2805b6888dc | 328 | if (rmem == NULL) { |
robert_jw | 0:b2805b6888dc | 329 | LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("mem_free(p == NULL) was called.\n")); |
robert_jw | 0:b2805b6888dc | 330 | return; |
robert_jw | 0:b2805b6888dc | 331 | } |
robert_jw | 0:b2805b6888dc | 332 | LWIP_ASSERT("mem_free: sanity check alignment", (((mem_ptr_t)rmem) & (MEM_ALIGNMENT-1)) == 0); |
robert_jw | 0:b2805b6888dc | 333 | |
robert_jw | 0:b2805b6888dc | 334 | LWIP_ASSERT("mem_free: legal memory", (u8_t *)rmem >= (u8_t *)ram && |
robert_jw | 0:b2805b6888dc | 335 | (u8_t *)rmem < (u8_t *)ram_end); |
robert_jw | 0:b2805b6888dc | 336 | |
robert_jw | 0:b2805b6888dc | 337 | if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) { |
robert_jw | 0:b2805b6888dc | 338 | SYS_ARCH_DECL_PROTECT(lev); |
robert_jw | 0:b2805b6888dc | 339 | LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_free: illegal memory\n")); |
robert_jw | 0:b2805b6888dc | 340 | /* protect mem stats from concurrent access */ |
robert_jw | 0:b2805b6888dc | 341 | SYS_ARCH_PROTECT(lev); |
robert_jw | 0:b2805b6888dc | 342 | MEM_STATS_INC(illegal); |
robert_jw | 0:b2805b6888dc | 343 | SYS_ARCH_UNPROTECT(lev); |
robert_jw | 0:b2805b6888dc | 344 | return; |
robert_jw | 0:b2805b6888dc | 345 | } |
robert_jw | 0:b2805b6888dc | 346 | /* protect the heap from concurrent access */ |
robert_jw | 0:b2805b6888dc | 347 | LWIP_MEM_FREE_PROTECT(); |
robert_jw | 0:b2805b6888dc | 348 | /* Get the corresponding struct mem ... */ |
robert_jw | 0:b2805b6888dc | 349 | mem = (struct mem *)(void *)((u8_t *)rmem - SIZEOF_STRUCT_MEM); |
robert_jw | 0:b2805b6888dc | 350 | /* ... which has to be in a used state ... */ |
robert_jw | 0:b2805b6888dc | 351 | LWIP_ASSERT("mem_free: mem->used", mem->used); |
robert_jw | 0:b2805b6888dc | 352 | /* ... and is now unused. */ |
robert_jw | 0:b2805b6888dc | 353 | mem->used = 0; |
robert_jw | 0:b2805b6888dc | 354 | |
robert_jw | 0:b2805b6888dc | 355 | if (mem < lfree) { |
robert_jw | 0:b2805b6888dc | 356 | /* the newly freed struct is now the lowest */ |
robert_jw | 0:b2805b6888dc | 357 | lfree = mem; |
robert_jw | 0:b2805b6888dc | 358 | } |
robert_jw | 0:b2805b6888dc | 359 | |
robert_jw | 0:b2805b6888dc | 360 | MEM_STATS_DEC_USED(used, mem->next - (mem_size_t)(((u8_t *)mem - ram))); |
robert_jw | 0:b2805b6888dc | 361 | |
robert_jw | 0:b2805b6888dc | 362 | /* finally, see if prev or next are free also */ |
robert_jw | 0:b2805b6888dc | 363 | plug_holes(mem); |
robert_jw | 0:b2805b6888dc | 364 | #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT |
robert_jw | 0:b2805b6888dc | 365 | mem_free_count = 1; |
robert_jw | 0:b2805b6888dc | 366 | #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ |
robert_jw | 0:b2805b6888dc | 367 | LWIP_MEM_FREE_UNPROTECT(); |
robert_jw | 0:b2805b6888dc | 368 | } |
robert_jw | 0:b2805b6888dc | 369 | |
robert_jw | 0:b2805b6888dc | 370 | /** |
robert_jw | 0:b2805b6888dc | 371 | * Shrink memory returned by mem_malloc(). |
robert_jw | 0:b2805b6888dc | 372 | * |
robert_jw | 0:b2805b6888dc | 373 | * @param rmem pointer to memory allocated by mem_malloc the is to be shrinked |
robert_jw | 0:b2805b6888dc | 374 | * @param newsize required size after shrinking (needs to be smaller than or |
robert_jw | 0:b2805b6888dc | 375 | * equal to the previous size) |
robert_jw | 0:b2805b6888dc | 376 | * @return for compatibility reasons: is always == rmem, at the moment |
robert_jw | 0:b2805b6888dc | 377 | * or NULL if newsize is > old size, in which case rmem is NOT touched |
robert_jw | 0:b2805b6888dc | 378 | * or freed! |
robert_jw | 0:b2805b6888dc | 379 | */ |
robert_jw | 0:b2805b6888dc | 380 | void * |
robert_jw | 0:b2805b6888dc | 381 | mem_trim(void *rmem, mem_size_t newsize) |
robert_jw | 0:b2805b6888dc | 382 | { |
robert_jw | 0:b2805b6888dc | 383 | mem_size_t size; |
robert_jw | 0:b2805b6888dc | 384 | mem_size_t ptr, ptr2; |
robert_jw | 0:b2805b6888dc | 385 | struct mem *mem, *mem2; |
robert_jw | 0:b2805b6888dc | 386 | /* use the FREE_PROTECT here: it protects with sem OR SYS_ARCH_PROTECT */ |
robert_jw | 0:b2805b6888dc | 387 | LWIP_MEM_FREE_DECL_PROTECT(); |
robert_jw | 0:b2805b6888dc | 388 | |
robert_jw | 0:b2805b6888dc | 389 | /* Expand the size of the allocated memory region so that we can |
robert_jw | 0:b2805b6888dc | 390 | adjust for alignment. */ |
robert_jw | 0:b2805b6888dc | 391 | newsize = LWIP_MEM_ALIGN_SIZE(newsize); |
robert_jw | 0:b2805b6888dc | 392 | |
robert_jw | 0:b2805b6888dc | 393 | if(newsize < MIN_SIZE_ALIGNED) { |
robert_jw | 0:b2805b6888dc | 394 | /* every data block must be at least MIN_SIZE_ALIGNED long */ |
robert_jw | 0:b2805b6888dc | 395 | newsize = MIN_SIZE_ALIGNED; |
robert_jw | 0:b2805b6888dc | 396 | } |
robert_jw | 0:b2805b6888dc | 397 | |
robert_jw | 0:b2805b6888dc | 398 | if (newsize > MEM_SIZE_ALIGNED) { |
robert_jw | 0:b2805b6888dc | 399 | return NULL; |
robert_jw | 0:b2805b6888dc | 400 | } |
robert_jw | 0:b2805b6888dc | 401 | |
robert_jw | 0:b2805b6888dc | 402 | LWIP_ASSERT("mem_trim: legal memory", (u8_t *)rmem >= (u8_t *)ram && |
robert_jw | 0:b2805b6888dc | 403 | (u8_t *)rmem < (u8_t *)ram_end); |
robert_jw | 0:b2805b6888dc | 404 | |
robert_jw | 0:b2805b6888dc | 405 | if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) { |
robert_jw | 0:b2805b6888dc | 406 | SYS_ARCH_DECL_PROTECT(lev); |
robert_jw | 0:b2805b6888dc | 407 | LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SEVERE, ("mem_trim: illegal memory\n")); |
robert_jw | 0:b2805b6888dc | 408 | /* protect mem stats from concurrent access */ |
robert_jw | 0:b2805b6888dc | 409 | SYS_ARCH_PROTECT(lev); |
robert_jw | 0:b2805b6888dc | 410 | MEM_STATS_INC(illegal); |
robert_jw | 0:b2805b6888dc | 411 | SYS_ARCH_UNPROTECT(lev); |
robert_jw | 0:b2805b6888dc | 412 | return rmem; |
robert_jw | 0:b2805b6888dc | 413 | } |
robert_jw | 0:b2805b6888dc | 414 | /* Get the corresponding struct mem ... */ |
robert_jw | 0:b2805b6888dc | 415 | mem = (struct mem *)(void *)((u8_t *)rmem - SIZEOF_STRUCT_MEM); |
robert_jw | 0:b2805b6888dc | 416 | /* ... and its offset pointer */ |
robert_jw | 0:b2805b6888dc | 417 | ptr = (mem_size_t)((u8_t *)mem - ram); |
robert_jw | 0:b2805b6888dc | 418 | |
robert_jw | 0:b2805b6888dc | 419 | size = mem->next - ptr - SIZEOF_STRUCT_MEM; |
robert_jw | 0:b2805b6888dc | 420 | LWIP_ASSERT("mem_trim can only shrink memory", newsize <= size); |
robert_jw | 0:b2805b6888dc | 421 | if (newsize > size) { |
robert_jw | 0:b2805b6888dc | 422 | /* not supported */ |
robert_jw | 0:b2805b6888dc | 423 | return NULL; |
robert_jw | 0:b2805b6888dc | 424 | } |
robert_jw | 0:b2805b6888dc | 425 | if (newsize == size) { |
robert_jw | 0:b2805b6888dc | 426 | /* No change in size, simply return */ |
robert_jw | 0:b2805b6888dc | 427 | return rmem; |
robert_jw | 0:b2805b6888dc | 428 | } |
robert_jw | 0:b2805b6888dc | 429 | |
robert_jw | 0:b2805b6888dc | 430 | /* protect the heap from concurrent access */ |
robert_jw | 0:b2805b6888dc | 431 | LWIP_MEM_FREE_PROTECT(); |
robert_jw | 0:b2805b6888dc | 432 | |
robert_jw | 0:b2805b6888dc | 433 | mem2 = (struct mem *)(void *)&ram[mem->next]; |
robert_jw | 0:b2805b6888dc | 434 | if(mem2->used == 0) { |
robert_jw | 0:b2805b6888dc | 435 | /* The next struct is unused, we can simply move it at little */ |
robert_jw | 0:b2805b6888dc | 436 | mem_size_t next; |
robert_jw | 0:b2805b6888dc | 437 | /* remember the old next pointer */ |
robert_jw | 0:b2805b6888dc | 438 | next = mem2->next; |
robert_jw | 0:b2805b6888dc | 439 | /* create new struct mem which is moved directly after the shrinked mem */ |
robert_jw | 0:b2805b6888dc | 440 | ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize; |
robert_jw | 0:b2805b6888dc | 441 | if (lfree == mem2) { |
robert_jw | 0:b2805b6888dc | 442 | lfree = (struct mem *)(void *)&ram[ptr2]; |
robert_jw | 0:b2805b6888dc | 443 | } |
robert_jw | 0:b2805b6888dc | 444 | mem2 = (struct mem *)(void *)&ram[ptr2]; |
robert_jw | 0:b2805b6888dc | 445 | mem2->used = 0; |
robert_jw | 0:b2805b6888dc | 446 | /* restore the next pointer */ |
robert_jw | 0:b2805b6888dc | 447 | mem2->next = next; |
robert_jw | 0:b2805b6888dc | 448 | /* link it back to mem */ |
robert_jw | 0:b2805b6888dc | 449 | mem2->prev = ptr; |
robert_jw | 0:b2805b6888dc | 450 | /* link mem to it */ |
robert_jw | 0:b2805b6888dc | 451 | mem->next = ptr2; |
robert_jw | 0:b2805b6888dc | 452 | /* last thing to restore linked list: as we have moved mem2, |
robert_jw | 0:b2805b6888dc | 453 | * let 'mem2->next->prev' point to mem2 again. but only if mem2->next is not |
robert_jw | 0:b2805b6888dc | 454 | * the end of the heap */ |
robert_jw | 0:b2805b6888dc | 455 | if (mem2->next != MEM_SIZE_ALIGNED) { |
robert_jw | 0:b2805b6888dc | 456 | ((struct mem *)(void *)&ram[mem2->next])->prev = ptr2; |
robert_jw | 0:b2805b6888dc | 457 | } |
robert_jw | 0:b2805b6888dc | 458 | MEM_STATS_DEC_USED(used, (size - newsize)); |
robert_jw | 0:b2805b6888dc | 459 | /* no need to plug holes, we've already done that */ |
robert_jw | 0:b2805b6888dc | 460 | } else if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED <= size) { |
robert_jw | 0:b2805b6888dc | 461 | /* Next struct is used but there's room for another struct mem with |
robert_jw | 0:b2805b6888dc | 462 | * at least MIN_SIZE_ALIGNED of data. |
robert_jw | 0:b2805b6888dc | 463 | * Old size ('size') must be big enough to contain at least 'newsize' plus a struct mem |
robert_jw | 0:b2805b6888dc | 464 | * ('SIZEOF_STRUCT_MEM') with some data ('MIN_SIZE_ALIGNED'). |
robert_jw | 0:b2805b6888dc | 465 | * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty |
robert_jw | 0:b2805b6888dc | 466 | * region that couldn't hold data, but when mem->next gets freed, |
robert_jw | 0:b2805b6888dc | 467 | * the 2 regions would be combined, resulting in more free memory */ |
robert_jw | 0:b2805b6888dc | 468 | ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize; |
robert_jw | 0:b2805b6888dc | 469 | mem2 = (struct mem *)(void *)&ram[ptr2]; |
robert_jw | 0:b2805b6888dc | 470 | if (mem2 < lfree) { |
robert_jw | 0:b2805b6888dc | 471 | lfree = mem2; |
robert_jw | 0:b2805b6888dc | 472 | } |
robert_jw | 0:b2805b6888dc | 473 | mem2->used = 0; |
robert_jw | 0:b2805b6888dc | 474 | mem2->next = mem->next; |
robert_jw | 0:b2805b6888dc | 475 | mem2->prev = ptr; |
robert_jw | 0:b2805b6888dc | 476 | mem->next = ptr2; |
robert_jw | 0:b2805b6888dc | 477 | if (mem2->next != MEM_SIZE_ALIGNED) { |
robert_jw | 0:b2805b6888dc | 478 | ((struct mem *)(void *)&ram[mem2->next])->prev = ptr2; |
robert_jw | 0:b2805b6888dc | 479 | } |
robert_jw | 0:b2805b6888dc | 480 | MEM_STATS_DEC_USED(used, (size - newsize)); |
robert_jw | 0:b2805b6888dc | 481 | /* the original mem->next is used, so no need to plug holes! */ |
robert_jw | 0:b2805b6888dc | 482 | } |
robert_jw | 0:b2805b6888dc | 483 | /* else { |
robert_jw | 0:b2805b6888dc | 484 | next struct mem is used but size between mem and mem2 is not big enough |
robert_jw | 0:b2805b6888dc | 485 | to create another struct mem |
robert_jw | 0:b2805b6888dc | 486 | -> don't do anyhting. |
robert_jw | 0:b2805b6888dc | 487 | -> the remaining space stays unused since it is too small |
robert_jw | 0:b2805b6888dc | 488 | } */ |
robert_jw | 0:b2805b6888dc | 489 | #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT |
robert_jw | 0:b2805b6888dc | 490 | mem_free_count = 1; |
robert_jw | 0:b2805b6888dc | 491 | #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ |
robert_jw | 0:b2805b6888dc | 492 | LWIP_MEM_FREE_UNPROTECT(); |
robert_jw | 0:b2805b6888dc | 493 | return rmem; |
robert_jw | 0:b2805b6888dc | 494 | } |
robert_jw | 0:b2805b6888dc | 495 | |
robert_jw | 0:b2805b6888dc | 496 | /** |
robert_jw | 0:b2805b6888dc | 497 | * Adam's mem_malloc() plus solution for bug #17922 |
robert_jw | 0:b2805b6888dc | 498 | * Allocate a block of memory with a minimum of 'size' bytes. |
robert_jw | 0:b2805b6888dc | 499 | * |
robert_jw | 0:b2805b6888dc | 500 | * @param size is the minimum size of the requested block in bytes. |
robert_jw | 0:b2805b6888dc | 501 | * @return pointer to allocated memory or NULL if no free memory was found. |
robert_jw | 0:b2805b6888dc | 502 | * |
robert_jw | 0:b2805b6888dc | 503 | * Note that the returned value will always be aligned (as defined by MEM_ALIGNMENT). |
robert_jw | 0:b2805b6888dc | 504 | */ |
robert_jw | 0:b2805b6888dc | 505 | void * |
robert_jw | 0:b2805b6888dc | 506 | mem_malloc(mem_size_t size) |
robert_jw | 0:b2805b6888dc | 507 | { |
robert_jw | 0:b2805b6888dc | 508 | mem_size_t ptr, ptr2; |
robert_jw | 0:b2805b6888dc | 509 | struct mem *mem, *mem2; |
robert_jw | 0:b2805b6888dc | 510 | #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT |
robert_jw | 0:b2805b6888dc | 511 | u8_t local_mem_free_count = 0; |
robert_jw | 0:b2805b6888dc | 512 | #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ |
robert_jw | 0:b2805b6888dc | 513 | LWIP_MEM_ALLOC_DECL_PROTECT(); |
robert_jw | 0:b2805b6888dc | 514 | |
robert_jw | 0:b2805b6888dc | 515 | if (size == 0) { |
robert_jw | 0:b2805b6888dc | 516 | return NULL; |
robert_jw | 0:b2805b6888dc | 517 | } |
robert_jw | 0:b2805b6888dc | 518 | |
robert_jw | 0:b2805b6888dc | 519 | /* Expand the size of the allocated memory region so that we can |
robert_jw | 0:b2805b6888dc | 520 | adjust for alignment. */ |
robert_jw | 0:b2805b6888dc | 521 | size = LWIP_MEM_ALIGN_SIZE(size); |
robert_jw | 0:b2805b6888dc | 522 | |
robert_jw | 0:b2805b6888dc | 523 | if(size < MIN_SIZE_ALIGNED) { |
robert_jw | 0:b2805b6888dc | 524 | /* every data block must be at least MIN_SIZE_ALIGNED long */ |
robert_jw | 0:b2805b6888dc | 525 | size = MIN_SIZE_ALIGNED; |
robert_jw | 0:b2805b6888dc | 526 | } |
robert_jw | 0:b2805b6888dc | 527 | |
robert_jw | 0:b2805b6888dc | 528 | if (size > MEM_SIZE_ALIGNED) { |
robert_jw | 0:b2805b6888dc | 529 | return NULL; |
robert_jw | 0:b2805b6888dc | 530 | } |
robert_jw | 0:b2805b6888dc | 531 | |
robert_jw | 0:b2805b6888dc | 532 | /* protect the heap from concurrent access */ |
robert_jw | 0:b2805b6888dc | 533 | sys_mutex_lock(&mem_mutex); |
robert_jw | 0:b2805b6888dc | 534 | LWIP_MEM_ALLOC_PROTECT(); |
robert_jw | 0:b2805b6888dc | 535 | #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT |
robert_jw | 0:b2805b6888dc | 536 | /* run as long as a mem_free disturbed mem_malloc */ |
robert_jw | 0:b2805b6888dc | 537 | do { |
robert_jw | 0:b2805b6888dc | 538 | local_mem_free_count = 0; |
robert_jw | 0:b2805b6888dc | 539 | #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ |
robert_jw | 0:b2805b6888dc | 540 | |
robert_jw | 0:b2805b6888dc | 541 | /* Scan through the heap searching for a free block that is big enough, |
robert_jw | 0:b2805b6888dc | 542 | * beginning with the lowest free block. |
robert_jw | 0:b2805b6888dc | 543 | */ |
robert_jw | 0:b2805b6888dc | 544 | for (ptr = (mem_size_t)((u8_t *)lfree - ram); ptr < MEM_SIZE_ALIGNED - size; |
robert_jw | 0:b2805b6888dc | 545 | ptr = ((struct mem *)(void *)&ram[ptr])->next) { |
robert_jw | 0:b2805b6888dc | 546 | mem = (struct mem *)(void *)&ram[ptr]; |
robert_jw | 0:b2805b6888dc | 547 | #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT |
robert_jw | 0:b2805b6888dc | 548 | mem_free_count = 0; |
robert_jw | 0:b2805b6888dc | 549 | LWIP_MEM_ALLOC_UNPROTECT(); |
robert_jw | 0:b2805b6888dc | 550 | /* allow mem_free to run */ |
robert_jw | 0:b2805b6888dc | 551 | LWIP_MEM_ALLOC_PROTECT(); |
robert_jw | 0:b2805b6888dc | 552 | if (mem_free_count != 0) { |
robert_jw | 0:b2805b6888dc | 553 | local_mem_free_count = mem_free_count; |
robert_jw | 0:b2805b6888dc | 554 | } |
robert_jw | 0:b2805b6888dc | 555 | mem_free_count = 0; |
robert_jw | 0:b2805b6888dc | 556 | #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ |
robert_jw | 0:b2805b6888dc | 557 | |
robert_jw | 0:b2805b6888dc | 558 | if ((!mem->used) && |
robert_jw | 0:b2805b6888dc | 559 | (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size) { |
robert_jw | 0:b2805b6888dc | 560 | /* mem is not used and at least perfect fit is possible: |
robert_jw | 0:b2805b6888dc | 561 | * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */ |
robert_jw | 0:b2805b6888dc | 562 | |
robert_jw | 0:b2805b6888dc | 563 | if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >= (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED)) { |
robert_jw | 0:b2805b6888dc | 564 | /* (in addition to the above, we test if another struct mem (SIZEOF_STRUCT_MEM) containing |
robert_jw | 0:b2805b6888dc | 565 | * at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem') |
robert_jw | 0:b2805b6888dc | 566 | * -> split large block, create empty remainder, |
robert_jw | 0:b2805b6888dc | 567 | * remainder must be large enough to contain MIN_SIZE_ALIGNED data: if |
robert_jw | 0:b2805b6888dc | 568 | * mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size, |
robert_jw | 0:b2805b6888dc | 569 | * struct mem would fit in but no data between mem2 and mem2->next |
robert_jw | 0:b2805b6888dc | 570 | * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty |
robert_jw | 0:b2805b6888dc | 571 | * region that couldn't hold data, but when mem->next gets freed, |
robert_jw | 0:b2805b6888dc | 572 | * the 2 regions would be combined, resulting in more free memory |
robert_jw | 0:b2805b6888dc | 573 | */ |
robert_jw | 0:b2805b6888dc | 574 | ptr2 = ptr + SIZEOF_STRUCT_MEM + size; |
robert_jw | 0:b2805b6888dc | 575 | /* create mem2 struct */ |
robert_jw | 0:b2805b6888dc | 576 | mem2 = (struct mem *)(void *)&ram[ptr2]; |
robert_jw | 0:b2805b6888dc | 577 | mem2->used = 0; |
robert_jw | 0:b2805b6888dc | 578 | mem2->next = mem->next; |
robert_jw | 0:b2805b6888dc | 579 | mem2->prev = ptr; |
robert_jw | 0:b2805b6888dc | 580 | /* and insert it between mem and mem->next */ |
robert_jw | 0:b2805b6888dc | 581 | mem->next = ptr2; |
robert_jw | 0:b2805b6888dc | 582 | mem->used = 1; |
robert_jw | 0:b2805b6888dc | 583 | |
robert_jw | 0:b2805b6888dc | 584 | if (mem2->next != MEM_SIZE_ALIGNED) { |
robert_jw | 0:b2805b6888dc | 585 | ((struct mem *)(void *)&ram[mem2->next])->prev = ptr2; |
robert_jw | 0:b2805b6888dc | 586 | } |
robert_jw | 0:b2805b6888dc | 587 | MEM_STATS_INC_USED(used, (size + SIZEOF_STRUCT_MEM)); |
robert_jw | 0:b2805b6888dc | 588 | } else { |
robert_jw | 0:b2805b6888dc | 589 | /* (a mem2 struct does no fit into the user data space of mem and mem->next will always |
robert_jw | 0:b2805b6888dc | 590 | * be used at this point: if not we have 2 unused structs in a row, plug_holes should have |
robert_jw | 0:b2805b6888dc | 591 | * take care of this). |
robert_jw | 0:b2805b6888dc | 592 | * -> near fit or excact fit: do not split, no mem2 creation |
robert_jw | 0:b2805b6888dc | 593 | * also can't move mem->next directly behind mem, since mem->next |
robert_jw | 0:b2805b6888dc | 594 | * will always be used at this point! |
robert_jw | 0:b2805b6888dc | 595 | */ |
robert_jw | 0:b2805b6888dc | 596 | mem->used = 1; |
robert_jw | 0:b2805b6888dc | 597 | MEM_STATS_INC_USED(used, mem->next - (mem_size_t)((u8_t *)mem - ram)); |
robert_jw | 0:b2805b6888dc | 598 | } |
robert_jw | 0:b2805b6888dc | 599 | |
robert_jw | 0:b2805b6888dc | 600 | if (mem == lfree) { |
robert_jw | 0:b2805b6888dc | 601 | /* Find next free block after mem and update lowest free pointer */ |
robert_jw | 0:b2805b6888dc | 602 | while (lfree->used && lfree != ram_end) { |
robert_jw | 0:b2805b6888dc | 603 | LWIP_MEM_ALLOC_UNPROTECT(); |
robert_jw | 0:b2805b6888dc | 604 | /* prevent high interrupt latency... */ |
robert_jw | 0:b2805b6888dc | 605 | LWIP_MEM_ALLOC_PROTECT(); |
robert_jw | 0:b2805b6888dc | 606 | lfree = (struct mem *)(void *)&ram[lfree->next]; |
robert_jw | 0:b2805b6888dc | 607 | } |
robert_jw | 0:b2805b6888dc | 608 | LWIP_ASSERT("mem_malloc: !lfree->used", ((lfree == ram_end) || (!lfree->used))); |
robert_jw | 0:b2805b6888dc | 609 | } |
robert_jw | 0:b2805b6888dc | 610 | LWIP_MEM_ALLOC_UNPROTECT(); |
robert_jw | 0:b2805b6888dc | 611 | sys_mutex_unlock(&mem_mutex); |
robert_jw | 0:b2805b6888dc | 612 | LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.", |
robert_jw | 0:b2805b6888dc | 613 | (mem_ptr_t)mem + SIZEOF_STRUCT_MEM + size <= (mem_ptr_t)ram_end); |
robert_jw | 0:b2805b6888dc | 614 | LWIP_ASSERT("mem_malloc: allocated memory properly aligned.", |
robert_jw | 0:b2805b6888dc | 615 | ((mem_ptr_t)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0); |
robert_jw | 0:b2805b6888dc | 616 | LWIP_ASSERT("mem_malloc: sanity check alignment", |
robert_jw | 0:b2805b6888dc | 617 | (((mem_ptr_t)mem) & (MEM_ALIGNMENT-1)) == 0); |
robert_jw | 0:b2805b6888dc | 618 | |
robert_jw | 0:b2805b6888dc | 619 | return (u8_t *)mem + SIZEOF_STRUCT_MEM; |
robert_jw | 0:b2805b6888dc | 620 | } |
robert_jw | 0:b2805b6888dc | 621 | } |
robert_jw | 0:b2805b6888dc | 622 | #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT |
robert_jw | 0:b2805b6888dc | 623 | /* if we got interrupted by a mem_free, try again */ |
robert_jw | 0:b2805b6888dc | 624 | } while(local_mem_free_count != 0); |
robert_jw | 0:b2805b6888dc | 625 | #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ |
robert_jw | 0:b2805b6888dc | 626 | LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("mem_malloc: could not allocate %"S16_F" bytes\n", (s16_t)size)); |
robert_jw | 0:b2805b6888dc | 627 | MEM_STATS_INC(err); |
robert_jw | 0:b2805b6888dc | 628 | LWIP_MEM_ALLOC_UNPROTECT(); |
robert_jw | 0:b2805b6888dc | 629 | sys_mutex_unlock(&mem_mutex); |
robert_jw | 0:b2805b6888dc | 630 | return NULL; |
robert_jw | 0:b2805b6888dc | 631 | } |
robert_jw | 0:b2805b6888dc | 632 | |
robert_jw | 0:b2805b6888dc | 633 | #endif /* MEM_USE_POOLS */ |
robert_jw | 0:b2805b6888dc | 634 | /** |
robert_jw | 0:b2805b6888dc | 635 | * Contiguously allocates enough space for count objects that are size bytes |
robert_jw | 0:b2805b6888dc | 636 | * of memory each and returns a pointer to the allocated memory. |
robert_jw | 0:b2805b6888dc | 637 | * |
robert_jw | 0:b2805b6888dc | 638 | * The allocated memory is filled with bytes of value zero. |
robert_jw | 0:b2805b6888dc | 639 | * |
robert_jw | 0:b2805b6888dc | 640 | * @param count number of objects to allocate |
robert_jw | 0:b2805b6888dc | 641 | * @param size size of the objects to allocate |
robert_jw | 0:b2805b6888dc | 642 | * @return pointer to allocated memory / NULL pointer if there is an error |
robert_jw | 0:b2805b6888dc | 643 | */ |
robert_jw | 0:b2805b6888dc | 644 | void *mem_calloc(mem_size_t count, mem_size_t size) |
robert_jw | 0:b2805b6888dc | 645 | { |
robert_jw | 0:b2805b6888dc | 646 | void *p; |
robert_jw | 0:b2805b6888dc | 647 | |
robert_jw | 0:b2805b6888dc | 648 | /* allocate 'count' objects of size 'size' */ |
robert_jw | 0:b2805b6888dc | 649 | p = mem_malloc(count * size); |
robert_jw | 0:b2805b6888dc | 650 | if (p) { |
robert_jw | 0:b2805b6888dc | 651 | /* zero the memory */ |
robert_jw | 0:b2805b6888dc | 652 | memset(p, 0, count * size); |
robert_jw | 0:b2805b6888dc | 653 | } |
robert_jw | 0:b2805b6888dc | 654 | return p; |
robert_jw | 0:b2805b6888dc | 655 | } |
robert_jw | 0:b2805b6888dc | 656 | |
robert_jw | 0:b2805b6888dc | 657 | #endif /* !MEM_LIBC_MALLOC */ |