3 * Dynamic memory manager
8 * Copyright (c) 2001-2003 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>
40 #include "lwip/arch.h"
47 #include "lwip/stats.h"
55 mem_size_t next
, prev
;
56 #if MEM_ALIGNMENT == 1
58 #elif MEM_ALIGNMENT == 2
60 #elif MEM_ALIGNMENT == 4
63 #error "unhandled MEM_ALIGNMENT size"
64 #endif /* MEM_ALIGNMENT */
67 static struct mem
*ram_end
;
69 static u8_t ramblock
[MEM_SIZE
+ sizeof(struct mem
) + MEM_ALIGNMENT
];
72 #if 0 /* this one does not align correctly for some, resulting in crashes */
73 #define SIZEOF_STRUCT_MEM (unsigned int)MEM_ALIGN_SIZE(sizeof(struct mem))
75 #define SIZEOF_STRUCT_MEM (sizeof(struct mem) + \
76 (((sizeof(struct mem) % MEM_ALIGNMENT) == 0)? 0 : \
77 (4 - (sizeof(struct mem) % MEM_ALIGNMENT))))
81 static struct mem
*lfree
; /* pointer to the lowest free block */
83 static sys_sem_t mem_sem
;
86 plug_holes(struct mem
*mem
)
91 LWIP_ASSERT("plug_holes: mem >= ram", (u8_t
*)mem
>= ram
);
92 LWIP_ASSERT("plug_holes: mem < ram_end", (u8_t
*)mem
< (u8_t
*)ram_end
);
93 LWIP_ASSERT("plug_holes: mem->used == 0", mem
->used
== 0);
95 /* plug hole forward */
96 LWIP_ASSERT("plug_holes: mem->next <= MEM_SIZE", mem
->next
<= MEM_SIZE
);
98 nmem
= (struct mem
*)&ram
[mem
->next
];
99 if (mem
!= nmem
&& nmem
->used
== 0 && (u8_t
*)nmem
!= (u8_t
*)ram_end
) {
103 mem
->next
= nmem
->next
;
104 ((struct mem
*)&ram
[nmem
->next
])->prev
= (u8_t
*)mem
- ram
;
107 /* plug hole backward */
108 pmem
= (struct mem
*)&ram
[mem
->prev
];
109 if (pmem
!= mem
&& pmem
->used
== 0) {
113 pmem
->next
= mem
->next
;
114 ((struct mem
*)&ram
[mem
->next
])->prev
= (u8_t
*)pmem
- ram
;
123 //Boman666: Originally ram was the array now called ramblock. I didn't experience any problem but ram could end up incorrecly
124 //aligned, causing a crash.
125 ram
=MEM_ALIGN(ramblock
+MEM_ALIGNMENT
-1);
126 mips_memset(ram
, 0, MEM_SIZE
);
127 mem
= (struct mem
*)ram
;
128 mem
->next
= MEM_SIZE
;
131 ram_end
= (struct mem
*)&ram
[MEM_SIZE
];
133 ram_end
->next
= MEM_SIZE
;
134 ram_end
->prev
= MEM_SIZE
;
136 mem_sem
= sys_sem_new(1);
138 lfree
= (struct mem
*)ram
;
141 lwip_stats
.mem
.avail
= MEM_SIZE
;
142 #endif /* MEM_STATS */
151 LWIP_DEBUGF(MEM_DEBUG
| DBG_TRACE
| 2, ("mem_free(p == NULL) was called.\n"));
155 sys_sem_wait(mem_sem
);
157 LWIP_ASSERT("mem_free: legal memory", (u8_t
*)rmem
>= (u8_t
*)ram
&&
158 (u8_t
*)rmem
< (u8_t
*)ram_end
);
160 if ((u8_t
*)rmem
< (u8_t
*)ram
|| (u8_t
*)rmem
>= (u8_t
*)ram_end
) {
161 LWIP_DEBUGF(MEM_DEBUG
| 3, ("mem_free: illegal memory\n"));
163 ++lwip_stats
.mem
.err
;
164 #endif /* MEM_STATS */
165 sys_sem_signal(mem_sem
);
168 mem
= (struct mem
*)((u8_t
*)rmem
- SIZEOF_STRUCT_MEM
);
170 LWIP_ASSERT("mem_free: mem->used", mem
->used
);
179 lwip_stats
.mem
.used
-= mem
->next
- ((u8_t
*)mem
- ram
);
181 #endif /* MEM_STATS */
183 sys_sem_signal(mem_sem
);
186 mem_reallocm(void *rmem
, mem_size_t newsize
)
189 nmem
= mem_malloc(newsize
);
191 return mem_realloc(rmem
, newsize
);
193 mips_memcpy(nmem
, rmem
, newsize
);
199 mem_realloc(void *rmem
, mem_size_t newsize
)
202 mem_size_t ptr
, ptr2
;
203 struct mem
*mem
, *mem2
;
205 /* Expand the size of the allocated memory region so that we can
206 adjust for alignment. */
207 if ((newsize
% MEM_ALIGNMENT
) != 0) {
208 newsize
+= MEM_ALIGNMENT
- ((newsize
+ SIZEOF_STRUCT_MEM
) % MEM_ALIGNMENT
);
211 if (newsize
> MEM_SIZE
) {
215 sys_sem_wait(mem_sem
);
217 LWIP_ASSERT("mem_realloc: legal memory", (u8_t
*)rmem
>= (u8_t
*)ram
&&
218 (u8_t
*)rmem
< (u8_t
*)ram_end
);
220 if ((u8_t
*)rmem
< (u8_t
*)ram
|| (u8_t
*)rmem
>= (u8_t
*)ram_end
) {
221 LWIP_DEBUGF(MEM_DEBUG
| 3, ("mem_realloc: illegal memory\n"));
224 mem
= (struct mem
*)((u8_t
*)rmem
- SIZEOF_STRUCT_MEM
);
226 ptr
= (u8_t
*)mem
- ram
;
228 size
= mem
->next
- ptr
- SIZEOF_STRUCT_MEM
;
230 lwip_stats
.mem
.used
-= (size
- newsize
);
231 #endif /* MEM_STATS */
233 if (newsize
+ SIZEOF_STRUCT_MEM
+ MIN_SIZE
< size
) {
234 ptr2
= ptr
+ SIZEOF_STRUCT_MEM
+ newsize
;
235 mem2
= (struct mem
*)&ram
[ptr2
];
237 mem2
->next
= mem
->next
;
240 if (mem2
->next
!= MEM_SIZE
) {
241 ((struct mem
*)&ram
[mem2
->next
])->prev
= ptr2
;
246 sys_sem_signal(mem_sem
);
250 mem_malloc(mem_size_t size
)
252 mem_size_t ptr
, ptr2
;
253 struct mem
*mem
, *mem2
;
259 /* Expand the size of the allocated memory region so that we can
260 adjust for alignment. */
261 if ((size
% MEM_ALIGNMENT
) != 0) {
262 size
+= MEM_ALIGNMENT
- ((size
+ SIZEOF_STRUCT_MEM
) % MEM_ALIGNMENT
);
265 if (size
> MEM_SIZE
) {
269 sys_sem_wait(mem_sem
);
271 for (ptr
= (u8_t
*)lfree
- ram
; ptr
< MEM_SIZE
; ptr
= ((struct mem
*)&ram
[ptr
])->next
) {
272 mem
= (struct mem
*)&ram
[ptr
];
274 mem
->next
- (ptr
+ SIZEOF_STRUCT_MEM
) >= size
+ SIZEOF_STRUCT_MEM
) {
276 ptr2
= ptr
+ SIZEOF_STRUCT_MEM
+ size
;
277 mem2
= (struct mem
*)&ram
[ptr2
];
280 mem2
->next
= mem
->next
;
282 if (mem2
->next
!= MEM_SIZE
) {
283 ((struct mem
*)&ram
[mem2
->next
])->prev
= ptr2
;
289 lwip_stats
.mem
.used
+= (size
+ SIZEOF_STRUCT_MEM
);
290 /* if (lwip_stats.mem.max < lwip_stats.mem.used) {
291 lwip_stats.mem.max = lwip_stats.mem.used;
293 if (lwip_stats
.mem
.max
< ptr2
) {
294 lwip_stats
.mem
.max
= ptr2
;
296 #endif /* MEM_STATS */
299 /* Find next free block after mem */
300 while (lfree
->used
&& lfree
!= ram_end
) {
301 lfree
= (struct mem
*)&ram
[lfree
->next
];
303 LWIP_ASSERT("mem_malloc: !lfree->used", !lfree
->used
);
305 sys_sem_signal(mem_sem
);
307 LWIP_ASSERT("mem_malloc: allocated memory not above ram_end.",
308 (u32_t
)mem
+ SIZEOF_STRUCT_MEM
+ size
<= (u32_t
)ram_end
);
309 LWIP_ASSERT("mem_malloc: allocated memory properly aligned.",
310 (unsigned long)((u8_t
*)mem
+ SIZEOF_STRUCT_MEM
) % MEM_ALIGNMENT
== 0);
311 return (u8_t
*)mem
+ SIZEOF_STRUCT_MEM
;
314 LWIP_DEBUGF(MEM_DEBUG
| 2, ("mem_malloc: could not allocate %d bytes\n", (int)size
));
316 ++lwip_stats
.mem
.err
;
317 #endif /* MEM_STATS */
318 sys_sem_signal(mem_sem
);