From 6f82c92c309a0f21374cce323119e26f86678735 Mon Sep 17 00:00:00 2001 From: Able Date: Sun, 24 Nov 2024 11:03:06 -0600 Subject: [PATCH] sorta alloc support. Compiler error checkpoint --- .../libraries/stn/src/alloc/block_alloc.hb | 119 +++++++++--------- sysdata/libraries/stn/src/alloc/fake_alloc.hb | 31 +++-- sysdata/programs/alloc_test/src/main.hb | 27 ++-- sysdata/system_config.toml | 4 +- 4 files changed, 90 insertions(+), 91 deletions(-) diff --git a/sysdata/libraries/stn/src/alloc/block_alloc.hb b/sysdata/libraries/stn/src/alloc/block_alloc.hb index 7cd29c6b6..0477b0f51 100644 --- a/sysdata/libraries/stn/src/alloc/block_alloc.hb +++ b/sysdata/libraries/stn/src/alloc/block_alloc.hb @@ -1,75 +1,74 @@ +.{log} := @use("../lib.hb") alloc_return := @use("alloc_return.hb") /* the block size is 64 bytes, 64 blocks of 64 bytes. this will very quickly lead to exhaustion of free blocks. */ BlockAlloc := struct { + // hi state: uint, ptr: ?^u8, $init := fn(): Self { return .(0, null) } + + $alloc := fn(self: Self, alloc_type: type, count: uint): alloc_return.AllocReturn { + offset := 1 + a := 1 + loop { + // a = self.state >> offset; + // check if the `offset` bit is 1, if it is move to the next offset + if a == 1 { + offset += 1 + log.info("Already Allocated\0") + } else { + // self it to 1 and return the ptr to the allocation + self.state |= a + // return ptr + offset * 64 + return .(64, self.ptr + offset * 64) + } + // there are only 64 blocks + if offset >= 64 { + return .(0, null) + } + } + } + + $dealloc := fn(self: Self, ptr: ^u8, alloc_type: type, count: uint): void { + // size := size_of(alloc_type)*count + size := 64 + // get the size alligned to the nearest block + // rounded_size := nearest_block_size_rounded_up(size) + rounded_size := 64 + + state_bit_start := { + // Do math here to figure out what starting ptr corresponds to what bit + 3 + } + + offset := 0 + + loop { + if rounded_size > 0 { + // set state_bit_start+offset to 0 + + // at the end move to the next one + offset += 1 + } else { + break + } + + rounded_size -= 64 + } + return void + } + + $deinit := fn(self: Self): void { + self.state = 0 + self.ptr = null + } } -// $alloc := fn(self: Self, alloc_type: Type, count: uint): alloc_return.AllocReturn { -// offset := 0 -// a := 0 -// loop { -// a = self.state << offset; -// // check if the `offset` bit is 1, if it is move to the next offset -// if a == 1 { -// offset += 1 -// } else { -// // self it to 1 and return the ptr to the allocation -// self.state |= a -// return (ptr + (offset * 64)) -// } -// // there are only 64 blocks -// if offset >= 64 { -// break; -// } -// } -// return null -// } - - - // request a kernel page -// ptr := memory.alloc(1) - - - -// $dealloc := fn(&self, ptr: ^u8, alloc_type: Type, count: uint): void { -// size := size_of(alloc_type)*count -// // get the size alligned to the nearest block -// rounded_size := nearest_block_size_rounded_up(size) - -// state_bit_start := { -// // Do math here to figure out what starting ptr corresponds to what bit -// 3 -// } - -// offset := 0 - -// loop { -// if rounded_size > 0 { -// // set state_bit_start+offset to 0 - -// // at the end move to the next one -// offset += 1 -// } else { -// break; -// } - -// rounded_size -= 64 -// } - -// return void -// } - -// $deinit := fn(&self): void { -// self.state := 0 - -// } -// } \ No newline at end of file +// ptr := memory.alloc(1) \ No newline at end of file diff --git a/sysdata/libraries/stn/src/alloc/fake_alloc.hb b/sysdata/libraries/stn/src/alloc/fake_alloc.hb index a7ccd76fc..82c422acc 100644 --- a/sysdata/libraries/stn/src/alloc/fake_alloc.hb +++ b/sysdata/libraries/stn/src/alloc/fake_alloc.hb @@ -1,22 +1,19 @@ alloc_return := @use("alloc_return.hb") - FakeAlloc := struct { - $init := fn():Self{ - return .() - } - - $alloc := fn(self: Self, alloc_type: type, count: uint): alloc_return.AllocReturn { - return .(0, null) - } - - $dealloc := fn(self: Self, ptr: ^u8, alloc_type: Type, count: uint) : void{ - return void - } - // Nothing to clean up here - $deinit := fn(self: Self): void { - return void - } -} + $init := fn(): Self { + return .() + } + $alloc := fn(self: Self, alloc_type: type, count: uint): alloc_return.AllocReturn { + return .(0, null) + } + $dealloc := fn(self: Self, ptr: ^u8, alloc_type: type, count: uint): void { + return void + } + // Nothing to clean up here + $deinit := fn(self: Self): void { + return void + } +} \ No newline at end of file diff --git a/sysdata/programs/alloc_test/src/main.hb b/sysdata/programs/alloc_test/src/main.hb index 7b4a7dcd2..67ed88451 100644 --- a/sysdata/programs/alloc_test/src/main.hb +++ b/sysdata/programs/alloc_test/src/main.hb @@ -1,25 +1,28 @@ stn := @use("../../../libraries/stn/src/lib.hb"); -.{allocators, panic} := stn +.{allocators, panic, log} := stn AStruct := struct { - a_field: u8, + a_field: u8, } main := fn(): void { - alloc := allocators.FakeAlloc.init() - astruct := alloc.alloc(AStruct, 2) + // alloc := allocators.FakeAlloc.init() + // astruct := alloc.alloc(AStruct, 2) // if astruct.ptr != null{ - // panic.panic("FakeAlloc actually allocated.") + // panic.panic("FakeAlloc actually allocated.") // } // alloc.dealloc(&astruct.ptr, AStruct, 2) - alloc.deinit() + // alloc.deinit() - // balloc := allocators.BlockAlloc.init() - // bstruct_ptr := balloc.alloc(AStruct, 2) - // if bstruct_ptr == null { - // panic("BlockAlloc actually didn't allocate.") - // } + balloc := allocators.BlockAlloc.init() + bstruct := balloc.alloc(AStruct, 2) + if bstruct.ptr == null { + log.info("Hi\0") + // panic.panic("BlockAlloc actually didn't allocate.") + } else { + log.info("Allocator functioned.\0") + } // balloc.dealloc(bstruct_ptr, AStruct, 2) // balloc.deinit() - return + return } \ No newline at end of file diff --git a/sysdata/system_config.toml b/sysdata/system_config.toml index 182c3dedc..61344f6f0 100644 --- a/sysdata/system_config.toml +++ b/sysdata/system_config.toml @@ -46,5 +46,5 @@ resolution = "1024x768x24" # [boot.limine.ableos.modules.processes] # path = "boot:///processes.hbf" -# [boot.limine.ableos.modules.alloc_test] -# path = "boot:///alloc_test.hbf" \ No newline at end of file +[boot.limine.ableos.modules.alloc_test] +path = "boot:///alloc_test.hbf" \ No newline at end of file