ZoneLoading: Add ZoneMemory and the possibility to unload zones and their memory

This commit is contained in:
Jan
2019-12-24 18:41:42 +01:00
parent d224eb8ce5
commit 7121f2e215
9 changed files with 116 additions and 2 deletions

View File

@ -7,9 +7,39 @@ Zone::Zone(std::string name, const zone_priority_t priority, IZoneAssetPools* po
m_pools = pools;
m_game = game;
m_language = ZoneLanguage::LANGUAGE_NONE;
m_memory = new ZoneMemory();
m_registered = false;
}
Zone::~Zone()
{
if(m_registered)
{
m_game->RemoveZone(this);
}
delete m_pools;
m_pools = nullptr;
delete m_memory;
m_memory = nullptr;
}
void Zone::Register()
{
if (!m_registered)
{
m_game->AddZone(this);
m_registered = true;
}
}
IZoneAssetPools* Zone::GetPools() const
{
return m_pools;
}
ZoneMemory* Zone::GetMemory() const
{
return m_memory;
}

View File

@ -2,6 +2,8 @@
#include "ZoneTypes.h"
#include "Pool/IZoneAssetPools.h"
#include "Game/IGame.h"
#include "Zone/XBlock.h"
#include "ZoneMemory.h"
#include <string>
class IGame;
@ -34,6 +36,10 @@ enum class ZoneLanguage
class Zone
{
IZoneAssetPools* m_pools;
std::vector<XBlock*> m_blocks;
ZoneMemory* m_memory;
bool m_registered;
public:
std::string m_name;
@ -42,6 +48,10 @@ public:
IGame* m_game;
Zone(std::string name, zone_priority_t priority, IZoneAssetPools* pools, IGame* game);
~Zone();
void Register();
IZoneAssetPools* GetPools() const;
ZoneMemory* GetMemory() const;
};

View File

@ -0,0 +1,40 @@
#include "ZoneMemory.h"
ZoneMemory::ZoneMemory()
= default;
ZoneMemory::~ZoneMemory()
{
for (auto block : m_blocks)
{
delete block;
}
m_blocks.clear();
for (auto allocation : m_allocations)
{
free(allocation);
}
m_allocations.clear();
}
void ZoneMemory::AddBlock(XBlock* block)
{
m_blocks.push_back(block);
}
void* ZoneMemory::Alloc(const size_t size)
{
void* result = malloc(size);
m_allocations.push_back(result);
return result;
}
char* ZoneMemory::Dup(const char* str)
{
char* result = _strdup(str);
m_allocations.push_back(result);
return result;
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "Zone/XBlock.h"
#include <vector>
class ZoneMemory
{
std::vector<XBlock*> m_blocks;
std::vector<void*> m_allocations;
public:
ZoneMemory();
~ZoneMemory();
void AddBlock(XBlock* block);
void* Alloc(size_t size);
char* Dup(const char* str);
};