Skip to content

Commit

Permalink
Removed Tracked Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
mlomb committed Jun 16, 2018
1 parent b92d41b commit 7fad3b4
Show file tree
Hide file tree
Showing 19 changed files with 241 additions and 195 deletions.
8 changes: 4 additions & 4 deletions CMake/Modules/FindMono.cmake
Expand Up @@ -18,9 +18,9 @@ else()
set(MONO_N "monosgen-2.0")
endif()

set(MONO_LIBRARIES "${MONO_N}.lib")
find_library(MONO_LIBRARIES ${MONO_N} PATHS ${MONO_LIBRARY_DIR})

message("Mono Path: ${MONO_PATH}")
# message(${MONO_INCLUDE_DIR})
# message(${MONO_LIBRARY_DIR})
# message(${MONO_LIBRARIES})
#message(${MONO_INCLUDE_DIR})
#message(${MONO_LIBRARY_DIR})
#message(${MONO_LIBRARIES})
2 changes: 1 addition & 1 deletion CMakeLists.txt
Expand Up @@ -165,7 +165,7 @@ list(APPEND OE_LIBRARIES
FreeImage
xsc_core
assimp
${MONO_LIBRARY_DIR}/${MONO_LIBRARIES}
${MONO_LIBRARIES}
)
list(APPEND OE_LIBRARIES_INC
${freetype_SOURCE_DIR}/include
Expand Down
4 changes: 4 additions & 0 deletions Code/include/OE/Engine/Component.hpp
Expand Up @@ -3,11 +3,15 @@

#include "OE/Config.hpp"

#include "OE/Meta/NativeReflection.hpp"

namespace OrbitEngine { namespace Engine {
class Transform;
class SceneObject;

class Component {
NATIVE_REFLECTION

public:
virtual ~Component();

Expand Down
19 changes: 9 additions & 10 deletions Code/include/OE/Engine/MemoryDomain.hpp
Expand Up @@ -5,7 +5,7 @@
#include <math.h>

#include "OE/Meta/NativeType.hpp"
#include "OE/Memory/Allocator.hpp"
#include "OE/Memory/MemoryPool.hpp"

namespace std {
template <>
Expand All @@ -26,11 +26,6 @@ namespace OrbitEngine {
class WeakPtr;
template<typename T>
class StrongPtr;

namespace Memory {
template<typename T>
class TrackedMemoryPool;
}
}

namespace OrbitEngine { namespace Engine {
Expand All @@ -43,19 +38,23 @@ namespace OrbitEngine { namespace Engine {
static MemoryDomain* Get();

template<typename T>
StrongPtr<T> Allocate();
StrongPtr<T> Create();
template<typename T>
void Deallocate(Ptr<T>& object);
void Destroy(Ptr<T>& object);
template<typename T>
const std::vector<WeakPtr<T>>& GetAll();

private:
std::unordered_map<Meta::NativeType*, Memory::Allocator*> m_Allocators;
std::unordered_map<Meta::NativeType*, void* /* std::vector<WeakPtr<T>> */> m_Objects;

template<typename T>
std::vector<WeakPtr<T>>& GetObjects();

template<typename T>
Memory::TrackedMemoryPool<T>* GetAllocator();
Memory::MemoryPool* GetAllocator();
template<typename T>
Memory::TrackedMemoryPool<T>* CreateAllocator();
Memory::MemoryPool* CreateAllocator();
};
} }

Expand Down
79 changes: 58 additions & 21 deletions Code/include/OE/Engine/MemoryDomain.inl
Expand Up @@ -6,61 +6,98 @@
#include "OE/Memory/Ptrs.hpp"
#include "OE/Memory/MemoryPool.hpp"

#include "OE/Misc/Log.hpp"

namespace OrbitEngine { namespace Engine {

template<typename T>
inline StrongPtr<T> MemoryDomain::Allocate()
inline StrongPtr<T> MemoryDomain::Create()
{
Meta::NativeType* type = Meta::NativeTypeResolver<T>::Get();
Memory::TrackedMemoryPool<T>* allocator = GetAllocator<T>();
OE_ASSERT(type);

Memory::MemoryPool* allocator = GetAllocator<T>();
if (!allocator)
allocator = CreateAllocator<T>();

StrongPtr<T> object = allocator->AllocatePtr();
if (object)
type->Construct(object.Get());
return object;
void* obj = allocator->Allocate();
if (obj == 0)
return StrongPtr<T>();
type->Construct(obj);

std::vector<WeakPtr<T>>& objs = GetObjects<T>();

StrongPtr<T> ptr = StrongPtr<T>(static_cast<T*>(obj));

/* -- */
auto it = std::lower_bound(objs.begin(), objs.end(), WeakPtr<T>(ptr), [](const WeakPtr<T>& lhs, const WeakPtr<T>& rhs) {
return lhs.Get() < rhs.Get();
});
objs.insert(it, ptr);
/* -- */

return ptr;
}

template<typename T>
inline void MemoryDomain::Deallocate(Ptr<T>& object)
inline void MemoryDomain::Destroy(Ptr<T>& object)
{
Meta::NativeType* type = Meta::NativeTypeResolver<T>::Get();
Memory::TrackedMemoryPool<T>* allocator = GetAllocator<T>();
OE_ASSERT(allocator)
OE_ASSERT(type);
Memory::MemoryPool* allocator = GetAllocator<T>();
OE_ASSERT(allocator);

type->Destruct(object.Get());
allocator->DeallocatePtr(object);
allocator->Deallocate(object.Get());

std::vector<WeakPtr<T>>& objs = GetObjects<T>();

/* -- */
auto it = std::lower_bound(objs.begin(), objs.end(), WeakPtr<T>(object), [](const WeakPtr<T>& lhs, const WeakPtr<T>& rhs) {
return lhs.Get() < rhs.Get();
});
objs.erase(it);
/* -- */
}

template<typename T>
inline const std::vector<WeakPtr<T>>& MemoryDomain::GetAll()
{
return GetAllocator<T>()->GetAll();
return GetObjects<T>();
}

template<typename T>
inline Memory::TrackedMemoryPool<T>* MemoryDomain::GetAllocator()
inline Memory::MemoryPool* MemoryDomain::GetAllocator()
{
Meta::NativeType* type = Meta::NativeTypeResolver<T>::Get();
OE_ASSERT(type)
auto it = m_Allocators.find(type);
auto it = m_Allocators.find(Meta::NativeTypeResolver<T>::Get());
if (it != m_Allocators.end())
return static_cast<Memory::TrackedMemoryPool<T>*>((*it).second);
return static_cast<Memory::MemoryPool*>((*it).second);
return nullptr;
}

template<typename T>
inline Memory::TrackedMemoryPool<T>* MemoryDomain::CreateAllocator()
inline Memory::MemoryPool* MemoryDomain::CreateAllocator()
{
Meta::NativeType* type = Meta::NativeTypeResolver<T>::Get();
OE_ASSERT(type)
Memory::TrackedMemoryPool<T>* allocator = new Memory::TrackedMemoryPool<T>(100);
const int capacity = 100;

m_Allocators.insert(std::make_pair(type, allocator));
Memory::MemoryPool* allocator = new Memory::MemoryPool(sizeof(T), capacity);

m_Allocators.insert(std::make_pair(Meta::NativeTypeResolver<T>::Get(), allocator));
GetObjects<T>().reserve(capacity);

return allocator;
}

template<typename T>
inline std::vector<WeakPtr<T>>& MemoryDomain::GetObjects()
{
Meta::NativeType* type = Meta::NativeTypeResolver<T>::Get();
OE_ASSERT(type);
auto it = m_Objects.find(type);
if (it == m_Objects.end())
m_Objects[type] = new std::vector<WeakPtr<T>>();
return *static_cast<std::vector<WeakPtr<T>>*>(m_Objects[type]);
}
} }

#endif
13 changes: 13 additions & 0 deletions Code/include/OE/Engine/SceneObject.hpp
Expand Up @@ -10,6 +10,7 @@

#include "OE/Config.hpp"
#include "OE/Misc/Log.hpp"
#include "OE/Memory/Ptrs.hpp"
#include "OE/Engine/Object.hpp"
#include "OE/Engine/Scene.hpp"
#include "OE/Engine/Component.hpp"
Expand All @@ -33,6 +34,9 @@ namespace OrbitEngine { namespace Engine {
void SetName(const std::string& name);
void SetParent(WeakPtr<SceneObject> parent, int position = 99999999);

template<typename T>
WeakPtr<T> AddComponent();

private:
friend class Scene;

Expand All @@ -42,7 +46,16 @@ namespace OrbitEngine { namespace Engine {

std::string m_Name;
std::vector<StrongPtr<SceneObject>> m_Childs;
std::vector<StrongPtr<Component>> m_Components;
};

template<typename T>
inline WeakPtr<T> SceneObject::AddComponent()
{
StrongPtr<T> ptr = MemoryDomain::Get()->Create<T>();
m_Components.push_back(ptr.template AsStrong<Component>());
return ptr;
}
} }

#endif
4 changes: 4 additions & 0 deletions Code/include/OE/Engine/TestComponent.hpp
Expand Up @@ -8,11 +8,15 @@
#include "OE/Math/Vec3.hpp"
#include "OE/Math/Vec4.hpp"

#include "OE/Meta/NativeReflection.hpp"

#include "Transform.hpp"

namespace OrbitEngine { namespace Engine {

class TestComponent : public Component {
NATIVE_REFLECTION

public:
TestComponent();
~TestComponent();
Expand Down
2 changes: 2 additions & 0 deletions Code/include/OE/Engine/Transform.hpp
Expand Up @@ -10,6 +10,8 @@
namespace OrbitEngine { namespace Engine {

class Transform : public Component {
NATIVE_REFLECTION

public:
Transform();
~Transform();
Expand Down
18 changes: 0 additions & 18 deletions Code/include/OE/Memory/MemoryPool.hpp
Expand Up @@ -4,7 +4,6 @@
#include <vector>

#include "OE/Memory/Allocator.hpp"
#include "OE/Memory/Ptrs.hpp"

namespace OrbitEngine { namespace Memory {
class MemoryPool : public Allocator {
Expand All @@ -23,23 +22,6 @@ namespace OrbitEngine { namespace Memory {
unsigned char* m_Memory;
unsigned char* m_Next;
};

template<typename T>
class TrackedMemoryPool : public MemoryPool {
public:
TrackedMemoryPool(int capacity);
virtual ~TrackedMemoryPool();

StrongPtr<T> AllocatePtr();
void DeallocatePtr(Ptr<T>& ptr);

const std::vector<WeakPtr<T>>& GetAll() const;

private:
std::vector<WeakPtr<T>> m_Used;
};
} }

#include "OE/Memory/MemoryPool.inl"

#endif
57 changes: 0 additions & 57 deletions Code/include/OE/Memory/MemoryPool.inl

This file was deleted.

0 comments on commit 7fad3b4

Please sign in to comment.