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

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

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