1 | /* |
---|
2 | * malloc.h |
---|
3 | * |
---|
4 | * Internals for the memory allocator |
---|
5 | */ |
---|
6 | |
---|
7 | #include <stdint.h> |
---|
8 | #include <stddef.h> |
---|
9 | #include "core.h" |
---|
10 | #include "thread.h" |
---|
11 | |
---|
12 | extern struct semaphore __malloc_semaphore; |
---|
13 | |
---|
14 | /* |
---|
15 | * This is a temporary hack. In Syslinux 5 this will be a pointer to |
---|
16 | * the owner module. |
---|
17 | */ |
---|
18 | typedef size_t malloc_tag_t; |
---|
19 | enum malloc_owner { |
---|
20 | MALLOC_FREE, |
---|
21 | MALLOC_HEAD, |
---|
22 | MALLOC_CORE, |
---|
23 | MALLOC_MODULE, |
---|
24 | }; |
---|
25 | |
---|
26 | enum arena_type { |
---|
27 | ARENA_TYPE_USED = 0, |
---|
28 | ARENA_TYPE_FREE = 1, |
---|
29 | ARENA_TYPE_HEAD = 2, |
---|
30 | ARENA_TYPE_DEAD = 3, |
---|
31 | }; |
---|
32 | enum heap { |
---|
33 | HEAP_MAIN, |
---|
34 | HEAP_LOWMEM, |
---|
35 | NHEAP |
---|
36 | }; |
---|
37 | |
---|
38 | #define ARENA_MAGIC 0x20130117 |
---|
39 | |
---|
40 | struct free_arena_header; |
---|
41 | |
---|
42 | /* |
---|
43 | * This structure should be a power of two. This becomes the |
---|
44 | * alignment unit. |
---|
45 | */ |
---|
46 | struct arena_header { |
---|
47 | malloc_tag_t tag; |
---|
48 | size_t attrs; /* Bits 0..1: Type |
---|
49 | 2..3: Heap, |
---|
50 | 4..31: MSB of the size */ |
---|
51 | struct free_arena_header *next, *prev; |
---|
52 | |
---|
53 | #ifdef DEBUG_MALLOC |
---|
54 | unsigned long _pad[3]; |
---|
55 | unsigned int magic; |
---|
56 | #endif |
---|
57 | }; |
---|
58 | |
---|
59 | /* Pad to 2*sizeof(struct arena_header) */ |
---|
60 | #define ARENA_PADDING ((2 * sizeof(struct arena_header)) - \ |
---|
61 | (sizeof(struct arena_header) + \ |
---|
62 | sizeof(struct free_arena_header *) + \ |
---|
63 | sizeof(struct free_arena_header *))) |
---|
64 | |
---|
65 | /* |
---|
66 | * This structure should be no more than twice the size of the |
---|
67 | * previous structure. |
---|
68 | */ |
---|
69 | struct free_arena_header { |
---|
70 | struct arena_header a; |
---|
71 | struct free_arena_header *next_free, *prev_free; |
---|
72 | size_t _pad[ARENA_PADDING]; |
---|
73 | }; |
---|
74 | |
---|
75 | #define ARENA_SIZE_MASK (~(uintptr_t)(sizeof(struct arena_header)-1)) |
---|
76 | #define ARENA_HEAP_MASK ((size_t)0xc) |
---|
77 | #define ARENA_HEAP_POS 2 |
---|
78 | #define ARENA_TYPE_MASK ((size_t)0x3) |
---|
79 | |
---|
80 | #define ARENA_ALIGN_UP(p) ((char *)(((uintptr_t)(p) + ~ARENA_SIZE_MASK) \ |
---|
81 | & ARENA_SIZE_MASK)) |
---|
82 | #define ARENA_ALIGN_DOWN(p) ((char *)((uintptr_t)(p) & ARENA_SIZE_MASK)) |
---|
83 | |
---|
84 | #define ARENA_SIZE_GET(attrs) ((attrs) & ARENA_SIZE_MASK) |
---|
85 | #define ARENA_HEAP_GET(attrs) (((attrs) & ARENA_HEAP_MASK) >> ARENA_HEAP_POS) |
---|
86 | #define ARENA_TYPE_GET(attrs) ((attrs) & ARENA_TYPE_MASK) |
---|
87 | |
---|
88 | #define ARENA_SIZE_SET(attrs, size) \ |
---|
89 | ((attrs) = ((size) & ARENA_SIZE_MASK) | ((attrs) & ~ARENA_SIZE_MASK)) |
---|
90 | #define ARENA_HEAP_SET(attrs, heap) \ |
---|
91 | ((attrs) = (((heap) << ARENA_HEAP_POS) & ARENA_HEAP_MASK) | \ |
---|
92 | ((attrs) & ~ARENA_HEAP_MASK)) |
---|
93 | #define ARENA_TYPE_SET(attrs, type) \ |
---|
94 | ((attrs) = ((attrs) & ~ARENA_TYPE_MASK) | \ |
---|
95 | ((type) & ARENA_TYPE_MASK)) |
---|
96 | |
---|
97 | extern struct free_arena_header __core_malloc_head[NHEAP]; |
---|
98 | void __inject_free_block(struct free_arena_header *ah); |
---|