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