Misc: Added IM_MALLOC/IM_FREE macros mimicking IM_NEW/IM_DELETE so user doesn't need to revert to using the ImGui::MemAlloc()/MemFree() calls directly.

This commit is contained in:
omar
2019-04-05 20:20:08 +02:00
parent 1c3311e4d6
commit c8fd4afd75
7 changed files with 53 additions and 42 deletions

View File

@ -1307,7 +1307,7 @@ void ImStrncpy(char* dst, const char* src, size_t count)
char* ImStrdup(const char* str)
{
size_t len = strlen(str);
void* buf = ImGui::MemAlloc(len + 1);
void* buf = IM_ALLOC(len + 1);
return (char*)memcpy(buf, (const void*)str, len + 1);
}
@ -1317,8 +1317,8 @@ char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src)
size_t src_size = strlen(src) + 1;
if (dst_buf_size < src_size)
{
ImGui::MemFree(dst);
dst = (char*)ImGui::MemAlloc(src_size);
IM_FREE(dst);
dst = (char*)IM_ALLOC(src_size);
if (p_dst_size)
*p_dst_size = src_size;
}
@ -1527,7 +1527,7 @@ FILE* ImFileOpen(const char* filename, const char* mode)
}
// Load file content into memory
// Memory allocated with ImGui::MemAlloc(), must be freed by user using ImGui::MemFree()
// Memory allocated with IM_ALLOC(), must be freed by user using IM_FREE() == ImGui::MemFree()
void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_t* out_file_size, int padding_bytes)
{
IM_ASSERT(filename && file_open_mode);
@ -1546,7 +1546,7 @@ void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_
}
size_t file_size = (size_t)file_size_signed;
void* file_data = ImGui::MemAlloc(file_size + padding_bytes);
void* file_data = IM_ALLOC(file_size + padding_bytes);
if (file_data == NULL)
{
fclose(f);
@ -1555,7 +1555,7 @@ void* ImFileLoadToMemory(const char* filename, const char* file_open_mode, size_
if (fread(file_data, 1, file_size, f) != file_size)
{
fclose(f);
ImGui::MemFree(file_data);
IM_FREE(file_data);
return NULL;
}
if (padding_bytes > 0)
@ -2962,6 +2962,7 @@ float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x)
return ImMax(wrap_pos_x - pos.x, 1.0f);
}
// IM_ALLOC() == ImGui::MemAlloc()
void* ImGui::MemAlloc(size_t size)
{
if (ImGuiContext* ctx = GImGui)
@ -2969,6 +2970,7 @@ void* ImGui::MemAlloc(size_t size)
return GImAllocatorAllocFunc(size, GImAllocatorUserData);
}
// IM_FREE() == ImGui::MemFree()
void ImGui::MemFree(void* ptr)
{
if (ptr)
@ -9093,7 +9095,7 @@ void ImGui::LoadIniSettingsFromDisk(const char* ini_filename)
if (!file_data)
return;
LoadIniSettingsFromMemory(file_data, (size_t)file_data_size);
ImGui::MemFree(file_data);
IM_FREE(file_data);
}
ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name)
@ -9117,7 +9119,7 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size)
// For our convenience and to make the code simpler, we'll also write zero-terminators within the buffer. So let's create a writable copy..
if (ini_size == 0)
ini_size = strlen(ini_data);
char* buf = (char*)ImGui::MemAlloc(ini_size + 1);
char* buf = (char*)IM_ALLOC(ini_size + 1);
char* buf_end = buf + ini_size;
memcpy(buf, ini_data, ini_size);
buf[ini_size] = 0;
@ -9164,7 +9166,7 @@ void ImGui::LoadIniSettingsFromMemory(const char* ini_data, size_t ini_size)
entry_handler->ReadLineFn(&g, entry_handler, entry_data, line);
}
}
ImGui::MemFree(buf);
IM_FREE(buf);
g.SettingsLoaded = true;
}