Skip to content

Commit d003674

Browse files
committed
Internals: Added ImChunkStream, used by window settings. (more generic followup to 4c13807, the class will be used more extensively by Tables)
1 parent a337e21 commit d003674

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

imgui.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,7 +2525,7 @@ ImGuiWindow::ImGuiWindow(ImGuiContext* context, const char* name)
25252525
LastTimeActive = -1.0f;
25262526
ItemWidthDefault = 0.0f;
25272527
FontWindowScale = 1.0f;
2528-
SettingsIdx = -1;
2528+
SettingsOffset = -1;
25292529

25302530
DrawList = &DrawListInst;
25312531
DrawList->_OwnerName = Name;
@@ -3818,7 +3818,6 @@ void ImGui::Shutdown(ImGuiContext* context)
38183818
g.PrivateClipboard.clear();
38193819
g.InputTextState.ClearFreeMemory();
38203820

3821-
g.SettingsWindowsNames.clear();
38223821
g.SettingsWindows.clear();
38233822
g.SettingsHandlers.clear();
38243823

@@ -4704,7 +4703,7 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImVec2 size, ImGuiWindowFl
47044703
if (ImGuiWindowSettings* settings = ImGui::FindWindowSettings(window->ID))
47054704
{
47064705
// Retrieve settings from .ini file
4707-
window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings);
4706+
window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings);
47084707
SetWindowConditionAllowFlags(window, ImGuiCond_FirstUseEver, false);
47094708
window->Pos = ImVec2(settings->Pos.x, settings->Pos.y);
47104709
window->Collapsed = settings->Collapsed;
@@ -9205,27 +9204,31 @@ void ImGui::MarkIniSettingsDirty(ImGuiWindow* window)
92059204
ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name)
92069205
{
92079206
ImGuiContext& g = *GImGui;
9208-
g.SettingsWindows.push_back(ImGuiWindowSettings());
9209-
ImGuiWindowSettings* settings = &g.SettingsWindows.back();
9207+
92109208
#if !IMGUI_DEBUG_INI_SETTINGS
92119209
// Skip to the "###" marker if any. We don't skip past to match the behavior of GetID()
92129210
// Preserve the full string when IMGUI_DEBUG_INI_SETTINGS is set to make .ini inspection easier.
92139211
if (const char* p = strstr(name, "###"))
92149212
name = p;
92159213
#endif
9216-
size_t name_len = strlen(name);
9217-
settings->NameOffset = g.SettingsWindowsNames.size();
9218-
g.SettingsWindowsNames.append(name, name + name_len + 1); // Append with zero terminator
9214+
const size_t name_len = strlen(name);
9215+
9216+
// Allocate chunk
9217+
const size_t chunk_size = sizeof(ImGuiWindowSettings) + name_len + 1;
9218+
ImGuiWindowSettings* settings = g.SettingsWindows.alloc_chunk(chunk_size);
9219+
IM_PLACEMENT_NEW(settings) ImGuiWindowSettings();
92199220
settings->ID = ImHashStr(name, name_len);
9221+
memcpy(settings->GetName(), name, name_len + 1); // Store with zero terminator
9222+
92209223
return settings;
92219224
}
92229225

92239226
ImGuiWindowSettings* ImGui::FindWindowSettings(ImGuiID id)
92249227
{
92259228
ImGuiContext& g = *GImGui;
9226-
for (int i = 0; i != g.SettingsWindows.Size; i++)
9227-
if (g.SettingsWindows[i].ID == id)
9228-
return &g.SettingsWindows[i];
9229+
for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings))
9230+
if (settings->ID == id)
9231+
return settings;
92299232
return NULL;
92309233
}
92319234

@@ -9380,11 +9383,11 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl
93809383
if (window->Flags & ImGuiWindowFlags_NoSavedSettings)
93819384
continue;
93829385

9383-
ImGuiWindowSettings* settings = (window->SettingsIdx != -1) ? &g.SettingsWindows[window->SettingsIdx] : ImGui::FindWindowSettings(window->ID);
9386+
ImGuiWindowSettings* settings = (window->SettingsOffset != -1) ? g.SettingsWindows.ptr_from_offset(window->SettingsOffset) : ImGui::FindWindowSettings(window->ID);
93849387
if (!settings)
93859388
{
93869389
settings = ImGui::CreateNewWindowSettings(window->Name);
9387-
window->SettingsIdx = g.SettingsWindows.index_from_ptr(settings);
9390+
window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings);
93889391
}
93899392
IM_ASSERT(settings->ID == window->ID);
93909393
settings->Pos = ImVec2ih((short)window->Pos.x, (short)window->Pos.y);
@@ -9393,11 +9396,10 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl
93939396
}
93949397

93959398
// Write to text buffer
9396-
buf->reserve(buf->size() + g.SettingsWindows.Size * 96); // ballpark reserve
9397-
for (int i = 0; i != g.SettingsWindows.Size; i++)
9399+
buf->reserve(buf->size() + g.SettingsWindows.size() * 6); // ballpark reserve
9400+
for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings))
93989401
{
9399-
const ImGuiWindowSettings* settings = &g.SettingsWindows[i];
9400-
const char* settings_name = g.SettingsWindowsNames.c_str() + settings->NameOffset;
9402+
const char* settings_name = settings->GetName();
94019403
buf->appendf("[%s][%s]\n", handler->TypeName, settings_name);
94029404
buf->appendf("Pos=%d,%d\n", settings->Pos.x, settings->Pos.y);
94039405
buf->appendf("Size=%d,%d\n", settings->Size.x, settings->Size.y);

imgui_internal.h

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ struct ImGuiTabItem; // Storage for a tab item (within a tab bar)
8888
struct ImGuiWindow; // Storage for one window
8989
struct ImGuiWindowTempData; // Temporary storage for one window (that's the data which in theory we could ditch at the end of the frame)
9090
struct ImGuiWindowSettings; // Storage for a window .ini settings (we keep one of those even if the actual window wasn't instanced during this session)
91-
template<typename T> struct ImPool; // Basic keyed storage for contiguous instances, slow/amortized insertion, O(1) indexable, O(Log N) queries by ID
9291

9392
// Use your programming IDE "Go to definition" facility on the names of the center columns to find the actual flags/enum lists.
9493
typedef int ImGuiLayoutType; // -> enum ImGuiLayoutType_ // Enum: Horizontal or vertical
@@ -145,6 +144,7 @@ extern IMGUI_API ImGuiContext* GImGui; // Current implicit context pointer
145144
// - Helpers: Maths
146145
// - Helper: ImBoolVector
147146
// - Helper: ImPool<>
147+
// - Helper: ImChunkStream<>
148148
//-----------------------------------------------------------------------------
149149

150150
// Macros
@@ -326,6 +326,28 @@ struct IMGUI_API ImPool
326326
int GetSize() const { return Buf.Size; }
327327
};
328328

329+
// Helper: ImChunkStream<>
330+
// Build and iterate a contiguous stream of variable-sized structures.
331+
// This is used by Settings to store persistent data while reducing allocation count.
332+
// We store the chunk size first, and align the final size on 4 bytes boundaries (this what the '(X + 3) & ~3' statement is for)
333+
// The tedious/zealous amount of casting is to avoid -Wcast-align warnings.
334+
template<typename T>
335+
struct IMGUI_API ImChunkStream
336+
{
337+
ImVector<char> Buf;
338+
339+
void clear() { Buf.clear(); }
340+
bool empty() const { return Buf.Size == 0; }
341+
int size() const { return Buf.Size; }
342+
T* alloc_chunk(size_t sz) { size_t HDR_SZ = 4; sz = ((HDR_SZ + sz) + 3u) & ~3u; int off = Buf.Size; Buf.resize(off + (int)sz); ((int*)(void*)(Buf.Data + off))[0] = (int)sz; return (T*)(void*)(Buf.Data + off + (int)HDR_SZ); }
343+
T* begin() { size_t HDR_SZ = 4; if (!Buf.Data) return NULL; return (T*)(void*)(Buf.Data + HDR_SZ); }
344+
T* next_chunk(T* p) { size_t HDR_SZ = 4; IM_ASSERT(p >= begin() && p < end()); p = (T*)(void*)((char*)(void*)p + chunk_size(p)); if (p == (T*)(void*)((char*)end() + HDR_SZ)) return (T*)0; IM_ASSERT(p < end()); return p; }
345+
int chunk_size(const T* p) { return ((const int*)p)[-1]; }
346+
T* end() { return (T*)(void*)(Buf.Data + Buf.Size); }
347+
int offset_from_ptr(const T* p) { IM_ASSERT(p >= begin() && p < end()); const ptrdiff_t off = (const char*)p - Buf.Data; return (int)off; }
348+
T* ptr_from_offset(int off) { IM_ASSERT(off >= 4 && off < Buf.Size); return (T*)(void*)(Buf.Data + off); }
349+
};
350+
329351
//-----------------------------------------------------------------------------
330352
// Misc data structures
331353
//-----------------------------------------------------------------------------
@@ -681,15 +703,16 @@ struct IMGUI_API ImGuiInputTextState
681703

682704
// Windows data saved in imgui.ini file
683705
// Because we never destroy or rename ImGuiWindowSettings, we can store the names in a separate buffer easily.
706+
// (this is designed to be stored in a ImChunkStream buffer, with the variable-length Name following our structure)
684707
struct ImGuiWindowSettings
685708
{
686-
int NameOffset; // Offset into SettingsWindowNames[]
687709
ImGuiID ID;
688710
ImVec2ih Pos;
689711
ImVec2ih Size;
690712
bool Collapsed;
691713

692-
ImGuiWindowSettings() { NameOffset = -1; ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = false; }
714+
ImGuiWindowSettings() { ID = 0; Pos = Size = ImVec2ih(0, 0); Collapsed = false; }
715+
char* GetName() { return (char*)(this + 1); }
693716
};
694717

695718
struct ImGuiSettingsHandler
@@ -1053,12 +1076,11 @@ struct ImGuiContext
10531076
ImVec2 PlatformImeLastPos;
10541077

10551078
// Settings
1056-
bool SettingsLoaded;
1057-
float SettingsDirtyTimer; // Save .ini Settings to memory when time reaches zero
1058-
ImGuiTextBuffer SettingsIniData; // In memory .ini settings
1059-
ImVector<ImGuiSettingsHandler> SettingsHandlers; // List of .ini settings handlers
1060-
ImVector<ImGuiWindowSettings> SettingsWindows; // ImGuiWindow .ini settings entries (parsed from the last loaded .ini file and maintained on saving)
1061-
ImGuiTextBuffer SettingsWindowsNames; // Names for SettingsWindows
1079+
bool SettingsLoaded;
1080+
float SettingsDirtyTimer; // Save .ini Settings to memory when time reaches zero
1081+
ImGuiTextBuffer SettingsIniData; // In memory .ini settings
1082+
ImVector<ImGuiSettingsHandler> SettingsHandlers; // List of .ini settings handlers
1083+
ImChunkStream<ImGuiWindowSettings> SettingsWindows; // ImGuiWindow .ini settings entries
10621084

10631085
// Logging
10641086
bool LogEnabled;
@@ -1369,7 +1391,7 @@ struct IMGUI_API ImGuiWindow
13691391
ImGuiStorage StateStorage;
13701392
ImVector<ImGuiColumns> ColumnsStorage;
13711393
float FontWindowScale; // User scale multiplier per-window, via SetWindowFontScale()
1372-
int SettingsIdx; // Index into SettingsWindow[] (indices are always valid as we only grow the array from the back)
1394+
int SettingsOffset; // Offset into SettingsWindows[] (offsets are always valid as we only grow the array from the back)
13731395

13741396
ImDrawList* DrawList; // == &DrawListInst (for backward compatibility reason with code using imgui_internal.h we keep this a pointer)
13751397
ImDrawList DrawListInst;

0 commit comments

Comments
 (0)