works now?

thread
Brett 2024-06-05 21:36:34 -04:00
parent 2571df3b09
commit 4d731b074d
3 changed files with 24 additions and 11 deletions

View File

@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25) 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_ADDRSAN "Enable the address sanitizer" OFF)
option(ENABLE_UBSAN "Enable the ub sanitizer" OFF) option(ENABLE_UBSAN "Enable the ub sanitizer" OFF)

View File

@ -297,9 +297,15 @@ blt::gp::stack_allocator alloc;
int main() int main()
{ {
constexpr blt::size_t MAX_ALIGNMENT = 8;
test(); test();
std::cout << alignof(silly) << " " << sizeof(silly) << std::endl; 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 << alignof(void*) << " " << sizeof(void*) << std::endl;
std::cout << blt::type_string<decltype(&"SillString")>() << std::endl; std::cout << blt::type_string<decltype(&"SillString")>() << std::endl;
@ -317,7 +323,7 @@ int main()
std::cout << alloc.pop<int>() << std::endl; std::cout << alloc.pop<int>() << std::endl;
std::cout << 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(silly{});
alloc.push(large{}); alloc.push(large{});
alloc.push(super_large{}); alloc.push(super_large{});

View File

@ -106,14 +106,15 @@ namespace blt::gp
template<typename T> template<typename T>
T pop() T pop()
{ {
constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT); constexpr auto TYPE_SIZE = aligned_size<T>();
if (head == nullptr) if (head == nullptr)
throw std::runtime_error("Silly boi the stack is empty!"); throw std::runtime_error("Silly boi the stack is empty!");
if (head->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(sizeof(T))) if (head->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(sizeof(T)))
throw std::runtime_error("Mismatched Types! Not enough space left in block!"); throw std::runtime_error((std::string("Mismatched Types! Not enough space left in block! Bytes: ") += std::to_string(
T t = *reinterpret_cast<T*>(head->metadata.offset - offset); head->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(T))).c_str());
head->metadata.offset -= offset; T t = *reinterpret_cast<T*>(head->metadata.offset - TYPE_SIZE);
if (head->used_bytes_in_block() == static_cast<blt::ptrdiff_t>(head->storage_size())) head->metadata.offset -= TYPE_SIZE;
if (head->used_bytes_in_block() == 0)
{ {
auto ptr = head; auto ptr = head;
head = head->metadata.prev; head = head->metadata.prev;
@ -125,7 +126,7 @@ namespace blt::gp
template<typename T> template<typename T>
T& from(blt::size_t bytes) T& from(blt::size_t bytes)
{ {
constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT); constexpr auto TYPE_SIZE = aligned_size<T>();
auto remaining_bytes = static_cast<blt::i64>(bytes); auto remaining_bytes = static_cast<blt::i64>(bytes);
blt::i64 bytes_into_block = 0; blt::i64 bytes_into_block = 0;
block* blk = head; block* blk = head;
@ -144,7 +145,7 @@ namespace blt::gp
} }
if (blk == nullptr) if (blk == nullptr)
throw std::runtime_error("Some nonsense is going on. This function already smells"); throw std::runtime_error("Some nonsense is going on. This function already smells");
return *reinterpret_cast<T*>((blk->metadata.offset - bytes_into_block) - offset); return *reinterpret_cast<T*>((blk->metadata.offset - bytes_into_block) - TYPE_SIZE);
} }
[[nodiscard]] bool empty() const [[nodiscard]] bool empty() const
@ -153,7 +154,7 @@ namespace blt::gp
return true; return true;
if (head->metadata.prev != nullptr) if (head->metadata.prev != nullptr)
return false; return false;
return head->used_bytes_in_block() == static_cast<blt::ptrdiff_t>(head->storage_size()); return head->used_bytes_in_block() == 0;
} }
stack_allocator() = default; stack_allocator() = default;
@ -274,6 +275,12 @@ namespace blt::gp
return reinterpret_cast<block*>(data); return reinterpret_cast<block*>(data);
} }
template<typename T>
static inline constexpr blt::size_t aligned_size() noexcept
{
return (sizeof(T) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT-1);
}
private: private:
block* head = nullptr; block* head = nullptr;
}; };