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