SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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