Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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