From 2571df3b09b7e61ffc2e106fa100c28769ce4d9a Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Wed, 5 Jun 2024 20:15:08 -0400 Subject: [PATCH] found a broken example --- CMakeLists.txt | 2 +- examples/main.cpp | 46 +++++++++++++++++++++++++++++++++++----- include/blt/gp/program.h | 18 ++++++++-------- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc87c22..c8adae8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.11) +project(blt-gp VERSION 0.0.12) 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 5fe57ec..bda3601 100644 --- a/examples/main.cpp +++ b/examples/main.cpp @@ -273,8 +273,24 @@ void test() struct silly { - long nyah; - int bruh; + long nyah = 50; + int bruh = 10; + + friend std::ostream& operator<<(std::ostream& stream, const silly& silly) + { + stream << "[" << silly.nyah << " " << silly.bruh << "]"; + return stream; + } +}; + +struct large +{ + unsigned char data[2048]; +}; + +struct super_large +{ + unsigned char data[9582]; }; blt::gp::stack_allocator alloc; @@ -283,17 +299,37 @@ int main() { test(); std::cout << alignof(silly) << " " << sizeof(silly) << std::endl; + std::cout << alignof(super_large) << " " << sizeof(super_large) << 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(550.3f); alloc.push(20.1230345); alloc.push(std::string("SillyString")); alloc.push(&"SillyString"); - std::cout << std::endl << std::endl; - std::cout << alloc.pop() << std::endl; + std::cout << std::endl; + std::cout << *alloc.pop() << std::endl; + std::cout << alloc.pop() << std::endl; + std::cout << alloc.pop() << std::endl; + std::cout << alloc.pop() << std::endl; + std::cout << alloc.pop() << std::endl; + std::cout << std::endl; + + std::cout << "Is empty? " << alloc.empty() << std::endl << std::endl; + alloc.push(silly{}); + alloc.push(large{}); + alloc.push(super_large{}); + alloc.push(silly{25, 24}); + alloc.push(large{}); + + std::cout << std::endl; + alloc.pop(); + std::cout << alloc.pop() << std::endl; + alloc.pop(); + alloc.pop(); + 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 5864bac..963e05c 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -109,15 +109,15 @@ namespace blt::gp 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() < static_cast(sizeof(T))) - throw std::runtime_error("Mismatched Types!"); + 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())) { auto ptr = head; head = head->metadata.prev; - delete ptr; + std::free(ptr); } return t; } @@ -182,7 +182,7 @@ namespace blt::gp { block* ptr = current; current = current->metadata.prev; - delete ptr; + std::free(ptr); } } @@ -209,15 +209,15 @@ namespace blt::gp return static_cast(metadata.size - sizeof(typename block::block_metadata_t)); } - [[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const - { - return storage_size() - static_cast(metadata.offset - buffer); - } - [[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const { return static_cast(metadata.offset - buffer); } + + [[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const + { + return storage_size() - used_bytes_in_block(); + } }; template