Merge pull request #596 from diamante0018/refactor/memory

[Memory]: Remove nullptr check before std::free
This commit is contained in:
Edo 2022-11-25 22:45:45 +00:00 committed by GitHub
commit a277640ed9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 15 deletions

View File

@ -4,7 +4,7 @@ namespace Utils
{ {
Utils::Memory::Allocator Memory::MemAllocator; Utils::Memory::Allocator Memory::MemAllocator;
void* Memory::AllocateAlign(size_t length, size_t alignment) void* Memory::AllocateAlign(std::size_t length, std::size_t alignment)
{ {
auto* data = _aligned_malloc(length, alignment); auto* data = _aligned_malloc(length, alignment);
assert(data); assert(data);
@ -12,9 +12,9 @@ namespace Utils
return data; return data;
} }
void* Memory::Allocate(size_t length) void* Memory::Allocate(std::size_t length)
{ {
auto* data = calloc(length, 1); auto* data = std::calloc(length, 1);
assert(data); assert(data);
return data; return data;
} }
@ -28,10 +28,7 @@ namespace Utils
void Memory::Free(void* data) void Memory::Free(void* data)
{ {
if (data) std::free(data);
{
::free(data);
}
} }
void Memory::Free(const void* data) void Memory::Free(const void* data)
@ -53,11 +50,11 @@ namespace Utils
} }
// Complementary function for memset, which checks if memory is filled with a char // Complementary function for memset, which checks if memory is filled with a char
bool Memory::IsSet(void* mem, char chr, size_t length) bool Memory::IsSet(void* mem, char chr, std::size_t length)
{ {
auto* memArr = static_cast<char*>(mem); auto* memArr = static_cast<char*>(mem);
for (size_t i = 0; i < length; ++i) for (std::size_t i = 0; i < length; ++i)
{ {
if (memArr[i] != chr) if (memArr[i] != chr)
{ {

View File

@ -73,7 +73,7 @@ namespace Utils
this->refMemory[memory] = callback; this->refMemory[memory] = callback;
} }
void* allocate(size_t length) void* allocate(std::size_t length)
{ {
std::lock_guard _(this->mutex); std::lock_guard _(this->mutex);
@ -87,7 +87,7 @@ namespace Utils
return this->allocateArray<T>(1); return this->allocateArray<T>(1);
} }
template <typename T> T* allocateArray(size_t count = 1) template <typename T> T* allocateArray(std::size_t count = 1)
{ {
return static_cast<T*>(this->allocate(count * sizeof(T))); return static_cast<T*>(this->allocate(count * sizeof(T)));
} }
@ -133,13 +133,13 @@ namespace Utils
std::unordered_map<void*, FreeCallback> refMemory; std::unordered_map<void*, FreeCallback> refMemory;
}; };
static void* AllocateAlign(size_t length, size_t alignment); static void* AllocateAlign(std::size_t length, std::size_t alignment);
static void* Allocate(size_t length); static void* Allocate(std::size_t length);
template <typename T> static T* Allocate() template <typename T> static T* Allocate()
{ {
return AllocateArray<T>(1); return AllocateArray<T>(1);
} }
template <typename T> static T* AllocateArray(size_t count = 1) template <typename T> static T* AllocateArray(std::size_t count = 1)
{ {
return static_cast<T*>(Allocate(count * sizeof(T))); return static_cast<T*>(Allocate(count * sizeof(T)));
} }
@ -159,7 +159,7 @@ namespace Utils
static void FreeAlign(void* data); static void FreeAlign(void* data);
static void FreeAlign(const void* data); static void FreeAlign(const void* data);
static bool IsSet(void* mem, char chr, size_t length); static bool IsSet(void* mem, char chr, std::size_t length);
static bool IsBadReadPtr(const void* ptr); static bool IsBadReadPtr(const void* ptr);
static bool IsBadCodePtr(const void* ptr); static bool IsBadCodePtr(const void* ptr);