创建mbed

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

Committer:
sunyiming
Date:
Tue Mar 06 08:53:46 2018 +0000
Revision:
1:6465a3f5c58a
??OK

Who changed what in which revision?

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