From 9cda86d55a80c9f032af84f657b9fe8e6c7f3f52 Mon Sep 17 00:00:00 2001 From: omar Date: Sat, 23 Dec 2017 16:24:33 +0100 Subject: [PATCH] Internals: Added IM_NEW, IM_DELETE helper macros (#1517, #484, #504) --- imgui.cpp | 28 ++++++++-------------------- imgui_draw.cpp | 8 +------- imgui_internal.h | 12 ++++++++---- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 649d2e765..6173f7121 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -1859,8 +1859,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) ItemWidthDefault = 0.0f; FontWindowScale = 1.0f; - DrawList = (ImDrawList*)ImGui::MemAlloc(sizeof(ImDrawList)); - IM_PLACEMENT_NEW(DrawList) ImDrawList(&context->DrawListSharedData); + DrawList = IM_NEW(ImDrawList)(&context->DrawListSharedData); DrawList->_OwnerName = Name; ParentWindow = NULL; RootWindow = NULL; @@ -1873,11 +1872,8 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name) ImGuiWindow::~ImGuiWindow() { - DrawList->~ImDrawList(); - ImGui::MemFree(DrawList); - DrawList = NULL; - ImGui::MemFree(Name); - Name = NULL; + IM_DELETE(DrawList); + IM_DELETE(Name); } ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) @@ -2579,8 +2575,7 @@ static void SettingsHandlerWindow_WriteAll(ImGuiContext& g, ImGuiTextBuffer* buf void ImGui::Initialize() { ImGuiContext& g = *GImGui; - g.LogClipboard = (ImGuiTextBuffer*)ImGui::MemAlloc(sizeof(ImGuiTextBuffer)); - IM_PLACEMENT_NEW(g.LogClipboard) ImGuiTextBuffer(); + g.LogClipboard = IM_NEW(ImGuiTextBuffer)(); // Add .ini handle for ImGuiWindow type ImGuiSettingsHandler ini_handler; @@ -2613,10 +2608,7 @@ void ImGui::Shutdown() SaveIniSettingsToDisk(g.IO.IniFilename); for (int i = 0; i < g.Windows.Size; i++) - { - g.Windows[i]->~ImGuiWindow(); - ImGui::MemFree(g.Windows[i]); - } + IM_DELETE(g.Windows[i]); g.Windows.clear(); g.WindowsSortBuffer.clear(); g.CurrentWindow = NULL; @@ -2628,7 +2620,7 @@ void ImGui::Shutdown() g.ActiveIdWindow = NULL; g.MovingWindow = NULL; for (int i = 0; i < g.SettingsWindows.Size; i++) - ImGui::MemFree(g.SettingsWindows[i].Name); + IM_DELETE(g.SettingsWindows[i].Name); g.ColorModifiers.clear(); g.StyleModifiers.clear(); g.FontStack.clear(); @@ -2653,10 +2645,7 @@ void ImGui::Shutdown() g.LogFile = NULL; } if (g.LogClipboard) - { - g.LogClipboard->~ImGuiTextBuffer(); - ImGui::MemFree(g.LogClipboard); - } + IM_DELETE(g.LogClipboard); g.Initialized = false; } @@ -4157,8 +4146,7 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFl ImGuiContext& g = *GImGui; // Create window the first time - ImGuiWindow* window = (ImGuiWindow*)ImGui::MemAlloc(sizeof(ImGuiWindow)); - IM_PLACEMENT_NEW(window) ImGuiWindow(&g, name); + ImGuiWindow* window = IM_NEW(ImGuiWindow)(&g, name); window->Flags = flags; g.WindowsById.SetVoidPtr(window->ID, window); diff --git a/imgui_draw.cpp b/imgui_draw.cpp index 02cf42cca..793aeffbc 100644 --- a/imgui_draw.cpp +++ b/imgui_draw.cpp @@ -1461,15 +1461,9 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg) // Create new font if (!font_cfg->MergeMode) - { - ImFont* font = (ImFont*)ImGui::MemAlloc(sizeof(ImFont)); - IM_PLACEMENT_NEW(font) ImFont(); - Fonts.push_back(font); - } + Fonts.push_back(IM_NEW(ImFont)); else - { IM_ASSERT(!Fonts.empty()); // When using MergeMode make sure that a font has already been added before. You can use ImGui::GetIO().Fonts->AddFontDefault() to add the default imgui font. - } ConfigData.push_back(*font_cfg); ImFontConfig& new_font_cfg = ConfigData.back(); diff --git a/imgui_internal.h b/imgui_internal.h index 1e0f8ae1f..c08f5082c 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -160,10 +160,14 @@ static inline ImVec2 ImMul(const ImVec2& lhs, const ImVec2& rhs) // We call C++ constructor on own allocated memory via the placement "new(ptr) Type()" syntax. // Defining a custom placement new() with a dummy parameter allows us to bypass including which on some platforms complains when user has disabled exceptions. -struct ImPlacementNewDummy {}; -inline void* operator new(size_t, ImPlacementNewDummy, void* ptr) { return ptr; } -inline void operator delete(void*, ImPlacementNewDummy, void*) {} -#define IM_PLACEMENT_NEW(_PTR) new(ImPlacementNewDummy(), _PTR) +struct ImNewAllocDummy {}; +struct ImNewPlacementDummy {}; +inline void* operator new(size_t, ImNewPlacementDummy, void* ptr) { return ptr; } +inline void operator delete(void*, ImNewPlacementDummy, void*) {} // This is only required so we can use the symetrical new() +inline void operator delete(void* p, ImNewAllocDummy) { ImGui::MemFree(p); } +#define IM_PLACEMENT_NEW(_PTR) new(ImNewPlacementDummy(), _PTR) +#define IM_NEW(_TYPE) new(ImNewPlacementDummy(), ImGui::MemAlloc(sizeof(_TYPE))) _TYPE +#define IM_DELETE(_PTR) delete(ImNewAllocDummy(), _PTR), _PTR = NULL //----------------------------------------------------------------------------- // Types