summaryrefslogtreecommitdiff
path: root/src/kernel/malloc.h
blob: a38d49b121317b4eb58bc49b80cfffd487fc9d74 (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
#pragma once
#include <kernel/arch/generic.h>
#include <shared/mem.h>
#include <stddef.h>

/* This seems to be fine for now.
 * KMALLOC_MAX >= 2048 would require extending kmalloc with a new allocation
 * type. KMALLOC_MAX >= 4096 would require a new (Buddy?) page allocator.
 * Both are pretty easy, but I don't want to maintain what would currently be
 * dead code. */
#define KMALLOC_MAX 1024

/* also see the string table in malloc.c */
enum MallocTag {
	TagInvalid,
	TagKernelFs,
	TagProcFs,
	TagUserFs,
	TagOpenPath,
	TagMountPath,
	TagMountUser,
	TagMountRoot,
	TagExecbuf,
	TagVfsReq,
	TagHandleset,
	TagHandle,
	TagProcess,
	TagProcessHandle,
	TagDevTime,
	TagRootCache,
	TagPageRefcount,
	TagLast,
};

void mem_init(void *memtop);
void mem_reserve(void *addr, size_t len);
void mem_debugprint(void);

void kmalloc_debugprint(void); /* internal, called by mem_debugprint */

// allocates `pages` consecutive pages
void *page_alloc(size_t pages);
// zeroes the allocated pages
void *page_zalloc(size_t pages);

// frees `pages` consecutive pages starting from *first
void page_free(void *first, size_t pages);

void *kmalloc(size_t len, enum MallocTag tag);
void kfree(void *ptr);

// TODO calloc
static inline void *kzalloc(size_t len, enum MallocTag tag) {
	void *b = kmalloc(len, tag);
	memset(b, 0, len);
	return b;
}