Library for Bert van Dam's book "ARM MICROCONTROLLERS" For all chapters with internet.

Dependencies:   mbed

Committer:
ICTFBI
Date:
Fri Oct 16 14:28:26 2015 +0000
Revision:
0:4edb816d21e1
Pre-update 16-10-15

Who changed what in which revision?

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