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