Fixing array resize bugs

This commit is contained in:
Krzosa Karol
2024-08-08 06:47:29 +02:00
parent b3b50715b4
commit bb44eab406
4 changed files with 66 additions and 50 deletions

View File

@@ -359,7 +359,7 @@ Slice<T> GetSlice(Slice<T> &arr, int64_t first_index = 0, int64_t one_past_last_
}
// Make arrays resize on every item
#define ARRAY_DEBUG 1
#define ARRAY_DEBUG 0
#if ARRAY_DEBUG
#define ARRAY_IF_DEBUG_ELSE(IF, ELSE) IF
#else
@@ -416,7 +416,8 @@ void Reserve(Array<T> *arr, int64_t size) {
template <class T>
void TryGrowing(Array<T> *arr) {
if (arr->len + 1 > arr->cap) {
int64_t new_size = ClampBottom((int64_t)16, arr->cap ARRAY_IF_DEBUG_ELSE(+1, *2));
int64_t initial_size = (int64_t)ARRAY_IF_DEBUG_ELSE(1, 16);
int64_t new_size = ClampBottom(initial_size, arr->cap ARRAY_IF_DEBUG_ELSE(+1, *2));
Reserve(arr, new_size);
}
}
@@ -424,7 +425,8 @@ void TryGrowing(Array<T> *arr) {
template <class T>
void TryGrowing(Array<T> *arr, int64_t item_count) {
if (arr->len + item_count > arr->cap) {
int64_t new_size = ClampBottom((int64_t)16, (arr->cap + item_count) ARRAY_IF_DEBUG_ELSE(+1, *2));
int64_t initial_size = (int64_t)ARRAY_IF_DEBUG_ELSE(1, 16);
int64_t new_size = ClampBottom(initial_size, (arr->cap + item_count) ARRAY_IF_DEBUG_ELSE(+1, *2));
Reserve(arr, new_size);
}
}

View File

@@ -80,9 +80,8 @@ void ToggleFullscreen() {
}
void ToggleConsole() {
Window *window = GetWindow(ConsoleWindowID);
if (ToggleVisibility(window)) {
SetActiveWindow(window->id);
if (ToggleVisibility(ConsoleWindowID)) {
SetActiveWindow(ConsoleWindowID);
} else {
SetActiveWindow(GetLastActiveWindow());
}
@@ -392,8 +391,7 @@ bool GlobalCommand(Event event) {
}
if (Ctrl(SDLK_0)) {
Window *window = GetWindow(DebugWindowID);
ToggleVisibility(window);
ToggleVisibility(DebugWindowID);
}
if (Press(SDLK_F5)) {
@@ -512,7 +510,6 @@ void ReportWarningf(const char *fmt, ...) {
STRING_FORMAT(scratch, fmt, string);
String16 string16 = ToString16(scratch, string);
AppendToConsole(string16);
Window *window = GetWindow(ConsoleWindowID);
SetVisibility(window, true);
SetActiveWindow(window->id);
SetVisibility(ConsoleWindowID, true);
SetActiveWindow(ConsoleWindowID);
}

View File

@@ -176,9 +176,9 @@ void Update(Event event) {
bool title_bar_is_active = ActiveWindow == window->title_bar_window;
bool is_active = IsActive(window);
if (is_active || title_bar_is_active) {
SetVisibility(window, true);
SetVisibility(window->id, true);
} else {
SetVisibility(window, false);
SetVisibility(window->id, false);
}
}
if (!window->visible) continue;

View File

@@ -95,7 +95,8 @@ void AddRowWindow() {
Insert(&Windows, window_copy, active_window_index + 1);
}
void SetVisibility(Window *window, bool v) {
void SetVisibility(WindowID window_id, bool v) {
Window *window = GetWindow(window_id);
window->visible = v;
if (window->title_bar_window.id != 0) {
@@ -104,9 +105,10 @@ void SetVisibility(Window *window, bool v) {
}
}
bool ToggleVisibility(Window *window) {
bool visible = !window->visible;
SetVisibility(window, visible);
bool ToggleVisibility(WindowID window_id) {
Window *window = GetWindow(window_id);
bool visible = !window->visible;
SetVisibility(window_id, visible);
return visible;
}
@@ -123,10 +125,16 @@ void InitScratchBuffer() {
void InitWindows() {
Allocator sys_allocator = Perm;
#if !ARRAY_DEBUG
Reserve(&Windows, 64);
Reserve(&Buffers, 256);
Reserve(&Views, 256);
#endif
{
Window *window = CreateWindow();
window->is_column = true;
Window *window = CreateWindow();
WindowID window_id = window->id;
window->is_column = true;
// window->draw_line_numbers = false;
Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*load_text_a*"));
View *view = CreateView(buffer->id);
@@ -134,48 +142,56 @@ void InitWindows() {
LoadUnicode(buffer);
// LoadBigTextAndBigLine(buffer, 10000000);
window->active_view = view->id;
CreateTitlebar(window->id);
CreateTitlebar(window_id);
}
{
Window *window = CreateWindow();
Window *window = CreateWindow();
WindowID window_id = window->id;
ConsoleWindowID = window_id;
window->absolute_position = true;
window->dont_save_in_active_window_history = true;
Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*console*"));
Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*console*"));
ConsoleBufferID = buffer->id;
// buffer->no_history = true;
View *view = CreateView(buffer->id);
window->active_view = view->id;
CreateTitlebar(window->id);
SetVisibility(window, false);
ConsoleWindowID = window->id;
ConsoleBufferID = buffer->id;
CreateTitlebar(window_id);
SetVisibility(window_id, false);
}
{
Window *window = CreateWindow();
Window *window = CreateWindow();
WindowID window_id = window->id;
DebugWindowID = window->id;
window->draw_line_numbers = false;
Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*debug*"));
window->absolute_position = true;
window->draw_line_numbers = false;
window->draw_scrollbar = false;
window->dont_save_in_active_window_history = true;
window->visible = false;
buffer->no_history = true;
View *view = CreateView(buffer->id);
window->z = 2;
window->active_view = view->id;
Window *titlebar = CreateTitlebar(window->id);
titlebar->z = 2;
SetVisibility(window, false);
DebugWindowID = window->id;
DebugBufferID = buffer->id;
Buffer *buffer = CreateBuffer(sys_allocator, BuffCWD("*debug*"));
DebugBufferID = buffer->id;
buffer->no_history = true;
View *view = CreateView(buffer->id);
window->active_view = view->id;
Window *titlebar = CreateTitlebar(window_id);
titlebar->z = 2;
SetVisibility(window_id, false);
}
{
Window *w = CreateWindow();
Window *w = CreateWindow();
WindowID window_id = w->id;
w->draw_scrollbar = false;
w->draw_line_numbers = false;
w->visible = false;
@@ -190,15 +206,16 @@ void InitWindows() {
w->active_view = v->id;
w->z = 1;
Window *titlebar = CreateTitlebar(w->id);
Window *titlebar = CreateTitlebar(window_id);
titlebar->z = 1;
SetVisibility(w, false);
SetVisibility(window_id, false);
CommandWindowID = w->id;
CommandWindowID = window_id;
}
{
Window *w = CreateWindow();
Window *w = CreateWindow();
WindowID window_id = w->id;
w->draw_scrollbar = false;
w->draw_line_numbers = false;
w->visible = false;
@@ -210,15 +227,17 @@ void InitWindows() {
View *v = CreateView(b->id);
w->active_view = v->id;
CreateTitlebar(w->id);
SetVisibility(w, false);
CreateTitlebar(window_id);
SetVisibility(window_id, false);
SearchBufferID = b->id;
SearchWindowID = w->id;
SearchWindowID = window_id;
}
{
Window *w = CreateWindow();
Window *w = CreateWindow();
WindowID window_id = w->id;
PopupWindowID = window_id;
w->draw_scrollbar = false;
w->draw_line_numbers = false;
w->visible = false;
@@ -232,11 +251,9 @@ void InitWindows() {
View *v = CreateView(b->id);
w->active_view = v->id;
Window *infobar = CreateTitlebar(w->id);
Window *infobar = CreateTitlebar(window_id);
infobar->z = 2;
SetVisibility(w, false);
PopupWindowID = w->id;
SetVisibility(window_id, false);
}
SetActiveWindow({0});