Port of LwIP performed by Ralf in 2010. Not recommended for use with recent mbed libraries, but good demos of raw LwIP possible

Dependents:   LwIP_raw_API_serverExample tiny-dtls

Committer:
RodColeman
Date:
Tue Sep 18 14:41:24 2012 +0000
Revision:
0:0791c1fece8e
[mbed] converted /Eth_TCP_Wei_Server/lwip

Who changed what in which revision?

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