Donatien Garnier / ZG2100NetIfSource
Committer:
donatien
Date:
Fri Aug 06 10:23:41 2010 +0000
Revision:
1:3a7c15057192
Parent:
0:b802fc31f1db

        

Who changed what in which revision?

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