map and hashmap allocators

This commit is contained in:
Mikulas Florek 2014-11-08 21:12:38 +01:00
parent da1aba16c2
commit ba7dc978fb
25 changed files with 77 additions and 60 deletions

View file

@ -100,8 +100,8 @@ ScriptCompiler::Status ScriptCompiler::getStatus(const Lumix::Path& path)
hash = crc32(path.c_str());
}
Lumix::Map<uint32_t, Status>::iterator iter = m_status.find(hash);
return iter == m_status.end() ? UNKNOWN : iter.second();
QMap<uint32_t, Status>::iterator iter = m_status.find(hash);
return iter == m_status.end() ? UNKNOWN : iter.value();
}

View file

@ -2,6 +2,7 @@
#include <QObject>
#include <QMap>
#include "core/array.h"
#include "core/delegate_list.h"
#include "core/map.h"
@ -50,7 +51,7 @@ private:
CompileCallbacks m_delegates;
Lumix::Path m_base_path;
Lumix::Array<ProcessInfo> m_processes;
Lumix::Map<uint32_t, Status> m_status;
Lumix::Map<uint32_t, Lumix::string> m_log;
QMap<uint32_t, Status> m_status;
QMap<uint32_t, Lumix::string> m_log;
};

View file

@ -16,7 +16,7 @@ public:
ASSERT(pipeline_object);
if(pipeline_object)
{
m_pipeline = Lumix::PipelineInstance::create(*pipeline_object);
m_pipeline = Lumix::PipelineInstance::create(*pipeline_object, engine.getAllocator());
m_pipeline->setRenderer(engine.getRenderer());
}

View file

@ -91,15 +91,14 @@ namespace Lumix
}
};
template<class K, class T, class Hasher = HashFunc<K>, class Allocator = DefaultAllocator>
template<class K, class T, class Hasher = HashFunc<K>>
class HashMap
{
public:
typedef T value_type;
typedef K key_type;
typedef Hasher hasher_type;
typedef Allocator allocator_type;
typedef HashMap<key_type, value_type, hasher_type, allocator_type> my_type;
typedef HashMap<key_type, value_type, hasher_type> my_type;
typedef HashNode<key_type, value_type> node_type;
typedef uint32_t size_type;
@ -107,17 +106,16 @@ namespace Lumix
static const size_type s_default_ids_count = 8;
template <class U, class S, class _Hasher, class _Allocator>
template <class U, class S, class _Hasher>
class HashMapIterator
{
public:
typedef U key_type;
typedef S value_type;
typedef _Hasher hasher_type;
typedef _Allocator allocator_type;
typedef HashNode<key_type, value_type> node_type;
typedef HashMap<key_type, value_type, hasher_type, allocator_type> hm_type;
typedef HashMapIterator<key_type, value_type, hasher_type, allocator_type> my_type;
typedef HashMap<key_type, value_type, hasher_type> hm_type;
typedef HashMapIterator<key_type, value_type, hasher_type> my_type;
friend class hm_type;
@ -201,22 +199,25 @@ namespace Lumix
node_type* m_current_node;
};
typedef HashMapIterator<key_type, value_type, hasher_type, allocator_type> iterator;
typedef HashMapIterator<key_type, value_type, hasher_type> iterator;
HashMap()
HashMap(IAllocator& allocator)
: m_sentinel(&m_sentinel)
, m_allocator(allocator)
{
init();
}
explicit HashMap(size_type buckets)
explicit HashMap(size_type buckets, IAllocator& allocator)
: m_sentinel(&m_sentinel)
, m_allocator(allocator)
{
init(buckets);
}
HashMap(const my_type& src)
: m_sentinel(&m_sentinel)
, m_allocator(src.m_allocator)
{
init(src.m_max_id);
copyTableUninitialized(src.m_table, &src.m_sentinel, src.m_max_id);
@ -532,6 +533,6 @@ namespace Lumix
size_type m_size;
size_type m_mask;
size_type m_max_id;
allocator_type m_allocator;
IAllocator& m_allocator;
};
} // ~namespace Lumix

View file

@ -7,11 +7,7 @@
namespace Lumix
{
/* action type
key pressed
key down
key down duration
*/
class IAllocator;
class LUMIX_CORE_API InputSystem
{
@ -25,7 +21,7 @@ namespace Lumix
};
public:
bool create();
bool create(IAllocator& allocator);
void destroy();
void update(float dt);

View file

@ -11,7 +11,7 @@ namespace Lumix
{
template <typename Key, typename Value, typename Allocator = DefaultAllocator>
template <typename Key, typename Value>
class Map
{
private:
@ -104,19 +104,13 @@ class Map
};
public:
Map(const Allocator& allocator)
Map(IAllocator& allocator)
: m_allocator(allocator)
{
m_root = NULL;
m_size = 0;
}
Map()
{
m_root = NULL;
m_size = 0;
}
~Map()
{
clear();
@ -471,7 +465,7 @@ class Map
private:
Node* m_root;
int m_size;
Allocator m_allocator;
IAllocator& m_allocator;
};

View file

@ -62,10 +62,10 @@ namespace Lumix
}
};
typedef Map<uint32_t, MemoryTracker::Entry*, MemTrackAllocator> map_alloc_order;
typedef Map<FileLineReport, intptr_t, MemTrackAllocator> file_line_map;
typedef Map<const char *, intptr_t, MemTrackAllocator> file_map;
typedef Map<FileLineReport, uint32_t, MemTrackAllocator> alloc_count_map;
typedef Map<uint32_t, MemoryTracker::Entry*> map_alloc_order;
typedef Map<FileLineReport, intptr_t> file_line_map;
typedef Map<const char *, intptr_t> file_map;
typedef Map<FileLineReport, uint32_t> alloc_count_map;
#pragma init_seg(compiler)
MemoryTracker MemoryTracker::s_instance;
@ -196,7 +196,7 @@ namespace Lumix
memTrackerLog("MemoryTracker", "MemoryTracker No leaks detected!");
}
map_alloc_order alloc_order_map;
map_alloc_order alloc_order_map(m_allocator);
for (EntryTable::iterator it = m_map.begin(); it != m_map.end(); ++it)
{
Entry& entry = *it;
@ -222,7 +222,7 @@ namespace Lumix
{
memTrackerLog("MemoryTracker", "Dumping objects ->");
file_line_map report_map;
file_line_map report_map(m_allocator);
{
MT::SpinLock lock(m_spin_mutex);
for (EntryTable::iterator it = m_map.begin(); it != m_map.end(); ++it)
@ -266,7 +266,7 @@ namespace Lumix
{
memTrackerLog("MemoryTracker", "Dumping objects ->");
file_map report_map;
file_map report_map(m_allocator);
{
MT::SpinLock lock(m_spin_mutex);
for (EntryTable::iterator it = m_map.begin(); it != m_map.end(); ++it)

View file

@ -12,12 +12,11 @@
namespace Lumix
{
class MemTrackAllocator
class MemTrackAllocator : public IAllocator
{
public:
void* allocate(size_t n) { return malloc(n); }
void deallocate(void* p) { free(p); }
void* reallocate(void* p, size_t n) { return realloc(p, n); }
};
class LUMIX_CORE_API MemoryTracker
@ -80,6 +79,7 @@ namespace Lumix
MT::SpinMutex m_spin_mutex;
intptr_t m_allocated_memory;
uint8_t m_mark;
MemTrackAllocator m_allocator;
static MemoryTracker s_instance;
static uint32_t s_alloc_counter;

View file

@ -9,6 +9,10 @@ namespace Lumix
struct InputSystemImpl
{
InputSystemImpl(IAllocator& allocator)
: m_actions(allocator)
{}
struct Action
{
InputSystem::InputType type;
@ -27,9 +31,9 @@ namespace Lumix
}
bool InputSystem::create()
bool InputSystem::create(IAllocator& allocator)
{
m_impl = LUMIX_NEW(InputSystemImpl)();
m_impl = LUMIX_NEW(InputSystemImpl)(allocator);
m_impl->m_mouse_rel_x = 0;
m_impl->m_mouse_rel_y = 0;
return true;

View file

@ -104,6 +104,7 @@ namespace Lumix
EntityTemplateSystemImpl(WorldEditor& editor)
: m_editor(editor)
, m_universe(NULL)
, m_instances(editor.getAllocator())
{
editor.universeCreated().bind<EntityTemplateSystemImpl, &EntityTemplateSystemImpl::onUniverseCreated>(this);
editor.universeDestroyed().bind<EntityTemplateSystemImpl, &EntityTemplateSystemImpl::onUniverseDestroyed>(this);

View file

@ -795,6 +795,12 @@ struct WorldEditorImpl : public WorldEditor
}
virtual IAllocator& getAllocator() override
{
return m_allocator;
}
virtual Engine& getEngine() override
{
return *m_engine;
@ -1709,6 +1715,8 @@ struct WorldEditorImpl : public WorldEditor
, m_universe_mutex(false)
, m_toggle_game_mode_requested(false)
, m_gizmo(*this)
, m_component_properties(m_allocator)
, m_components(m_allocator)
{
m_go_to_parameters.m_is_active = false;
m_undo_index = -1;
@ -2105,6 +2113,7 @@ struct WorldEditorImpl : public WorldEditor
float m_speed;
};
DefaultAllocator m_allocator;
GoToParameters m_go_to_parameters;
MT::Mutex m_universe_mutex;
Gizmo m_gizmo;

View file

@ -60,6 +60,7 @@ namespace Lumix
virtual IPropertyDescriptor* getProperty(const char* component_type, const char* property_name) = 0;
virtual void executeCommand(class IEditorCommand* command) = 0;
virtual Engine& getEngine() = 0;
virtual IAllocator& getAllocator() = 0;
virtual void render(IRenderDevice& render_device) = 0;
virtual void renderIcons(IRenderDevice& render_device) = 0;
virtual Component getEditCamera() = 0;

View file

@ -86,7 +86,7 @@ namespace Lumix
return false;
}
m_plugin_manager.addPlugin(m_renderer);
if (!m_input_system.create())
if (!m_input_system.create(m_allocator))
{
return false;
}
@ -127,8 +127,8 @@ namespace Lumix
virtual Universe* createUniverse() override
{
m_universe = m_allocator.newObject<Universe>();
m_hierarchy = Hierarchy::create(*m_universe);
m_universe = m_allocator.newObject<Universe>(m_allocator);
m_hierarchy = Hierarchy::create(*m_universe, m_allocator);
const Array<IPlugin*>& plugins = m_plugin_manager.getPlugins();
for (int i = 0; i < plugins.size(); ++i)
{

View file

@ -77,10 +77,11 @@ class Model : public Resource
};
public:
Model(const Path& path, ResourceManager& resource_manager)
Model(const Path& path, ResourceManager& resource_manager, IAllocator& allocator)
: Resource(path, resource_manager)
, m_geometry()
, m_bounding_radius()
, m_bone_map(allocator)
{ }
~Model();

View file

@ -8,7 +8,7 @@ namespace Lumix
{
Resource* ModelManager::createResource(const Path& path)
{
return m_allocator.newObject<Model>(path, getOwner());
return m_allocator.newObject<Model>(path, getOwner(), m_allocator);
}
void ModelManager::destroyResource(Resource& resource)

View file

@ -334,9 +334,10 @@ struct PipelineImpl : public Pipeline
struct PipelineInstanceImpl : public PipelineInstance
{
PipelineInstanceImpl(Pipeline& pipeline)
PipelineInstanceImpl(Pipeline& pipeline, IAllocator& allocator)
: m_source(static_cast<PipelineImpl&>(pipeline))
, m_active_camera(Component::INVALID)
, m_custom_commands_handlers(allocator)
{
m_scene = NULL;
m_light_dir.set(0, -1, 0);
@ -793,9 +794,9 @@ Pipeline::Pipeline(const Path& path, ResourceManager& resource_manager)
}
PipelineInstance* PipelineInstance::create(Pipeline& pipeline)
PipelineInstance* PipelineInstance::create(Pipeline& pipeline, IAllocator& allocator)
{
return LUMIX_NEW(PipelineInstanceImpl)(pipeline);
return LUMIX_NEW(PipelineInstanceImpl)(pipeline, allocator);
}

View file

@ -67,7 +67,7 @@ class LUMIX_ENGINE_API PipelineInstance abstract
virtual void resize(int w, int h) = 0;
virtual FrameBuffer* getShadowmapFramebuffer() = 0;
static PipelineInstance* create(Pipeline& src);
static PipelineInstance* create(Pipeline& src, IAllocator& allocator);
static void destroy(PipelineInstance* pipeline);
virtual Renderer& getRenderer() = 0;

View file

@ -131,6 +131,7 @@ namespace Lumix
, m_universe(universe)
, m_renderer(renderer)
, m_allocator(allocator)
, m_dynamic_renderable_cache(allocator)
{
m_universe.entityMoved().bind<RenderSceneImpl, &RenderSceneImpl::onEntityMoved>(this);
m_timer = Timer::create();

View file

@ -173,6 +173,8 @@ namespace Lumix
, m_brush_position(0, 0, 0)
, m_brush_size(1)
, m_allocator(allocator)
, m_grass_quads(m_allocator)
, m_last_camera_position(m_allocator)
{
generateGeometry();
}

View file

@ -103,6 +103,7 @@ class Terrain
void forceGrassUpdate();
private:
IAllocator& m_allocator;
Mesh* m_mesh;
TerrainQuad* m_root;
Geometry m_geometry;
@ -119,11 +120,10 @@ class Terrain
Array<GrassType*> m_grass_types;
Array<GrassQuad*> m_free_grass_quads;
Map<Component, Array<GrassQuad*> > m_grass_quads;
Map<Component, Vec3> m_last_camera_position;
Map<Component, Vec3> m_last_camera_position;
Vec3 m_brush_position;
float m_brush_size;
bool m_force_grass_update;
IAllocator& m_allocator;
};

View file

@ -6,7 +6,8 @@ namespace
{
void UT_insert(const char* params)
{
Lumix::HashMap<int32_t, int32_t> hash_table;
Lumix::DefaultAllocator allocator;
Lumix::HashMap<int32_t, int32_t> hash_table(allocator);
LUMIX_EXPECT_TRUE(hash_table.empty());
};

View file

@ -17,8 +17,10 @@ class HierarchyImpl : public Hierarchy
typedef HashMap<int32_t, int32_t> Parents;
public:
HierarchyImpl(Universe& universe)
HierarchyImpl(Universe& universe, IAllocator& allocator)
: m_universe(universe)
, m_parents(allocator)
, m_children(allocator)
{
universe.entityMoved().bind<HierarchyImpl, &HierarchyImpl::onEntityMoved>(this);
}
@ -168,9 +170,9 @@ class HierarchyImpl : public Hierarchy
};
Hierarchy* Hierarchy::create(Universe& universe)
Hierarchy* Hierarchy::create(Universe& universe, IAllocator& allocator)
{
return LUMIX_NEW(HierarchyImpl)(universe);
return LUMIX_NEW(HierarchyImpl)(universe, allocator);
}

View file

@ -24,7 +24,7 @@ namespace Lumix
public:
static Hierarchy* create(Universe& universe);
static Hierarchy* create(Universe& universe, IAllocator& allocator);
static void destroy(Hierarchy* hierarchy);
virtual ~Hierarchy() {}

View file

@ -17,7 +17,9 @@ Universe::~Universe()
}
Universe::Universe()
Universe::Universe(IAllocator& allocator)
: m_name_to_id_map(allocator)
, m_id_to_name_map(allocator)
{
m_positions.reserve(RESERVED_ENTITIES);
m_rotations.reserve(RESERVED_ENTITIES);

View file

@ -28,7 +28,7 @@ class LUMIX_ENGINE_API Universe final
{
friend struct Entity;
public:
Universe();
Universe(IAllocator& allocator);
~Universe();
Entity createEntity();