Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Mon Jun 16 07:40:08 2014 +0000
Revision:
1:809b59c7a800
Parent:
0:51f1ef89ec7b
mbed-lib and other libs are a based on a project, published in a Elektor-book "ARM-microkontroller Part II".

Who changed what in which revision?

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