1 | /*
|
---|
2 | * Copyright (c) 2009 Martin Decky
|
---|
3 | * Copyright (c) 2009 Petr Tuma
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <stdalign.h>
|
---|
38 | #include <stdbool.h>
|
---|
39 | #include <stddef.h>
|
---|
40 | #include <as.h>
|
---|
41 | #include <align.h>
|
---|
42 | #include <macros.h>
|
---|
43 | #include <assert.h>
|
---|
44 | #include <errno.h>
|
---|
45 | #include <bitops.h>
|
---|
46 | #include <mem.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 | #include <adt/gcdlcm.h>
|
---|
49 | #include <malloc.h>
|
---|
50 |
|
---|
51 | #include "private/malloc.h"
|
---|
52 | #include "private/fibril.h"
|
---|
53 |
|
---|
54 | /** Magic used in heap headers. */
|
---|
55 | #define HEAP_BLOCK_HEAD_MAGIC UINT32_C(0xBEEF0101)
|
---|
56 |
|
---|
57 | /** Magic used in heap footers. */
|
---|
58 | #define HEAP_BLOCK_FOOT_MAGIC UINT32_C(0xBEEF0202)
|
---|
59 |
|
---|
60 | /** Magic used in heap descriptor. */
|
---|
61 | #define HEAP_AREA_MAGIC UINT32_C(0xBEEFCAFE)
|
---|
62 |
|
---|
63 | /** Allocation alignment.
|
---|
64 | *
|
---|
65 | * This also covers the alignment of fields
|
---|
66 | * in the heap header and footer.
|
---|
67 | *
|
---|
68 | */
|
---|
69 | #define BASE_ALIGN 16
|
---|
70 |
|
---|
71 | /** Heap shrink granularity
|
---|
72 | *
|
---|
73 | * Try not to pump and stress the heap too much
|
---|
74 | * by shrinking and enlarging it too often.
|
---|
75 | * A heap area won't shrink if the released
|
---|
76 | * free block is smaller than this constant.
|
---|
77 | *
|
---|
78 | */
|
---|
79 | #define SHRINK_GRANULARITY (64 * PAGE_SIZE)
|
---|
80 |
|
---|
81 | /** Overhead of each heap block. */
|
---|
82 | #define STRUCT_OVERHEAD \
|
---|
83 | (sizeof(heap_block_head_t) + sizeof(heap_block_foot_t))
|
---|
84 |
|
---|
85 | /** Calculate real size of a heap block.
|
---|
86 | *
|
---|
87 | * Add header and footer size.
|
---|
88 | *
|
---|
89 | */
|
---|
90 | #define GROSS_SIZE(size) ((size) + STRUCT_OVERHEAD)
|
---|
91 |
|
---|
92 | /** Calculate net size of a heap block.
|
---|
93 | *
|
---|
94 | * Subtract header and footer size.
|
---|
95 | *
|
---|
96 | */
|
---|
97 | #define NET_SIZE(size) ((size) - STRUCT_OVERHEAD)
|
---|
98 |
|
---|
99 | /** Overhead of each area. */
|
---|
100 | #define AREA_OVERHEAD(size) \
|
---|
101 | (ALIGN_UP(GROSS_SIZE(size) + sizeof(heap_area_t), BASE_ALIGN))
|
---|
102 |
|
---|
103 | /** Get first block in heap area.
|
---|
104 | *
|
---|
105 | */
|
---|
106 | #define AREA_FIRST_BLOCK_HEAD(area) \
|
---|
107 | (ALIGN_UP(((uintptr_t) (area)) + sizeof(heap_area_t), BASE_ALIGN))
|
---|
108 |
|
---|
109 | /** Get last block in heap area.
|
---|
110 | *
|
---|
111 | */
|
---|
112 | #define AREA_LAST_BLOCK_FOOT(area) \
|
---|
113 | (((uintptr_t) (area)->end) - sizeof(heap_block_foot_t))
|
---|
114 |
|
---|
115 | #define AREA_LAST_BLOCK_HEAD(area) \
|
---|
116 | ((uintptr_t) BLOCK_HEAD(((heap_block_foot_t *) AREA_LAST_BLOCK_FOOT(area))))
|
---|
117 |
|
---|
118 | /** Get header in heap block.
|
---|
119 | *
|
---|
120 | */
|
---|
121 | #define BLOCK_HEAD(foot) \
|
---|
122 | ((heap_block_head_t *) \
|
---|
123 | (((uintptr_t) (foot)) + sizeof(heap_block_foot_t) - (foot)->size))
|
---|
124 |
|
---|
125 | /** Get footer in heap block.
|
---|
126 | *
|
---|
127 | */
|
---|
128 | #define BLOCK_FOOT(head) \
|
---|
129 | ((heap_block_foot_t *) \
|
---|
130 | (((uintptr_t) (head)) + (head)->size - sizeof(heap_block_foot_t)))
|
---|
131 |
|
---|
132 | /** Heap area.
|
---|
133 | *
|
---|
134 | * The memory managed by the heap allocator is divided into
|
---|
135 | * multiple discontinuous heaps. Each heap is represented
|
---|
136 | * by a separate address space area which has this structure
|
---|
137 | * at its very beginning.
|
---|
138 | *
|
---|
139 | */
|
---|
140 | typedef struct heap_area {
|
---|
141 | /** Start of the heap area (including this structure)
|
---|
142 | *
|
---|
143 | * Aligned on page boundary.
|
---|
144 | *
|
---|
145 | */
|
---|
146 | void *start;
|
---|
147 |
|
---|
148 | /** End of the heap area (aligned on page boundary) */
|
---|
149 | void *end;
|
---|
150 |
|
---|
151 | /** Previous heap area */
|
---|
152 | struct heap_area *prev;
|
---|
153 |
|
---|
154 | /** Next heap area */
|
---|
155 | struct heap_area *next;
|
---|
156 |
|
---|
157 | /** A magic value */
|
---|
158 | uint32_t magic;
|
---|
159 | } heap_area_t;
|
---|
160 |
|
---|
161 | /** Header of a heap block
|
---|
162 | *
|
---|
163 | */
|
---|
164 | typedef struct {
|
---|
165 | /* Size of the block (including header and footer) */
|
---|
166 | size_t size;
|
---|
167 |
|
---|
168 | /* Indication of a free block */
|
---|
169 | bool free;
|
---|
170 |
|
---|
171 | /** Heap area this block belongs to */
|
---|
172 | heap_area_t *area;
|
---|
173 |
|
---|
174 | /* A magic value to detect overwrite of heap header */
|
---|
175 | uint32_t magic;
|
---|
176 | } heap_block_head_t;
|
---|
177 |
|
---|
178 | /** Footer of a heap block
|
---|
179 | *
|
---|
180 | */
|
---|
181 | typedef struct {
|
---|
182 | /* Size of the block (including header and footer) */
|
---|
183 | size_t size;
|
---|
184 |
|
---|
185 | /* A magic value to detect overwrite of heap footer */
|
---|
186 | uint32_t magic;
|
---|
187 | } heap_block_foot_t;
|
---|
188 |
|
---|
189 | /** First heap area */
|
---|
190 | static heap_area_t *first_heap_area = NULL;
|
---|
191 |
|
---|
192 | /** Last heap area */
|
---|
193 | static heap_area_t *last_heap_area = NULL;
|
---|
194 |
|
---|
195 | /** Next heap block to examine (next fit algorithm) */
|
---|
196 | static heap_block_head_t *next_fit = NULL;
|
---|
197 |
|
---|
198 | /** Futex for thread-safe heap manipulation */
|
---|
199 | static fibril_rmutex_t malloc_mutex;
|
---|
200 |
|
---|
201 | #define malloc_assert(expr) safe_assert(expr)
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * Make sure the base alignment is sufficient.
|
---|
205 | */
|
---|
206 | static_assert(BASE_ALIGN >= alignof(heap_area_t), "");
|
---|
207 | static_assert(BASE_ALIGN >= alignof(heap_block_head_t), "");
|
---|
208 | static_assert(BASE_ALIGN >= alignof(heap_block_foot_t), "");
|
---|
209 | static_assert(BASE_ALIGN >= alignof(max_align_t), "");
|
---|
210 |
|
---|
211 | /** Serializes access to the heap from multiple threads. */
|
---|
212 | static inline void heap_lock(void)
|
---|
213 | {
|
---|
214 | fibril_rmutex_lock(&malloc_mutex);
|
---|
215 | }
|
---|
216 |
|
---|
217 | /** Serializes access to the heap from multiple threads. */
|
---|
218 | static inline void heap_unlock(void)
|
---|
219 | {
|
---|
220 | fibril_rmutex_unlock(&malloc_mutex);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /** Initialize a heap block
|
---|
224 | *
|
---|
225 | * Fill in the structures related to a heap block.
|
---|
226 | * Should be called only inside the critical section.
|
---|
227 | *
|
---|
228 | * @param addr Address of the block.
|
---|
229 | * @param size Size of the block including the header and the footer.
|
---|
230 | * @param free Indication of a free block.
|
---|
231 | * @param area Heap area the block belongs to.
|
---|
232 | *
|
---|
233 | */
|
---|
234 | static void block_init(void *addr, size_t size, bool free, heap_area_t *area)
|
---|
235 | {
|
---|
236 | /* Calculate the position of the header and the footer */
|
---|
237 | heap_block_head_t *head = (heap_block_head_t *) addr;
|
---|
238 |
|
---|
239 | head->size = size;
|
---|
240 | head->free = free;
|
---|
241 | head->area = area;
|
---|
242 | head->magic = HEAP_BLOCK_HEAD_MAGIC;
|
---|
243 |
|
---|
244 | heap_block_foot_t *foot = BLOCK_FOOT(head);
|
---|
245 |
|
---|
246 | foot->size = size;
|
---|
247 | foot->magic = HEAP_BLOCK_FOOT_MAGIC;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /** Check a heap block
|
---|
251 | *
|
---|
252 | * Verifies that the structures related to a heap block still contain
|
---|
253 | * the magic constants. This helps detect heap corruption early on.
|
---|
254 | * Should be called only inside the critical section.
|
---|
255 | *
|
---|
256 | * @param addr Address of the block.
|
---|
257 | *
|
---|
258 | */
|
---|
259 | static void block_check(void *addr)
|
---|
260 | {
|
---|
261 | heap_block_head_t *head = (heap_block_head_t *) addr;
|
---|
262 |
|
---|
263 | malloc_assert(head->magic == HEAP_BLOCK_HEAD_MAGIC);
|
---|
264 |
|
---|
265 | heap_block_foot_t *foot = BLOCK_FOOT(head);
|
---|
266 |
|
---|
267 | malloc_assert(foot->magic == HEAP_BLOCK_FOOT_MAGIC);
|
---|
268 | malloc_assert(head->size == foot->size);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /** Check a heap area structure
|
---|
272 | *
|
---|
273 | * Should be called only inside the critical section.
|
---|
274 | *
|
---|
275 | * @param addr Address of the heap area.
|
---|
276 | *
|
---|
277 | */
|
---|
278 | static void area_check(void *addr)
|
---|
279 | {
|
---|
280 | heap_area_t *area = (heap_area_t *) addr;
|
---|
281 |
|
---|
282 | malloc_assert(area->magic == HEAP_AREA_MAGIC);
|
---|
283 | malloc_assert(addr == area->start);
|
---|
284 | malloc_assert(area->start < area->end);
|
---|
285 | malloc_assert(((uintptr_t) area->start % PAGE_SIZE) == 0);
|
---|
286 | malloc_assert(((uintptr_t) area->end % PAGE_SIZE) == 0);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /** Create new heap area
|
---|
290 | *
|
---|
291 | * Should be called only inside the critical section.
|
---|
292 | *
|
---|
293 | * @param size Size of the area.
|
---|
294 | *
|
---|
295 | */
|
---|
296 | static bool area_create(size_t size)
|
---|
297 | {
|
---|
298 | /* Align the heap area size on page boundary */
|
---|
299 | size_t asize = ALIGN_UP(size, PAGE_SIZE);
|
---|
300 | void *astart = as_area_create(AS_AREA_ANY, asize,
|
---|
301 | AS_AREA_WRITE | AS_AREA_READ | AS_AREA_CACHEABLE, AS_AREA_UNPAGED);
|
---|
302 | if (astart == AS_MAP_FAILED)
|
---|
303 | return false;
|
---|
304 |
|
---|
305 | heap_area_t *area = (heap_area_t *) astart;
|
---|
306 |
|
---|
307 | area->start = astart;
|
---|
308 | area->end = (void *) ((uintptr_t) astart + asize);
|
---|
309 | area->prev = NULL;
|
---|
310 | area->next = NULL;
|
---|
311 | area->magic = HEAP_AREA_MAGIC;
|
---|
312 |
|
---|
313 | void *block = (void *) AREA_FIRST_BLOCK_HEAD(area);
|
---|
314 | size_t bsize = (size_t) (area->end - block);
|
---|
315 |
|
---|
316 | block_init(block, bsize, true, area);
|
---|
317 |
|
---|
318 | if (last_heap_area == NULL) {
|
---|
319 | first_heap_area = area;
|
---|
320 | last_heap_area = area;
|
---|
321 | } else {
|
---|
322 | area->prev = last_heap_area;
|
---|
323 | last_heap_area->next = area;
|
---|
324 | last_heap_area = area;
|
---|
325 | }
|
---|
326 |
|
---|
327 | return true;
|
---|
328 | }
|
---|
329 |
|
---|
330 | /** Try to enlarge a heap area
|
---|
331 | *
|
---|
332 | * Should be called only inside the critical section.
|
---|
333 | *
|
---|
334 | * @param area Heap area to grow.
|
---|
335 | * @param size Gross size to grow (bytes).
|
---|
336 | *
|
---|
337 | * @return True if successful.
|
---|
338 | *
|
---|
339 | */
|
---|
340 | static bool area_grow(heap_area_t *area, size_t size)
|
---|
341 | {
|
---|
342 | if (size == 0)
|
---|
343 | return true;
|
---|
344 |
|
---|
345 | area_check(area);
|
---|
346 |
|
---|
347 | /* New heap area size */
|
---|
348 | size_t gross_size = (size_t) (area->end - area->start) + size;
|
---|
349 | size_t asize = ALIGN_UP(gross_size, PAGE_SIZE);
|
---|
350 | void *end = (void *) ((uintptr_t) area->start + asize);
|
---|
351 |
|
---|
352 | /* Check for overflow */
|
---|
353 | if (end < area->start)
|
---|
354 | return false;
|
---|
355 |
|
---|
356 | /* Resize the address space area */
|
---|
357 | errno_t ret = as_area_resize(area->start, asize, 0);
|
---|
358 | if (ret != EOK)
|
---|
359 | return false;
|
---|
360 |
|
---|
361 | heap_block_head_t *last_head =
|
---|
362 | (heap_block_head_t *) AREA_LAST_BLOCK_HEAD(area);
|
---|
363 |
|
---|
364 | if (last_head->free) {
|
---|
365 | /* Add the new space to the last block. */
|
---|
366 | size_t net_size = (size_t) (end - area->end) + last_head->size;
|
---|
367 | malloc_assert(net_size > 0);
|
---|
368 | block_init(last_head, net_size, true, area);
|
---|
369 | } else {
|
---|
370 | /* Add new free block */
|
---|
371 | size_t net_size = (size_t) (end - area->end);
|
---|
372 | if (net_size > 0)
|
---|
373 | block_init(area->end, net_size, true, area);
|
---|
374 | }
|
---|
375 |
|
---|
376 | /* Update heap area parameters */
|
---|
377 | area->end = end;
|
---|
378 |
|
---|
379 | return true;
|
---|
380 | }
|
---|
381 |
|
---|
382 | /** Try to shrink heap
|
---|
383 | *
|
---|
384 | * Should be called only inside the critical section.
|
---|
385 | * In all cases the next pointer is reset.
|
---|
386 | *
|
---|
387 | * @param area Last modified heap area.
|
---|
388 | *
|
---|
389 | */
|
---|
390 | static void heap_shrink(heap_area_t *area)
|
---|
391 | {
|
---|
392 | area_check(area);
|
---|
393 |
|
---|
394 | heap_block_foot_t *last_foot =
|
---|
395 | (heap_block_foot_t *) AREA_LAST_BLOCK_FOOT(area);
|
---|
396 | heap_block_head_t *last_head = BLOCK_HEAD(last_foot);
|
---|
397 |
|
---|
398 | block_check((void *) last_head);
|
---|
399 | malloc_assert(last_head->area == area);
|
---|
400 |
|
---|
401 | if (last_head->free) {
|
---|
402 | /*
|
---|
403 | * The last block of the heap area is
|
---|
404 | * unused. The area might be potentially
|
---|
405 | * shrunk.
|
---|
406 | */
|
---|
407 |
|
---|
408 | heap_block_head_t *first_head =
|
---|
409 | (heap_block_head_t *) AREA_FIRST_BLOCK_HEAD(area);
|
---|
410 |
|
---|
411 | block_check((void *) first_head);
|
---|
412 | malloc_assert(first_head->area == area);
|
---|
413 |
|
---|
414 | size_t shrink_size = ALIGN_DOWN(last_head->size, PAGE_SIZE);
|
---|
415 |
|
---|
416 | if (first_head == last_head) {
|
---|
417 | /*
|
---|
418 | * The entire heap area consists of a single
|
---|
419 | * free heap block. This means we can get rid
|
---|
420 | * of it entirely.
|
---|
421 | */
|
---|
422 |
|
---|
423 | heap_area_t *prev = area->prev;
|
---|
424 | heap_area_t *next = area->next;
|
---|
425 |
|
---|
426 | if (prev != NULL) {
|
---|
427 | area_check(prev);
|
---|
428 | prev->next = next;
|
---|
429 | } else
|
---|
430 | first_heap_area = next;
|
---|
431 |
|
---|
432 | if (next != NULL) {
|
---|
433 | area_check(next);
|
---|
434 | next->prev = prev;
|
---|
435 | } else
|
---|
436 | last_heap_area = prev;
|
---|
437 |
|
---|
438 | as_area_destroy(area->start);
|
---|
439 | } else if (shrink_size >= SHRINK_GRANULARITY) {
|
---|
440 | /*
|
---|
441 | * Make sure that we always shrink the area
|
---|
442 | * by a multiple of page size and update
|
---|
443 | * the block layout accordingly.
|
---|
444 | */
|
---|
445 |
|
---|
446 | size_t asize = (size_t) (area->end - area->start) - shrink_size;
|
---|
447 | void *end = (void *) ((uintptr_t) area->start + asize);
|
---|
448 |
|
---|
449 | /* Resize the address space area */
|
---|
450 | errno_t ret = as_area_resize(area->start, asize, 0);
|
---|
451 | if (ret != EOK)
|
---|
452 | abort();
|
---|
453 |
|
---|
454 | /* Update heap area parameters */
|
---|
455 | area->end = end;
|
---|
456 | size_t excess = ((size_t) area->end) - ((size_t) last_head);
|
---|
457 |
|
---|
458 | if (excess > 0) {
|
---|
459 | if (excess >= STRUCT_OVERHEAD) {
|
---|
460 | /*
|
---|
461 | * The previous block cannot be free and there
|
---|
462 | * is enough free space left in the area to
|
---|
463 | * create a new free block.
|
---|
464 | */
|
---|
465 | block_init((void *) last_head, excess, true, area);
|
---|
466 | } else {
|
---|
467 | /*
|
---|
468 | * The excess is small. Therefore just enlarge
|
---|
469 | * the previous block.
|
---|
470 | */
|
---|
471 | heap_block_foot_t *prev_foot = (heap_block_foot_t *)
|
---|
472 | (((uintptr_t) last_head) - sizeof(heap_block_foot_t));
|
---|
473 | heap_block_head_t *prev_head = BLOCK_HEAD(prev_foot);
|
---|
474 |
|
---|
475 | block_check((void *) prev_head);
|
---|
476 |
|
---|
477 | block_init(prev_head, prev_head->size + excess,
|
---|
478 | prev_head->free, area);
|
---|
479 | }
|
---|
480 | }
|
---|
481 | }
|
---|
482 | }
|
---|
483 |
|
---|
484 | next_fit = NULL;
|
---|
485 | }
|
---|
486 |
|
---|
487 | /** Initialize the heap allocator
|
---|
488 | *
|
---|
489 | * Create initial heap memory area. This routine is
|
---|
490 | * only called from libc initialization, thus we do not
|
---|
491 | * take any locks.
|
---|
492 | *
|
---|
493 | */
|
---|
494 | void __malloc_init(void)
|
---|
495 | {
|
---|
496 | if (fibril_rmutex_initialize(&malloc_mutex) != EOK)
|
---|
497 | abort();
|
---|
498 |
|
---|
499 | if (!area_create(PAGE_SIZE))
|
---|
500 | abort();
|
---|
501 | }
|
---|
502 |
|
---|
503 | void __malloc_fini(void)
|
---|
504 | {
|
---|
505 | fibril_rmutex_destroy(&malloc_mutex);
|
---|
506 | }
|
---|
507 |
|
---|
508 | /** Split heap block and mark it as used.
|
---|
509 | *
|
---|
510 | * Should be called only inside the critical section.
|
---|
511 | *
|
---|
512 | * @param cur Heap block to split.
|
---|
513 | * @param size Number of bytes to split and mark from the beginning
|
---|
514 | * of the block.
|
---|
515 | *
|
---|
516 | */
|
---|
517 | static void split_mark(heap_block_head_t *cur, const size_t size)
|
---|
518 | {
|
---|
519 | malloc_assert(cur->size >= size);
|
---|
520 |
|
---|
521 | /* See if we should split the block. */
|
---|
522 | size_t split_limit = GROSS_SIZE(size);
|
---|
523 |
|
---|
524 | if (cur->size > split_limit) {
|
---|
525 | /* Block big enough -> split. */
|
---|
526 | void *next = ((void *) cur) + size;
|
---|
527 | block_init(next, cur->size - size, true, cur->area);
|
---|
528 | block_init(cur, size, false, cur->area);
|
---|
529 | } else {
|
---|
530 | /* Block too small -> use as is. */
|
---|
531 | cur->free = false;
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | /** Allocate memory from heap area starting from given block
|
---|
536 | *
|
---|
537 | * Should be called only inside the critical section.
|
---|
538 | * As a side effect this function also sets the current
|
---|
539 | * pointer on successful allocation.
|
---|
540 | *
|
---|
541 | * @param area Heap area where to allocate from.
|
---|
542 | * @param first_block Starting heap block.
|
---|
543 | * @param final_block Heap block where to finish the search
|
---|
544 | * (may be NULL).
|
---|
545 | * @param real_size Gross number of bytes to allocate.
|
---|
546 | * @param falign Physical alignment of the block.
|
---|
547 | *
|
---|
548 | * @return Address of the allocated block or NULL on not enough memory.
|
---|
549 | *
|
---|
550 | */
|
---|
551 | static void *malloc_area(heap_area_t *area, heap_block_head_t *first_block,
|
---|
552 | heap_block_head_t *final_block, size_t real_size, size_t falign)
|
---|
553 | {
|
---|
554 | area_check((void *) area);
|
---|
555 | malloc_assert((void *) first_block >= (void *) AREA_FIRST_BLOCK_HEAD(area));
|
---|
556 | malloc_assert((void *) first_block < area->end);
|
---|
557 |
|
---|
558 | for (heap_block_head_t *cur = first_block; (void *) cur < area->end;
|
---|
559 | cur = (heap_block_head_t *) (((void *) cur) + cur->size)) {
|
---|
560 | block_check(cur);
|
---|
561 |
|
---|
562 | /* Finish searching on the final block */
|
---|
563 | if ((final_block != NULL) && (cur == final_block))
|
---|
564 | break;
|
---|
565 |
|
---|
566 | /* Try to find a block that is free and large enough. */
|
---|
567 | if ((cur->free) && (cur->size >= real_size)) {
|
---|
568 | /*
|
---|
569 | * We have found a suitable block.
|
---|
570 | * Check for alignment properties.
|
---|
571 | */
|
---|
572 | void *addr = (void *)
|
---|
573 | ((uintptr_t) cur + sizeof(heap_block_head_t));
|
---|
574 | void *aligned = (void *)
|
---|
575 | ALIGN_UP((uintptr_t) addr, falign);
|
---|
576 |
|
---|
577 | if (addr == aligned) {
|
---|
578 | /* Exact block start including alignment. */
|
---|
579 | split_mark(cur, real_size);
|
---|
580 |
|
---|
581 | next_fit = cur;
|
---|
582 | return addr;
|
---|
583 | } else {
|
---|
584 | /* Block start has to be aligned */
|
---|
585 | size_t excess = (size_t) (aligned - addr);
|
---|
586 |
|
---|
587 | if (cur->size >= real_size + excess) {
|
---|
588 | /*
|
---|
589 | * The current block is large enough to fit
|
---|
590 | * data in (including alignment).
|
---|
591 | */
|
---|
592 | if ((void *) cur > (void *) AREA_FIRST_BLOCK_HEAD(area)) {
|
---|
593 | /*
|
---|
594 | * There is a block before the current block.
|
---|
595 | * This previous block can be enlarged to
|
---|
596 | * compensate for the alignment excess.
|
---|
597 | */
|
---|
598 | heap_block_foot_t *prev_foot = (heap_block_foot_t *)
|
---|
599 | ((void *) cur - sizeof(heap_block_foot_t));
|
---|
600 |
|
---|
601 | heap_block_head_t *prev_head = (heap_block_head_t *)
|
---|
602 | ((void *) cur - prev_foot->size);
|
---|
603 |
|
---|
604 | block_check(prev_head);
|
---|
605 |
|
---|
606 | size_t reduced_size = cur->size - excess;
|
---|
607 | heap_block_head_t *next_head = ((void *) cur) + excess;
|
---|
608 |
|
---|
609 | if ((!prev_head->free) &&
|
---|
610 | (excess >= STRUCT_OVERHEAD)) {
|
---|
611 | /*
|
---|
612 | * The previous block is not free and there
|
---|
613 | * is enough free space left to fill in
|
---|
614 | * a new free block between the previous
|
---|
615 | * and current block.
|
---|
616 | */
|
---|
617 | block_init(cur, excess, true, area);
|
---|
618 | } else {
|
---|
619 | /*
|
---|
620 | * The previous block is free (thus there
|
---|
621 | * is no need to induce additional
|
---|
622 | * fragmentation to the heap) or the
|
---|
623 | * excess is small. Therefore just enlarge
|
---|
624 | * the previous block.
|
---|
625 | */
|
---|
626 | block_init(prev_head, prev_head->size + excess,
|
---|
627 | prev_head->free, area);
|
---|
628 | }
|
---|
629 |
|
---|
630 | block_init(next_head, reduced_size, true, area);
|
---|
631 | split_mark(next_head, real_size);
|
---|
632 |
|
---|
633 | next_fit = next_head;
|
---|
634 | return aligned;
|
---|
635 | } else {
|
---|
636 | /*
|
---|
637 | * The current block is the first block
|
---|
638 | * in the heap area. We have to make sure
|
---|
639 | * that the alignment excess is large enough
|
---|
640 | * to fit a new free block just before the
|
---|
641 | * current block.
|
---|
642 | */
|
---|
643 | while (excess < STRUCT_OVERHEAD) {
|
---|
644 | aligned += falign;
|
---|
645 | excess += falign;
|
---|
646 | }
|
---|
647 |
|
---|
648 | /* Check for current block size again */
|
---|
649 | if (cur->size >= real_size + excess) {
|
---|
650 | size_t reduced_size = cur->size - excess;
|
---|
651 | cur = (heap_block_head_t *)
|
---|
652 | (AREA_FIRST_BLOCK_HEAD(area) + excess);
|
---|
653 |
|
---|
654 | block_init((void *) AREA_FIRST_BLOCK_HEAD(area),
|
---|
655 | excess, true, area);
|
---|
656 | block_init(cur, reduced_size, true, area);
|
---|
657 | split_mark(cur, real_size);
|
---|
658 |
|
---|
659 | next_fit = cur;
|
---|
660 | return aligned;
|
---|
661 | }
|
---|
662 | }
|
---|
663 | }
|
---|
664 | }
|
---|
665 | }
|
---|
666 | }
|
---|
667 |
|
---|
668 | return NULL;
|
---|
669 | }
|
---|
670 |
|
---|
671 | /** Try to enlarge any of the heap areas.
|
---|
672 | *
|
---|
673 | * If successful, allocate block of the given size in the area.
|
---|
674 | * Should be called only inside the critical section.
|
---|
675 | *
|
---|
676 | * @param size Gross size of item to allocate (bytes).
|
---|
677 | * @param align Memory address alignment.
|
---|
678 | *
|
---|
679 | * @return Allocated block.
|
---|
680 | * @return NULL on failure.
|
---|
681 | *
|
---|
682 | */
|
---|
683 | static void *heap_grow_and_alloc(size_t size, size_t align)
|
---|
684 | {
|
---|
685 | if (size == 0)
|
---|
686 | return NULL;
|
---|
687 |
|
---|
688 | /* First try to enlarge some existing area */
|
---|
689 | for (heap_area_t *area = first_heap_area; area != NULL;
|
---|
690 | area = area->next) {
|
---|
691 |
|
---|
692 | if (area_grow(area, size + align)) {
|
---|
693 | heap_block_head_t *first =
|
---|
694 | (heap_block_head_t *) AREA_LAST_BLOCK_HEAD(area);
|
---|
695 |
|
---|
696 | void *addr =
|
---|
697 | malloc_area(area, first, NULL, size, align);
|
---|
698 | malloc_assert(addr != NULL);
|
---|
699 | return addr;
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 | /* Eventually try to create a new area */
|
---|
704 | if (area_create(AREA_OVERHEAD(size + align))) {
|
---|
705 | heap_block_head_t *first =
|
---|
706 | (heap_block_head_t *) AREA_FIRST_BLOCK_HEAD(last_heap_area);
|
---|
707 |
|
---|
708 | void *addr =
|
---|
709 | malloc_area(last_heap_area, first, NULL, size, align);
|
---|
710 | malloc_assert(addr != NULL);
|
---|
711 | return addr;
|
---|
712 | }
|
---|
713 |
|
---|
714 | return NULL;
|
---|
715 | }
|
---|
716 |
|
---|
717 | /** Allocate a memory block
|
---|
718 | *
|
---|
719 | * Should be called only inside the critical section.
|
---|
720 | *
|
---|
721 | * @param size The size of the block to allocate.
|
---|
722 | * @param align Memory address alignment.
|
---|
723 | *
|
---|
724 | * @return Address of the allocated block or NULL on not enough memory.
|
---|
725 | *
|
---|
726 | */
|
---|
727 | static void *malloc_internal(const size_t size, const size_t align)
|
---|
728 | {
|
---|
729 | malloc_assert(first_heap_area != NULL);
|
---|
730 |
|
---|
731 | if (align == 0)
|
---|
732 | return NULL;
|
---|
733 |
|
---|
734 | size_t falign = lcm(align, BASE_ALIGN);
|
---|
735 |
|
---|
736 | /* Check for integer overflow. */
|
---|
737 | if (falign < align)
|
---|
738 | return NULL;
|
---|
739 |
|
---|
740 | /*
|
---|
741 | * The size of the allocated block needs to be naturally
|
---|
742 | * aligned, because the footer structure also needs to reside
|
---|
743 | * on a naturally aligned address in order to avoid unaligned
|
---|
744 | * memory accesses.
|
---|
745 | */
|
---|
746 | size_t gross_size = GROSS_SIZE(ALIGN_UP(size, BASE_ALIGN));
|
---|
747 |
|
---|
748 | /* Try the next fit approach */
|
---|
749 | heap_block_head_t *split = next_fit;
|
---|
750 |
|
---|
751 | if (split != NULL) {
|
---|
752 | void *addr = malloc_area(split->area, split, NULL, gross_size,
|
---|
753 | falign);
|
---|
754 |
|
---|
755 | if (addr != NULL)
|
---|
756 | return addr;
|
---|
757 | }
|
---|
758 |
|
---|
759 | /* Search the entire heap */
|
---|
760 | for (heap_area_t *area = first_heap_area; area != NULL;
|
---|
761 | area = area->next) {
|
---|
762 | heap_block_head_t *first = (heap_block_head_t *)
|
---|
763 | AREA_FIRST_BLOCK_HEAD(area);
|
---|
764 |
|
---|
765 | void *addr = malloc_area(area, first, split, gross_size,
|
---|
766 | falign);
|
---|
767 |
|
---|
768 | if (addr != NULL)
|
---|
769 | return addr;
|
---|
770 | }
|
---|
771 |
|
---|
772 | /* Finally, try to grow heap space and allocate in the new area. */
|
---|
773 | return heap_grow_and_alloc(gross_size, falign);
|
---|
774 | }
|
---|
775 |
|
---|
776 | /** Allocate memory
|
---|
777 | *
|
---|
778 | * @param size Number of bytes to allocate.
|
---|
779 | *
|
---|
780 | * @return Allocated memory or NULL.
|
---|
781 | *
|
---|
782 | */
|
---|
783 | void *malloc(const size_t size)
|
---|
784 | {
|
---|
785 | heap_lock();
|
---|
786 | void *block = malloc_internal(size, BASE_ALIGN);
|
---|
787 | heap_unlock();
|
---|
788 |
|
---|
789 | return block;
|
---|
790 | }
|
---|
791 |
|
---|
792 | /** Allocate memory with specified alignment
|
---|
793 | *
|
---|
794 | * @param align Alignment in byes.
|
---|
795 | * @param size Number of bytes to allocate.
|
---|
796 | *
|
---|
797 | * @return Allocated memory or NULL.
|
---|
798 | *
|
---|
799 | */
|
---|
800 | void *memalign(const size_t align, const size_t size)
|
---|
801 | {
|
---|
802 | if (align == 0)
|
---|
803 | return NULL;
|
---|
804 |
|
---|
805 | size_t palign =
|
---|
806 | 1 << (fnzb(max(sizeof(void *), align) - 1) + 1);
|
---|
807 |
|
---|
808 | heap_lock();
|
---|
809 | void *block = malloc_internal(size, palign);
|
---|
810 | heap_unlock();
|
---|
811 |
|
---|
812 | return block;
|
---|
813 | }
|
---|
814 |
|
---|
815 | /** Reallocate memory block
|
---|
816 | *
|
---|
817 | * @param addr Already allocated memory or NULL.
|
---|
818 | * @param size New size of the memory block.
|
---|
819 | *
|
---|
820 | * @return Reallocated memory or NULL.
|
---|
821 | *
|
---|
822 | */
|
---|
823 | void *realloc(void *const addr, const size_t size)
|
---|
824 | {
|
---|
825 | if (size == 0) {
|
---|
826 | free(addr);
|
---|
827 | return NULL;
|
---|
828 | }
|
---|
829 |
|
---|
830 | if (addr == NULL)
|
---|
831 | return malloc(size);
|
---|
832 |
|
---|
833 | heap_lock();
|
---|
834 |
|
---|
835 | /* Calculate the position of the header. */
|
---|
836 | heap_block_head_t *head =
|
---|
837 | (heap_block_head_t *) (addr - sizeof(heap_block_head_t));
|
---|
838 |
|
---|
839 | block_check(head);
|
---|
840 | malloc_assert(!head->free);
|
---|
841 |
|
---|
842 | heap_area_t *area = head->area;
|
---|
843 |
|
---|
844 | area_check(area);
|
---|
845 | malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
|
---|
846 | malloc_assert((void *) head < area->end);
|
---|
847 |
|
---|
848 | void *ptr = NULL;
|
---|
849 | bool reloc = false;
|
---|
850 | size_t real_size = GROSS_SIZE(ALIGN_UP(size, BASE_ALIGN));
|
---|
851 | size_t orig_size = head->size;
|
---|
852 |
|
---|
853 | if (orig_size > real_size) {
|
---|
854 | /* Shrink */
|
---|
855 | if (orig_size - real_size >= STRUCT_OVERHEAD) {
|
---|
856 | /*
|
---|
857 | * Split the original block to a full block
|
---|
858 | * and a trailing free block.
|
---|
859 | */
|
---|
860 | block_init((void *) head, real_size, false, area);
|
---|
861 | block_init((void *) head + real_size,
|
---|
862 | orig_size - real_size, true, area);
|
---|
863 | heap_shrink(area);
|
---|
864 | }
|
---|
865 |
|
---|
866 | ptr = ((void *) head) + sizeof(heap_block_head_t);
|
---|
867 | } else {
|
---|
868 | heap_block_head_t *next_head =
|
---|
869 | (heap_block_head_t *) (((void *) head) + head->size);
|
---|
870 | bool have_next = ((void *) next_head < area->end);
|
---|
871 |
|
---|
872 | if (((void *) head) + real_size > area->end) {
|
---|
873 | /*
|
---|
874 | * The current area is too small to hold the resized
|
---|
875 | * block. Make sure there are no used blocks standing
|
---|
876 | * in our way and try to grow the area using real_size
|
---|
877 | * as a safe upper bound.
|
---|
878 | */
|
---|
879 |
|
---|
880 | bool have_next_next;
|
---|
881 |
|
---|
882 | if (have_next) {
|
---|
883 | have_next_next = (((void *) next_head) +
|
---|
884 | next_head->size < area->end);
|
---|
885 | }
|
---|
886 | if (!have_next || (next_head->free && !have_next_next)) {
|
---|
887 | /*
|
---|
888 | * There is no next block in this area or
|
---|
889 | * it is a free block and there is no used
|
---|
890 | * block following it. There can't be any
|
---|
891 | * free block following it either as
|
---|
892 | * two free blocks would be merged.
|
---|
893 | */
|
---|
894 | (void) area_grow(area, real_size);
|
---|
895 | }
|
---|
896 | }
|
---|
897 |
|
---|
898 | /*
|
---|
899 | * Look at the next block. If it is free and the size is
|
---|
900 | * sufficient then merge the two. Otherwise just allocate a new
|
---|
901 | * block, copy the original data into it and free the original
|
---|
902 | * block.
|
---|
903 | */
|
---|
904 |
|
---|
905 | if (have_next && (head->size + next_head->size >= real_size) &&
|
---|
906 | next_head->free) {
|
---|
907 | block_check(next_head);
|
---|
908 | block_init(head, head->size + next_head->size, false,
|
---|
909 | area);
|
---|
910 | split_mark(head, real_size);
|
---|
911 |
|
---|
912 | ptr = ((void *) head) + sizeof(heap_block_head_t);
|
---|
913 | next_fit = NULL;
|
---|
914 | } else {
|
---|
915 | reloc = true;
|
---|
916 | }
|
---|
917 | }
|
---|
918 |
|
---|
919 | heap_unlock();
|
---|
920 |
|
---|
921 | if (reloc) {
|
---|
922 | ptr = malloc(size);
|
---|
923 | if (ptr != NULL) {
|
---|
924 | memcpy(ptr, addr, NET_SIZE(orig_size));
|
---|
925 | free(addr);
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 | return ptr;
|
---|
930 | }
|
---|
931 |
|
---|
932 | /** Free a memory block
|
---|
933 | *
|
---|
934 | * @param addr The address of the block.
|
---|
935 | *
|
---|
936 | */
|
---|
937 | void free(void *const addr)
|
---|
938 | {
|
---|
939 | if (addr == NULL)
|
---|
940 | return;
|
---|
941 |
|
---|
942 | heap_lock();
|
---|
943 |
|
---|
944 | /* Calculate the position of the header. */
|
---|
945 | heap_block_head_t *head =
|
---|
946 | (heap_block_head_t *) (addr - sizeof(heap_block_head_t));
|
---|
947 |
|
---|
948 | block_check(head);
|
---|
949 | malloc_assert(!head->free);
|
---|
950 |
|
---|
951 | heap_area_t *area = head->area;
|
---|
952 |
|
---|
953 | area_check(area);
|
---|
954 | malloc_assert((void *) head >= (void *) AREA_FIRST_BLOCK_HEAD(area));
|
---|
955 | malloc_assert((void *) head < area->end);
|
---|
956 |
|
---|
957 | /* Mark the block itself as free. */
|
---|
958 | head->free = true;
|
---|
959 |
|
---|
960 | /* Look at the next block. If it is free, merge the two. */
|
---|
961 | heap_block_head_t *next_head =
|
---|
962 | (heap_block_head_t *) (((void *) head) + head->size);
|
---|
963 |
|
---|
964 | if ((void *) next_head < area->end) {
|
---|
965 | block_check(next_head);
|
---|
966 | if (next_head->free)
|
---|
967 | block_init(head, head->size + next_head->size, true, area);
|
---|
968 | }
|
---|
969 |
|
---|
970 | /* Look at the previous block. If it is free, merge the two. */
|
---|
971 | if ((void *) head > (void *) AREA_FIRST_BLOCK_HEAD(area)) {
|
---|
972 | heap_block_foot_t *prev_foot =
|
---|
973 | (heap_block_foot_t *) (((void *) head) - sizeof(heap_block_foot_t));
|
---|
974 |
|
---|
975 | heap_block_head_t *prev_head =
|
---|
976 | (heap_block_head_t *) (((void *) head) - prev_foot->size);
|
---|
977 |
|
---|
978 | block_check(prev_head);
|
---|
979 |
|
---|
980 | if (prev_head->free)
|
---|
981 | block_init(prev_head, prev_head->size + head->size, true,
|
---|
982 | area);
|
---|
983 | }
|
---|
984 |
|
---|
985 | heap_shrink(area);
|
---|
986 |
|
---|
987 | heap_unlock();
|
---|
988 | }
|
---|
989 |
|
---|
990 | void *heap_check(void)
|
---|
991 | {
|
---|
992 | heap_lock();
|
---|
993 |
|
---|
994 | if (first_heap_area == NULL) {
|
---|
995 | heap_unlock();
|
---|
996 | return (void *) -1;
|
---|
997 | }
|
---|
998 |
|
---|
999 | /* Walk all heap areas */
|
---|
1000 | for (heap_area_t *area = first_heap_area; area != NULL;
|
---|
1001 | area = area->next) {
|
---|
1002 |
|
---|
1003 | /* Check heap area consistency */
|
---|
1004 | if ((area->magic != HEAP_AREA_MAGIC) ||
|
---|
1005 | ((void *) area != area->start) ||
|
---|
1006 | (area->start >= area->end) ||
|
---|
1007 | (((uintptr_t) area->start % PAGE_SIZE) != 0) ||
|
---|
1008 | (((uintptr_t) area->end % PAGE_SIZE) != 0)) {
|
---|
1009 | heap_unlock();
|
---|
1010 | return (void *) area;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | /* Walk all heap blocks */
|
---|
1014 | for (heap_block_head_t *head = (heap_block_head_t *)
|
---|
1015 | AREA_FIRST_BLOCK_HEAD(area); (void *) head < area->end;
|
---|
1016 | head = (heap_block_head_t *) (((void *) head) + head->size)) {
|
---|
1017 |
|
---|
1018 | /* Check heap block consistency */
|
---|
1019 | if (head->magic != HEAP_BLOCK_HEAD_MAGIC) {
|
---|
1020 | heap_unlock();
|
---|
1021 | return (void *) head;
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | heap_block_foot_t *foot = BLOCK_FOOT(head);
|
---|
1025 |
|
---|
1026 | if ((foot->magic != HEAP_BLOCK_FOOT_MAGIC) ||
|
---|
1027 | (head->size != foot->size)) {
|
---|
1028 | heap_unlock();
|
---|
1029 | return (void *) foot;
|
---|
1030 | }
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | heap_unlock();
|
---|
1035 |
|
---|
1036 | return NULL;
|
---|
1037 | }
|
---|
1038 |
|
---|
1039 | /** @}
|
---|
1040 | */
|
---|