Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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