diff --git a/imgui.cpp b/imgui.cpp index 24cdb4e42..b7c135cc6 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -168,9 +168,6 @@ #endif #include -#define IMGUI_INTERNAL_USE -#include "immem.h" -#undef IMGUI_INTERNAL_USE // Block sizes for each memory. Don't really know about the optimized values #define DRAWLIST_BLOCK_SIZE 128 @@ -645,10 +642,6 @@ struct ImGuiState ImGuiTextBuffer LogClipboard; int LogAutoExpandMaxDepth; - // Memory Pools - PoolAlloc DrawListPool; - PoolAlloc GuiWindowPool; - ImGuiState() { Initialized = false; @@ -954,7 +947,7 @@ ImGuiWindow::ImGuiWindow(const char* name, ImVec2 default_pos, ImVec2 default_si FocusIdxRequestCurrent = IM_INT_MAX; FocusIdxRequestNext = IM_INT_MAX; - ImDrawList *buff = GImGui.DrawListPool.alloc(); + void *buff = GImGui.IO.MallocFn(sizeof(ImDrawList)); IM_ASSERT(buff); DrawList = new(buff) ImDrawList(); } @@ -962,7 +955,7 @@ ImGuiWindow::ImGuiWindow(const char* name, ImVec2 default_pos, ImVec2 default_si ImGuiWindow::~ImGuiWindow() { DrawList->~ImDrawList(); - GImGui.DrawListPool.free(DrawList); + GImGui.IO.FreeFn(DrawList); DrawList = NULL; StrDup_Free(Name); Name = NULL; @@ -1044,7 +1037,9 @@ static ImGuiIniData* FindWindowSettings(const char* name) if (ImStricmp(ini->Name, name) == 0) return ini; } - ImGuiIniData* ini = new ImGuiIniData(); + + void *buff = GImGui.IO.MallocFn(sizeof(ImGuiIniData)); + ImGuiIniData* ini = new(buff) ImGuiIniData(); ini->Name = StrDup(name); ini->Collapsed = false; ini->Pos = ImVec2(FLT_MAX,FLT_MAX); @@ -1073,12 +1068,12 @@ static void LoadSettings() return; if (fseek(f, 0, SEEK_SET)) return; - char* f_data = new char[f_size+1]; + char* f_data = (char*)g.IO.MallocFn(f_size+1); f_size = (long)fread(f_data, 1, f_size, f); // Text conversion alter read size so let's not be fussy about return value fclose(f); if (f_size == 0) { - delete[] f_data; + g.IO.FreeFn(f_data); return; } f_data[f_size] = 0; @@ -1112,7 +1107,7 @@ static void LoadSettings() line_start = line_end+1; } - delete[] f_data; + g.IO.FreeFn(f_data); } static void SaveSettings() @@ -1188,9 +1183,6 @@ void NewFrame() g.LogClipboard.init(); - g.DrawListPool.create(DRAWLIST_BLOCK_SIZE, g.IO.MallocFn, g.IO.FreeFn); - g.GuiWindowPool.create(GUIWINDOW_BLOCK_SIZE, g.IO.MallocFn, g.IO.FreeFn); - LoadSettings(); if (!g.IO.Font) { @@ -1198,7 +1190,8 @@ void NewFrame() const void* fnt_data; unsigned int fnt_size; ImGui::GetDefaultFontData(&fnt_data, &fnt_size, NULL, NULL); - g.IO.Font = new ImBitmapFont(); + void *buff = g.IO.MallocFn(sizeof(ImBitmapFont)); + g.IO.Font = new(buff) ImBitmapFont(); g.IO.Font->LoadFromMemory(fnt_data, fnt_size); g.IO.FontHeight = g.IO.Font->GetFontSize(); } @@ -1319,15 +1312,17 @@ void Shutdown() for (size_t i = 0; i < g.Windows.size(); i++) { g.Windows[i]->~ImGuiWindow(); - g.GuiWindowPool.free(g.Windows[i]); + g.IO.FreeFn(g.Windows[i]); } g.Windows.clear(); g.CurrentWindowStack.clear(); g.FocusedWindow = NULL; g.HoveredWindow = NULL; g.HoveredWindowExcludingChilds = NULL; - for (size_t i = 0; i < g.Settings.size(); i++) - delete g.Settings[i]; + for (size_t i = 0; i < g.Settings.size(); i++) { + g.Settings[i]->~ImGuiIniData(); + g.IO.FreeFn(g.Settings[i]); + } g.Settings.clear(); g.RenderDrawLists.clear(); g.ColorEditModeStorage.Clear(); @@ -1338,7 +1333,8 @@ void Shutdown() } if (g.IO.Font) { - delete g.IO.Font; + g.IO.Font->~ImBitmapFont(); + g.IO.FreeFn(g.IO.Font); g.IO.Font = NULL; } @@ -1348,9 +1344,6 @@ void Shutdown() g.PrivateClipboard = NULL; } - g.DrawListPool.destroy(); - g.GuiWindowPool.destroy(); - g.LogClipboard.destroy(); g.Initialized = false; @@ -1831,7 +1824,7 @@ bool Begin(const char* name, bool* open, ImVec2 size, float fill_alpha, ImGuiWin ImGuiWindow* window = FindWindow(name); if (!window) { - ImGuiWindow *buff = GImGui.GuiWindowPool.alloc(); + void *buff = g.IO.MallocFn(sizeof(ImGuiWindow)); IM_ASSERT(buff); if (flags & (ImGuiWindowFlags_ChildWindow | ImGuiWindowFlags_Tooltip)) diff --git a/immem.h b/immem.h deleted file mode 100644 index d37785c6c..000000000 --- a/immem.h +++ /dev/null @@ -1,210 +0,0 @@ -/*********************************************************************************** - * Author: Sepehr Taghdisian (sep.tagh@gmail.com) - */ - -#ifndef IMMEM_H -#define IMMEM_H - -// Note: This file is meant to included only inside imgui.cpp - -#ifndef IMGUI_INTERNAL_USE - #error "This file is intented for internal use only (as include)" -#endif - -/* Linked-List ************************************************************************************/ -struct LinkedList -{ - LinkedList *next; - LinkedList *prev; - void *data; - - LinkedList() : next(NULL), prev(NULL) {} -}; - - -static void list_add(struct LinkedList** plist, struct LinkedList* item, void* data) -{ - item->next = (*plist); - item->prev = NULL; - if (*plist != NULL) - (*plist)->prev = item; - *plist = item; - item->data = data; -} - -static void list_addlast(struct LinkedList** plist, struct LinkedList* item, void* data) -{ - if (*plist != NULL) { - struct LinkedList* last = *plist; - while (last->next != NULL) last = last->next; - last->next = item; - item->prev = last; - item->next = NULL; - } else { - *plist = item; - item->prev = item->next = NULL; - } - - item->data = data; -} - -static void list_remove(struct LinkedList** plist, struct LinkedList* item) -{ - if (item->next != NULL) item->next->prev = item->prev; - if (item->prev != NULL) item->prev->next = item->next; - if (*plist == item) *plist = item->next; - item->next = item->prev = NULL; -} - -/* PoolAlloc **************************************************************************************/ -template -class PoolAlloc -{ -private: - LinkedList *m_blocks; /* first node of m_blocks */ - int m_block_cnt; - int m_items_max; /* maximum number of items allowed (per block) */ - ImGui_MallocCallback m_malloc; - ImGui_FreeCallback m_free; - -private: - struct Block - { - LinkedList node; /* linked-list node */ - unsigned char *buffer; /* memory buffer that holds all objects */ - void **ptrs; /* pointer references to the buffer */ - int iter; /* iterator for current buffer position */ - }; - -private: - Block* create_block(int block_size) - { - // Allocate in one call - size_t total_sz = - sizeof(Block) + - sizeof(T)*block_size + - sizeof(void*)*block_size; - unsigned char *buff = (unsigned char*)m_malloc(total_sz); - if (buff == NULL) - return NULL; - memset(buff, 0x00, total_sz); - - Block *block = (Block*)buff; - buff += sizeof(Block); - block->buffer = buff; - buff += sizeof(T)*block_size; - block->ptrs = (void**)buff; - - // Assign pointer refs - for (int i = 0; i < block_size; i++) - block->ptrs[block_size-i-1] = block->buffer + i*sizeof(T); - block->iter = block_size; - - /* add to linked-list of the pool */ - list_addlast(&m_blocks, &block->node, block); - m_block_cnt++; - return block; - } - - void destroy_block(Block *block) - { - list_remove(&m_blocks, &block->node); - m_free(block); - m_block_cnt--; - } - -public: - PoolAlloc() - { - m_blocks = NULL; - m_block_cnt = 0; - m_items_max = 0; - m_malloc = NULL; - m_free = NULL; - } - - bool create(int block_sz, ImGui_MallocCallback malloc_fn, ImGui_FreeCallback free_fn) - { - m_items_max = block_sz; - m_malloc = malloc_fn; - m_free = free_fn; - - // First block - Block *block = create_block(block_sz); - if (block == NULL) { - destroy(); - return false; - } - - return true; - } - - void destroy() - { - LinkedList* node = m_blocks; - while (node != NULL) { - LinkedList* next = node->next; - destroy_block((Block*)node->data); - node = next; - } - } - - T* alloc() - { - LinkedList* node = m_blocks; - - while (node != NULL) { - Block *block = (Block*)node->data; - if (block->iter > 0) - return (T*)block->ptrs[--block->iter]; - - node = node->next; - } - - /* couldn't find a free block, create a new one */ - Block *block = create_block(m_items_max); - if (block == NULL) - return NULL; - - return (T*)block->ptrs[--block->iter]; - } - - void free(T *ptr) - { - // find the block that pointer belongs to, and free the pointer from that block - LinkedList *node = m_blocks; - int buffer_sz = m_items_max*sizeof(T); - unsigned char *u8ptr = (unsigned char*)ptr; - - while (node != NULL) { - Block *block = (Block*)node->data; - if (u8ptr >= block->buffer && u8ptr < (block->buffer + buffer_sz)) { - IM_ASSERT(block->iter != m_items_max); - block->ptrs[block->iter++] = ptr; - return; - } - node = node->next; - } - - // Memory block does not belong to the pool?! - IM_ASSERT(0); - } - - void clear() - { - int block_size = m_items_max; - LinkedList* node = m_blocks; - while (node != NULL) { - Block *block = (Block*)node->data; - - /* only re-assign pointer references to buffer */ - for (int i = 0; i < block_size; i++) - block->ptrs[block_size-i-1] = block->buffer + i*sizeof(T); - block->iter = block_size; - - node = node->next; - } - } -}; - -#endif // IMMEM_H diff --git a/libimgui.pro b/libimgui.pro index bc8579d5b..c153b4e38 100644 --- a/libimgui.pro +++ b/libimgui.pro @@ -17,5 +17,4 @@ SOURCES += \ HEADERS += \ imgui.h \ - imconfig.h \ - immem.h + imconfig.h