Ethernetwebsoc

Dependencies:   C12832_lcd LM75B WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
GordonSin
Date:
Fri May 31 04:09:54 2013 +0000
Revision:
0:0ed2a7c7190c
31/5/2013;

Who changed what in which revision?

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