mimil owns / NetServicesProxy

Dependents:   SenseClient

Committer:
mimil
Date:
Tue Sep 06 13:26:45 2011 +0000
Revision:
0:308f83189a3f

        

Who changed what in which revision?

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