summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordzwdz2024-07-17 17:00:19 +0200
committerdzwdz2024-07-17 17:00:19 +0200
commite88aee660ea668cc96d8a5a11060b6e02b2f0fd7 (patch)
treefda7e1c8d897ff40631028443737b27848d1ef06
parent473112b1541cf81fa3670e0d1cb6de1c4a3281de (diff)
kernel: actually store the tag (and size) in the kmalloc metadata
I'm storing more information in less space. Win-win.
-rw-r--r--src/kernel/malloc.c64
1 files changed, 34 insertions, 30 deletions
diff --git a/src/kernel/malloc.c b/src/kernel/malloc.c
index ca44cfe..9f87274 100644
--- a/src/kernel/malloc.c
+++ b/src/kernel/malloc.c
@@ -6,29 +6,27 @@
#include <stdbool.h>
#include <stdint.h>
-#define DESCLEN 8
-
#define SCMIN 6 /* 1<<6 == 64 */
#define SCMAX 12 /* 1<<11 == 2048 */
-const char *tagnames[] = {
- "TagInvalid",
- "TagKernelFs",
- "TagProcFs",
- "TagUserFs",
- "TagOpenPath",
- "TagMountPath",
- "TagMountUser",
- "TagMountRoot",
- "TagExecbuf",
- "TagVfsReq",
- "TagHandleset",
- "TagHandle",
- "TagProcess",
- "TagProcessHandle",
- "TagDevTime",
- "TagRootCache",
- "TagPageRefcount",
+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 */
};
static_assert(sizeof(tagnames) == sizeof(const char *) * TagLast);
@@ -46,6 +44,14 @@ struct Slab {
/* 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};
@@ -76,7 +82,7 @@ crawllist(Slab *al, int sizeclass, bool full)
int regsize = 1ull << sizeclass;
for (; al; al = al->next) {
kprintf(
- "%08p %016llx %s %d\n",
+ "%08p %016llx %s %db \n",
al, al->bitmap, full ? "full" : "part", 1ull<<al->sizeclass
);
@@ -88,8 +94,10 @@ crawllist(Slab *al, int sizeclass, bool full)
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)) {
- kprintf("%08p %.8s\n", p, p + regsize - DESCLEN);
+ assert(hdr->tag != TagInvalid && hdr->tag < TagLast);
+ kprintf("%08p %4db %s\n", p, hdr->len, tagnames[hdr->tag]);
}
}
}
@@ -137,10 +145,8 @@ kmalloc(size_t len, enum MallocTag tag)
uint64_t idx;
assert(tag != TagInvalid && tag < TagLast);
- const char *desc = tagnames[tag] + 3; // very temporary
-
assert(len <= KMALLOC_MAX);
- len += DESCLEN;
+ len += sizeof(AllocHdr);
sizeclass = getsizeclass(len);
regsize = 1ull << sizeclass;
@@ -162,11 +168,9 @@ kmalloc(size_t len, enum MallocTag tag)
ret = ((char*)al) + regsize * idx;
al->bitmap |= 1ull<<idx;
- char *descloc = ret + regsize - DESCLEN;
- memset(descloc, ' ', DESCLEN);
- for (int i = 0; i < DESCLEN && desc[i]; i++) {
- descloc[i] = desc[i];
- }
+ 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)) {