aboutsummaryrefslogtreecommitdiffstats
path: root/com32/lib/malloc.h
blob: 9665d83df60366608a11db92ea985e98053d2cae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * malloc.h
 *
 * Internals for the memory allocator
 */

#include <stdint.h>
#include <stddef.h>

/*
 * This is the minimum chunk size we will ask the kernel for; this should
 * be a multiple of the page size on all architectures.
 */
#define MALLOC_CHUNK_SIZE	65536
#define MALLOC_CHUNK_MASK       (MALLOC_CHUNK_SIZE-1)


struct free_arena_header;

/*
 * This structure should be a power of two.  This becomes the
 * alignment unit.
 */
struct arena_header {
    void *tag;
    size_t attrs;			/* Bits 0..1: Type, 2..3: Unused, 4..31: MSB of the size  */
    struct free_arena_header *next, *prev;
};


#define ARENA_TYPE_USED 0x0
#define ARENA_TYPE_FREE 0x1
#define ARENA_TYPE_HEAD 0x2
#ifdef DEBUG_MALLOC
#define ARENA_TYPE_DEAD 0x3
#endif

#define ARENA_SIZE_MASK (~(uintptr_t)(sizeof(struct arena_header)-1))
#define ARENA_TYPE_MASK	((size_t)0x3)

#define ARENA_ALIGN_UP(p)	((char *)(((uintptr_t)(p) + ~ARENA_SIZE_MASK) & ARENA_SIZE_MASK))
#define ARENA_ALIGN_DOWN(p)	((char *)((uintptr_t)(p) & ARENA_SIZE_MASK))

#define ARENA_SIZE_GET(attrs)	((attrs) & ARENA_SIZE_MASK)
#define ARENA_TYPE_GET(attrs)	((attrs) & ARENA_TYPE_MASK)

#define ARENA_SIZE_SET(attrs, size)	\
	((attrs) = ((size) & ARENA_SIZE_MASK) | ((attrs) & ~ARENA_SIZE_MASK))
#define ARENA_TYPE_SET(attrs, type) \
	((attrs) = ((attrs) & ~ARENA_TYPE_MASK) | ((type) & ARENA_TYPE_MASK))

/*
 * This structure should be no more than twice the size of the
 * previous structure.
 */
struct free_arena_header {
    struct arena_header a;
    struct free_arena_header *next_free, *prev_free;
};

extern struct free_arena_header __malloc_head;
void __inject_free_block(struct free_arena_header *ah);