#include #include #include #include #include #define logf(FMT, ...) \ printf("%s(): " FMT "\n", __FUNCTION__ __VA_OPT__(, ) __VA_ARGS__); typedef uint16_t W; typedef W WPtr; typedef W AllocEntryPtr; typedef struct { AllocEntryPtr p_prev; AllocEntryPtr p_next; WPtr ptr; W size; } AllocEntry; typedef struct { AllocEntryPtr p_entries_begin; AllocEntryPtr p_entries_end; AllocEntryPtr p_first; AllocEntryPtr p_last; W alloc_base; } AllocTableHead; constexpr W alloc_base = 0x2000; constexpr W alloc_end = 0x3fff; constexpr W max_allocs = 0x100; constexpr W size_of_alloc_table_head = 0x20; constexpr W size_of_alloc_entry = 0x4; W memory[65536]; AllocTableHead *table = (AllocTableHead *)&memory[alloc_base]; void kmalloc_init(void) { *table = (AllocTableHead) { .p_entries_begin = alloc_base + size_of_alloc_table_head, .p_entries_end = alloc_base + size_of_alloc_table_head + size_of_alloc_entry * max_allocs, .p_first = 0, .p_last = 0, .alloc_base = alloc_base + size_of_alloc_table_head + size_of_alloc_entry * max_allocs, }; logf("Initialized allocator"); logf(" p_table = 0x%x", alloc_base); logf(" p_entries_begin = 0x%x", table->p_entries_begin); logf(" p_entries_end = 0x%x", table->p_entries_end); logf(" alloc_base = 0x%x", table->alloc_base); } static WPtr find_ptr_with_space_for_allocation(W size) { WPtr ptr = table->alloc_base; AllocEntryPtr p_entry = table->p_first; while (p_entry) { AllocEntry *entry = (AllocEntry *)&memory[p_entry]; bool overlapping = ptr + size > entry->ptr && ptr < entry->ptr + entry->size; if (!overlapping) break; ptr = entry->ptr + entry->size; p_entry = entry->p_next; } if (ptr + size > alloc_end) return 0; // Out of memory. return ptr; } static AllocEntryPtr find_empty_entry_in_allocation_table(void) { for (AllocEntryPtr p_entry = table->p_entries_begin; p_entry < table->p_entries_end; p_entry += size_of_alloc_entry) { AllocEntry *entry = (AllocEntry *)&memory[p_entry]; if (!entry->ptr) return p_entry; } return 0; // Out of entry slots. } WPtr kmalloc(W size) { WPtr ptr = find_ptr_with_space_for_allocation(size); logf("ptr = 0x%x", ptr); if (!ptr) { logf("Out of memory!"); return 0; // Out of memory. } AllocEntryPtr p_entry = find_empty_entry_in_allocation_table(); logf("p_entry = 0x%x", p_entry); AllocEntry *entry = (AllocEntry *)&memory[p_entry]; *entry = (AllocEntry) { .p_prev = table->p_last, .p_next = 0, .ptr = ptr, .size = size, }; logf("Adding entry"); logf(" p_prev = 0x%x", entry->p_prev); logf(" p_next = 0x%x", entry->p_next); logf(" ptr = 0x%x", entry->ptr); logf(" size = 0x%x", entry->size); table->p_last = p_entry; if (!table->p_first) table->p_first = p_entry; logf("table->p_first = 0x%x", table->p_first); logf("table->p_last = 0x%x", table->p_last); if (entry->p_prev) { AllocEntry *prev = (AllocEntry *)&memory[entry->p_prev]; prev->p_next = p_entry; } logf("New allocation at 0x%x of size %u", ptr, size); return ptr; } static AllocEntryPtr find_alloc_entry_by_ptr(WPtr ptr) { for (AllocEntryPtr p_entry = table->p_entries_begin; p_entry < table->p_entries_end; p_entry += size_of_alloc_entry) { AllocEntry *entry = (AllocEntry *)&memory[p_entry]; logf("entry->ptr = 0x%x", entry->ptr); if (entry->ptr == ptr) return p_entry; } return 0; // Invalid pointer. } static void delete_entry(AllocEntryPtr p_entry) { AllocEntry *entry = (AllocEntry *)&memory[p_entry]; if (entry->p_next) { AllocEntry *next = (AllocEntry *)&memory[entry->p_next]; next->p_prev = entry->p_prev; } else { table->p_last = entry->p_prev; } if (entry->p_prev) { AllocEntry *prev = (AllocEntry *)&memory[entry->p_prev]; prev->p_next = prev->p_next; } else { table->p_first = entry->p_next; } } void kfree(WPtr ptr) { logf("ptr = 0x%x", ptr); AllocEntryPtr p_entry = find_alloc_entry_by_ptr(ptr); logf("p_entry = 0x%x", p_entry); if (!p_entry) return; // Ignore invalid pointer. delete_entry(p_entry); logf("table->p_first = 0x%x", table->p_first); logf("table->p_last = 0x%x", table->p_last); } WPtr krealloc(WPtr ptr, W size) { logf("ptr = 0x%x", ptr); AllocEntryPtr p_entry = find_alloc_entry_by_ptr(ptr); logf("p_entry = 0x%x", p_entry); AllocEntry *entry = (AllocEntry *)&memory[p_entry]; W move_size = size < entry->size ? entry->size : size; delete_entry(p_entry); WPtr new_ptr = kmalloc(size); memmove(&memory[new_ptr], &memory[ptr], move_size); return new_ptr; } int main(void) { logf("Calling kmalloc_init()"); kmalloc_init(); logf("Calling p0 = kmalloc(120)"); WPtr p0 = kmalloc(120); logf("Calling p1 = kmalloc(120)"); WPtr p1 = kmalloc(120); assert(p0 != p1); logf("Calling p2 = kmalloc(120)"); WPtr p2 = kmalloc(120); (void)p2; logf("Calling kfree(p1)"); kfree(p1); logf("Calling p3 = kmalloc(60)"); WPtr p3 = kmalloc(60); assert(p1 == p3); logf("Calling p4 = kmalloc(60)"); WPtr p4 = kmalloc(60); logf("kfree(p4)"); kfree(p4); logf("p5 = krealloc(p3, 120)"); WPtr p5 = krealloc(p3, 120); assert(p3 == p5); logf("works!"); }