memory bugfix
This commit is contained in:
parent
e7683d6e0e
commit
2e8213c1a1
@ -1,6 +0,0 @@
|
|||||||
metadatas:
|
|
||||||
- guid: a4064a70-89d5-4d8f-b3c3-6486dcf786c0
|
|
||||||
name: ""
|
|
||||||
t_hash: engineapi::ShaderProgram
|
|
||||||
meta: ~
|
|
||||||
includes: ~
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
metadatas:
|
|
||||||
- guid: 020486aa-8987-4bd1-9de9-eeb7ab82c437
|
|
||||||
name: ""
|
|
||||||
t_hash: engineapi::ShaderProgram
|
|
||||||
meta: ~
|
|
||||||
includes: ~
|
|
||||||
@ -203,7 +203,7 @@ namespace api {
|
|||||||
}
|
}
|
||||||
inline ResourceSystem::ResourceSystem()
|
inline ResourceSystem::ResourceSystem()
|
||||||
{
|
{
|
||||||
SINGLETON_VAL();
|
SINGLETON_PTR();
|
||||||
impl = new(GlobalPool()) ResourceSystemImpl(this);
|
impl = new(GlobalPool()) ResourceSystemImpl(this);
|
||||||
}
|
}
|
||||||
inline void api::ResourceSystem::Initialize()
|
inline void api::ResourceSystem::Initialize()
|
||||||
|
|||||||
@ -49,7 +49,7 @@ namespace api {
|
|||||||
class ResourceSystemImpl;
|
class ResourceSystemImpl;
|
||||||
class ASSET_API ResourceSystem : public ISystem
|
class ASSET_API ResourceSystem : public ISystem
|
||||||
{
|
{
|
||||||
SINGLETON_API_PTR(ResourceSystem)
|
SINGLETON_IMPL(ResourceSystem)
|
||||||
public:
|
public:
|
||||||
struct ResourceFileBlock;
|
struct ResourceFileBlock;
|
||||||
template<typename R>
|
template<typename R>
|
||||||
|
|||||||
23
engine/modules/engine/core/3rdparty/zlog.h
vendored
23
engine/modules/engine/core/3rdparty/zlog.h
vendored
@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "singleton.h"
|
||||||
#include "source_location.h"
|
#include "source_location.h"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -18,39 +19,39 @@ namespace zlog {
|
|||||||
m_logger->flush();
|
m_logger->flush();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
CORE_API zloger& get_zlog();
|
UNIQUER_INLINE(zloger, zlog, "zlog::zlog")
|
||||||
inline void flush() {
|
inline void flush() {
|
||||||
get_zlog().flush();
|
UNIQUER_VAL(zlog).flush();
|
||||||
}
|
}
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void info(format fmt, Args &&...args) {
|
void info(format fmt, Args &&...args) {
|
||||||
get_zlog().log(level_enum::info, fmt, std::forward<Args>(args)...);
|
UNIQUER_VAL(zlog).log(level_enum::info, fmt, std::forward<Args>(args)...);
|
||||||
};
|
};
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void debug(format fmt, Args &&...args) {
|
void debug(format fmt, Args &&...args) {
|
||||||
get_zlog().log(level_enum::debug, fmt, std::forward<Args>(args)...);
|
UNIQUER_VAL(zlog).log(level_enum::debug, fmt, std::forward<Args>(args)...);
|
||||||
};
|
};
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void warn(format fmt, Args &&...args) {
|
void warn(format fmt, Args &&...args) {
|
||||||
get_zlog().log(level_enum::warn, fmt, std::forward<Args>(args)...);
|
UNIQUER_VAL(zlog).log(level_enum::warn, fmt, std::forward<Args>(args)...);
|
||||||
};
|
};
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void error(format fmt, Args &&...args) {
|
void error(format fmt, Args &&...args) {
|
||||||
get_zlog().log(level_enum::err, fmt, std::forward<Args>(args)...);
|
UNIQUER_VAL(zlog).log(level_enum::err, fmt, std::forward<Args>(args)...);
|
||||||
};
|
};
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void errorf(format fmt, Args &&...args) {
|
void errorf(format fmt, Args &&...args) {
|
||||||
get_zlog().log(level_enum::err, fmt, std::forward<Args>(args)...);
|
UNIQUER_VAL(zlog).log(level_enum::err, fmt, std::forward<Args>(args)...);
|
||||||
get_zlog().flush();
|
UNIQUER_VAL(zlog).flush();
|
||||||
};
|
};
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void fatal(format fmt, Args &&...args) {
|
void fatal(format fmt, Args &&...args) {
|
||||||
get_zlog().log(level_enum::critical, fmt, std::forward<Args>(args)...);
|
UNIQUER_VAL(zlog).log(level_enum::critical, fmt, std::forward<Args>(args)...);
|
||||||
get_zlog().flush();
|
UNIQUER_VAL(zlog).flush();
|
||||||
throw std::runtime_error(std::string(fmt.value));
|
throw std::runtime_error(std::string(fmt.value));
|
||||||
};
|
};
|
||||||
inline void fatal(format fmt) {
|
inline void fatal(format fmt) {
|
||||||
get_zlog().log(level_enum::critical, fmt);
|
UNIQUER_VAL(zlog).log(level_enum::critical, fmt);
|
||||||
throw std::runtime_error(std::string(fmt.value));
|
throw std::runtime_error(std::string(fmt.value));
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@ -1,14 +1,6 @@
|
|||||||
#include "os/file_manager.h"
|
#include "os/file_manager.h"
|
||||||
#include "os/file_system.h"
|
#include "os/file_system.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "zlog.h"
|
|
||||||
namespace zlog {
|
|
||||||
zloger& get_zlog()
|
|
||||||
{
|
|
||||||
static zloger zlog;
|
|
||||||
return zlog;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
namespace api {
|
namespace api {
|
||||||
SINGLETON_DEFINE(FileManager)
|
SINGLETON_DEFINE(FileManager)
|
||||||
class FileManagerImpl
|
class FileManagerImpl
|
||||||
@ -114,7 +106,7 @@ namespace api {
|
|||||||
FileManager::FileManager()
|
FileManager::FileManager()
|
||||||
{
|
{
|
||||||
impl = new(GlobalPool()) FileManagerImpl();
|
impl = new(GlobalPool()) FileManagerImpl();
|
||||||
SINGLETON_VAL()
|
SINGLETON_PTR()
|
||||||
}
|
}
|
||||||
FileManager::~FileManager()
|
FileManager::~FileManager()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -5,7 +5,6 @@ namespace api {
|
|||||||
using refl::Any;
|
using refl::Any;
|
||||||
using refl::UClass;
|
using refl::UClass;
|
||||||
using refl::type_info;
|
using refl::type_info;
|
||||||
using pmr::string_hash;
|
|
||||||
struct JsonVTable {
|
struct JsonVTable {
|
||||||
using Read = bool(*)(yyjson_val*, const void*);
|
using Read = bool(*)(yyjson_val*, const void*);
|
||||||
using Write = yyjson_mut_val*(*)(yyjson_mut_doc*, const void*);
|
using Write = yyjson_mut_val*(*)(yyjson_mut_doc*, const void*);
|
||||||
|
|||||||
@ -16,7 +16,7 @@ namespace api {
|
|||||||
uint32_t flag{0};
|
uint32_t flag{0};
|
||||||
FILE_OP op{ FILE_OP::NONE};
|
FILE_OP op{ FILE_OP::NONE};
|
||||||
size_t size{0};
|
size_t size{0};
|
||||||
pmr::memory_resource* pool;
|
pmr::memory_resource* pool{nullptr};
|
||||||
std::variant<std::ifstream, std::ofstream, nullptr_t> vfile{nullptr};
|
std::variant<std::ifstream, std::ofstream, nullptr_t> vfile{nullptr};
|
||||||
public:
|
public:
|
||||||
using PackagePath::PackagePath;
|
using PackagePath::PackagePath;
|
||||||
|
|||||||
@ -6,7 +6,7 @@ namespace api
|
|||||||
class CORE_API FileManager
|
class CORE_API FileManager
|
||||||
{
|
{
|
||||||
FileManagerImpl* impl;
|
FileManagerImpl* impl;
|
||||||
SINGLETON_API_PTR(FileManager)
|
SINGLETON_IMPL(FileManager)
|
||||||
public:
|
public:
|
||||||
FileManager();
|
FileManager();
|
||||||
~FileManager();
|
~FileManager();
|
||||||
|
|||||||
@ -30,6 +30,7 @@ namespace zlog {
|
|||||||
zloger::~zloger() noexcept
|
zloger::~zloger() noexcept
|
||||||
{
|
{
|
||||||
m_logger->flush();
|
m_logger->flush();
|
||||||
spdlog::drop_all();
|
//spdlog::drop_all();
|
||||||
|
//spdlog::shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,6 +5,9 @@
|
|||||||
namespace api {
|
namespace api {
|
||||||
FileHandle& FileHandle::Open(FILE_OP _op, bool is_binarry)
|
FileHandle& FileHandle::Open(FILE_OP _op, bool is_binarry)
|
||||||
{
|
{
|
||||||
|
if (!pool) {
|
||||||
|
pool = FramePool();
|
||||||
|
}
|
||||||
op = _op;
|
op = _op;
|
||||||
pmr::string file_path = RealPath();
|
pmr::string file_path = RealPath();
|
||||||
switch (op)
|
switch (op)
|
||||||
|
|||||||
@ -3,7 +3,7 @@ static_component("core","engine")
|
|||||||
files = {"include/module/module.h"}
|
files = {"include/module/module.h"}
|
||||||
})
|
})
|
||||||
add_includedirs("3rdparty", {public = true})
|
add_includedirs("3rdparty", {public = true})
|
||||||
add_headerfiles("include/**.h","include/**.inl", "impl/*.inl")
|
add_headerfiles("include/**.h","include/**.inl", "impl/*.inl","3rdparty/**.h")
|
||||||
add_files("src/**.cpp")
|
add_files("src/**.cpp")
|
||||||
add_deps("zlib")
|
add_deps("zlib")
|
||||||
add_packages("spdlog", {public = true})
|
add_packages("spdlog", {public = true})
|
||||||
@ -18,7 +18,7 @@ namespace api {
|
|||||||
}
|
}
|
||||||
RenderAPI::RenderAPI(RenderContext* ctx) : context(*ctx)
|
RenderAPI::RenderAPI(RenderContext* ctx) : context(*ctx)
|
||||||
{
|
{
|
||||||
SINGLETON_VAL();
|
SINGLETON_PTR();
|
||||||
}
|
}
|
||||||
RenderAPI::~RenderAPI()
|
RenderAPI::~RenderAPI()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -3,7 +3,7 @@ namespace api {
|
|||||||
SINGLETON_DEFINE(Window)
|
SINGLETON_DEFINE(Window)
|
||||||
inline Window::Window(CreatePFN createPFN, const Args& args, int width, int height) noexcept : mHeight(height), mWidth(width)
|
inline Window::Window(CreatePFN createPFN, const Args& args, int width, int height) noexcept : mHeight(height), mWidth(width)
|
||||||
{
|
{
|
||||||
SINGLETON_VAL();
|
SINGLETON_PTR();
|
||||||
uint32_t windowFlags = args.windowFlags | SDL_WINDOW_SHOWN;
|
uint32_t windowFlags = args.windowFlags | SDL_WINDOW_SHOWN;
|
||||||
windowFlags |= args.resizeable ? SDL_WINDOW_RESIZABLE : 0;
|
windowFlags |= args.resizeable ? SDL_WINDOW_RESIZABLE : 0;
|
||||||
windowFlags |= args.headless ? SDL_WINDOW_HIDDEN : 0;
|
windowFlags |= args.headless ? SDL_WINDOW_HIDDEN : 0;
|
||||||
|
|||||||
@ -27,7 +27,7 @@ namespace api {
|
|||||||
void Execute(FRenderView& view);
|
void Execute(FRenderView& view);
|
||||||
void Clear();
|
void Clear();
|
||||||
TextureDesc Resource(Name name) {
|
TextureDesc Resource(Name name) {
|
||||||
constexpr size_t surface = pmr::string_hash("surface");
|
constexpr size_t surface = string_hash("surface");
|
||||||
if (name.Hash() == surface) {
|
if (name.Hash() == surface) {
|
||||||
return mSurface;
|
return mSurface;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@ namespace api {
|
|||||||
FrameGraph graph;
|
FrameGraph graph;
|
||||||
RenderAPI(RenderContext* ctx);
|
RenderAPI(RenderContext* ctx);
|
||||||
virtual ~RenderAPI();
|
virtual ~RenderAPI();
|
||||||
SINGLETON_API_PTR(RenderAPI)
|
SINGLETON_IMPL(RenderAPI)
|
||||||
public:
|
public:
|
||||||
void* operator new(size_t size) {
|
void* operator new(size_t size) {
|
||||||
return ::operator new(size, GlobalPool());
|
return ::operator new(size, GlobalPool());
|
||||||
|
|||||||
@ -14,7 +14,7 @@ namespace api {
|
|||||||
bool resizeable = true;
|
bool resizeable = true;
|
||||||
bool headless = false;
|
bool headless = false;
|
||||||
};
|
};
|
||||||
SINGLETON_API_PTR(Window)
|
SINGLETON_IMPL(Window)
|
||||||
public:
|
public:
|
||||||
void* operator new(size_t size) {
|
void* operator new(size_t size) {
|
||||||
return ::operator new(size, GlobalPool());
|
return ::operator new(size, GlobalPool());
|
||||||
|
|||||||
@ -22,7 +22,6 @@ namespace api
|
|||||||
}
|
}
|
||||||
return spirv_out;
|
return spirv_out;
|
||||||
}
|
}
|
||||||
using pmr::string_hash;
|
|
||||||
shaderc_shader_kind GlslToSpirv::GetShaderKind(Name name)
|
shaderc_shader_kind GlslToSpirv::GetShaderKind(Name name)
|
||||||
{
|
{
|
||||||
switch (name.Hash())
|
switch (name.Hash())
|
||||||
|
|||||||
@ -27,12 +27,14 @@ namespace singleapi {
|
|||||||
it->second.isAlive = true;
|
it->second.isAlive = true;
|
||||||
return MemoryInfo{ it->second.data, isAlive };
|
return MemoryInfo{ it->second.data, isAlive };
|
||||||
}
|
}
|
||||||
void* KillInstance(size_t hash) {
|
bool KillInstance(size_t hash) {
|
||||||
|
bool isAlive = false;
|
||||||
auto it = instances.find(hash);
|
auto it = instances.find(hash);
|
||||||
if (it != instances.end()) {
|
if (it != instances.end()) {
|
||||||
|
isAlive = it->second.isAlive;
|
||||||
it->second.isAlive = false;
|
it->second.isAlive = false;
|
||||||
}
|
}
|
||||||
return nullptr;
|
return isAlive;
|
||||||
}
|
}
|
||||||
GlobalManager(pmr::unsynchronized_pool_resource* pool) : instances{pool} {}
|
GlobalManager(pmr::unsynchronized_pool_resource* pool) : instances{pool} {}
|
||||||
~GlobalManager() {}
|
~GlobalManager() {}
|
||||||
@ -66,22 +68,39 @@ public:
|
|||||||
}
|
}
|
||||||
~UniquePtr() {
|
~UniquePtr() {
|
||||||
using namespace singleapi;
|
using namespace singleapi;
|
||||||
if (ms_Singleton) {
|
bool isAlive = false;
|
||||||
ms_Singleton->~T();
|
|
||||||
}
|
|
||||||
if constexpr (hash == 0) {
|
if constexpr (hash == 0) {
|
||||||
size_t tHash = typeid(T).hash_code();
|
size_t tHash = typeid(T).hash_code();
|
||||||
ms_Singleton = (T*)GlobalManager::Ptr()->KillInstance(tHash);
|
isAlive = GlobalManager::Ptr()->KillInstance(tHash);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
ms_Singleton = (T*)GlobalManager::Ptr()->KillInstance(hash);
|
isAlive = GlobalManager::Ptr()->KillInstance(hash);
|
||||||
|
}
|
||||||
|
if (ms_Singleton && isAlive) {
|
||||||
|
ms_Singleton->~T();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static T* Ptr(void) {
|
static T* Ptr(void) {
|
||||||
return ms_Singleton;
|
return ms_Singleton;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
#define SINGLETON_API_PTR(T) \
|
constexpr inline size_t string_hash(std::string_view str) noexcept
|
||||||
|
{
|
||||||
|
constexpr size_t fnv_offset_basis = 0xcbf29ce484222325;
|
||||||
|
constexpr size_t fnv_prime = 0x100000001b3;
|
||||||
|
|
||||||
|
auto hash = fnv_offset_basis;
|
||||||
|
for (auto& elem : str)
|
||||||
|
{
|
||||||
|
hash *= fnv_prime;
|
||||||
|
hash ^= elem;
|
||||||
|
}
|
||||||
|
hash *= fnv_prime;
|
||||||
|
hash ^= 0;
|
||||||
|
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
#define SINGLETON_IMPL(T) \
|
||||||
protected:\
|
protected:\
|
||||||
static T* ms_Singleton;\
|
static T* ms_Singleton;\
|
||||||
public:\
|
public:\
|
||||||
@ -89,4 +108,9 @@ public:\
|
|||||||
#define SINGLETON_DEFINE(T) T* T::ms_Singleton = nullptr;\
|
#define SINGLETON_DEFINE(T) T* T::ms_Singleton = nullptr;\
|
||||||
T* T::Ptr(){ return ms_Singleton; }
|
T* T::Ptr(){ return ms_Singleton; }
|
||||||
|
|
||||||
#define SINGLETON_VAL() ms_Singleton = this;
|
#define SINGLETON_PTR() ms_Singleton = this;
|
||||||
|
|
||||||
|
#define UNIQUER_INLINE(cls, name, hash) inline UniquePtr<cls, string_hash(hash)> name##Ptr;
|
||||||
|
#define UNIQUER_STATIC(cls, name, hash) static UniquePtr<cls, string_hash(hash)> name##Ptr;
|
||||||
|
#define UNIQUER_VAL(name) (*name##Ptr.Ptr())
|
||||||
|
#define UNIQUER_PTR(name) name##Ptr.Ptr()
|
||||||
@ -5,7 +5,7 @@ namespace pmr {
|
|||||||
using std::pmr::unsynchronized_pool_resource;
|
using std::pmr::unsynchronized_pool_resource;
|
||||||
using std::pmr::memory_resource;
|
using std::pmr::memory_resource;
|
||||||
using std::pmr::vector;
|
using std::pmr::vector;
|
||||||
class FrameAllocator : public memory_resource {
|
class ZLIB_API FrameAllocator : public memory_resource {
|
||||||
private:
|
private:
|
||||||
char* buffer;
|
char* buffer;
|
||||||
size_t capacity;
|
size_t capacity;
|
||||||
@ -21,10 +21,10 @@ namespace pmr {
|
|||||||
~FrameAllocator() noexcept;
|
~FrameAllocator() noexcept;
|
||||||
bool empty() const { return offset == 0; }
|
bool empty() const { return offset == 0; }
|
||||||
void reset() { offset = 0; };
|
void reset() { offset = 0; };
|
||||||
void* try_allocate(size_t bytes, size_t alignment);
|
friend void* try_allocate(FrameAllocator& allocator, size_t bytes, size_t alignment);
|
||||||
protected:
|
private:
|
||||||
|
void* do_allocate(size_t bytes, size_t alignment)override;
|
||||||
void move_clear() { buffer = nullptr; capacity = 0; offset = 0; };
|
void move_clear() { buffer = nullptr; capacity = 0; offset = 0; };
|
||||||
void* do_allocate(size_t bytes, size_t alignment) override;
|
|
||||||
void do_deallocate(void* p, size_t bytes, size_t alignment) override {};
|
void do_deallocate(void* p, size_t bytes, size_t alignment) override {};
|
||||||
bool do_is_equal(const memory_resource& other) const noexcept override { return this == &other; };
|
bool do_is_equal(const memory_resource& other) const noexcept override { return this == &other; };
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,31 +1,12 @@
|
|||||||
namespace pmr {
|
namespace pmr {
|
||||||
inline FrameAllocator& FrameAllocator::operator=(FrameAllocator&& o)noexcept
|
inline void* try_allocate(FrameAllocator& allac, size_t bytes, size_t alignment)
|
||||||
{
|
{
|
||||||
std::construct_at(this, std::forward<FrameAllocator&&>(o));
|
if (allac.capacity - allac.offset > bytes) {
|
||||||
return *this;
|
return allac.do_allocate(bytes, alignment);
|
||||||
}
|
|
||||||
inline FrameAllocator::FrameAllocator(FrameAllocator&& o) noexcept :buffer(o.buffer),capacity(o.capacity),offset(o.offset)
|
|
||||||
{
|
|
||||||
o.move_clear();
|
|
||||||
}
|
|
||||||
inline FrameAllocator::FrameAllocator(size_t size)noexcept
|
|
||||||
{
|
|
||||||
buffer = new char[size];
|
|
||||||
capacity = size;
|
|
||||||
offset = 0;
|
|
||||||
}
|
|
||||||
inline FrameAllocator::~FrameAllocator()noexcept
|
|
||||||
{
|
|
||||||
if(buffer)
|
|
||||||
delete[] buffer;
|
|
||||||
}
|
|
||||||
inline void* FrameAllocator::try_allocate(size_t bytes, size_t alignment)
|
|
||||||
{
|
|
||||||
if (capacity - offset > bytes) {
|
|
||||||
return do_allocate(bytes, alignment);
|
|
||||||
}
|
}
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void* FrameAllocator::do_allocate(size_t bytes, size_t alignment)
|
inline void* FrameAllocator::do_allocate(size_t bytes, size_t alignment)
|
||||||
{
|
{
|
||||||
size_t space = capacity - offset;
|
size_t space = capacity - offset;
|
||||||
@ -39,7 +20,7 @@ namespace pmr {
|
|||||||
inline void* FrameAllocatorPool::allocate(size_t bytes, size_t alignment)
|
inline void* FrameAllocatorPool::allocate(size_t bytes, size_t alignment)
|
||||||
{
|
{
|
||||||
for (auto& alllocator : allocators) {
|
for (auto& alllocator : allocators) {
|
||||||
if (auto ptr = alllocator.try_allocate(bytes, alignment)) {
|
if (auto ptr = try_allocate(alllocator, bytes, alignment)) {
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -59,4 +40,26 @@ namespace pmr {
|
|||||||
count = count > 0 ? count : 1;
|
count = count > 0 ? count : 1;
|
||||||
allocators.erase(allocators.begin() + count, allocators.end());
|
allocators.erase(allocators.begin() + count, allocators.end());
|
||||||
}
|
}
|
||||||
|
#ifdef ZLIB_API_VAL //内存分配和释放必须位于同一模块(DLL)
|
||||||
|
inline FrameAllocator& FrameAllocator::operator=(FrameAllocator&& o)noexcept
|
||||||
|
{
|
||||||
|
std::construct_at(this, std::forward<FrameAllocator&&>(o));
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
inline FrameAllocator::FrameAllocator(FrameAllocator&& o) noexcept :buffer(o.buffer), capacity(o.capacity), offset(o.offset)
|
||||||
|
{
|
||||||
|
o.move_clear();
|
||||||
|
}
|
||||||
|
inline FrameAllocator::FrameAllocator(size_t size)noexcept
|
||||||
|
{
|
||||||
|
buffer = new char[size];
|
||||||
|
capacity = size;
|
||||||
|
offset = 0;
|
||||||
|
}
|
||||||
|
inline FrameAllocator::~FrameAllocator()noexcept
|
||||||
|
{
|
||||||
|
if (buffer)
|
||||||
|
delete[] buffer;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
@ -1,59 +1,68 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "singleton.h"
|
#include "singleton.h"
|
||||||
namespace pmr {
|
|
||||||
constexpr inline size_t string_hash(std::string_view str) noexcept
|
|
||||||
{
|
|
||||||
constexpr size_t fnv_offset_basis = 0xcbf29ce484222325;
|
|
||||||
constexpr size_t fnv_prime = 0x100000001b3;
|
|
||||||
|
|
||||||
auto hash = fnv_offset_basis;
|
|
||||||
for (auto& elem : str)
|
|
||||||
{
|
|
||||||
hash *= fnv_prime;
|
|
||||||
hash ^= elem;
|
|
||||||
}
|
|
||||||
hash *= fnv_prime;
|
|
||||||
hash ^= 0;
|
|
||||||
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//全局生命周期,不回收内存
|
//全局生命周期,不回收内存
|
||||||
inline UniquePtr<pmr::FrameAllocatorPool, pmr::string_hash("pmr::globalpool")> GlobalPoolPtr;
|
|
||||||
//局部生命周期,每帧回收内存
|
//局部生命周期,每帧回收内存
|
||||||
inline UniquePtr<pmr::FrameAllocatorPool, pmr::string_hash("pmr::framepool")> FramePoolPtr;
|
ZLIB_API extern pmr::FrameAllocatorPool* GlobalPool();
|
||||||
inline pmr::FrameAllocatorPool* GlobalPool() {
|
ZLIB_API extern pmr::FrameAllocatorPool* FramePool();
|
||||||
return GlobalPoolPtr.Ptr();
|
|
||||||
}
|
|
||||||
inline pmr::FrameAllocatorPool* FramePool() {
|
|
||||||
return FramePoolPtr.Ptr();
|
|
||||||
}
|
|
||||||
extern void* operator new(std::size_t size);
|
extern void* operator new(std::size_t size);
|
||||||
extern void operator delete(void* ptr) noexcept;
|
extern void operator delete(void* ptr) noexcept;
|
||||||
|
extern void operator delete[](void* ptr) noexcept;
|
||||||
extern void* operator new(std::size_t size, std::align_val_t align);
|
extern void* operator new(std::size_t size, std::align_val_t align);
|
||||||
#ifdef ZLIB_API_VAL
|
#ifdef ZLIB_API_VAL
|
||||||
|
ZLIB_API inline pmr::FrameAllocatorPool* GlobalPool() {
|
||||||
|
static pmr::FrameAllocatorPool* globalPool = new pmr::FrameAllocatorPool();
|
||||||
|
return globalPool;
|
||||||
|
}
|
||||||
|
ZLIB_API inline pmr::FrameAllocatorPool* FramePool() {
|
||||||
|
static pmr::FrameAllocatorPool* framePool = new pmr::FrameAllocatorPool();
|
||||||
|
return framePool;
|
||||||
|
}
|
||||||
struct MemDetail {
|
struct MemDetail {
|
||||||
int count{ 0 };
|
int count{ 0 };
|
||||||
int new_count{ 0 };
|
int new_count{ 0 };
|
||||||
int del_count{ 0 };
|
int del_count{ 0 };
|
||||||
};
|
};
|
||||||
thread_local pmr::unsynchronized_pool_resource MemPool;
|
struct MemPoolWrap {
|
||||||
|
char pMemory[sizeof(pmr::unsynchronized_pool_resource)];
|
||||||
|
uint32_t id;
|
||||||
|
MemPoolWrap() {
|
||||||
|
static uint32_t sThreadId = 0;
|
||||||
|
id = sThreadId++;
|
||||||
|
std::construct_at((pmr::unsynchronized_pool_resource*)&pMemory);
|
||||||
|
}
|
||||||
|
~MemPoolWrap() {
|
||||||
|
auto pool = (pmr::unsynchronized_pool_resource*)&pMemory;
|
||||||
|
if (id) {
|
||||||
|
pool->~unsynchronized_pool_resource();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pmr::unsynchronized_pool_resource* operator->() {
|
||||||
|
return (pmr::unsynchronized_pool_resource*)&pMemory;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
thread_local MemPoolWrap MemPool;
|
||||||
thread_local MemDetail detail;
|
thread_local MemDetail detail;
|
||||||
void* operator new(std::size_t size) {
|
void* operator new(std::size_t size) {
|
||||||
std::size_t alignment = alignof(std::max_align_t);
|
std::size_t alignment = alignof(std::max_align_t);
|
||||||
detail.count++;
|
detail.count++;
|
||||||
detail.new_count++;
|
detail.new_count++;
|
||||||
return MemPool.allocate(size, alignment);
|
return MemPool->allocate(size, alignment);
|
||||||
}
|
}
|
||||||
// 默认对齐方式 delete
|
// 默认对齐方式 delete
|
||||||
void operator delete(void* ptr) noexcept {
|
void operator delete(void* ptr) noexcept {
|
||||||
detail.count--;
|
detail.count--;
|
||||||
detail.del_count++;
|
detail.del_count++;
|
||||||
MemPool.deallocate(ptr, 0);
|
MemPool->deallocate(ptr, 0);
|
||||||
|
}
|
||||||
|
// 默认对齐方式 delete
|
||||||
|
void operator delete[](void* ptr) noexcept {
|
||||||
|
detail.count--;
|
||||||
|
detail.del_count++;
|
||||||
|
MemPool->deallocate(ptr, 0);
|
||||||
}
|
}
|
||||||
// 自定义对齐 align
|
// 自定义对齐 align
|
||||||
void* operator new(std::size_t size, std::align_val_t align) {
|
void* operator new(std::size_t size, std::align_val_t align) {
|
||||||
std::size_t alignment = static_cast<std::size_t>(align);
|
std::size_t alignment = static_cast<std::size_t>(align);
|
||||||
return MemPool.allocate(size, alignment);
|
return MemPool->allocate(size, alignment);
|
||||||
}
|
}
|
||||||
#endif //
|
#endif //
|
||||||
@ -1,8 +1,8 @@
|
|||||||
namespace pmr {
|
namespace pmr {
|
||||||
using NameTable_t = table<size_t,const string>;
|
using NameTable_t = table<size_t,const string>;
|
||||||
inline NameTable_t& TableRef() {
|
inline NameTable_t& TableRef() {
|
||||||
static UniquePtr<NameTable_t, string_hash("pmr::nametable")> Table;
|
UNIQUER_STATIC(NameTable_t, Table, "pmr::nametable")
|
||||||
return *Table.Ptr();
|
return UNIQUER_VAL(Table);
|
||||||
}
|
}
|
||||||
struct NameTable {
|
struct NameTable {
|
||||||
static const string& Find(size_t id);
|
static const string& Find(size_t id);
|
||||||
|
|||||||
@ -6,7 +6,6 @@ namespace refl {
|
|||||||
using pmr::Name;
|
using pmr::Name;
|
||||||
using pmr::CName;
|
using pmr::CName;
|
||||||
using pmr::FName;
|
using pmr::FName;
|
||||||
using pmr::string_hash;
|
|
||||||
using pmr::table;
|
using pmr::table;
|
||||||
using std::span;
|
using std::span;
|
||||||
using Offset = uint32_t;
|
using Offset = uint32_t;
|
||||||
|
|||||||
@ -4,7 +4,6 @@ namespace gen {
|
|||||||
using refl::real_type_t;
|
using refl::real_type_t;
|
||||||
using pmr::CName;
|
using pmr::CName;
|
||||||
using pmr::FName;
|
using pmr::FName;
|
||||||
using pmr::string_hash;
|
|
||||||
template<typename T, size_t hash>
|
template<typename T, size_t hash>
|
||||||
struct MetaImpl {};
|
struct MetaImpl {};
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|||||||
@ -40,9 +40,10 @@ namespace refl {
|
|||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
inline UniquePtr<table<Name, const UClass*>, string_hash("refl::ClassTable")> ClassTablePtr;
|
using __tClassTable = table<Name, const UClass*>;
|
||||||
|
UNIQUER_INLINE(__tClassTable, ClassTable, "refl::ClassTable")
|
||||||
inline const UClass* find_info(Name name) {
|
inline const UClass* find_info(Name name) {
|
||||||
auto& ClassTable = *ClassTablePtr.Ptr();
|
auto& ClassTable = UNIQUER_VAL(ClassTable);
|
||||||
if (auto it = ClassTable.find(name); it != ClassTable.end()) {
|
if (auto it = ClassTable.find(name); it != ClassTable.end()) {
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
@ -66,7 +67,7 @@ namespace refl {
|
|||||||
else {
|
else {
|
||||||
cls = new(GlobalPool()) UClass_Auto<T>{};
|
cls = new(GlobalPool()) UClass_Auto<T>{};
|
||||||
}
|
}
|
||||||
auto& ClassTable = *ClassTablePtr.Ptr();
|
auto& ClassTable = UNIQUER_VAL(ClassTable);
|
||||||
ClassTable[name.View()] = cls;
|
ClassTable[name.View()] = cls;
|
||||||
return cls;
|
return cls;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -23,6 +23,9 @@
|
|||||||
#define CONCATENATE_IMPL(arg1, arg2) arg1##arg2
|
#define CONCATENATE_IMPL(arg1, arg2) arg1##arg2
|
||||||
#define MY_UNIQUE_NAME(base) CONCATENATE(base, __LINE__)
|
#define MY_UNIQUE_NAME(base) CONCATENATE(base, __LINE__)
|
||||||
|
|
||||||
|
#define TOSTRING_IMPL(X) #X
|
||||||
|
#define TOSTRING(X) TOSTRING_IMPL(X)
|
||||||
|
|
||||||
#define USING_CTOR_NAME MY_UNIQUE_NAME(__Ctor)
|
#define USING_CTOR_NAME MY_UNIQUE_NAME(__Ctor)
|
||||||
#define USING_FUNC_NAME MY_UNIQUE_NAME(__Func)
|
#define USING_FUNC_NAME MY_UNIQUE_NAME(__Func)
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ function static_component(name, owner, opt)
|
|||||||
end
|
end
|
||||||
function shared_module(name, owner, opt)
|
function shared_module(name, owner, opt)
|
||||||
target(name)
|
target(name)
|
||||||
|
add_defines(string.upper(name).."_ROOT="..os.curdir():gsub("\\", "\\\\"),{public = false})
|
||||||
set_kind("shared")
|
set_kind("shared")
|
||||||
set_group("Engine/"..owner.."__dyn")
|
set_group("Engine/"..owner.."__dyn")
|
||||||
add_rules("engine.api")
|
add_rules("engine.api")
|
||||||
@ -44,6 +45,7 @@ function add_dependency(...)
|
|||||||
end
|
end
|
||||||
function game_instance(name, opt)
|
function game_instance(name, opt)
|
||||||
target(name)
|
target(name)
|
||||||
|
set_rundir(".")
|
||||||
set_kind("binary")
|
set_kind("binary")
|
||||||
set_group("Games")
|
set_group("Games")
|
||||||
add_rules("engine.api")
|
add_rules("engine.api")
|
||||||
|
|||||||
@ -1,9 +1,15 @@
|
|||||||
#include "api.h"
|
#include "api.h"
|
||||||
|
#include "os/file_manager.h"
|
||||||
class ENGINE_API EngineModule : public api::IDynamicModule
|
class ENGINE_API EngineModule : public api::IDynamicModule
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void OnLoad(int argc, char** argv) override {};
|
void OnLoad(int argc, char** argv) override {
|
||||||
void OnUnload() override {};
|
#ifdef ENGINE_ROOT
|
||||||
|
api::FileManager::Ptr()->Mount("engine", TOSTRING(ENGINE_ROOT));
|
||||||
|
#endif
|
||||||
|
};
|
||||||
|
void OnUnload() override {
|
||||||
|
};
|
||||||
void InitMetaData(void) override {
|
void InitMetaData(void) override {
|
||||||
mInfo.dependencies = {
|
mInfo.dependencies = {
|
||||||
{"core", "1.0.1", "static" },
|
{"core", "1.0.1", "static" },
|
||||||
|
|||||||
@ -6,6 +6,7 @@ target("editor")
|
|||||||
add_includedirs("include/editor")
|
add_includedirs("include/editor")
|
||||||
add_files("src/editor/*.cpp")
|
add_files("src/editor/*.cpp")
|
||||||
target("engine")
|
target("engine")
|
||||||
|
add_defines("ENGINE_ROOT="..os.curdir():gsub("\\", "\\\\"),{public = false})
|
||||||
add_includedirs("include", {public = true})
|
add_includedirs("include", {public = true})
|
||||||
set_kind("shared")
|
set_kind("shared")
|
||||||
set_group("Engine")
|
set_group("Engine")
|
||||||
|
|||||||
@ -10,6 +10,7 @@ int main(int argc,char* argv[]) {
|
|||||||
auto ptr = api::ModuleManager::Ptr();
|
auto ptr = api::ModuleManager::Ptr();
|
||||||
ptr->MakeGraph("zworld", true, argc, argv);
|
ptr->MakeGraph("zworld", true, argc, argv);
|
||||||
ptr->MainLoop();
|
ptr->MainLoop();
|
||||||
|
ptr->DestroyGraph();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -1,6 +1,5 @@
|
|||||||
game_instance("zworld", "src/zworld.h")
|
game_instance("zworld", "src/zworld.h")
|
||||||
target("zworld")
|
target("zworld")
|
||||||
set_rundir(".")
|
|
||||||
add_files("src/*.cpp")
|
add_files("src/*.cpp")
|
||||||
add_headerfiles("src/*.h")
|
add_headerfiles("src/*.h")
|
||||||
add_dependency("engine", "editor", "vulkan", {public = true})
|
add_dependency("engine", "editor", "vulkan", {public = true})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user