Bonjour/Zerconf library

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

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