I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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