HTTPClient using static IP

Dependencies:   mbed

Committer:
mr_q
Date:
Mon May 30 11:53:37 2011 +0000
Revision:
0:d8f2f7d5f31b
v0.01 Draft

Who changed what in which revision?

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