Net stack with AutoIP enabled

Dependencies:   mbed

Committer:
darran
Date:
Fri Jul 02 17:21:58 2010 +0000
Revision:
0:ac21159e27f4

        

Who changed what in which revision?

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