Releasing debian version 4.04+dfsg-9.
[syslinux-debian/hramrach.git] / core / mem / malloc.h
blobb8ec44d7f432b4df39a10bfab22ac9be88327130
1 /*
2 * malloc.h
4 * Internals for the memory allocator
5 */
7 #include <stdint.h>
8 #include <stddef.h>
9 #include "core.h"
12 * This is a temporary hack. In Syslinux 5 this will be a pointer to
13 * the owner module.
15 typedef size_t malloc_tag_t;
16 enum malloc_owner {
17 MALLOC_FREE,
18 MALLOC_HEAD,
19 MALLOC_CORE,
20 MALLOC_MODULE,
23 struct free_arena_header;
26 * This structure should be a power of two. This becomes the
27 * alignment unit.
29 struct arena_header {
30 malloc_tag_t tag;
31 size_t attrs; /* Bits 0..1: Type
32 2..3: Heap,
33 4..31: MSB of the size */
34 struct free_arena_header *next, *prev;
37 enum arena_type {
38 ARENA_TYPE_USED = 0,
39 ARENA_TYPE_FREE = 1,
40 ARENA_TYPE_HEAD = 2,
41 ARENA_TYPE_DEAD = 3,
43 enum heap {
44 HEAP_MAIN,
45 HEAP_LOWMEM,
46 NHEAP
49 #define ARENA_SIZE_MASK (~(uintptr_t)(sizeof(struct arena_header)-1))
50 #define ARENA_HEAP_MASK ((size_t)0xc)
51 #define ARENA_HEAP_POS 2
52 #define ARENA_TYPE_MASK ((size_t)0x3)
54 #define ARENA_ALIGN_UP(p) ((char *)(((uintptr_t)(p) + ~ARENA_SIZE_MASK) \
55 & ARENA_SIZE_MASK))
56 #define ARENA_ALIGN_DOWN(p) ((char *)((uintptr_t)(p) & ARENA_SIZE_MASK))
58 #define ARENA_SIZE_GET(attrs) ((attrs) & ARENA_SIZE_MASK)
59 #define ARENA_HEAP_GET(attrs) (((attrs) & ARENA_HEAP_MASK) >> ARENA_HEAP_POS)
60 #define ARENA_TYPE_GET(attrs) ((attrs) & ARENA_TYPE_MASK)
62 #define ARENA_SIZE_SET(attrs, size) \
63 ((attrs) = ((size) & ARENA_SIZE_MASK) | ((attrs) & ~ARENA_SIZE_MASK))
64 #define ARENA_HEAP_SET(attrs, heap) \
65 ((attrs) = (((heap) << ARENA_HEAP_POS) & ARENA_HEAP_MASK) | \
66 ((attrs) & ~ARENA_HEAP_MASK))
67 #define ARENA_TYPE_SET(attrs, type) \
68 ((attrs) = ((attrs) & ~ARENA_TYPE_MASK) | \
69 ((type) & ARENA_TYPE_MASK))
72 * This structure should be no more than twice the size of the
73 * previous structure.
75 struct free_arena_header {
76 struct arena_header a;
77 struct free_arena_header *next_free, *prev_free;
78 size_t _pad[2]; /* Pad to 2*sizeof(struct arena_header) */
81 extern struct free_arena_header __malloc_head[NHEAP];
82 void __inject_free_block(struct free_arena_header *ah);