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