diff --git a/source/mijin/io/stream.hpp b/source/mijin/io/stream.hpp index 64180b3..f8364e8 100644 --- a/source/mijin/io/stream.hpp +++ b/source/mijin/io/stream.hpp @@ -134,6 +134,9 @@ public: ~FileStream() override; StreamError open(const char* path, FileOpenMode mode_); + inline StreamError open(const std::string& path, FileOpenMode mode_) { + return open(path.c_str(), mode_); + } void close(); [[nodiscard]] inline bool isOpen() const { return handle != nullptr; } diff --git a/source/mijin/memory/object_pool.hpp b/source/mijin/memory/object_pool.hpp new file mode 100644 index 0000000..397172e --- /dev/null +++ b/source/mijin/memory/object_pool.hpp @@ -0,0 +1,62 @@ + +#pragma once + +#if !defined(MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED) +#define MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED 1 + +#include +#include +#include +#include +#include +#include +#include "../debug/assert.hpp" + +namespace mijin +{ +template +class ObjectPool +{ +private: + struct GapInfo + { + std::uint32_t objectCount; + std::uint32_t nextGap; + }; + static constexpr std::size_t ALIGN = std::max(alignof(TObject), alignof(GapInfo)); + + struct alignas(ALIGN) ObjectData + { + std::array bytes; + }; + struct Page + { + std::uint32_t freeOffset = 0; + std::array data; + }; + + std::vector> pages; +public: + [[nodiscard]] TObject* allocate(std::size_t count = 1) noexcept; + void free(TObject* object, std::size_t count = 1) noexcept; +}; + +template +TObject* ObjectPool::allocate(std::size_t count) noexcept +{ + MIJIN_ASSERT(count <= OBJECTS_PER_PAGE, "Cannot allocate more than OBJECTS_PER_PAGE elements at once!"); + // first try to find a free spot in the existing pages + + for (std::unique_ptr& page : pages) + { + std::uint32_t offset = page->freeOffset; + while (offset < OBJECTS_PER_PAGE) + { + GapInfo& gapInfo = *std::bit_cast(&page->data[offset]); + + } + } +} +} // namespace mijin + +#endif // !defined(MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED) diff --git a/source/mijin/util/align.hpp b/source/mijin/util/align.hpp new file mode 100644 index 0000000..cb8f537 --- /dev/null +++ b/source/mijin/util/align.hpp @@ -0,0 +1,21 @@ + +#pragma once + +#if !defined(MIJIN_UTIL_ALIGN_HPP_INCLUDED) +#define MIJIN_UTIL_ALIGN_HPP_INCLUDED 1 + +namespace mijin +{ +template +constexpr T alignUp(T value, T alignTo) noexcept +{ + if (value % alignTo != 0) + { + return value + alignTo - (value % alignTo); + } + return value; +} + +} // namespace mijin + +#endif // !defined(MIJIN_UTIL_ALIGN_HPP_INCLUDED) \ No newline at end of file diff --git a/source/mijin/util/flag.hpp b/source/mijin/util/flag.hpp index 0a4609f..8405d13 100644 --- a/source/mijin/util/flag.hpp +++ b/source/mijin/util/flag.hpp @@ -16,7 +16,7 @@ namespace mijin // #define MIJIN_DEFINE_FLAG(name) \ -struct name : Flag \ +struct name : mijin::Flag \ { \ private: \ struct Proxy_ { \ diff --git a/source/mijin/util/hash.hpp b/source/mijin/util/hash.hpp new file mode 100644 index 0000000..9ebe40b --- /dev/null +++ b/source/mijin/util/hash.hpp @@ -0,0 +1,19 @@ + + +#pragma once + +#if !defined(MIJIN_UTIL_HASH_HPP_INCLUDED) +#define MIJIN_UTIL_HASH_HPP_INCLUDED 1 + +#include + +namespace mijin +{ +template> +inline void hashCombine(std::size_t& seed, const T& value, const THasher& hasher = {}) +{ + seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2); +} +} + +#endif // MIJIN_UTIL_HASH_HPP_INCLUDED