Internals: Added IM_NEW, IM_DELETE helper macros (#1517, #484, #504)

This commit is contained in:
omar 2017-12-23 16:24:33 +01:00
parent a5739a0aa3
commit 9cda86d55a
3 changed files with 17 additions and 31 deletions

View File

@ -1859,8 +1859,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name)
ItemWidthDefault = 0.0f; ItemWidthDefault = 0.0f;
FontWindowScale = 1.0f; FontWindowScale = 1.0f;
DrawList = (ImDrawList*)ImGui::MemAlloc(sizeof(ImDrawList)); DrawList = IM_NEW(ImDrawList)(&context->DrawListSharedData);
IM_PLACEMENT_NEW(DrawList) ImDrawList(&context->DrawListSharedData);
DrawList->_OwnerName = Name; DrawList->_OwnerName = Name;
ParentWindow = NULL; ParentWindow = NULL;
RootWindow = NULL; RootWindow = NULL;
@ -1873,11 +1872,8 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name)
ImGuiWindow::~ImGuiWindow() ImGuiWindow::~ImGuiWindow()
{ {
DrawList->~ImDrawList(); IM_DELETE(DrawList);
ImGui::MemFree(DrawList); IM_DELETE(Name);
DrawList = NULL;
ImGui::MemFree(Name);
Name = NULL;
} }
ImGuiID ImGuiWindow::GetID(const char* str, const char* str_end) 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() void ImGui::Initialize()
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
g.LogClipboard = (ImGuiTextBuffer*)ImGui::MemAlloc(sizeof(ImGuiTextBuffer)); g.LogClipboard = IM_NEW(ImGuiTextBuffer)();
IM_PLACEMENT_NEW(g.LogClipboard) ImGuiTextBuffer();
// Add .ini handle for ImGuiWindow type // Add .ini handle for ImGuiWindow type
ImGuiSettingsHandler ini_handler; ImGuiSettingsHandler ini_handler;
@ -2613,10 +2608,7 @@ void ImGui::Shutdown()
SaveIniSettingsToDisk(g.IO.IniFilename); SaveIniSettingsToDisk(g.IO.IniFilename);
for (int i = 0; i < g.Windows.Size; i++) for (int i = 0; i < g.Windows.Size; i++)
{ IM_DELETE(g.Windows[i]);
g.Windows[i]->~ImGuiWindow();
ImGui::MemFree(g.Windows[i]);
}
g.Windows.clear(); g.Windows.clear();
g.WindowsSortBuffer.clear(); g.WindowsSortBuffer.clear();
g.CurrentWindow = NULL; g.CurrentWindow = NULL;
@ -2628,7 +2620,7 @@ void ImGui::Shutdown()
g.ActiveIdWindow = NULL; g.ActiveIdWindow = NULL;
g.MovingWindow = NULL; g.MovingWindow = NULL;
for (int i = 0; i < g.SettingsWindows.Size; i++) 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.ColorModifiers.clear();
g.StyleModifiers.clear(); g.StyleModifiers.clear();
g.FontStack.clear(); g.FontStack.clear();
@ -2653,10 +2645,7 @@ void ImGui::Shutdown()
g.LogFile = NULL; g.LogFile = NULL;
} }
if (g.LogClipboard) if (g.LogClipboard)
{ IM_DELETE(g.LogClipboard);
g.LogClipboard->~ImGuiTextBuffer();
ImGui::MemFree(g.LogClipboard);
}
g.Initialized = false; g.Initialized = false;
} }
@ -4157,8 +4146,7 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFl
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
// Create window the first time // Create window the first time
ImGuiWindow* window = (ImGuiWindow*)ImGui::MemAlloc(sizeof(ImGuiWindow)); ImGuiWindow* window = IM_NEW(ImGuiWindow)(&g, name);
IM_PLACEMENT_NEW(window) ImGuiWindow(&g, name);
window->Flags = flags; window->Flags = flags;
g.WindowsById.SetVoidPtr(window->ID, window); g.WindowsById.SetVoidPtr(window->ID, window);

View File

@ -1461,15 +1461,9 @@ ImFont* ImFontAtlas::AddFont(const ImFontConfig* font_cfg)
// Create new font // Create new font
if (!font_cfg->MergeMode) if (!font_cfg->MergeMode)
{ Fonts.push_back(IM_NEW(ImFont));
ImFont* font = (ImFont*)ImGui::MemAlloc(sizeof(ImFont));
IM_PLACEMENT_NEW(font) ImFont();
Fonts.push_back(font);
}
else 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. 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); ConfigData.push_back(*font_cfg);
ImFontConfig& new_font_cfg = ConfigData.back(); ImFontConfig& new_font_cfg = ConfigData.back();

View File

@ -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. // 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 <new> which on some platforms complains when user has disabled exceptions. // Defining a custom placement new() with a dummy parameter allows us to bypass including <new> which on some platforms complains when user has disabled exceptions.
struct ImPlacementNewDummy {}; struct ImNewAllocDummy {};
inline void* operator new(size_t, ImPlacementNewDummy, void* ptr) { return ptr; } struct ImNewPlacementDummy {};
inline void operator delete(void*, ImPlacementNewDummy, void*) {} inline void* operator new(size_t, ImNewPlacementDummy, void* ptr) { return ptr; }
#define IM_PLACEMENT_NEW(_PTR) new(ImPlacementNewDummy(), _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 // Types