Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Thu Sep 26 00:44:20 2013 -0500
Revision:
5:3f93dd1d4cb3
Exported program and replaced contents of the repo with the source
to build and debug using keil mdk. Libs NOT upto date are lwip, lwip-sys
and socket. these have newer versions under mbed_official but were starting
from a know working point

Who changed what in which revision?

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