3 * Dynamic memory manager
8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
14 * 1. Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * This file is part of the lwIP TCP/IP stack.
35 * Author: Adam Dunkels <adam@sics.se>
41 #include "lwip/arch.h"
48 #include "lwip/stats.h"
50 #if (MEM_LIBC_MALLOC == 0)
51 /* lwIP replacement for your libc malloc() */
54 mem_size_t next, prev;
55 #if MEM_ALIGNMENT == 1
57 #elif MEM_ALIGNMENT == 2
59 #elif MEM_ALIGNMENT == 4
61 #elif MEM_ALIGNMENT == 8
64 #error "unhandled MEM_ALIGNMENT size"
65 #endif /* MEM_ALIGNMENT */
68 static struct mem *ram_end;
71 static u8_t ram[MEM_SIZE + sizeof(struct mem) + MEM_ALIGNMENT];
73 /* Christiaan alignment fix */
75 static struct mem ram_heap[1 + ( (MEM_SIZE + sizeof(struct mem) - 1) / sizeof(struct mem))];
79 #if 0 /* this one does not align correctly for some, resulting in crashes */
80 #define SIZEOF_STRUCT_MEM (unsigned int)MEM_ALIGN_SIZE(sizeof(struct mem))
82 #define SIZEOF_STRUCT_MEM (sizeof(struct mem) + \
83 (((sizeof(struct mem) % MEM_ALIGNMENT) == 0)? 0 : \
84 (4 - (sizeof(struct mem) % MEM_ALIGNMENT))))
87 static struct mem *lfree; /* pointer to the lowest free block */
89 static sys_sem_t mem_sem;
92 plug_holes(struct mem *mem)
97 LWIP_ASSERT("plug_holes: mem >= ram", (u8_t *)mem >= ram);
98 LWIP_ASSERT("plug_holes: mem < ram_end", (u8_t *)mem < (u8_t *)ram_end);
99 LWIP_ASSERT("plug_holes: mem->used == 0", mem->used == 0);
101 /* plug hole forward */
102 LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE", mem->next <= MEM_SIZE);
104 nmem = (struct mem *)&ram[mem->next];
105 if (mem != nmem && nmem->used == 0 && (u8_t *)nmem != (u8_t *)ram_end) {
109 mem->next = nmem->next;
110 ((struct mem *)&ram[nmem->next])->prev = (u8_t *)mem - ram;
113 /* plug hole backward */
114 pmem = (struct mem *)&ram[mem->prev];
115 if (pmem != mem && pmem->used == 0) {
119 pmem->next = mem->next;
120 ((struct mem *)&ram[mem->next])->prev = (u8_t *)pmem - ram;
132 /* Christiaan alignment fix */
133 ram = (u8_t*)ram_heap;
135 memset(ram, 0, MEM_SIZE);
136 mem = (struct mem *)ram;
137 mem->next = MEM_SIZE;
140 ram_end = (struct mem *)&ram[MEM_SIZE];
142 ram_end->next = MEM_SIZE;
143 ram_end->prev = MEM_SIZE;
145 mem_sem = sys_sem_new(1);
147 lfree = (struct mem *)ram;
150 lwip_stats.mem.avail = MEM_SIZE;
151 #endif /* MEM_STATS */
160 LWIP_DEBUGF(MEM_DEBUG | DBG_TRACE | 2, ("mem_free(p == NULL) was called.\n"));
164 sys_sem_wait(mem_sem);
166 LWIP_ASSERT("mem_free: legal memory", (u8_t *)rmem >= (u8_t *)ram &&
167 (u8_t *)rmem < (u8_t *)ram_end);
169 if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) {
170 LWIP_DEBUGF(MEM_DEBUG | 3, ("mem_free: illegal memory\n"));
172 ++lwip_stats.mem.err;
173 #endif /* MEM_STATS */
174 sys_sem_signal(mem_sem);
177 mem = (struct mem *)((u8_t *)rmem - SIZEOF_STRUCT_MEM);
179 LWIP_ASSERT("mem_free: mem->used", mem->used);
188 lwip_stats.mem.used -= mem->next - ((u8_t *)mem - ram);
190 #endif /* MEM_STATS */
192 sys_sem_signal(mem_sem);
196 mem_realloc(void *rmem, mem_size_t newsize)
199 mem_size_t ptr, ptr2;
200 struct mem *mem, *mem2;
202 /* Expand the size of the allocated memory region so that we can
203 adjust for alignment. */
204 if ((newsize % MEM_ALIGNMENT) != 0) {
205 newsize += MEM_ALIGNMENT - ((newsize + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT);
208 if (newsize > MEM_SIZE) {
212 sys_sem_wait(mem_sem);
214 LWIP_ASSERT("mem_realloc: legal memory", (u8_t *)rmem >= (u8_t *)ram &&
215 (u8_t *)rmem < (u8_t *)ram_end);
217 if ((u8_t *)rmem < (u8_t *)ram || (u8_t *)rmem >= (u8_t *)ram_end) {
218 LWIP_DEBUGF(MEM_DEBUG | 3, ("mem_realloc: illegal memory\n"));
221 mem = (struct mem *)((u8_t *)rmem - SIZEOF_STRUCT_MEM);
223 ptr = (u8_t *)mem - ram;
225 size = mem->next - ptr - SIZEOF_STRUCT_MEM;
227 lwip_stats.mem.used -= (size - newsize);
228 #endif /* MEM_STATS */
230 if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size) {
231 ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
232 mem2 = (struct mem *)&ram[ptr2];
234 mem2->next = mem->next;
237 if (mem2->next != MEM_SIZE) {
238 ((struct mem *)&ram[mem2->next])->prev = ptr2;
243 sys_sem_signal(mem_sem);
249 * Adam's mem_malloc(), suffers from bug #17922
250 * Set if to 0 for alternative mem_malloc().
253 mem_malloc(mem_size_t size)
255 mem_size_t ptr, ptr2;
256 struct mem *mem, *mem2;
262 /* Expand the size of the allocated memory region so that we can
263 adjust for alignment. */
264 if ((size % MEM_ALIGNMENT) != 0) {
265 size += MEM_ALIGNMENT - ((size + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT);
268 if (size > MEM_SIZE) {
272 sys_sem_wait(mem_sem);
274 for (ptr = (u8_t *)lfree - ram; ptr < MEM_SIZE; ptr = ((struct mem *)&ram[ptr])->next) {
275 mem = (struct mem *)&ram[ptr];
277 mem->next - (ptr + SIZEOF_STRUCT_MEM) >= size + SIZEOF_STRUCT_MEM) {
278 ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
279 mem2 = (struct mem *)&ram[ptr2];
282 mem2->next = mem->next;
284 if (mem2->next != MEM_SIZE) {
285 ((struct mem *)&ram[mem2->next])->prev = ptr2;
291 lwip_stats.mem.used += (size + SIZEOF_STRUCT_MEM);
292 /* if (lwip_stats.mem.max < lwip_stats.mem.used) {
293 lwip_stats.mem.max = lwip_stats.mem.used;
295 if (lwip_stats.mem.max < ptr2) {
296 lwip_stats.mem.max = ptr2;
298 #endif /* MEM_STATS */
301 /* Find next free block after mem */
302 while (lfree->used && lfree != ram_end) {
303 lfree = (struct mem *)&ram[lfree->next];
305 LWIP_ASSERT("mem_malloc: !lfree->used", !lfree->used);
307 sys_sem_signal(mem_sem);
308 LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.",
309 (mem_ptr_t)mem + SIZEOF_STRUCT_MEM + size <= (mem_ptr_t)ram_end);
310 LWIP_ASSERT("mem_malloc: allocated memory properly aligned.",
311 (unsigned long)((u8_t *)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0);
312 return (u8_t *)mem + SIZEOF_STRUCT_MEM;
315 LWIP_DEBUGF(MEM_DEBUG | 2, ("mem_malloc: could not allocate %"S16_F" bytes\n", (s16_t)size));
317 ++lwip_stats.mem.err;
318 #endif /* MEM_STATS */
319 sys_sem_signal(mem_sem);
324 * Adam's mem_malloc() plus solution for bug #17922
327 mem_malloc(mem_size_t size)
329 mem_size_t ptr, ptr2;
330 struct mem *mem, *mem2;
336 /* Expand the size of the allocated memory region so that we can
337 adjust for alignment. */
338 if ((size % MEM_ALIGNMENT) != 0) {
339 size += MEM_ALIGNMENT - ((size + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT);
342 if (size > MEM_SIZE) {
346 sys_sem_wait(mem_sem);
348 for (ptr = (u8_t *)lfree - ram; ptr < MEM_SIZE - size; ptr = ((struct mem *)&ram[ptr])->next) {
349 mem = (struct mem *)&ram[ptr];
353 ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
355 if (mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) >= size) {
356 /* split large block, create empty remainder */
359 /* create mem2 struct */
360 mem2 = (struct mem *)&ram[ptr2];
362 mem2->next = mem->next;
365 if (mem2->next != MEM_SIZE) {
366 ((struct mem *)&ram[mem2->next])->prev = ptr2;
369 else if (mem->next - (ptr + SIZEOF_STRUCT_MEM) > size) {
370 /* near fit, no split, no mem2 creation,
371 round up to mem->next */
375 else if (mem->next - (ptr + SIZEOF_STRUCT_MEM) == size) {
376 /* exact fit, do not split, no mem2 creation */
383 lwip_stats.mem.used += (size + SIZEOF_STRUCT_MEM);
384 if (lwip_stats.mem.max < ptr2) {
385 lwip_stats.mem.max = ptr2;
387 #endif /* MEM_STATS */
389 /* Find next free block after mem */
390 while (lfree->used && lfree != ram_end) {
391 lfree = (struct mem *)&ram[lfree->next];
393 LWIP_ASSERT("mem_malloc: !lfree->used", !lfree->used);
395 sys_sem_signal(mem_sem);
396 LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.",
397 (mem_ptr_t)mem + SIZEOF_STRUCT_MEM + size <= (mem_ptr_t)ram_end);
398 LWIP_ASSERT("mem_malloc: allocated memory properly aligned.",
399 (unsigned long)((u8_t *)mem + SIZEOF_STRUCT_MEM) % MEM_ALIGNMENT == 0);
400 return (u8_t *)mem + SIZEOF_STRUCT_MEM;
404 LWIP_DEBUGF(MEM_DEBUG | 2, ("mem_malloc: could not allocate %"S16_F" bytes\n", (s16_t)size));
406 ++lwip_stats.mem.err;
407 #endif /* MEM_STATS */
408 sys_sem_signal(mem_sem);
413 #endif /* MEM_LIBC_MALLOC == 0 */