From bde3f6b90d46fa22fb3725e245a49d6a44e402d3 Mon Sep 17 00:00:00 2001 From: ocornut Date: Sun, 1 Feb 2015 14:39:13 +0000 Subject: [PATCH] Removed the dependency on realloc functionality #117 + fixed one alloc in NewFrame() --- imgui.cpp | 9 ++------- imgui.h | 16 +++++++++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 97051529e..8a34b1b88 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -128,6 +128,7 @@ Occasionally introducing changes that are breaking the API. The breakage are generally minor and easy to fix. Here is a change-log of API breaking changes, if you are using one of the functions listed, expect to have to fix some code. + - 2015/02/01 (1.31) - removed IO.MemReallocFn (unused) - 2015/01/19 (1.30) - renamed ImGuiStorage::GetIntPtr()/GetFloatPtr() to GetIntRef()/GetIntRef() because Ptr was conflicting with actual pointer storage functions. - 2015/01/11 (1.30) - big font/image API change! now loads TTF file. allow for multiple fonts. no need for a PNG loader. (1.30) - removed GetDefaultFontData(). uses io.Fonts->GetTextureData*() API to retrieve uncompressed pixels. @@ -537,7 +538,6 @@ ImGuiIO::ImGuiIO() // User functions RenderDrawListsFn = NULL; MemAllocFn = malloc; - MemReallocFn = realloc; MemFreeFn = free; GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations SetClipboardTextFn = SetClipboardTextFn_DefaultImpl; @@ -1484,11 +1484,6 @@ void ImGui::MemFree(void* ptr) { return GImGui.IO.MemFreeFn(ptr); } - -void* ImGui::MemRealloc(void* ptr, size_t sz) -{ - return GImGui.IO.MemReallocFn(ptr, sz); -} static ImGuiIniData* FindWindowSettings(const char* name) { @@ -1754,7 +1749,7 @@ void ImGui::NewFrame() // No window should be open at the beginning of the frame. // But in order to allow the user to call NewFrame() multiple times without calling Render(), we are doing an explicit clear. - g.CurrentWindowStack.clear(); + g.CurrentWindowStack.resize(0); // Create implicit window - we will only render it if the user has added something to it. ImGui::Begin("Debug", NULL, ImVec2(400,400)); diff --git a/imgui.h b/imgui.h index 9ee587db1..892a0f2bf 100644 --- a/imgui.h +++ b/imgui.h @@ -69,10 +69,9 @@ struct ImVec4 namespace ImGui { - // Proxy functions to access the MemAllocFn/MemFreeFn/MemReallocFn pointers in ImGui::GetIO(). The only reason they exist here is to allow ImVector<> to compile inline. + // Proxy functions to access the MemAllocFn/MemFreeFn pointers in ImGui::GetIO(). The only reason they exist here is to allow ImVector<> to compile inline. IMGUI_API void* MemAlloc(size_t sz); IMGUI_API void MemFree(void* ptr); - IMGUI_API void* MemRealloc(void* ptr, size_t sz); } // std::vector<> like class to avoid dragging dependencies (also: windows implementation of STL with debug enabled is absurdly slow, so let's bypass it so our code runs fast in debug). @@ -115,8 +114,16 @@ public: inline const value_type& back() const { IM_ASSERT(Size > 0); return Data[Size-1]; } inline void swap(ImVector& rhs) { const size_t rhs_size = rhs.Size; rhs.Size = Size; Size = rhs_size; const size_t rhs_cap = rhs.Capacity; rhs.Capacity = Capacity; Capacity = rhs_cap; value_type* rhs_data = rhs.Data; rhs.Data = Data; Data = rhs_data; } - inline void reserve(size_t new_capacity) { Data = (value_type*)ImGui::MemRealloc(Data, new_capacity * sizeof(value_type)); Capacity = new_capacity; } inline void resize(size_t new_size) { if (new_size > Capacity) reserve(new_size); Size = new_size; } + inline void reserve(size_t new_capacity) + { + if (new_capacity <= Capacity) return; + T* new_data = (value_type*)ImGui::MemAlloc(new_capacity * sizeof(value_type)); + memcpy(new_data, Data, Size * sizeof(value_type)); + ImGui::MemFree(Data); + Data = new_data; + Capacity = new_capacity; + } inline void push_back(const value_type& v) { if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); Data[Size++] = v; } inline void pop_back() { IM_ASSERT(Size > 0); Size--; } @@ -523,9 +530,8 @@ struct ImGuiIO const char* (*GetClipboardTextFn)(); void (*SetClipboardTextFn)(const char* text); - // Optional: override memory allocations (default to posix malloc/realloc/free). MemFreeFn() may be called with a NULL pointer. + // Optional: override memory allocations (default to posix malloc/free). MemFreeFn() may be called with a NULL pointer. void* (*MemAllocFn)(size_t sz); - void* (*MemReallocFn)(void* ptr, size_t sz); void (*MemFreeFn)(void* ptr); // Optional: notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese inputs in Windows)