use a more logical ordering

thread
Brett 2024-06-06 02:25:42 -04:00
parent fc8133376d
commit 3d81d30f34
4 changed files with 639 additions and 617 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.14) project(blt-gp VERSION 0.0.15)
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

@ -23,367 +23,369 @@
#include <vector> #include <vector>
#include <random> #include <random>
// small scale //// small scale
enum class op //enum class op
{ //{
ADD, // ADD,
SUB, // SUB,
MUL, // MUL,
DIV, // DIV,
LIT // LIT
}; //};
//
std::string to_string(op o) //std::string to_string(op o)
{ //{
switch (o) // switch (o)
{ // {
case op::ADD: // case op::ADD:
return "ADD"; // return "ADD";
case op::SUB: // case op::SUB:
return "SUB"; // return "SUB";
case op::MUL: // case op::MUL:
return "MUL"; // return "MUL";
case op::DIV: // case op::DIV:
return "DIV"; // return "DIV";
case op::LIT: // case op::LIT:
return "LIT"; // return "LIT";
} // }
return ""; // return "";
} //}
//
constexpr static long SEED = 41912; //constexpr static long SEED = 41912;
//
op generate_op() //op generate_op()
{ //{
static std::mt19937_64 engine(SEED); // static std::mt19937_64 engine(SEED);
static std::uniform_int_distribution dist(0, static_cast<int>(op::LIT) - 1); // static std::uniform_int_distribution dist(0, static_cast<int>(op::LIT) - 1);
return static_cast<op>(dist(engine)); // return static_cast<op>(dist(engine));
} //}
//
bool choice() //bool choice()
{ //{
static std::mt19937_64 engine(SEED); // static std::mt19937_64 engine(SEED);
static std::uniform_int_distribution dist(0, 1); // static std::uniform_int_distribution dist(0, 1);
return dist(engine); // return dist(engine);
} //}
//
float random_value() //float random_value()
{ //{
static std::mt19937_64 engine(SEED); // static std::mt19937_64 engine(SEED);
static std::uniform_real_distribution dist(0.0f, 10.0f); // static std::uniform_real_distribution dist(0.0f, 10.0f);
return dist(engine); // return dist(engine);
} //}
//
void test() //void test()
{ //{
std::vector<op> operations; // std::vector<op> operations;
std::vector<float> values; // std::vector<float> values;
//
std::stack<op> tree_generator; // std::stack<op> tree_generator;
tree_generator.push(generate_op()); // tree_generator.push(generate_op());
//
while (!tree_generator.empty()) // while (!tree_generator.empty())
{ // {
auto opn = tree_generator.top(); // auto opn = tree_generator.top();
tree_generator.pop(); // tree_generator.pop();
//
operations.push_back(opn); // operations.push_back(opn);
if (opn == op::LIT) // if (opn == op::LIT)
{ // {
values.push_back(random_value()); // values.push_back(random_value());
continue; // continue;
} // }
//
// child 1 // // child 1
if (choice()) // if (choice())
tree_generator.push(generate_op()); // tree_generator.push(generate_op());
else // else
tree_generator.push(op::LIT); // tree_generator.push(op::LIT);
//
// child 2 // // child 2
if (choice()) // if (choice())
tree_generator.push(generate_op()); // tree_generator.push(generate_op());
else // else
tree_generator.push(op::LIT); // tree_generator.push(op::LIT);
} // }
//
for (const auto& v : operations) // for (const auto& v : operations)
std::cout << to_string(v) << " "; // std::cout << to_string(v) << " ";
std::cout << std::endl; // std::cout << std::endl;
//
{ // {
std::stack<blt::size_t> process; // std::stack<blt::size_t> process;
for (const auto& v : operations) // for (const auto& v : operations)
{ // {
switch (v) // switch (v)
{ // {
case op::ADD: // case op::ADD:
case op::SUB: // case op::SUB:
case op::MUL: // case op::MUL:
case op::DIV: // case op::DIV:
process.emplace(2); // process.emplace(2);
std::cout << "("; // std::cout << "(";
break; // break;
case op::LIT: // case op::LIT:
break; // break;
} // }
std::cout << to_string(v); // std::cout << to_string(v);
while (!process.empty()) // while (!process.empty())
{ // {
auto top = process.top(); // auto top = process.top();
process.pop(); // process.pop();
if (top == 0) // if (top == 0)
{ // {
std::cout << ")"; // std::cout << ")";
continue; // continue;
} else // } else
{ // {
std::cout << " "; // std::cout << " ";
process.push(top - 1); // process.push(top - 1);
break; // break;
} // }
} // }
} // }
while (!process.empty()) // while (!process.empty())
{ // {
auto top = process.top(); // auto top = process.top();
process.pop(); // process.pop();
if (top == 0) // if (top == 0)
{ // {
std::cout << ") "; // std::cout << ") ";
continue; // continue;
} else // } else
{ // {
std::cerr << "FUCK YOU\n"; // std::cerr << "FUCK YOU\n";
break; // break;
} // }
} // }
std::cout << std::endl; // std::cout << std::endl;
} // }
//
for (const auto& v : values) // for (const auto& v : values)
std::cout << v << " "; // std::cout << v << " ";
std::cout << std::endl; // std::cout << std::endl;
//
{ // {
std::stack<blt::size_t> process; // std::stack<blt::size_t> process;
blt::size_t index = 0; // blt::size_t index = 0;
for (const auto& v : operations) // for (const auto& v : operations)
{ // {
switch (v) // switch (v)
{ // {
case op::ADD: // case op::ADD:
case op::SUB: // case op::SUB:
case op::MUL: // case op::MUL:
case op::DIV: // case op::DIV:
process.emplace(2); // process.emplace(2);
std::cout << "("; // std::cout << "(";
std::cout << to_string(v); // std::cout << to_string(v);
break; // break;
case op::LIT: // case op::LIT:
std::cout << values[index++]; // std::cout << values[index++];
break; // break;
} // }
//
while (!process.empty()) // while (!process.empty())
{ // {
auto top = process.top(); // auto top = process.top();
process.pop(); // process.pop();
if (top == 0) // if (top == 0)
{ // {
std::cout << ")"; // std::cout << ")";
continue; // continue;
} else // } else
{ // {
std::cout << " "; // std::cout << " ";
process.push(top - 1); // process.push(top - 1);
break; // break;
} // }
} // }
} // }
while (!process.empty()) // while (!process.empty())
{ // {
auto top = process.top(); // auto top = process.top();
process.pop(); // process.pop();
if (top == 0) // if (top == 0)
{ // {
std::cout << ") "; // std::cout << ") ";
continue; // continue;
} else // } else
{ // {
std::cerr << "FUCK YOU\n"; // std::cerr << "FUCK YOU\n";
break; // break;
} // }
} // }
std::cout << std::endl; // std::cout << std::endl;
} // }
//
std::stack<float> process; // std::stack<float> process;
std::stack<op> operators; // std::stack<op> operators;
//
for (const auto& v : operations) // for (const auto& v : operations)
operators.push(v); // operators.push(v);
//
while (!operators.empty()) // while (!operators.empty())
{ // {
auto oper = operators.top(); // auto oper = operators.top();
operators.pop(); // operators.pop();
if (oper == op::LIT) // if (oper == op::LIT)
{ // {
process.push(values.back()); // process.push(values.back());
values.pop_back(); // values.pop_back();
} else // } else
{ // {
auto v1 = process.top(); // auto v1 = process.top();
process.pop(); // process.pop();
auto v2 = process.top(); // auto v2 = process.top();
process.pop(); // process.pop();
std::cout << "processing oper " << to_string(oper) << " with values " << v1 << " " << v2 << std::endl; // std::cout << "processing oper " << to_string(oper) << " with values " << v1 << " " << v2 << std::endl;
switch (oper) // switch (oper)
{ // {
case op::ADD: // case op::ADD:
values.push_back(v1 + v2); // values.push_back(v1 + v2);
operators.push(op::LIT); // operators.push(op::LIT);
break; // break;
case op::SUB: // case op::SUB:
values.push_back(v1 - v2); // values.push_back(v1 - v2);
operators.push(op::LIT); // operators.push(op::LIT);
break; // break;
case op::MUL: // case op::MUL:
values.push_back(v1 * v2); // values.push_back(v1 * v2);
operators.push(op::LIT); // operators.push(op::LIT);
break; // break;
case op::DIV: // case op::DIV:
if (v2 == 0) // if (v2 == 0)
v2 = 1; // v2 = 1;
values.push_back(v1 / v2); // values.push_back(v1 / v2);
operators.push(op::LIT); // operators.push(op::LIT);
break; // break;
case op::LIT: // case op::LIT:
break; // break;
} // }
std::cout << "\tresult: " << values.back() << std::endl; // std::cout << "\tresult: " << values.back() << std::endl;
} // }
} // }
//
std::cout << process.size() << std::endl; // std::cout << process.size() << std::endl;
std::cout << process.top() << std::endl; // std::cout << process.top() << std::endl;
//
} //}
//
struct silly //struct silly
{ //{
long nyah = 50; // long nyah = 50;
int bruh = 10; // int bruh = 10;
//
friend std::ostream& operator<<(std::ostream& stream, const silly& silly) // friend std::ostream& operator<<(std::ostream& stream, const silly& silly)
{ // {
stream << "[" << silly.nyah << " " << silly.bruh << "]"; // stream << "[" << silly.nyah << " " << silly.bruh << "]";
return stream; // return stream;
} // }
}; //};
//
struct large //struct large
{ //{
unsigned char data[2048]; // unsigned char data[2048];
}; //};
//
struct super_large //struct super_large
{ //{
unsigned char data[9582]; // unsigned char data[9582];
}; //};
blt::gp::stack_allocator alloc; blt::gp::stack_allocator alloc;
float nyah(float f, int i, bool b)
{
return f + static_cast<float>(i * b);
}
int main() int main()
{ {
constexpr blt::size_t MAX_ALIGNMENT = 8; // 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) << " " << ((sizeof(super_large) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) // std::cout << alignof(super_large) << " " << sizeof(super_large) << " " << ((sizeof(super_large) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1))
<< std::endl; // << std::endl;
std::cout << ((sizeof(char) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) << " " // std::cout << ((sizeof(char) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) << " "
<< ((sizeof(short) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) << std::endl; // << ((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::cout << ((sizeof(int) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1)) << " " << ((sizeof(long) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1))
<< std::endl; // << 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;
//
alloc.push(50); // alloc.push(50);
alloc.push(550.3f); // alloc.push(550.3f);
alloc.push(20.1230345); // alloc.push(20.1230345);
alloc.push(true); // alloc.push(true);
alloc.push(false); // alloc.push(false);
alloc.push(std::string("SillyString")); // alloc.push(std::string("SillyString"));
alloc.push(&"SillyString"); // alloc.push(&"SillyString");
//
std::cout << std::endl; // std::cout << std::endl;
std::cout << *alloc.pop<decltype(&"SillString")>() << std::endl; // std::cout << *alloc.pop<decltype(&"SillString")>() << std::endl;
std::cout << alloc.pop<std::string>() << std::endl; // std::cout << alloc.pop<std::string>() << std::endl;
std::cout << alloc.pop<bool>() << std::endl; // std::cout << alloc.pop<bool>() << std::endl;
std::cout << alloc.pop<bool>() << std::endl; // std::cout << alloc.pop<bool>() << std::endl;
std::cout << alloc.pop<double>() << std::endl; // std::cout << alloc.pop<double>() << std::endl;
std::cout << alloc.pop<float>() << std::endl; // std::cout << alloc.pop<float>() << std::endl;
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::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{});
alloc.push(silly{25, 24}); // alloc.push(silly{25, 24});
alloc.push(large{}); // alloc.push(large{});
//
std::cout << std::endl; // std::cout << std::endl;
std::cout << "Is empty? " << alloc.empty() << std::endl; // std::cout << "Is empty? " << alloc.empty() << std::endl;
alloc.pop<large>(); // alloc.pop<large>();
std::cout << "Is empty? " << alloc.empty() << std::endl; // std::cout << "Is empty? " << alloc.empty() << std::endl;
std::cout << alloc.pop<silly>() << std::endl; // std::cout << alloc.pop<silly>() << std::endl;
std::cout << "Is empty? " << alloc.empty() << std::endl; // std::cout << "Is empty? " << alloc.empty() << std::endl;
alloc.pop<super_large>(); // alloc.pop<super_large>();
std::cout << "Is empty? " << alloc.empty() << std::endl; // std::cout << "Is empty? " << alloc.empty() << std::endl;
alloc.pop<large>(); // alloc.pop<large>();
std::cout << "Is empty? " << alloc.empty() << std::endl; // std::cout << "Is empty? " << alloc.empty() << std::endl;
std::cout << alloc.pop<silly>() << std::endl; // std::cout << alloc.pop<silly>() << std::endl;
std::cout << std::endl; // std::cout << std::endl;
//
std::cout << "Is empty? " << alloc.empty() << " bytes left: " << alloc.bytes_in_head() << std::endl; // std::cout << "Is empty? " << alloc.empty() << " bytes left: " << alloc.bytes_in_head() << std::endl;
std::cout << std::endl; // std::cout << std::endl;
//
alloc.push(silly{2, 5}); // alloc.push(silly{2, 5});
alloc.push(large{}); // alloc.push(large{});
alloc.push(super_large{}); // alloc.push(super_large{});
alloc.push(silly{80, 10}); // alloc.push(silly{80, 10});
alloc.push(large{}); // alloc.push(large{});
alloc.push(50); // alloc.push(50);
alloc.push(550.3f); // alloc.push(550.3f);
alloc.push(20.1230345); // alloc.push(20.1230345);
alloc.push(std::string("SillyString")); // alloc.push(std::string("SillyString"));
alloc.push(33.22f); alloc.push(33.22f);
alloc.push(120); alloc.push(120);
alloc.push(true); alloc.push(true);
blt::gp::operation<float, float, int, bool> silly_op(nyah);
std::cout << silly_op(alloc) << std::endl;
blt::gp::operation<float, float, int, bool> silly_op([](float f, int i, bool b) -> float { // std::cout << std::endl;
std::cout << "We found values: " << f << " " << i << " " << b << std::endl; //
return f + static_cast<float>(i * b); // //auto* pointer = static_cast<void*>(head->metadata.offset);
}); // //return std::align(alignment, bytes, pointer, remaining_bytes);
//
// float f = 10.5;
// int i = 412;
// bool b = true;
//
// std::array<void*, 3> arr{reinterpret_cast<void*>(&f), reinterpret_cast<void*>(&i), reinterpret_cast<void*>(&b)};
//
// blt::span<void*, 3> spv{arr};
//
// std::cout << silly_op.operator()(spv) << std::endl;
std::cout << alloc.run(silly_op) << std::endl; //std::cout << "Hello World!" << std::endl;
std::cout << std::endl;
//auto* pointer = static_cast<void*>(head->metadata.offset);
//return std::align(alignment, bytes, pointer, remaining_bytes);
float f = 10.5;
int i = 412;
bool b = true;
std::array<void*, 3> arr{reinterpret_cast<void*>(&f), reinterpret_cast<void*>(&i), reinterpret_cast<void*>(&b)};
blt::span<void*, 3> spv{arr};
std::cout << silly_op.operator()(spv) << std::endl;
std::cout << "Hello World!" << std::endl;
} }

View File

@ -19,6 +19,7 @@
#ifndef BLT_GP_PROGRAM_H #ifndef BLT_GP_PROGRAM_H
#define BLT_GP_PROGRAM_H #define BLT_GP_PROGRAM_H
#include <blt/gp/stack.h>
#include <cstddef> #include <cstddef>
#include <blt/gp/fwdecl.h> #include <blt/gp/fwdecl.h>
#include <functional> #include <functional>
@ -96,292 +97,54 @@ namespace blt::gp
operation(operation&& move) = default; operation(operation&& move) = default;
template<typename T, std::enable_if_t<std::is_same_v<T, function_t>, void>> explicit operation(const function_t& functor): func(functor)
explicit operation(const T& functor): func(functor)
{} {}
template<typename T, std::enable_if_t<!std::is_same_v<T, function_t>, void>>
explicit operation(const T& functor)
{
func = [&functor](Args... args) {
return functor(args...);
};
}
explicit operation(function_t&& functor): func(std::move(functor)) explicit operation(function_t&& functor): func(std::move(functor))
{} {}
[[nodiscard]] inline Return operator()(Args... args) const template<blt::u64 index>
{ inline constexpr blt::size_t getByteOffset() const
return func(args...);
}
Return operator()(blt::span<void*> args)
{
auto pack_sequence = std::make_integer_sequence<blt::u64, sizeof...(Args)>();
return function_evaluator(args, pack_sequence);
}
std::function<Return(blt::span<void*>)> to_functor()
{
return [this](blt::span<void*> args) {
return this->operator()(args);
};
}
private:
template<typename T, blt::size_t index>
static inline T& access_pack_index(blt::span<void*> args)
{
return *reinterpret_cast<T*>(args[index]);
}
template<typename T, T... indexes>
Return function_evaluator(blt::span<void*> args, std::integer_sequence<T, indexes...>)
{
return func(access_pack_index<Args, indexes>(args)...);
}
function_t func;
};
class stack_allocator
{
constexpr static blt::size_t PAGE_SIZE = 0x1000;
constexpr static blt::size_t MAX_ALIGNMENT = 8;
public:
/**
* Pushes an instance of an object on to the stack
* @tparam T type to push
* @param value universal reference to the object to push
*/
template<typename T>
void push(T&& value)
{
auto ptr = allocate_bytes<T>();
head->metadata.offset = static_cast<blt::u8*>(ptr) + aligned_size<T>();
new(ptr) T(std::forward<T>(value));
}
template<typename T>
T pop()
{
constexpr static 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>(aligned_size<T>()))
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;
std::free(ptr);
}
return t;
}
template<typename T>
T& from(blt::size_t bytes)
{
constexpr static auto TYPE_SIZE = aligned_size<T>();
auto remaining_bytes = static_cast<blt::i64>(bytes);
blt::i64 bytes_into_block = 0;
block* blk = head;
while (remaining_bytes > 0)
{
if (blk == nullptr)
throw std::runtime_error("Requested size is beyond the scope of this stack!");
auto bytes_available = blk->used_bytes_in_block() - remaining_bytes;
bytes_into_block = remaining_bytes;
if (bytes_available < 0)
{
remaining_bytes = -bytes_available;
blk = head->metadata.prev;
} else
break;
}
if (blk == nullptr)
throw std::runtime_error("Some nonsense is going on. This function already smells");
if (blk->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(aligned_size<T>()))
throw std::runtime_error((std::string("Mismatched Types! Not enough space left in block! Bytes: ") += std::to_string(
blk->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(T))).c_str());
return *reinterpret_cast<T*>((blk->metadata.offset - bytes_into_block) - TYPE_SIZE);
}
template<blt::u64 index, typename... Args>
blt::size_t getByteOffset()
{ {
blt::size_t offset = 0; blt::size_t offset = 0;
blt::size_t current_index = 0; blt::size_t current_index = 0;
((offset += (current_index++ > index ? aligned_size<Args>() : 0)), ...); ((offset += (current_index++ > index ? stack_allocator::aligned_size<Args>() : 0)), ...);
return offset; return offset;
} }
template<typename CurrentArgument, blt::u64 index, typename... Args> template<typename CurrentArgument, blt::u64 index>
CurrentArgument& getArgument() inline CurrentArgument& getArgument(stack_allocator& allocator) const
{ {
auto bytes = getByteOffset<index, Args...>(); auto bytes = getByteOffset<index>();
return from<CurrentArgument>(bytes); return allocator.from<CurrentArgument>(bytes);
} }
template<typename Return, typename... Args, blt::u64... indices> template<blt::u64... indices>
Return sequence_to_indices(const operation<Return, Args...>& function, std::integer_sequence<blt::u64, indices...>) inline Return sequence_to_indices(stack_allocator& allocator, std::integer_sequence<blt::u64, indices...>) const
{ {
return function(getArgument<Args, indices, Args...>()...); return func(getArgument<Args, indices>(allocator)...);
} }
template<typename Return, typename... Args> [[nodiscard]] inline Return operator()(stack_allocator& allocator) const
Return run(const operation<Return, Args...>& function)
{ {
auto seq = std::make_integer_sequence<blt::u64, sizeof...(Args)>(); auto seq = std::make_integer_sequence<blt::u64, sizeof...(Args)>();
return sequence_to_indices(function, seq); return sequence_to_indices(allocator, seq);
}
[[nodiscard]] bool empty() const
{
if (head == nullptr)
return true;
if (head->metadata.prev != nullptr)
return false;
return head->used_bytes_in_block() == 0;
}
[[nodiscard]] blt::ptrdiff_t bytes_in_head() const
{
if (head == nullptr)
return 0;
return head->used_bytes_in_block();
}
stack_allocator() = default;
stack_allocator(const stack_allocator& copy) = delete;
stack_allocator& operator=(const stack_allocator& copy) = delete;
stack_allocator(stack_allocator&& move) noexcept
{
head = move.head;
move.head = nullptr;
}
stack_allocator& operator=(stack_allocator&& move) noexcept
{
head = move.head;
move.head = nullptr;
return *this;
}
~stack_allocator()
{
block* current = head;
while (current != nullptr)
{
block* ptr = current;
current = current->metadata.prev;
std::free(ptr);
}
} }
private: private:
struct block // template<typename T, blt::size_t index>
{ // static inline T& access_pack_index(blt::span<void*> args)
struct block_metadata_t // {
{ // return *reinterpret_cast<T*>(args[index]);
blt::size_t size = 0; // }
block* next = nullptr; //
block* prev = nullptr; // template<typename T, T... indexes>
blt::u8* offset = nullptr; // Return function_evaluator(blt::span<void*> args, std::integer_sequence<T, indexes...>)
} metadata; // {
blt::u8 buffer[8]{}; // return func(access_pack_index<Args, indexes>(args)...);
// }
explicit block(blt::size_t size)
{
metadata.size = size;
metadata.offset = buffer;
}
[[nodiscard]] blt::ptrdiff_t storage_size() const
{
return static_cast<blt::ptrdiff_t>(metadata.size - sizeof(typename block::block_metadata_t));
}
[[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const
{
return static_cast<blt::ptrdiff_t>(metadata.offset - buffer);
}
[[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const
{
return storage_size() - used_bytes_in_block();
}
};
template<typename T> function_t func;
void* allocate_bytes()
{
auto ptr = get_aligned_pointer(sizeof(T));
if (ptr == nullptr)
push_block_for<T>();
ptr = get_aligned_pointer(sizeof(T));
if (ptr == nullptr)
throw std::bad_alloc();
return ptr;
}
void* get_aligned_pointer(blt::size_t bytes)
{
if (head == nullptr)
return nullptr;
blt::size_t remaining_bytes = head->remaining_bytes_in_block();
auto* pointer = static_cast<void*>(head->metadata.offset);
return std::align(MAX_ALIGNMENT, bytes, pointer, remaining_bytes);
}
template<typename T>
void push_block_for()
{
push_block(std::max(PAGE_SIZE, to_nearest_page_size(sizeof(T))));
}
void push_block(blt::size_t size)
{
auto blk = allocate_block(size);
if (head == nullptr)
{
head = blk;
return;
}
head->metadata.next = blk;
blk->metadata.prev = head;
head = blk;
}
static size_t to_nearest_page_size(blt::size_t bytes)
{
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<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;
}; };

257
include/blt/gp/stack.h Normal file
View File

@ -0,0 +1,257 @@
#pragma once
/*
* Copyright (C) 2024 Brett Terpstra
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_GP_STACK_H
#define BLT_GP_STACK_H
#include <blt/std/types.h>
#include <utility>
#include <stdexcept>
#include <cstdlib>
#include <memory>
namespace blt::gp
{
class stack_allocator
{
constexpr static blt::size_t PAGE_SIZE = 0x1000;
constexpr static blt::size_t MAX_ALIGNMENT = 8;
public:
/**
* Pushes an instance of an object on to the stack
* @tparam T type to push
* @param value universal reference to the object to push
*/
template<typename T>
void push(T&& value)
{
auto ptr = allocate_bytes<T>();
head->metadata.offset = static_cast<blt::u8*>(ptr) + aligned_size<T>();
new(ptr) T(std::forward<T>(value));
}
template<typename T>
T pop()
{
constexpr static 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>(aligned_size<T>()))
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;
std::free(ptr);
}
return t;
}
template<typename T>
T& from(blt::size_t bytes)
{
constexpr static auto TYPE_SIZE = aligned_size<T>();
auto remaining_bytes = static_cast<blt::i64>(bytes);
blt::i64 bytes_into_block = 0;
block* blk = head;
while (remaining_bytes > 0)
{
if (blk == nullptr)
throw std::runtime_error("Requested size is beyond the scope of this stack!");
auto bytes_available = blk->used_bytes_in_block() - remaining_bytes;
bytes_into_block = remaining_bytes;
if (bytes_available < 0)
{
remaining_bytes = -bytes_available;
blk = head->metadata.prev;
} else
break;
}
if (blk == nullptr)
throw std::runtime_error("Some nonsense is going on. This function already smells");
if (blk->used_bytes_in_block() < static_cast<blt::ptrdiff_t>(aligned_size<T>()))
throw std::runtime_error((std::string("Mismatched Types! Not enough space left in block! Bytes: ") += std::to_string(
blk->used_bytes_in_block()) += " Size: " + std::to_string(sizeof(T))).c_str());
return *reinterpret_cast<T*>((blk->metadata.offset - bytes_into_block) - TYPE_SIZE);
}
void pop_bytes(blt::ptrdiff_t bytes)
{
while (bytes > 0)
{
auto diff = head->used_bytes_in_block() - bytes;
// if there is not enough room left to pop completely off the block, then move to the next previous block
// and pop from it, update the amount of bytes to reflect the amount removed from the current block
if (diff <= 0)
{
bytes -= head->used_bytes_in_block();
head = head->metadata.prev;
} else // otherwise update the offset pointer
head->metadata.offset -= bytes;
}
}
[[nodiscard]] bool empty() const
{
if (head == nullptr)
return true;
if (head->metadata.prev != nullptr)
return false;
return head->used_bytes_in_block() == 0;
}
[[nodiscard]] blt::ptrdiff_t bytes_in_head() const
{
if (head == nullptr)
return 0;
return head->used_bytes_in_block();
}
stack_allocator() = default;
stack_allocator(const stack_allocator& copy) = delete;
stack_allocator& operator=(const stack_allocator& copy) = delete;
stack_allocator(stack_allocator&& move) noexcept
{
head = move.head;
move.head = nullptr;
}
stack_allocator& operator=(stack_allocator&& move) noexcept
{
head = move.head;
move.head = nullptr;
return *this;
}
~stack_allocator()
{
block* current = head;
while (current != nullptr)
{
block* ptr = current;
current = current->metadata.prev;
std::free(ptr);
}
}
template<typename T>
static inline constexpr blt::size_t aligned_size() noexcept
{
return (sizeof(T) + (MAX_ALIGNMENT - 1)) & ~(MAX_ALIGNMENT - 1);
}
private:
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[8]{};
explicit block(blt::size_t size)
{
metadata.size = size;
metadata.offset = buffer;
}
[[nodiscard]] blt::ptrdiff_t storage_size() const
{
return static_cast<blt::ptrdiff_t>(metadata.size - sizeof(typename block::block_metadata_t));
}
[[nodiscard]] blt::ptrdiff_t used_bytes_in_block() const
{
return static_cast<blt::ptrdiff_t>(metadata.offset - buffer);
}
[[nodiscard]] blt::ptrdiff_t remaining_bytes_in_block() const
{
return storage_size() - used_bytes_in_block();
}
};
template<typename T>
void* allocate_bytes()
{
auto ptr = get_aligned_pointer(sizeof(T));
if (ptr == nullptr)
push_block_for<T>();
ptr = get_aligned_pointer(sizeof(T));
if (ptr == nullptr)
throw std::bad_alloc();
return ptr;
}
void* get_aligned_pointer(blt::size_t bytes)
{
if (head == nullptr)
return nullptr;
blt::size_t remaining_bytes = head->remaining_bytes_in_block();
auto* pointer = static_cast<void*>(head->metadata.offset);
return std::align(MAX_ALIGNMENT, bytes, pointer, remaining_bytes);
}
template<typename T>
void push_block_for()
{
push_block(std::max(PAGE_SIZE, to_nearest_page_size(sizeof(T))));
}
void push_block(blt::size_t size)
{
auto blk = allocate_block(size);
if (head == nullptr)
{
head = blk;
return;
}
head->metadata.next = blk;
blk->metadata.prev = head;
head = blk;
}
static size_t to_nearest_page_size(blt::size_t bytes)
{
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<block*>(data);
}
private:
block* head = nullptr;
};
}
#endif //BLT_GP_STACK_H