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