aboutsummaryrefslogtreecommitdiff
path: root/src/nodes.c
blob: 5edb783698804695a69a1951867bdce09a969e86 (plain) (blame)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2021 - 2022, Kim Kuparinen < kimi.h.kuparinen@gmail.com > */

/**
 * @file nodes.c
 * The node subsystem. Each client has to initialize their own node system,
 * after which they can request nodes of a size specified at init.
 *
 * Node allocation is implemented through a similar system used by jemalloc
 * (https://github.com/jemalloc/jemalloc), but instead of having a number of
 * different sized buckets there is only the one size specified by the user.
 * This cuts down on complexity and improves performance, at a somewhat major
 * flexibility cost. Still, this kernel generally only allocates nodes of the
 * same size again and again, and this approach seems sensible.
 *
 *
 * Quick overview of the allocator, all nodes live in memory pages. Each memory
 * page has a small header at the front, with some metadata about number of free
 * and used node slots. When a memory page is filled, a new one is allocated by
 * the physical memory subsystem and the pages are linked together in a common
 * list. At the same time, a second linked list is maintained which maintains
 * which pages have empty slots. When a node is freed, the page it belonged to
 * is added to the free list (if it didn't already exist there) and when a new
 * node is requested, the free list is looked through first.
 *
 * \todo More in-depth documentation about the node algorithm.
 */

#include <kmi/mem.h>
#include <kmi/pmem.h>
#include <kmi/bits.h>
#include <kmi/nodes.h>
#include <kmi/string.h>

/* the structure of each node_region is approximately
 *
 * struct node_region | bitmap | array of node_size nodes
 *
 * where array starts on a multiple of node_size to ensure alignment and
 * bitmap is a bitmap of whether node at index is free or used (1 being used, 0
 * being free)
 */

/**
 * Get start of node region from pointer.
 *
 * @param r Pointer to node inside node region.
 * @return Corresponding node region.
 */
#define node_region(r) \
	((struct node_region *)((uintptr_t)(r) & ~(BASE_PAGE_SIZE - 1)))

/**
 * Create new node region.
 *
 * @return Pointer to created region.
 */
static struct node_region *__create_region()
{
	struct node_region *r = (struct node_region *)alloc_page(BASE_PAGE);
	memset(r, FREE, BASE_PAGE_SIZE);
	return r;
}

void init_nodes(struct node_root *r, size_t node_size)
{
	r->head = __create_region();
	r->av_head = r->head;
	r->node_size = node_size;
	r->bitmap = sizeof(struct node_region);

	/* ideal values */
	size_t max_nodes = BASE_PAGE_SIZE / node_size;
	/* make sure not to truncate division */
	size_t bitmap_size = (max_nodes / 8) + 1;
	uintptr_t first_node = r->bitmap + bitmap_size;
	/* actual values */
	r->first_node = align_up(first_node, node_size);
	r->max_nodes = max_nodes - (r->first_node / node_size);
}

void destroy_nodes(struct node_root *r)
{
	struct node_region *nr = r->head;
	while (nr) {
		struct node_region *d = nr;
		nr = nr->prev;
		free_page(BASE_PAGE, (pm_t)d);
	}
}

/**
 * Find free node in node region.
 *
 * @param r Node root to work in.
 * @param nr Node region to look in.
 * @return Pointer to free node.
 */
static void *__find_free_node(struct node_root *r, struct node_region *nr)
{
	uint8_t *bitmap = r->bitmap + (uint8_t *)nr;
	for (size_t i = 0; i < r->max_nodes; ++i) {
		if (bitmap_is_set(bitmap, i))
			continue;

		bitmap_set(bitmap, i);
		return (i * r->node_size) + (r->first_node + (uint8_t *)nr);
	}

	return 0;
}

/**
 * Pop free list head.
 *
 * @param r Node region root to work in.
 */
static void __pop_av_head(struct node_root *r)
{
	struct node_region *t = r->av_head;
	r->av_head = r->av_head->next;
	if (r->av_head)
		r->av_head->av_prev = 0;

	t->av_next = 0;
	t->av_prev = 0;
}

void *get_node(struct node_root *r)
{
	if (!r)
		return 0;

	if (!r->av_head) {
		r->av_head = __create_region();

		r->av_head->prev = r->head;
		r->head->next = r->av_head;

		r->head = r->av_head;
	}

	void *p = __find_free_node(r, r->av_head);
	if (++r->av_head->used_nodes == r->max_nodes)
		__pop_av_head(r);

	return p;
}

/**
 * Push free list head.
 *
 * @param r Node region root to work in.
 * @param nr Node region to push.
 */
static void __push_av_head(struct node_root *r, struct node_region *nr)
{
	nr->av_prev = 0;
	nr->av_next = r->av_head;
	if (r->av_head)
		r->av_head->av_prev = nr;

	r->av_head = nr;
}

/**
 * Free a node region.
 *
 * @param r Node region root to work in.
 * @param nr Node region to free.
 */
static void __free_region(struct node_root *r, struct node_region *nr)
{
	struct node_region *av_n = nr->av_next;
	struct node_region *av_p = nr->av_prev;

	if (av_n)
		av_n->av_prev = av_p;

	if (av_p)
		av_p->av_next = av_n;

	if (nr == r->av_head)
		__pop_av_head(r);

	struct node_region *n = nr->next;
	struct node_region *p = nr->prev;

	if (n)
		n->prev = p;

	if (p)
		p->next = n;

	if (nr == r->head) {
		if (r->head->prev) {
			r->head->next = 0;
			r->head = r->head->prev;
		} else
			return;
	}

	free_page(BASE_PAGE, (pm_t)nr);
}

void free_node(struct node_root *r, void *p)
{
	struct node_region *nr = node_region(p);
	uint8_t *bitmap = r->bitmap + (uint8_t *)nr;
	size_t i =
		((uintptr_t)p - (r->first_node + (uintptr_t)nr)) / r->node_size;
	bitmap_clear(bitmap, i);

	if (--nr->used_nodes == 0) {
		__free_region(r, nr);
		return;
	}

	else if (!nr->av_next && !nr->av_prev)
		__push_av_head(r, nr);
}