silly
parent
2831e25ba5
commit
9f41bec462
|
@ -1,5 +1,5 @@
|
||||||
cmake_minimum_required(VERSION 3.25)
|
cmake_minimum_required(VERSION 3.25)
|
||||||
project(blt-gp VERSION 0.0.10)
|
project(blt-gp VERSION 0.0.11)
|
||||||
|
|
||||||
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)
|
||||||
|
|
|
@ -277,10 +277,23 @@ struct silly
|
||||||
int bruh;
|
int bruh;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
blt::gp::stack_allocator alloc;
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
test();
|
test();
|
||||||
std::cout << alignof(silly) << " " << sizeof(silly) << std::endl;
|
std::cout << alignof(silly) << " " << sizeof(silly) << std::endl;
|
||||||
|
std::cout << alignof(void*) << " " << sizeof(void*) << std::endl;
|
||||||
|
std::cout << blt::type_string<decltype(&"SillString")>() << std::endl;
|
||||||
|
|
||||||
|
alloc.push(50);
|
||||||
|
alloc.push(550.0f);
|
||||||
|
alloc.push(20.1230345);
|
||||||
|
alloc.push(std::string("SillyString"));
|
||||||
|
alloc.push(&"SillyString");
|
||||||
|
|
||||||
|
std::cout << std::endl << std::endl;
|
||||||
|
std::cout << alloc.pop<decltype(&"SillString")>() << std::endl;
|
||||||
|
|
||||||
blt::size_t remaining_bytes = 4096;
|
blt::size_t remaining_bytes = 4096;
|
||||||
//auto* pointer = static_cast<void*>(head->metadata.offset);
|
//auto* pointer = static_cast<void*>(head->metadata.offset);
|
||||||
|
|
|
@ -99,19 +99,20 @@ namespace blt::gp
|
||||||
void push(T&& value)
|
void push(T&& value)
|
||||||
{
|
{
|
||||||
auto ptr = allocate_bytes<T>();
|
auto ptr = allocate_bytes<T>();
|
||||||
head->metadata.offset = ptr + sizeof(T);
|
head->metadata.offset = static_cast<blt::u8*>(ptr) + sizeof(T);
|
||||||
new(ptr) T(std::forward<T>(value));
|
new(ptr) T(std::forward<T>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
T pop()
|
T pop()
|
||||||
{
|
{
|
||||||
|
constexpr auto offset = std::max(sizeof(T), MAX_ALIGNMENT);
|
||||||
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->remaining_bytes_in_block() - head->storage_size() < sizeof(T))
|
if (head->remaining_bytes_in_block() - head->storage_size() < static_cast<blt::ptrdiff_t>(sizeof(T)))
|
||||||
throw std::runtime_error("Mismatched Types!");
|
throw std::runtime_error("Mismatched Types!");
|
||||||
T t = *reinterpret_cast<T*>(head->metadata.offset - sizeof(T));
|
T t = *reinterpret_cast<T*>(head->metadata.offset - offset);
|
||||||
head->metadata.offset -= sizeof(T);
|
head->metadata.offset -= offset;
|
||||||
if (head->used_bytes_in_block() == static_cast<blt::ptrdiff_t>(head->storage_size()))
|
if (head->used_bytes_in_block() == static_cast<blt::ptrdiff_t>(head->storage_size()))
|
||||||
{
|
{
|
||||||
auto ptr = head;
|
auto ptr = head;
|
||||||
|
@ -124,6 +125,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);
|
||||||
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;
|
||||||
|
@ -142,7 +144,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) - sizeof(T));
|
return *reinterpret_cast<T*>((blk->metadata.offset - bytes_into_block) - offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] bool empty() const
|
[[nodiscard]] bool empty() const
|
||||||
|
@ -221,22 +223,22 @@ namespace blt::gp
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void* allocate_bytes()
|
void* allocate_bytes()
|
||||||
{
|
{
|
||||||
auto ptr = get_aligned_pointer(sizeof(T), alignof(T));
|
auto ptr = get_aligned_pointer(sizeof(T));
|
||||||
if (ptr == nullptr)
|
if (ptr == nullptr)
|
||||||
push_block_for<T>();
|
push_block_for<T>();
|
||||||
ptr = get_aligned_pointer(sizeof(T), alignof(T));
|
ptr = get_aligned_pointer(sizeof(T));
|
||||||
if (ptr == nullptr)
|
if (ptr == nullptr)
|
||||||
throw std::bad_alloc();
|
throw std::bad_alloc();
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* get_aligned_pointer(blt::size_t bytes, blt::size_t alignment)
|
void* get_aligned_pointer(blt::size_t bytes)
|
||||||
{
|
{
|
||||||
if (head == nullptr)
|
if (head == nullptr)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
blt::size_t remaining_bytes = head->remaining_bytes_in_block();
|
blt::size_t remaining_bytes = head->remaining_bytes_in_block();
|
||||||
auto* pointer = static_cast<void*>(head->metadata.offset);
|
auto* pointer = static_cast<void*>(head->metadata.offset);
|
||||||
return std::align(alignment, bytes, pointer, remaining_bytes);
|
return std::align(MAX_ALIGNMENT, bytes, pointer, remaining_bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
Loading…
Reference in New Issue