This library is stripped down version of NetServices library. HTTP server and client function is NOT supported.

Dependents:   imu-daq-eth

Committer:
idinor
Date:
Wed Jul 20 11:45:39 2011 +0000
Revision:
0:dcf3c92487ca

        

Who changed what in which revision?

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