summaryrefslogtreecommitdiff
path: root/src/kernel/malloc.c
blob: 2fe4792d9e1a58547ae9e2b2f9f4d4add13c0468 (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
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
#include <kernel/arch/generic.h>
#include <kernel/malloc.h>
#include <kernel/panic.h>
#include <kernel/util.h>
#include <shared/mem.h>
#include <stdbool.h>
#include <stdint.h>

#define SCMIN  6 /* 1<<6  == 64 */
#define SCMAX 12 /* 1<<11 == 2048 */

static const char *tagnames[] = {
	"Invalid", /* TagInvalid */
	"KernelFs", /* TagKernelFs */
	"ProcFs", /* TagProcFs */
	"UserFs", /* TagUserFs */
	"OpenPath", /* TagOpenPath */
	"MountPath", /* TagMountPath */
	"MountUser", /* TagMountUser */
	"MountRoot", /* TagMountRoot */
	"Execbuf", /* TagExecbuf */
	"VfsReq", /* TagVfsReq */
	"Handleset", /* TagHandleset */
	"Handle", /* TagHandle */
	"Process", /* TagProcess */
	"ProcessHandle", /* TagProcessHandle */
	"DevTime", /* TagDevTime */
	"RootCache", /* TagRootCache */
	"PageRefcount", /* TagPageRefcount */
	"Intr", /* TagIntr */
};
static_assert(sizeof(tagnames) == sizeof(const char *) * TagLast);

typedef struct Slab Slab;
struct Slab {
	/* The slab is divided up into 1<<sizeclass sized regions.
	 * SCMIN <= sizeclass < SCMAX */
	int sizeclass;
	/* Conveniently, 4096/64 = 64, so I can fit the entire region bitmap
	 * in a word. */
	uint64_t bitmap;
	Slab **slot, *next;
};

/* The code assumes the header fits within the first region. */
static_assert(sizeof(Slab) <= 1<<SCMIN);

/* stored at the end of a region */
typedef struct AllocHdr AllocHdr;
struct AllocHdr {
	uint16_t len : 10;
	uint16_t tag : 6;
};
static_assert(sizeof(AllocHdr) == 2); /* just making sure */

/* Yes, I'm wasting a bit of memory here. */
static Slab *freeslabs[SCMAX] = {NULL};
static Slab *fullslabs[SCMAX] = {NULL};
static uint64_t curslabs = 0;

static uint64_t
findfreebit(uint64_t bitmap)
{
	/* I'm hoping gcc is smart enough to optimize this into a BSF */
	bitmap = ~bitmap;
	for (uint64_t i = 0; i < 64; i++) {
		if (bitmap & (1ull<<i)) return i;
	}
	return 64;
}

static bool
isfull(uint64_t bitmap, int sizeclass)
{
	return findfreebit(bitmap)<<sizeclass == PAGE_SIZE;
}

static uint64_t
crawllist(Slab *al, int sizeclass, bool full)
{
	(void) full;
	uint64_t tally = 0;
	int regsize = 1ull << sizeclass;
	for (; al; al = al->next) {
		kprintf(
			"%08p %016llx %s %db \n",
			al, al->bitmap, full ? "full" : "part", 1ull<<al->sizeclass
		);

		assert(al->sizeclass == sizeclass);
		assert(al->bitmap & 1);
		assert(al->slot && *al->slot == al);
		assert(isfull(al->bitmap, sizeclass) == full);
		tally++;

		for (int i = 1; i * regsize < PAGE_SIZE; i++) {
			char *p = ((char*)al) + i * regsize;
			AllocHdr *hdr = (void*)p + regsize - sizeof(AllocHdr);
			if (al->bitmap & (1ull<<i)) {
				assert(hdr->tag != TagInvalid && hdr->tag < TagLast);
				kprintf("%08p %4db %s\n", p, hdr->len, tagnames[hdr->tag]);
			}
		}
	}
	return tally;
}

void
kmalloc_debugprint(void)
{
	uint64_t tally = 0;
	for (int i = 0; i < SCMAX; i++) {
		tally += crawllist(freeslabs[i], i, false);
		tally += crawllist(fullslabs[i], i, true);
	}
	assert(tally == curslabs);
	// TODO count waste
}

static int
getsizeclass(size_t len)
{
	int i;
	for (i = SCMIN; i < SCMAX-1; i++) { /* last iteration can be skipped */
		if (len <= (1ull<<i)) break;
	}
	assert(len <= (1ull<<i));
	return i;
}

static Slab *
getheader(void *addr)
{
	Slab *hdr = (void*)((uintptr_t)addr & ~PAGE_MASK);
	assert(SCMIN <= hdr->sizeclass && hdr->sizeclass < SCMAX);
	assert(hdr->bitmap & 1);
	return hdr;
}

void *
kmalloc(size_t len, enum MallocTag tag)
{
	Slab *al;
	void *ret;
	int sizeclass, regsize;
	uint64_t idx;

	assert(tag != TagInvalid && tag < TagLast);
	assert(len <= KMALLOC_MAX);
	len += sizeof(AllocHdr);
	sizeclass = getsizeclass(len);
	regsize = 1ull << sizeclass;

	if (freeslabs[sizeclass] == NULL) {
		al = page_alloc(1);
		al->sizeclass = sizeclass;
		al->bitmap = 1; /* mark header as allocated */
		al->slot = &freeslabs[sizeclass];
		al->next = NULL;
		freeslabs[sizeclass] = al;
		curslabs++;
	}
	al = freeslabs[sizeclass];

	idx = findfreebit(al->bitmap);
	assert(1 <= idx && idx < 64);
	assert((al->bitmap & (1ull<<idx)) == 0);
	assert(regsize * idx < PAGE_SIZE);
	ret = ((char*)al) + regsize * idx;
	al->bitmap |= 1ull<<idx;

	AllocHdr *hdr = ret + regsize - sizeof(AllocHdr);
	hdr->len = len - sizeof(AllocHdr);
	hdr->tag = tag;

	/* if this was the last free region, move to fullslabs */
	if (isfull(al->bitmap, sizeclass)) {
		*al->slot = al->next;
		al->next->slot = al->slot;

		al->next = fullslabs[sizeclass];
		al->next->slot = &al->next;
		fullslabs[sizeclass] = al;
		al->slot = &fullslabs[sizeclass];
	}

	return ret;
}

void
kfree(void *ptr)
{
	assert(ptr);
	Slab *al = getheader(ptr);
	int sizeclass = al->sizeclass;
	int regsize = 1ull << sizeclass;

	uint64_t off = ((uint64_t)ptr) & PAGE_MASK;
	uint64_t idx = off >> sizeclass;
	bool full = isfull(al->bitmap, sizeclass);

	assert(((char*)al) + idx * regsize == ptr); /* aligned? */
	assert(1 <= idx); /* not the header? */
	assert(al->bitmap & (1ull<<idx)); /* actually allocated? */
	al->bitmap &= ~(1ull<<idx);

	memset(ptr, 0xCC, regsize);

	if (al->bitmap == 1) { /* we're empty */
		*al->slot = al->next;
		al->next->slot = al->slot;
		page_free(al, 1);
		curslabs--;
	} else if (full) { /* we're no longer full */
		*al->slot = al->next;
		al->next->slot = al->slot;

		al->next = freeslabs[sizeclass];
		al->next->slot = &al->next;
		freeslabs[sizeclass] = al;
		al->slot = &freeslabs[sizeclass];
		assert(fullslabs[sizeclass] != al);
	}
}