From e88aee660ea668cc96d8a5a11060b6e02b2f0fd7 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Wed, 17 Jul 2024 17:00:19 +0200 Subject: kernel: actually store the tag (and size) in the kmalloc metadata I'm storing more information in less space. Win-win. --- src/kernel/malloc.c | 64 ++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 30 deletions(-) (limited to 'src/kernel/malloc.c') 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 #include -#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<next) { kprintf( - "%08p %016llx %s %d\n", + "%08p %016llx %s %db \n", al, al->bitmap, full ? "full" : "part", 1ull<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<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<len = len - sizeof(AllocHdr); + hdr->tag = tag; /* if this was the last free region, move to fullslabs */ if (isfull(al->bitmap, sizeclass)) { -- cgit v1.2.3