diff --git a/furvm/include/furvm/stack.hpp b/furvm/include/furvm/stack.hpp new file mode 100644 index 0000000..772ac6d --- /dev/null +++ b/furvm/include/furvm/stack.hpp @@ -0,0 +1,56 @@ +#ifndef FURVM_STACK_HPP +#define FURVM_STACK_HPP + +#include +#include +#include + +namespace furvm { + +template +struct stack { + stack(std::size_t capacity = (1024ULL * 1024ULL) / sizeof(T)) + : begin(new T[capacity]()), cursor(begin), capacity(capacity) {} + + T* begin; + T* cursor; + std::size_t capacity; + std::stack frames; + + void push_frame() { frames.push(cursor); } + + void pop_frame() { + cursor = frames.top(); + frames.pop(); + } +}; + +template +class stack_allocator { +public: + stack_allocator() = default; + + stack_allocator(stack& stack) + : m_ref(&stack) {} + + template + constexpr stack_allocator(const stack_allocator& other) noexcept + : m_ref(other.m_ref) {} +public: + T* allocate(std::size_t n) { + if (m_ref == nullptr) throw std::bad_alloc(); + if (m_ref->capacity - (m_ref->cursor - m_ref->begin) < n) throw std::bad_alloc(); + + T* ptr = m_ref->cursor; + m_ref->cursor += n; + return ptr; + } + + void deallocate(T* ptr, std::size_t n) {} +private: + stack* m_ref = nullptr; +}; + +} // namespace furvm + +#endif // FURVM_STACK_HPP