Ian Craggs / Mbed 2 deprecated IBMIoTClientEthernetExample-MACfix-Debug

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample-MACfix-DebugFix by James Sutton

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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