Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mem.c
00001 /** 00002 * @file 00003 * Dynamic memory manager 00004 * 00005 * This is a lightweight replacement for the standard C library malloc(). 00006 * 00007 * If you want to use the standard C library malloc() instead, define 00008 * MEM_LIBC_MALLOC to 1 in your lwipopts.h 00009 * 00010 * To let mem_malloc() use pools (prevents fragmentation and is much faster than 00011 * a heap but might waste some memory), define MEM_USE_POOLS to 1, define 00012 * MEM_USE_CUSTOM_POOLS to 1 and create a file "lwippools.h" that includes a list 00013 * of pools like this (more pools can be added between _START and _END): 00014 * 00015 * Define three pools with sizes 256, 512, and 1512 bytes 00016 * LWIP_MALLOC_MEMPOOL_START 00017 * LWIP_MALLOC_MEMPOOL(20, 256) 00018 * LWIP_MALLOC_MEMPOOL(10, 512) 00019 * LWIP_MALLOC_MEMPOOL(5, 1512) 00020 * LWIP_MALLOC_MEMPOOL_END 00021 */ 00022 00023 /* 00024 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00025 * All rights reserved. 00026 * 00027 * Redistribution and use in source and binary forms, with or without modification, 00028 * are permitted provided that the following conditions are met: 00029 * 00030 * 1. Redistributions of source code must retain the above copyright notice, 00031 * this list of conditions and the following disclaimer. 00032 * 2. Redistributions in binary form must reproduce the above copyright notice, 00033 * this list of conditions and the following disclaimer in the documentation 00034 * and/or other materials provided with the distribution. 00035 * 3. The name of the author may not be used to endorse or promote products 00036 * derived from this software without specific prior written permission. 00037 * 00038 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00039 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00040 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00041 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00042 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00043 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00044 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00045 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00046 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00047 * OF SUCH DAMAGE. 00048 * 00049 * This file is part of the lwIP TCP/IP stack. 00050 * 00051 * Author: Adam Dunkels <adam@sics.se> 00052 * Simon Goldschmidt 00053 * 00054 */ 00055 00056 #include "lwip/opt.h" 00057 00058 #if !MEM_LIBC_MALLOC /* don't build if not configured for use in lwipopts.h */ 00059 00060 #include "lwip/def.h" 00061 #include "lwip/mem.h" 00062 #include "lwip/sys.h" 00063 #include "lwip/stats.h" 00064 00065 #include <string.h> 00066 00067 #if MEM_USE_POOLS 00068 /* lwIP head implemented with different sized pools */ 00069 00070 /** 00071 * Allocate memory: determine the smallest pool that is big enough 00072 * to contain an element of 'size' and get an element from that pool. 00073 * 00074 * @param size the size in bytes of the memory needed 00075 * @return a pointer to the allocated memory or NULL if the pool is empty 00076 */ 00077 void * 00078 mem_malloc(mem_size_t size) 00079 { 00080 struct memp_malloc_helper *element; 00081 memp_t poolnr; 00082 mem_size_t required_size = size + sizeof(struct memp_malloc_helper); 00083 00084 for (poolnr = MEMP_POOL_FIRST; poolnr <= MEMP_POOL_LAST; poolnr++) { 00085 #if MEM_USE_POOLS_TRY_BIGGER_POOL 00086 again: 00087 #endif /* MEM_USE_POOLS_TRY_BIGGER_POOL */ 00088 /* is this pool big enough to hold an element of the required size 00089 plus a struct memp_malloc_helper that saves the pool this element came from? */ 00090 if (required_size <= memp_sizes[poolnr]) { 00091 break; 00092 } 00093 } 00094 if (poolnr > MEMP_POOL_LAST) { 00095 LWIP_ASSERT("mem_malloc(): no pool is that big!", 0); 00096 return NULL; 00097 } 00098 element = (struct memp_malloc_helper*)memp_malloc(poolnr); 00099 if (element == NULL) { 00100 /* No need to DEBUGF or ASSERT: This error is already 00101 taken care of in memp.c */ 00102 #if MEM_USE_POOLS_TRY_BIGGER_POOL 00103 /** Try a bigger pool if this one is empty! */ 00104 if (poolnr < MEMP_POOL_LAST) { 00105 poolnr++; 00106 goto again; 00107 } 00108 #endif /* MEM_USE_POOLS_TRY_BIGGER_POOL */ 00109 return NULL; 00110 } 00111 00112 /* save the pool number this element came from */ 00113 element->poolnr = poolnr; 00114 /* and return a pointer to the memory directly after the struct memp_malloc_helper */ 00115 element++; 00116 00117 return element; 00118 } 00119 00120 /** 00121 * Free memory previously allocated by mem_malloc. Loads the pool number 00122 * and calls memp_free with that pool number to put the element back into 00123 * its pool 00124 * 00125 * @param rmem the memory element to free 00126 */ 00127 void 00128 mem_free(void *rmem) 00129 { 00130 struct memp_malloc_helper *hmem = (struct memp_malloc_helper*)rmem; 00131 00132 LWIP_ASSERT("rmem != NULL", (rmem != NULL)); 00133 LWIP_ASSERT("rmem == MEM_ALIGN(rmem)", (rmem == LWIP_MEM_ALIGN(rmem))); 00134 00135 /* get the original struct memp_malloc_helper */ 00136 hmem--; 00137 00138 LWIP_ASSERT("hmem != NULL", (hmem != NULL)); 00139 LWIP_ASSERT("hmem == MEM_ALIGN(hmem)", (hmem == LWIP_MEM_ALIGN(hmem))); 00140 LWIP_ASSERT("hmem->poolnr < MEMP_MAX", (hmem->poolnr < MEMP_MAX)); 00141 00142 /* and put it in the pool we saved earlier */ 00143 memp_free(hmem->poolnr, hmem); 00144 } 00145 00146 #else /* MEM_USE_POOLS */ 00147 /* lwIP replacement for your libc malloc() */ 00148 00149 /** 00150 * The heap is made up as a list of structs of this type. 00151 * This does not have to be aligned since for getting its size, 00152 * we only use the macro SIZEOF_STRUCT_MEM, which automatically alignes. 00153 */ 00154 struct mem { 00155 /** index (-> ram[next]) of the next struct */ 00156 mem_size_t next; 00157 /** index (-> ram[next]) of the next struct */ 00158 mem_size_t prev; 00159 /** 1: this area is used; 0: this area is unused */ 00160 u8_t used; 00161 }; 00162 00163 /** All allocated blocks will be MIN_SIZE bytes big, at least! 00164 * MIN_SIZE can be overridden to suit your needs. Smaller values save space, 00165 * larger values could prevent too small blocks to fragment the RAM too much. */ 00166 #ifndef MIN_SIZE 00167 #define MIN_SIZE 12 00168 #endif /* MIN_SIZE */ 00169 /* some alignment macros: we define them here for better source code layout */ 00170 #define MIN_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MIN_SIZE) 00171 #define SIZEOF_STRUCT_MEM LWIP_MEM_ALIGN_SIZE(sizeof(struct mem)) 00172 #define MEM_SIZE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEM_SIZE) 00173 00174 /** the heap. we need one struct mem at the end and some room for alignment */ 00175 static u8_t ram_heap[MEM_SIZE_ALIGNED + (2*SIZEOF_STRUCT_MEM) + MEM_ALIGNMENT] MEM_POSITION; 00176 /** pointer to the heap (ram_heap): for alignment, ram is now a pointer instead of an array */ 00177 static u8_t *ram; 00178 /** the last entry, always unused! */ 00179 static struct mem *ram_end; 00180 /** pointer to the lowest free block, this is used for faster search */ 00181 static struct mem *lfree; 00182 00183 /** concurrent access protection */ 00184 static sys_sem_t mem_sem; 00185 00186 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT 00187 00188 static volatile u8_t mem_free_count; 00189 00190 /* Allow mem_free from other (e.g. interrupt) context */ 00191 #define LWIP_MEM_FREE_DECL_PROTECT() SYS_ARCH_DECL_PROTECT(lev_free) 00192 #define LWIP_MEM_FREE_PROTECT() SYS_ARCH_PROTECT(lev_free) 00193 #define LWIP_MEM_FREE_UNPROTECT() SYS_ARCH_UNPROTECT(lev_free) 00194 #define LWIP_MEM_ALLOC_DECL_PROTECT() SYS_ARCH_DECL_PROTECT(lev_alloc) 00195 #define LWIP_MEM_ALLOC_PROTECT() SYS_ARCH_PROTECT(lev_alloc) 00196 #define LWIP_MEM_ALLOC_UNPROTECT() SYS_ARCH_UNPROTECT(lev_alloc) 00197 00198 #else /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ 00199 00200 /* Protect the heap only by using a semaphore */ 00201 #define LWIP_MEM_FREE_DECL_PROTECT() 00202 #define LWIP_MEM_FREE_PROTECT() sys_arch_sem_wait(mem_sem, 0) 00203 #define LWIP_MEM_FREE_UNPROTECT() sys_sem_signal(mem_sem) 00204 /* mem_malloc is protected using semaphore AND LWIP_MEM_ALLOC_PROTECT */ 00205 #define LWIP_MEM_ALLOC_DECL_PROTECT() 00206 #define LWIP_MEM_ALLOC_PROTECT() 00207 #define LWIP_MEM_ALLOC_UNPROTECT() 00208 00209 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ 00210 00211 00212 /** 00213 * "Plug holes" by combining adjacent empty struct mems. 00214 * After this function is through, there should not exist 00215 * one empty struct mem pointing to another empty struct mem. 00216 * 00217 * @param mem this points to a struct mem which just has been freed 00218 * @internal this function is only called by mem_free() and mem_realloc() 00219 * 00220 * This assumes access to the heap is protected by the calling function 00221 * already. 00222 */ 00223 static void 00224 plug_holes(struct mem *mem) 00225 { 00226 struct mem *nmem; 00227 struct mem *pmem; 00228 00229 LWIP_ASSERT("plug_holes: mem >= ram", (u8_t *)mem >= ram); 00230 LWIP_ASSERT("plug_holes: mem < ram_end", (u8_t *)mem < (u8_t *)ram_end); 00231 LWIP_ASSERT("plug_holes: mem->used == 0", mem->used == 0); 00232 00233 /* plug hole forward */ 00234 LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE_ALIGNED", mem->next <= MEM_SIZE_ALIGNED); 00235 00236 nmem = (struct mem *)&ram[mem->next]; 00237 if (mem != nmem && nmem->used == 0 && (u8_t *)nmem != (u8_t *)ram_end) { 00238 /* if mem->next is unused and not end of ram, combine mem and mem->next */ 00239 if (lfree == nmem) { 00240 lfree = mem; 00241 } 00242 mem->next = nmem->next; 00243 ((struct mem *)&ram[nmem->next])->prev = (u8_t *)mem - ram; 00244 } 00245 00246 /* plug hole backward */ 00247 pmem = (struct mem *)&ram[mem->prev]; 00248 if (pmem != mem && pmem->used == 0) { 00249 /* if mem->prev is unused, combine mem and mem->prev */ 00250 if (lfree == mem) { 00251 lfree = pmem; 00252 } 00253 pmem->next = mem->next; 00254 ((struct mem *)&ram[mem->next])->prev = (u8_t *)pmem - ram; 00255 } 00256 } 00257 00258 /** 00259 * Zero the heap and initialize start, end and lowest-free 00260 */ 00261 void 00262 mem_init(void) 00263 { 00264 struct mem *mem; 00265 00266 LWIP_ASSERT("Sanity check alignment", 00267 (SIZEOF_STRUCT_MEM & (MEM_ALIGNMENT-1)) == 0); 00268 00269 /* align the heap */ 00270 ram = (u8_t *) LWIP_MEM_ALIGN(ram_heap); 00271 /* initialize the start of the heap */ 00272 mem = (struct mem *)ram; 00273 mem->next = MEM_SIZE_ALIGNED; 00274 mem->prev = 0; 00275 mem->used = 0; 00276 /* initialize the end of the heap */ 00277 ram_end = (struct mem *)&ram[MEM_SIZE_ALIGNED]; 00278 ram_end->used = 1; 00279 ram_end->next = MEM_SIZE_ALIGNED; 00280 ram_end->prev = MEM_SIZE_ALIGNED; 00281 00282 mem_sem = sys_sem_new(1); 00283 00284 /* initialize the lowest-free pointer to the start of the heap */ 00285 lfree = (struct mem *)ram; 00286 00287 MEM_STATS_AVAIL(avail, MEM_SIZE_ALIGNED); 00288 } 00289 00290 /** 00291 * Put a struct mem back on the heap 00292 * 00293 * @param rmem is the data portion of a struct mem as returned by a previous 00294 * call to mem_malloc() 00295 */ 00296 void 00297 mem_free(void *rmem) 00298 { 00299 struct mem *mem; 00300 LWIP_MEM_FREE_DECL_PROTECT(); 00301 00302 if (rmem == NULL) { 00303 LWIP_DEBUGF(MEM_DEBUG | LWIP_DBG_TRACE | 2, ("mem_free(p == NULL) was called.\n")); 00304 return; 00305 } 00306 LWIP_ASSERT("mem_free: sanity check alignment", (((mem_ptr_t)rmem) & (MEM_ALIGNMENT-1)) == 0); 00307 00308 LWIP_ASSERT("mem_free: legal memory", (u8_t *)rmem >= (u8_t *)ram && 00309 (u8_t *)rmem < (u8_t *)ram_end); 00310 00311 if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) { 00312 SYS_ARCH_DECL_PROTECT(lev); 00313 LWIP_DEBUGF(MEM_DEBUG | 3, ("mem_free: illegal memory\n")); 00314 /* protect mem stats from concurrent access */ 00315 SYS_ARCH_PROTECT(lev); 00316 MEM_STATS_INC(illegal); 00317 SYS_ARCH_UNPROTECT(lev); 00318 return; 00319 } 00320 /* protect the heap from concurrent access */ 00321 LWIP_MEM_FREE_PROTECT(); 00322 /* Get the corresponding struct mem ... */ 00323 mem = (struct mem *)((u8_t *)rmem - SIZEOF_STRUCT_MEM); 00324 /* ... which has to be in a used state ... */ 00325 LWIP_ASSERT("mem_free: mem->used", mem->used); 00326 /* ... and is now unused. */ 00327 mem->used = 0; 00328 00329 if (mem < lfree) { 00330 /* the newly freed struct is now the lowest */ 00331 lfree = mem; 00332 } 00333 00334 MEM_STATS_DEC_USED(used, mem->next - ((u8_t *)mem - ram)); 00335 00336 /* finally, see if prev or next are free also */ 00337 plug_holes(mem); 00338 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT 00339 mem_free_count = 1; 00340 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ 00341 LWIP_MEM_FREE_UNPROTECT(); 00342 } 00343 00344 /** 00345 * In contrast to its name, mem_realloc can only shrink memory, not expand it. 00346 * Since the only use (for now) is in pbuf_realloc (which also can only shrink), 00347 * this shouldn't be a problem! 00348 * 00349 * @param rmem pointer to memory allocated by mem_malloc the is to be shrinked 00350 * @param newsize required size after shrinking (needs to be smaller than or 00351 * equal to the previous size) 00352 * @return for compatibility reasons: is always == rmem, at the moment 00353 * or NULL if newsize is > old size, in which case rmem is NOT touched 00354 * or freed! 00355 */ 00356 void * 00357 mem_realloc(void *rmem, mem_size_t newsize) 00358 { 00359 mem_size_t size; 00360 mem_size_t ptr, ptr2; 00361 struct mem *mem, *mem2; 00362 /* use the FREE_PROTECT here: it protects with sem OR SYS_ARCH_PROTECT */ 00363 LWIP_MEM_FREE_DECL_PROTECT(); 00364 00365 /* Expand the size of the allocated memory region so that we can 00366 adjust for alignment. */ 00367 newsize = LWIP_MEM_ALIGN_SIZE(newsize); 00368 00369 if(newsize < MIN_SIZE_ALIGNED) { 00370 /* every data block must be at least MIN_SIZE_ALIGNED long */ 00371 newsize = MIN_SIZE_ALIGNED; 00372 } 00373 00374 if (newsize > MEM_SIZE_ALIGNED) { 00375 return NULL; 00376 } 00377 00378 LWIP_ASSERT("mem_realloc: legal memory", (u8_t *)rmem >= (u8_t *)ram && 00379 (u8_t *)rmem < (u8_t *)ram_end); 00380 00381 if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) { 00382 SYS_ARCH_DECL_PROTECT(lev); 00383 LWIP_DEBUGF(MEM_DEBUG | 3, ("mem_realloc: illegal memory\n")); 00384 /* protect mem stats from concurrent access */ 00385 SYS_ARCH_PROTECT(lev); 00386 MEM_STATS_INC(illegal); 00387 SYS_ARCH_UNPROTECT(lev); 00388 return rmem; 00389 } 00390 /* Get the corresponding struct mem ... */ 00391 mem = (struct mem *)((u8_t *)rmem - SIZEOF_STRUCT_MEM); 00392 /* ... and its offset pointer */ 00393 ptr = (u8_t *)mem - ram; 00394 00395 size = mem->next - ptr - SIZEOF_STRUCT_MEM; 00396 LWIP_ASSERT("mem_realloc can only shrink memory", newsize <= size); 00397 if (newsize > size) { 00398 /* not supported */ 00399 return NULL; 00400 } 00401 if (newsize == size) { 00402 /* No change in size, simply return */ 00403 return rmem; 00404 } 00405 00406 /* protect the heap from concurrent access */ 00407 LWIP_MEM_FREE_PROTECT(); 00408 00409 MEM_STATS_DEC_USED(used, (size - newsize)); 00410 00411 mem2 = (struct mem *)&ram[mem->next]; 00412 if(mem2->used == 0) { 00413 /* The next struct is unused, we can simply move it at little */ 00414 mem_size_t next; 00415 /* remember the old next pointer */ 00416 next = mem2->next; 00417 /* create new struct mem which is moved directly after the shrinked mem */ 00418 ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize; 00419 if (lfree == mem2) { 00420 lfree = (struct mem *)&ram[ptr2]; 00421 } 00422 mem2 = (struct mem *)&ram[ptr2]; 00423 mem2->used = 0; 00424 /* restore the next pointer */ 00425 mem2->next = next; 00426 /* link it back to mem */ 00427 mem2->prev = ptr; 00428 /* link mem to it */ 00429 mem->next = ptr2; 00430 /* last thing to restore linked list: as we have moved mem2, 00431 * let 'mem2->next->prev' point to mem2 again. but only if mem2->next is not 00432 * the end of the heap */ 00433 if (mem2->next != MEM_SIZE_ALIGNED) { 00434 ((struct mem *)&ram[mem2->next])->prev = ptr2; 00435 } 00436 /* no need to plug holes, we've already done that */ 00437 } else if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED <= size) { 00438 /* Next struct is used but there's room for another struct mem with 00439 * at least MIN_SIZE_ALIGNED of data. 00440 * Old size ('size') must be big enough to contain at least 'newsize' plus a struct mem 00441 * ('SIZEOF_STRUCT_MEM') with some data ('MIN_SIZE_ALIGNED'). 00442 * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty 00443 * region that couldn't hold data, but when mem->next gets freed, 00444 * the 2 regions would be combined, resulting in more free memory */ 00445 ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize; 00446 mem2 = (struct mem *)&ram[ptr2]; 00447 if (mem2 < lfree) { 00448 lfree = mem2; 00449 } 00450 mem2->used = 0; 00451 mem2->next = mem->next; 00452 mem2->prev = ptr; 00453 mem->next = ptr2; 00454 if (mem2->next != MEM_SIZE_ALIGNED) { 00455 ((struct mem *)&ram[mem2->next])->prev = ptr2; 00456 } 00457 /* the original mem->next is used, so no need to plug holes! */ 00458 } 00459 /* else { 00460 next struct mem is used but size between mem and mem2 is not big enough 00461 to create another struct mem 00462 -> don't do anyhting. 00463 -> the remaining space stays unused since it is too small 00464 } */ 00465 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT 00466 mem_free_count = 1; 00467 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ 00468 LWIP_MEM_FREE_UNPROTECT(); 00469 return rmem; 00470 } 00471 00472 /** 00473 * Adam's mem_malloc() plus solution for bug #17922 00474 * Allocate a block of memory with a minimum of 'size' bytes. 00475 * 00476 * @param size is the minimum size of the requested block in bytes. 00477 * @return pointer to allocated memory or NULL if no free memory was found. 00478 * 00479 * Note that the returned value will always be aligned (as defined by MEM_ALIGNMENT). 00480 */ 00481 void * 00482 mem_malloc(mem_size_t size) 00483 { 00484 mem_size_t ptr, ptr2; 00485 struct mem *mem, *mem2; 00486 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT 00487 u8_t local_mem_free_count = 0; 00488 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ 00489 LWIP_MEM_ALLOC_DECL_PROTECT(); 00490 00491 if (size == 0) { 00492 return NULL; 00493 } 00494 00495 /* Expand the size of the allocated memory region so that we can 00496 adjust for alignment. */ 00497 size = LWIP_MEM_ALIGN_SIZE(size); 00498 00499 if(size < MIN_SIZE_ALIGNED) { 00500 /* every data block must be at least MIN_SIZE_ALIGNED long */ 00501 size = MIN_SIZE_ALIGNED; 00502 } 00503 00504 if (size > MEM_SIZE_ALIGNED) { 00505 return NULL; 00506 } 00507 00508 /* protect the heap from concurrent access */ 00509 sys_arch_sem_wait(mem_sem, 0); 00510 LWIP_MEM_ALLOC_PROTECT(); 00511 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT 00512 /* run as long as a mem_free disturbed mem_malloc */ 00513 do { 00514 local_mem_free_count = 0; 00515 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ 00516 00517 /* Scan through the heap searching for a free block that is big enough, 00518 * beginning with the lowest free block. 00519 */ 00520 for (ptr = (u8_t *)lfree - ram; ptr < MEM_SIZE_ALIGNED - size; 00521 ptr = ((struct mem *)&ram[ptr])->next) { 00522 mem = (struct mem *)&ram[ptr]; 00523 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT 00524 mem_free_count = 0; 00525 LWIP_MEM_ALLOC_UNPROTECT(); 00526 /* allow mem_free to run */ 00527 LWIP_MEM_ALLOC_PROTECT(); 00528 if (mem_free_count != 0) { 00529 local_mem_free_count = mem_free_count; 00530 } 00531 mem_free_count = 0; 00532 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ 00533 00534 if ((!mem->used) && 00535 (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size) { 00536 /* mem is not used and at least perfect fit is possible: 00537 * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */ 00538 00539 if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >= (size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED)) { 00540 /* (in addition to the above, we test if another struct mem (SIZEOF_STRUCT_MEM) containing 00541 * at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem') 00542 * -> split large block, create empty remainder, 00543 * remainder must be large enough to contain MIN_SIZE_ALIGNED data: if 00544 * mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size, 00545 * struct mem would fit in but no data between mem2 and mem2->next 00546 * @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty 00547 * region that couldn't hold data, but when mem->next gets freed, 00548 * the 2 regions would be combined, resulting in more free memory 00549 */ 00550 ptr2 = ptr + SIZEOF_STRUCT_MEM + size; 00551 /* create mem2 struct */ 00552 mem2 = (struct mem *)&ram[ptr2]; 00553 mem2->used = 0; 00554 mem2->next = mem->next; 00555 mem2->prev = ptr; 00556 /* and insert it between mem and mem->next */ 00557 mem->next = ptr2; 00558 mem->used = 1; 00559 00560 if (mem2->next != MEM_SIZE_ALIGNED) { 00561 ((struct mem *)&ram[mem2->next])->prev = ptr2; 00562 } 00563 MEM_STATS_INC_USED(used, (size + SIZEOF_STRUCT_MEM)); 00564 } else { 00565 /* (a mem2 struct does no fit into the user data space of mem and mem->next will always 00566 * be used at this point: if not we have 2 unused structs in a row, plug_holes should have 00567 * take care of this). 00568 * -> near fit or excact fit: do not split, no mem2 creation 00569 * also can't move mem->next directly behind mem, since mem->next 00570 * will always be used at this point! 00571 */ 00572 mem->used = 1; 00573 MEM_STATS_INC_USED(used, mem->next - ((u8_t *)mem - ram)); 00574 } 00575 00576 if (mem == lfree) { 00577 /* Find next free block after mem and update lowest free pointer */ 00578 while (lfree->used && lfree != ram_end) { 00579 LWIP_MEM_ALLOC_UNPROTECT(); 00580 /* prevent high interrupt latency... */ 00581 LWIP_MEM_ALLOC_PROTECT(); 00582 lfree = (struct mem *)&ram[lfree->next]; 00583 } 00584 LWIP_ASSERT("mem_malloc: !lfree->used", ((lfree == ram_end) || (!lfree->used))); 00585 } 00586 LWIP_MEM_ALLOC_UNPROTECT(); 00587 sys_sem_signal(mem_sem); 00588 LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.", 00589 (mem_ptr_t)mem + SIZEOF_STRUCT_MEM + size <= (mem_ptr_t)ram_end); 00590 LWIP_ASSERT("mem_malloc: allocated memory properly aligned.", 00591 ((mem_ptr_t)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0); 00592 LWIP_ASSERT("mem_malloc: sanity check alignment", 00593 (((mem_ptr_t)mem) & (MEM_ALIGNMENT-1)) == 0); 00594 00595 return (u8_t *)mem + SIZEOF_STRUCT_MEM; 00596 } 00597 } 00598 #if LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT 00599 /* if we got interrupted by a mem_free, try again */ 00600 } while(local_mem_free_count != 0); 00601 #endif /* LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT */ 00602 LWIP_DEBUGF(MEM_DEBUG | 2, ("mem_malloc: could not allocate %"S16_F" bytes\n", (s16_t)size)); 00603 MEM_STATS_INC(err); 00604 LWIP_MEM_ALLOC_UNPROTECT(); 00605 sys_sem_signal(mem_sem); 00606 return NULL; 00607 } 00608 00609 #endif /* MEM_USE_POOLS */ 00610 /** 00611 * Contiguously allocates enough space for count objects that are size bytes 00612 * of memory each and returns a pointer to the allocated memory. 00613 * 00614 * The allocated memory is filled with bytes of value zero. 00615 * 00616 * @param count number of objects to allocate 00617 * @param size size of the objects to allocate 00618 * @return pointer to allocated memory / NULL pointer if there is an error 00619 */ 00620 void *mem_calloc(mem_size_t count, mem_size_t size) 00621 { 00622 void *p; 00623 00624 /* allocate 'count' objects of size 'size' */ 00625 p = mem_malloc(count * size); 00626 if (p) { 00627 /* zero the memory */ 00628 memset(p, 0, count * size); 00629 } 00630 return p; 00631 } 00632 00633 #endif /* !MEM_LIBC_MALLOC */
Generated on Tue Jul 12 2022 20:39:37 by
