From daf49e9d829bde0bd3d5124139e9191a54521ae6 Mon Sep 17 00:00:00 2001 From: ocornut Date: Fri, 22 Sep 2023 14:41:30 +0200 Subject: [PATCH] Made ImFileOpen reuse a memory buffer so .ini saving doesn't allocate once every time. Added commented out MemAlloc/MemFree debug log. --- imgui.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 9edd1aa238d7..80c672b5dd38 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -2037,8 +2037,9 @@ ImFileHandle ImFileOpen(const char* filename, const char* mode) // Previously we used ImTextCountCharsFromUtf8/ImTextStrFromUtf8 here but we now need to support ImWchar16 and ImWchar32! const int filename_wsize = ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0); const int mode_wsize = ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, NULL, 0); - ImVector buf; - buf.resize(filename_wsize + mode_wsize); + ImGuiContext& g = *GImGui; + g.TempBuffer.reserve((filename_wsize + mode_wsize) * sizeof(wchar_t)); + wchar_t* buf = (wchar_t*)(void*)g.TempBuffer.Data; ::MultiByteToWideChar(CP_UTF8, 0, filename, -1, (wchar_t*)&buf[0], filename_wsize); ::MultiByteToWideChar(CP_UTF8, 0, mode, -1, (wchar_t*)&buf[filename_wsize], mode_wsize); return ::_wfopen((const wchar_t*)&buf[0], (const wchar_t*)&buf[filename_wsize]); @@ -4225,17 +4226,24 @@ float ImGui::CalcWrapWidthForPos(const ImVec2& pos, float wrap_pos_x) // IM_ALLOC() == ImGui::MemAlloc() void* ImGui::MemAlloc(size_t size) { + void* ptr = (*GImAllocatorAllocFunc)(size, GImAllocatorUserData); if (ImGuiContext* ctx = GImGui) + { ctx->IO.MetricsActiveAllocations++; - return (*GImAllocatorAllocFunc)(size, GImAllocatorUserData); + //printf("[%05d] MemAlloc(%d) -> 0x%p\n", ctx->FrameCount, size, ptr); + } + return ptr; } // IM_FREE() == ImGui::MemFree() void ImGui::MemFree(void* ptr) { - if (ptr) + if (ptr != NULL) if (ImGuiContext* ctx = GImGui) + { ctx->IO.MetricsActiveAllocations--; + //printf("[%05d] MemFree(0x%p)\n", ctx->FrameCount, ptr); + } return (*GImAllocatorFreeFunc)(ptr, GImAllocatorUserData); }