Added more utility and fixed Flag type.
This commit is contained in:
parent
537229104d
commit
04520a5d35
@ -134,6 +134,9 @@ public:
|
|||||||
~FileStream() override;
|
~FileStream() override;
|
||||||
|
|
||||||
StreamError open(const char* path, FileOpenMode mode_);
|
StreamError open(const char* path, FileOpenMode mode_);
|
||||||
|
inline StreamError open(const std::string& path, FileOpenMode mode_) {
|
||||||
|
return open(path.c_str(), mode_);
|
||||||
|
}
|
||||||
void close();
|
void close();
|
||||||
[[nodiscard]] inline bool isOpen() const { return handle != nullptr; }
|
[[nodiscard]] inline bool isOpen() const { return handle != nullptr; }
|
||||||
|
|
||||||
|
62
source/mijin/memory/object_pool.hpp
Normal file
62
source/mijin/memory/object_pool.hpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED)
|
||||||
|
#define MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
#include "../debug/assert.hpp"
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
template<typename TObject, std::size_t OBJECTS_PER_PAGE = 1024>
|
||||||
|
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<std::byte, sizeof(TObject)> bytes;
|
||||||
|
};
|
||||||
|
struct Page
|
||||||
|
{
|
||||||
|
std::uint32_t freeOffset = 0;
|
||||||
|
std::array<ObjectData, OBJECTS_PER_PAGE> data;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<Page>> pages;
|
||||||
|
public:
|
||||||
|
[[nodiscard]] TObject* allocate(std::size_t count = 1) noexcept;
|
||||||
|
void free(TObject* object, std::size_t count = 1) noexcept;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename TObject, std::size_t OBJECTS_PER_PAGE>
|
||||||
|
TObject* ObjectPool<TObject, OBJECTS_PER_PAGE>::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>& page : pages)
|
||||||
|
{
|
||||||
|
std::uint32_t offset = page->freeOffset;
|
||||||
|
while (offset < OBJECTS_PER_PAGE)
|
||||||
|
{
|
||||||
|
GapInfo& gapInfo = *std::bit_cast<GapInfo*>(&page->data[offset]);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // namespace mijin
|
||||||
|
|
||||||
|
#endif // !defined(MIJIN_MEMORY_OBJECT_POOL_HPP_INCLUDED)
|
21
source/mijin/util/align.hpp
Normal file
21
source/mijin/util/align.hpp
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_UTIL_ALIGN_HPP_INCLUDED)
|
||||||
|
#define MIJIN_UTIL_ALIGN_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
template<typename T>
|
||||||
|
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)
|
@ -16,7 +16,7 @@ namespace mijin
|
|||||||
//
|
//
|
||||||
|
|
||||||
#define MIJIN_DEFINE_FLAG(name) \
|
#define MIJIN_DEFINE_FLAG(name) \
|
||||||
struct name : Flag \
|
struct name : mijin::Flag \
|
||||||
{ \
|
{ \
|
||||||
private: \
|
private: \
|
||||||
struct Proxy_ { \
|
struct Proxy_ { \
|
||||||
|
19
source/mijin/util/hash.hpp
Normal file
19
source/mijin/util/hash.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if !defined(MIJIN_UTIL_HASH_HPP_INCLUDED)
|
||||||
|
#define MIJIN_UTIL_HASH_HPP_INCLUDED 1
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
namespace mijin
|
||||||
|
{
|
||||||
|
template<typename T, typename THasher = std::hash<T>>
|
||||||
|
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
|
Loading…
x
Reference in New Issue
Block a user