From 8c79c8dd15e10426763d74755fd2c3fafd994f4c Mon Sep 17 00:00:00 2001 From: Brett Date: Tue, 4 Jun 2024 14:04:49 -0400 Subject: [PATCH] silly stack --- CMakeLists.txt | 2 +- include/blt/gp/program.h | 39 ++++++++++++++++++++++++++------------- lib/blt | 2 +- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9854000..c630fde 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.25) -project(blt-gp VERSION 0.0.7) +project(blt-gp VERSION 0.0.8) option(ENABLE_ADDRSAN "Enable the address sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) diff --git a/include/blt/gp/program.h b/include/blt/gp/program.h index 9d98755..99da360 100644 --- a/include/blt/gp/program.h +++ b/include/blt/gp/program.h @@ -16,6 +16,7 @@ * along with this program. If not, see . */ +#include #ifndef BLT_GP_PROGRAM_H #define BLT_GP_PROGRAM_H @@ -86,38 +87,50 @@ namespace blt::gp class stack_allocator { - constexpr static blt::size_t BLOCK_SIZE = 4096; + constexpr static blt::size_t PAGE_SIZE = 0x1000; public: private: - template - static inline auto to_block(T* p) - { - return reinterpret_cast(reinterpret_cast(p) & static_cast(~(BLOCK_SIZE - 1))); - } - struct block { struct block_metadata_t { + blt::size_t size = 0; block* next = nullptr; block* prev = nullptr; blt::u8* offset = nullptr; } metadata; - blt::u8 buffer[BLOCK_SIZE - sizeof(block_metadata_t)]{}; + blt::u8 buffer[8]{}; - block() + explicit block(blt::size_t size) { + metadata.size = size; metadata.offset = buffer; } - // remaining space inside the block after accounting for the metadata - static constexpr blt::size_t BLOCK_REMAINDER = BLOCK_SIZE - sizeof(typename block::block_metadata_t); + [[nodiscard]] blt::size_t remainder_after_meta() const + { + return metadata.size - sizeof(typename block::block_metadata_t); + } + + [[nodiscard]] blt::ptrdiff_t remaining_bytes() const + { + return static_cast(metadata.offset - buffer); + } }; - static block* allocate_block() + static size_t to_nearest_page_size(blt::size_t bytes) { - return reinterpret_cast(std::aligned_alloc(BLOCK_SIZE, BLOCK_SIZE)); + constexpr static blt::size_t MASK = ~(PAGE_SIZE - 1); + return (bytes & MASK) + PAGE_SIZE; + } + + static block* allocate_block(blt::size_t bytes) + { + auto size = to_nearest_page_size(bytes); + auto* data = std::aligned_alloc(PAGE_SIZE, size); + new(data) block{size}; + return reinterpret_cast(data); } private: diff --git a/lib/blt b/lib/blt index bc68e6d..8d3bfbc 160000 --- a/lib/blt +++ b/lib/blt @@ -1 +1 @@ -Subproject commit bc68e6dd4a3dfa1de9be2fb8e9302b52c4f3db5c +Subproject commit 8d3bfbcdc36fb769257ae58c0374ef44253e0a34