NetServices Stack source

Dependents:   HelloWorld ServoInterfaceBoardExample1 4180_Lab4

Committer:
donatien
Date:
Fri Jun 11 16:05:15 2010 +0000
Revision:
0:632c9925f013
Child:
5:dd63a1e02b1b

        

Who changed what in which revision?

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