Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

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