From d5241d7dfe8d5b06fd6cd1a52fafe562d7bbb994 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Tue, 13 Dec 2016 21:51:05 +0100 Subject: [PATCH] [Memory] Use mutex for memory allocation --- src/Utils/Memory.hpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/Utils/Memory.hpp b/src/Utils/Memory.hpp index f8c234c7..e3236da1 100644 --- a/src/Utils/Memory.hpp +++ b/src/Utils/Memory.hpp @@ -20,6 +20,8 @@ namespace Utils void clear() { + std::lock_guard _(this->mutex); + for (auto i = this->refMemory.begin(); i != this->refMemory.end(); ++i) { if (i->first && i->second) @@ -40,6 +42,8 @@ namespace Utils void free(void* data) { + std::lock_guard _(this->mutex); + auto i = this->refMemory.find(data); if (i != this->refMemory.end()) { @@ -62,11 +66,15 @@ namespace Utils void reference(void* memory, FreeCallback callback) { + std::lock_guard _(this->mutex); + this->refMemory[memory] = callback; } void* allocate(size_t length) { + std::lock_guard _(this->mutex); + void* data = Memory::Allocate(length); this->pool.push_back(data); return data; @@ -87,6 +95,8 @@ namespace Utils char* duplicateString(std::string string) { + std::lock_guard _(this->mutex); + char* data = Memory::DuplicateString(string); this->pool.push_back(data); return data; @@ -95,6 +105,7 @@ namespace Utils private: std::vector pool; std::map refMemory; + std::mutex mutex; }; static void* AllocateAlign(size_t length, size_t alignment);