203 lines
5.9 KiB
C++
203 lines
5.9 KiB
C++
#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/>.
|
|
*/
|
|
|
|
#include <cstddef>
|
|
#ifndef BLT_GP_PROGRAM_H
|
|
#define BLT_GP_PROGRAM_H
|
|
|
|
#include <blt/gp/fwdecl.h>
|
|
#include <functional>
|
|
#include <blt/std/ranges.h>
|
|
#include <blt/std/types.h>
|
|
#include <blt/std/utility.h>
|
|
#include <type_traits>
|
|
#include <string_view>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
namespace blt::gp
|
|
{
|
|
class identifier
|
|
{
|
|
};
|
|
|
|
class type
|
|
{
|
|
public:
|
|
template<typename T>
|
|
static type make_type(blt::size_t id)
|
|
{
|
|
return type(sizeof(T), id, blt::type_string<T>());
|
|
}
|
|
|
|
[[nodiscard]] blt::size_t size() const
|
|
{
|
|
return size_;
|
|
}
|
|
|
|
[[nodiscard]] blt::size_t id() const
|
|
{
|
|
return id_;
|
|
}
|
|
|
|
[[nodiscard]] std::string_view name() const
|
|
{
|
|
return name_;
|
|
}
|
|
|
|
private:
|
|
type(size_t size, size_t id, std::string_view name): size_(size), id_(id), name_(name)
|
|
{}
|
|
|
|
blt::size_t size_;
|
|
blt::size_t id_;
|
|
std::string name_;
|
|
};
|
|
|
|
class type_system
|
|
{
|
|
public:
|
|
type_system() = default;
|
|
|
|
template<typename T>
|
|
inline type register_type()
|
|
{
|
|
types.push_back(type::make_type<T>(types.size()));
|
|
return types.back();
|
|
}
|
|
|
|
private:
|
|
std::vector<type> types;
|
|
};
|
|
|
|
class stack_allocator
|
|
{
|
|
constexpr static blt::size_t PAGE_SIZE = 0x1000;
|
|
public:
|
|
|
|
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::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<blt::ptrdiff_t>(metadata.offset - buffer);
|
|
}
|
|
};
|
|
|
|
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;
|
|
};
|
|
|
|
template<typename Return, typename... Args>
|
|
class operation
|
|
{
|
|
public:
|
|
using function_t = std::function<Return(Args...)>;
|
|
|
|
operation(const operation& copy) = default;
|
|
|
|
operation(operation&& move) = default;
|
|
|
|
template<typename T, std::enable_if_t<std::is_same_v<T, function_t>, void>>
|
|
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))
|
|
{}
|
|
|
|
inline Return operator()(Args... args)
|
|
{
|
|
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;
|
|
};
|
|
|
|
|
|
}
|
|
|
|
#endif //BLT_GP_PROGRAM_H
|