aboutsummaryrefslogtreecommitdiff
path: root/kernel/lib/slab_alloc.c
blob: 63ee0e0952f9aa89701b292af16c656174cccffb (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <slab_alloc.h>

typedef struct object {
	struct object *next;
} object_t;

typedef struct cache {
	void* region_addr;
	
	uint32_t n_free_objs;
	object_t* first_free_obj;

	struct cache *next_cache;	// next cache in this slab
} cache_t;

typedef struct region {
	void* region_addr;
	size_t region_size;
	struct region *next_region;
	bool contains_descriptors;
} region_t;

typedef union descriptor {
	cache_t c;
	region_t r;
	union descriptor *next_free;
} descriptor_t;

typedef struct slab {
	cache_t *first_cache;		// linked list of caches
} slab_t;

struct mem_allocator {
	const slab_type_t *types;
	slab_t *slabs;

	descriptor_t *first_free_descriptor;
	region_t *all_regions;

	page_alloc_fun_t alloc_fun;
	page_free_fun_t free_fun;
};

// ============================================== //
// Helper functions for the manipulation of lists //
// ============================================== //

static void add_free_descriptor(mem_allocator_t *a, descriptor_t *c) {
	c->next_free = a->first_free_descriptor;
	a->first_free_descriptor = c;
}

static descriptor_t *take_descriptor(mem_allocator_t *a) {
	if (a->first_free_descriptor == 0) {
		void* p = a->alloc_fun(PAGE_SIZE);
		if (p == 0) return 0;

		const void* end = p + PAGE_SIZE;
		for (descriptor_t *i = (descriptor_t*)p; i + 1 <= (descriptor_t*)end; i++) {
			add_free_descriptor(a, i);
		}

		// register the descriptor region
		descriptor_t *dd = a->first_free_descriptor;
		ASSERT(dd != 0);
		a->first_free_descriptor = dd->next_free;

		region_t *drd = &dd->r;
		drd->region_addr = p;
		drd->region_size = PAGE_SIZE;
		drd->contains_descriptors = true;
		drd->next_region = a->all_regions;
		a->all_regions = drd;
	}

	descriptor_t *x = a->first_free_descriptor;
	ASSERT(x != 0);
	a->first_free_descriptor = x->next_free;
	return x;
}

// ============================== //
// The actual allocator functions //
// ============================== //

mem_allocator_t* create_slab_allocator(const slab_type_t *types, page_alloc_fun_t af, page_free_fun_t ff) {
	union {
		void* addr;
		mem_allocator_t *a;
		slab_t *s;
		descriptor_t *d;
	} ptr;

	ptr.addr = af(PAGE_SIZE);
	if (ptr.addr == 0) return 0;	// could not allocate
	const void* end_addr = ptr.addr + PAGE_SIZE;

	mem_allocator_t *a = ptr.a;
	ptr.a++;

	a->all_regions = 0;
	a->alloc_fun = af;
	a->free_fun = ff;

	a->types = types;
	a->slabs = ptr.s;
	for (const slab_type_t *t = types; t->obj_size != 0; t++) {
		ASSERT(t->obj_size >= sizeof(object_t));
		ptr.s->first_cache = 0;
		ptr.s++;
	}

	a->first_free_descriptor = 0;
	while (ptr.d + 1 <= (descriptor_t*)end_addr) {
		add_free_descriptor(a, ptr.d);
		ptr.d++;
	}

	return a;
}

static void stack_and_destroy_regions(page_free_fun_t ff, region_t *r) {
	if (r == 0) return;
	void* addr = r->region_addr;
	ASSERT(r != r->next_region);
	stack_and_destroy_regions(ff, r->next_region);
	ff(addr);
}
void destroy_slab_allocator(mem_allocator_t *a) {
	for (int i = 0; a->types[i].obj_size != 0; i++) {
		for (cache_t *c = a->slabs[i].first_cache; c != 0; c = c->next_cache) {
			a->free_fun(c->region_addr);
		}
	}
	region_t *dr = 0;
	region_t *i = a->all_regions;
	while (i != 0) {
		region_t *r = i;
		i = r->next_region;
		if (r->contains_descriptors) {
			r->next_region = dr;
			dr = r;
		} else {
			a->free_fun(r->region_addr);
		}
	}
	stack_and_destroy_regions(a->free_fun, dr);
	a->free_fun(a);
}

void* slab_alloc(mem_allocator_t* a, size_t sz) {
	for (int i = 0; a->types[i].obj_size != 0; i++) {
		const size_t obj_size = a->types[i].obj_size;
		if (sz <= obj_size) {
			// find a cache with free space
			cache_t *fc = a->slabs[i].first_cache;
			while (fc != 0 && fc->n_free_objs == 0) {
				ASSERT(fc->first_free_obj == 0); // make sure n_free == 0 iff no object in the free stack
				fc = fc->next_cache;
			}
			// if none found, try to allocate a new one
			if (fc == 0) {
				descriptor_t *fcd = take_descriptor(a);
				if (fcd == 0) return 0;

				fc = &fcd->c;
				ASSERT((descriptor_t*)fc == fcd);

				const size_t cache_size = a->types[i].pages_per_cache * PAGE_SIZE;
				fc->region_addr = a->alloc_fun(cache_size);
				if (fc->region_addr == 0) {
					add_free_descriptor(a, fcd);
					return 0;
				}

				fc->n_free_objs = 0;
				fc->first_free_obj = 0;
				for (void* p = fc->region_addr; p + obj_size <= fc->region_addr + cache_size; p += obj_size) {
					object_t *x = (object_t*)p;
					x->next = fc->first_free_obj;
					fc->first_free_obj = x;
					fc->n_free_objs++;
				}
				ASSERT(fc->n_free_objs == cache_size / obj_size);

				fc->next_cache = a->slabs[i].first_cache;
				a->slabs[i].first_cache = fc;
			}
			// allocate on fc
			ASSERT(fc != 0 && fc->n_free_objs > 0);

			object_t *x = fc->first_free_obj;
			fc->first_free_obj = x->next;
			fc->n_free_objs--;

			ASSERT((fc->n_free_objs == 0) == (fc->first_free_obj == 0));

			// TODO : if fc is full, put it at the end
			return x;
		}
	}

	// otherwise directly allocate using a->alloc_fun
	descriptor_t *rd = take_descriptor(a);
	if (rd == 0) return 0;
	region_t *r = &rd->r;
	ASSERT((descriptor_t*)r == rd);

	r->region_addr = a->alloc_fun(sz);
	if (r->region_addr == 0) {
		add_free_descriptor(a, rd);
		return 0;
	} else {
		r->region_size = sz;
		r->contains_descriptors = false;

		r->next_region = a->all_regions;
		a->all_regions = r;

		return (void*)r->region_addr;
	}
}

void slab_free(mem_allocator_t* a, void* addr) {

	for (int i = 0; a->types[i].obj_size != 0; i++) {
		size_t region_size = PAGE_SIZE * a->types[i].pages_per_cache;
		for (cache_t *r = a->slabs[i].first_cache; r != 0; r = r->next_cache) {
			if (addr >= r->region_addr && addr < r->region_addr + region_size) {
				ASSERT((addr - r->region_addr) % a->types[i].obj_size == 0);

				object_t *o = (object_t*)addr;
				o->next = r->first_free_obj;
				r->first_free_obj = o;
				r->n_free_objs++;

				if (r->n_free_objs == region_size / a->types[i].obj_size) {
					// region is completely unused, free it.
					if (a->slabs[i].first_cache == r) {
						a->slabs[i].first_cache = r->next_cache;
					} else {
						for (cache_t *it = a->slabs[i].first_cache; it->next_cache != 0; it = it->next_cache) {
							if (it->next_cache == r) {
								it->next_cache = r->next_cache;
								break;
							}
						}
					}
					a->free_fun(r->region_addr);
					add_free_descriptor(a, (descriptor_t*)r);
				}
				return;
			}
		}
	}

	// otherwise the block was directly allocated : look for it in regions.
	ASSERT(a->all_regions != 0);

	if (a->all_regions->region_addr == addr) {
		a->free_fun(addr);	// found it, free it

		region_t *r = a->all_regions;
		a->all_regions = r->next_region;
		add_free_descriptor(a, (descriptor_t*)r);
	} else {
		for (region_t *i = a->all_regions; i->next_region != 0; i = i->next_region) {
			if (i->next_region->region_addr == addr) {
				a->free_fun(addr);	// found it, free it

				region_t *r = i->next_region;
				ASSERT(!r->contains_descriptors);
				i->next_region = r->next_region;
				add_free_descriptor(a, (descriptor_t*)r);
				return;
			}
		}
		ASSERT(false);
	}
}