Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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