kmallllllllllllloc

This commit is contained in:
2026-09-28 19:14:54 +02:00
parent a2cd3aa1b1
commit c74ea060e5
2 changed files with 64 additions and 18 deletions
+32
View File
@@ -15,6 +15,32 @@ void* kzalloc(size_t size);
// zeroed-out memory. Returns a pointer to allocated memory or NULL.
void* kcalloc(size_t count, size_t size);
// 9 bytes in 8 bytes
#define TAG_SHIFT 56
#define TAG_MASK ((uintptr_t)0xFF << TAG_SHIFT)
#define SIGN_BIT ((uintptr_t)1 << 55)
typedef struct {
uintptr_t bits;
} tagged_ptr;
static inline tagged_ptr tp_make(void* p, uint8_t tag) {
uintptr_t addr = (uintptr_t)p & ~TAG_MASK;
return (tagged_ptr){addr | ((uintptr_t)tag << TAG_SHIFT)};
}
static inline uint8_t tp_tag(tagged_ptr t) {
return (uint8_t)(t.bits >> TAG_SHIFT);
}
static inline void* tp_ptr(tagged_ptr t) {
/* Bits 48..55 were untouched and still equal the canonical sign.
Copy bit 55 into bits 56..63. */
uintptr_t addr =
(t.bits & SIGN_BIT) ? (t.bits | TAG_MASK) : (t.bits & ~TAG_MASK);
return (void*)addr;
}
// powers of 2
#define KMALLOC_MAX_SIZE 17
#define LOWEST_POWER 3
@@ -28,6 +54,12 @@ struct partialy_freelist {
struct partialy_freelist* partialy_freelist_nextnode;
};
// this is used when object is too small to hold the big one in this case we use
// first object as pointer storage and dont allocate it
struct partialy_freelist_short {
struct partialy_freelist_short* partialy_freelist_nextnode;
};
union freelists_list {
struct freelist* freelists[KMALLOC_MAX_SIZE];
struct {