Web server based weather station using Sparkfun Weather Meters.

Dependencies:   FatFileSystem mbed WeatherMeters SDFileSystem

Committer:
AdamGreen
Date:
Thu Feb 23 21:38:39 2012 +0000
Revision:
0:616601bde9fb

        

Who changed what in which revision?

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