refactor(memory): remove nullptr check before std::free

This commit is contained in:
6arelyFuture 2022-11-25 22:57:11 +00:00
parent 51dc00790b
commit 452cabfa7b
No known key found for this signature in database
GPG Key ID: 22F9079C86CFAB31
2 changed files with 15 additions and 17 deletions

View File

@ -30,7 +30,7 @@ void memory::allocator::free(const void* data) {
this->free(const_cast<void*>(data));
}
void* memory::allocator::allocate(const size_t length) {
void* memory::allocator::allocate(const std::size_t length) {
std::lock_guard _(this->mutex_);
const auto data = memory::allocate(length);
@ -48,7 +48,9 @@ char* memory::allocator::duplicate_string(const std::string& string) {
return data;
}
void* memory::allocate(const size_t length) { return calloc(length, 1); }
void* memory::allocate(const std::size_t length) {
return std::calloc(length, 1);
}
char* memory::duplicate_string(const std::string& string) {
const auto new_string = allocate_array<char>(string.size() + 1);
@ -56,18 +58,14 @@ char* memory::duplicate_string(const std::string& string) {
return new_string;
}
void memory::free(void* data) {
if (data) {
::free(data);
}
}
void memory::free(void* data) { std::free(data); }
void memory::free(const void* data) { free(const_cast<void*>(data)); }
bool memory::is_set(const void* mem, const char chr, const size_t length) {
bool memory::is_set(const void* mem, const char chr, const std::size_t length) {
const auto mem_arr = static_cast<const char*>(mem);
for (size_t i = 0; i < length; ++i) {
for (std::size_t i = 0; i < length; ++i) {
if (mem_arr[i] != chr) {
return false;
}
@ -119,9 +117,9 @@ bool memory::is_rdata_ptr(void* pointer) {
if (name == rdata) {
const auto target = size_t(pointer);
const size_t source_start =
size_t(pointer_lib.get_ptr()) + section->PointerToRawData;
const size_t source_end = source_start + section->SizeOfRawData;
const std::size_t source_start =
std::size_t(pointer_lib.get_ptr()) + section->PointerToRawData;
const std::size_t source_end = source_start + section->SizeOfRawData;
return target >= source_start && target <= source_end;
}

View File

@ -16,11 +16,11 @@ public:
void free(const void* data);
void* allocate(size_t length);
void* allocate(std::size_t length);
template <typename T> T* allocate() { return this->allocate_array<T>(1); }
template <typename T> T* allocate_array(const size_t count = 1) {
template <typename T> T* allocate_array(const std::size_t count = 1) {
return static_cast<T*>(this->allocate(count * sizeof(T)));
}
@ -33,11 +33,11 @@ public:
std::vector<void*> pool_;
};
static void* allocate(size_t length);
static void* allocate(std::size_t length);
template <typename T> static T* allocate() { return allocate_array<T>(1); }
template <typename T> static T* allocate_array(const size_t count = 1) {
template <typename T> static T* allocate_array(const std::size_t count = 1) {
return static_cast<T*>(allocate(count * sizeof(T)));
}
@ -46,7 +46,7 @@ public:
static void free(void* data);
static void free(const void* data);
static bool is_set(const void* mem, char chr, size_t length);
static bool is_set(const void* mem, char chr, std::size_t length);
static bool is_bad_read_ptr(const void* ptr);
static bool is_bad_code_ptr(const void* ptr);