works now?
parent
2571df3b09
commit
4d731b074d
|
@ -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)
|
||||
|
|
|
@ -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<decltype(&"SillString")>() << std::endl;
|
||||
|
||||
|
@ -317,7 +323,7 @@ int main()
|
|||
std::cout << alloc.pop<int>() << 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{});
|
||||
|
|
|
@ -106,14 +106,15 @@ namespace blt::gp
|
|||
template<typename T>
|
||||
T pop()
|
||||
{
|
||||
constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT);
|
||||
constexpr auto TYPE_SIZE = aligned_size<T>();
|
||||
if (head == nullptr)
|
||||
throw std::runtime_error("Silly boi the stack is empty!");
|
||||
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!");
|
||||
T t = *reinterpret_cast<T*>(head->metadata.offset - offset);
|
||||
head->metadata.offset -= offset;
|
||||
if (head->used_bytes_in_block() == static_cast<blt::ptrdiff_t>(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<T*>(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<typename T>
|
||||
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);
|
||||
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<T*>((blk->metadata.offset - bytes_into_block) - offset);
|
||||
return *reinterpret_cast<T*>((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<blt::ptrdiff_t>(head->storage_size());
|
||||
return head->used_bytes_in_block() == 0;
|
||||
}
|
||||
|
||||
stack_allocator() = default;
|
||||
|
@ -274,6 +275,12 @@ namespace blt::gp
|
|||
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:
|
||||
block* head = nullptr;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue