Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Committer:
hlipka
Date:
Mon Jan 10 21:03:11 2011 +0000
Revision:
0:8b387bed54c2
initial version

Who changed what in which revision?

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