From 9f41bec46231a23a441710c3cdade9ba60c5a8c1 Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 5 Jun 2024 14:08:24 -0400 Subject: [PATCH] silly --- CMakeLists.txt | 2 +- examples/main.cpp | 13 +++++++++++++ include/blt/gp/program.h | 20 +++++++++++--------- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9e48988..cc87c22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.10) +project(blt-gp VERSION 0.0.11) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/examples/main.cpp b/examples/main.cpp index dafb019..5fe57ec 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -277,10 +277,23 @@ struct silly int bruh; }; +blt::gp::stack_allocator alloc; + int main() { test(); std::cout << alignof(silly) << " " << sizeof(silly) << std::endl; + std::cout << alignof(void*) << " " << sizeof(void*) << std::endl; + std::cout << blt::type_string() << std::endl; + + alloc.push(50); + alloc.push(550.0f); + alloc.push(20.1230345); + alloc.push(std::string("SillyString")); + alloc.push(&"SillyString"); + + std::cout << std::endl << std::endl; + std::cout << alloc.pop() << std::endl; blt::size_t remaining_bytes = 4096; //auto* pointer = static_cast(head->metadata.offset); diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index 8fe1177..5864bac 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -99,19 +99,20 @@ namespace blt::gp void push(T&& value) { auto ptr = allocate_bytes(); - head->metadata.offset = ptr + sizeof(T); + head->metadata.offset = static_cast(ptr) + sizeof(T); new(ptr) T(std::forward(value)); } template T pop() { + constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT); if (head == nullptr) throw std::runtime_error("Silly boi the stack is empty!"); - if (head->remaining_bytes_in_block() - head->storage_size() < sizeof(T)) + if (head->remaining_bytes_in_block() - head->storage_size() < static_cast(sizeof(T))) throw std::runtime_error("Mismatched Types!"); - T t = *reinterpret_cast(head->metadata.offset - sizeof(T)); - head->metadata.offset -= sizeof(T); + T t = *reinterpret_cast(head->metadata.offset - offset); + head->metadata.offset -= offset; if (head->used_bytes_in_block() == static_cast(head->storage_size())) { auto ptr = head; @@ -124,6 +125,7 @@ namespace blt::gp template T& from(blt::size_t bytes) { + constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT); auto remaining_bytes = static_cast(bytes); blt::i64 bytes_into_block = 0; block* blk = head; @@ -142,7 +144,7 @@ namespace blt::gp } if (blk == nullptr) throw std::runtime_error("Some nonsense is going on. This function already smells"); - return *reinterpret_cast((blk->metadata.offset - bytes_into_block) - sizeof(T)); + return *reinterpret_cast((blk->metadata.offset - bytes_into_block) - offset); } [[nodiscard]] bool empty() const @@ -221,22 +223,22 @@ namespace blt::gp template void* allocate_bytes() { - auto ptr = get_aligned_pointer(sizeof(T), alignof(T)); + auto ptr = get_aligned_pointer(sizeof(T)); if (ptr == nullptr) push_block_for(); - ptr = get_aligned_pointer(sizeof(T), alignof(T)); + ptr = get_aligned_pointer(sizeof(T)); if (ptr == nullptr) throw std::bad_alloc(); return ptr; } - void* get_aligned_pointer(blt::size_t bytes, blt::size_t alignment) + void* get_aligned_pointer(blt::size_t bytes) { if (head == nullptr) return nullptr; blt::size_t remaining_bytes = head->remaining_bytes_in_block(); auto* pointer = static_cast(head->metadata.offset); - return std::align(alignment, bytes, pointer, remaining_bytes); + return std::align(MAX_ALIGNMENT, bytes, pointer, remaining_bytes); } template