From 4d731b074d2ae98480c72fae4b03f0988cd50a10 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 5 Jun 2024 21:36:34 -0400 Subject: [PATCH] works now? --- CMakeLists.txt | 2 +- examples/main.cpp | 10 ++++++++-- include/blt/gp/program.h | 23 +++++++++++++++-------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c8adae8..a5d1e58 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.12) +project(blt-gp VERSION 0.0.13) 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 bda3601..cd0f71e 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -297,9 +297,15 @@ blt::gp::stack_allocator alloc; int main() { + constexpr blt::size_t MAX_ALIGNMENT = 8; test(); std::cout << alignof(silly) << " " << sizeof(silly) << std::endl; - std::cout << alignof(super_large) << " " << sizeof(super_large) << std::endl; + std::cout << alignof(super_large) << " " << sizeof(super_large) << " " << ((sizeof(super_large) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) + << std::endl; + std::cout << ((sizeof(char) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) << " " + << ((sizeof(short) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) << std::endl; + std::cout << ((sizeof(int) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) << " " << ((sizeof(long) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) + << std::endl; std::cout << alignof(void*) << " " << sizeof(void*) << std::endl; std::cout << blt::type_string() << std::endl; @@ -317,7 +323,7 @@ int main() std::cout << alloc.pop() << std::endl; std::cout << std::endl; - std::cout << "Is empty? " << alloc.empty() << std::endl << std::endl; + std::cout << "Is empty? " << alloc.empty() << std::endl; alloc.push(silly{}); alloc.push(large{}); alloc.push(super_large{}); diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index 963e05c..c243d18 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -106,14 +106,15 @@ namespace blt::gp template T pop() { - constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT); + constexpr auto TYPE_SIZE = aligned_size(); if (head == nullptr) throw std::runtime_error("Silly boi the stack is empty!"); if (head->used_bytes_in_block() < static_cast(sizeof(T))) - throw std::runtime_error("Mismatched Types! Not enough space left in block!"); - T t = *reinterpret_cast(head->metadata.offset - offset); - head->metadata.offset -= offset; - if (head->used_bytes_in_block() == static_cast(head->storage_size())) + throw std::runtime_error((std::string("Mismatched Types! Not enough space left in block! Bytes: ") += std::to_string( + head->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(T))).c_str()); + T t = *reinterpret_cast(head->metadata.offset - TYPE_SIZE); + head->metadata.offset -= TYPE_SIZE; + if (head->used_bytes_in_block() == 0) { auto ptr = head; head = head->metadata.prev; @@ -125,7 +126,7 @@ namespace blt::gp template T& from(blt::size_t bytes) { - constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT); + constexpr auto TYPE_SIZE = aligned_size(); auto remaining_bytes = static_cast(bytes); blt::i64 bytes_into_block = 0; block* blk = head; @@ -144,7 +145,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) - offset); + return *reinterpret_cast((blk->metadata.offset - bytes_into_block) - TYPE_SIZE); } [[nodiscard]] bool empty() const @@ -153,7 +154,7 @@ namespace blt::gp return true; if (head->metadata.prev != nullptr) return false; - return head->used_bytes_in_block() == static_cast(head->storage_size()); + return head->used_bytes_in_block() == 0; } stack_allocator() = default; @@ -273,6 +274,12 @@ namespace blt::gp new(data) block{size}; return reinterpret_cast(data); } + + template + static inline constexpr blt::size_t aligned_size() noexcept + { + return (sizeof(T) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT-1); + } private: block* head = nullptr;