[Memory] Use mutex for memory allocation

This commit is contained in:
momo5502 2016-12-13 21:51:05 +01:00
parent bc5aed43d8
commit d5241d7dfe

View File

@ -20,6 +20,8 @@ namespace Utils
void clear() void clear()
{ {
std::lock_guard<std::mutex> _(this->mutex);
for (auto i = this->refMemory.begin(); i != this->refMemory.end(); ++i) for (auto i = this->refMemory.begin(); i != this->refMemory.end(); ++i)
{ {
if (i->first && i->second) if (i->first && i->second)
@ -40,6 +42,8 @@ namespace Utils
void free(void* data) void free(void* data)
{ {
std::lock_guard<std::mutex> _(this->mutex);
auto i = this->refMemory.find(data); auto i = this->refMemory.find(data);
if (i != this->refMemory.end()) if (i != this->refMemory.end())
{ {
@ -62,11 +66,15 @@ namespace Utils
void reference(void* memory, FreeCallback callback) void reference(void* memory, FreeCallback callback)
{ {
std::lock_guard<std::mutex> _(this->mutex);
this->refMemory[memory] = callback; this->refMemory[memory] = callback;
} }
void* allocate(size_t length) void* allocate(size_t length)
{ {
std::lock_guard<std::mutex> _(this->mutex);
void* data = Memory::Allocate(length); void* data = Memory::Allocate(length);
this->pool.push_back(data); this->pool.push_back(data);
return data; return data;
@ -87,6 +95,8 @@ namespace Utils
char* duplicateString(std::string string) char* duplicateString(std::string string)
{ {
std::lock_guard<std::mutex> _(this->mutex);
char* data = Memory::DuplicateString(string); char* data = Memory::DuplicateString(string);
this->pool.push_back(data); this->pool.push_back(data);
return data; return data;
@ -95,6 +105,7 @@ namespace Utils
private: private:
std::vector<void*> pool; std::vector<void*> pool;
std::map<void*, FreeCallback> refMemory; std::map<void*, FreeCallback> refMemory;
std::mutex mutex;
}; };
static void* AllocateAlign(size_t length, size_t alignment); static void* AllocateAlign(size_t length, size_t alignment);