summaryrefslogtreecommitdiff
path: root/lib/heap.c
blob: 03d542532b6b4cfff0beaafb81f89b559fd718b5 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>

#include "config.h"

struct heap_node
{
	size_t size;
	union
	{
		struct
		{
			struct heap_node *prev;
			struct heap_node *next;
		};
		struct
		{
			char block;
		};
	};
};

struct heap_head
{
	size_t allocated_size;
	size_t maximum_size;
	size_t minimum_alloc;
	struct heap_node *root;
};

struct heap_head *heap;

void *allocate_superblock(void)
{
	return mmap(nullptr, HEAP_SEGMENT_SIZE, 0, 0, -1, 0);
}

static struct heap_node *try_create_heap()
{
	if (heap != nullptr) return heap->root;

	char *superblock = allocate_superblock();

	heap = (struct heap_head *)superblock;
	heap->root = (struct heap_node *)(superblock + sizeof(struct heap_head));

	heap->allocated_size = HEAP_SEGMENT_SIZE;
	heap->maximum_size = HEAP_MAX_SIZE;
	heap->minimum_alloc = sizeof(heap->root);

	heap->root->next = nullptr;
	heap->root->prev = nullptr;
	heap->root->size = HEAP_SEGMENT_SIZE - sizeof(superblock) - sizeof(heap->root->size);

	return heap->root;
}

static struct heap_node *try_grow_heap(struct heap_node *prev)
{
	if (heap->allocated_size >= heap->maximum_size)
	{
		return nullptr;
	}

	struct heap_node *node = allocate_superblock();

	if (node == nullptr)
	{
		return nullptr;
	}

	node->size = HEAP_SEGMENT_SIZE - sizeof(node->size);
	node->prev = prev;
	prev->next = node;

	return node;
}

static inline size_t get_node_size(size_t size)
{
	return size + sizeof(size);
}

static inline bool can_divide_node(struct heap_node *parent)
{
	return parent->size <= heap->minimum_alloc + sizeof(struct heap_node);
}

static inline void *new_node_address(struct heap_node *parent, size_t size)
{
	return &parent->block + parent->size - get_node_size(size);
}

static inline struct heap_node *divide_node(struct heap_node *parent, size_t size)
{
	struct heap_node *child = new_node_address(parent, size);

	parent->size -= get_node_size(size);
	child->size = size;

	return child;
}

static inline struct heap_node *try_divide_node(struct heap_node *node, size_t size)
{
	return can_divide_node(node) ? divide_node(node, size) : node;
}

static inline struct heap_node *allocate_node(size_t size)
{
	struct heap_node *node = try_create_heap();

	while(node != nullptr)
	{
		if (node->size > size)
		{
			break;
		}

		node = node->next
			? node->next
			: try_grow_heap(node);
	}

	return node;
}

void *malloc(size_t size)
{
	// TODO: min(heap->minimum_alloc, size);
	size = size >= heap->minimum_alloc 
		? size 
		: heap->minimum_alloc;

	struct heap_node *node = try_divide_node(allocate_node(size), size);

	return node != nullptr
		? &node->block
		: nullptr;
}

void *calloc(size_t count, size_t size)
{
	void *block = malloc(count * size);

	if (block != nullptr)
	{
		memset(block, 0, count * size);
	}

	return block;
}

static inline struct heap_node *block_to_node(char *block)
{
	return (struct heap_node *)(block - sizeof(size_t));
}

void free(void *block)
{
	// TODO: insertion sort by address of node
	// TODO: coalesce adjacent nodes
	if (block == nullptr)
	{
		return;
	}

	struct heap_node *node = block_to_node(block);
	node->prev = nullptr;
	node->next = heap->root;
	heap->root = node;
	
	if (node->next != nullptr)
	{
		node->next->prev = node;
	}
}