Bring back GC and new files for commands

This commit is contained in:
Krzosa Karol
2025-05-05 16:49:28 +02:00
parent 2c29df0171
commit 20422d9023
8 changed files with 103 additions and 93 deletions

View File

@@ -178,6 +178,14 @@ void Dealloc(Allocator alo, T **p) {
alo.proc(alo.object, AllocatorKind_Deallocate, *p, 0);
*p = NULL;
}
#define DeallocEx(alo, p) (alo).proc((alo).object, AllocatorKind_Deallocate, (p), 0);
template <class T>
void Dealloc2(Allocator alo, T *p) {
if (*p == NULL) return;
alo.proc(alo.object, AllocatorKind_Deallocate, *p, 0);
*p = NULL;
}
Allocator GetSystemAllocator();
#define MemoryZero(x, size) memset(x, 0, size)

View File

@@ -38,8 +38,8 @@ struct Buffer {
int no_history : 1;
int no_line_starts : 1;
int dirty : 1;
int gc : 1;
int changed_on_disk : 1;
int garbage : 1;
};
};

View File

@@ -598,7 +598,7 @@ void SaveBuffer(View *view) {
if (success) {
buffer->file_mod_time = GetFileModTime(buffer->name);
buffer->dirty = false;
buffer->gc = false;
buffer->garbage = false;
} else {
ReportWarningf("Failed to save file with name: %.*s", FmtString(buffer->name));
}

View File

@@ -16,26 +16,26 @@ String FieldString(lua_State *L, String name) {
return result;
}
void Command_JumpNew(BSet *set) {
CheckpointBeforeGoto(set->window);
String current_dir = ChopLastSlash(set->buffer->name);
String buffer_name = GetUniqueBufferName(current_dir, "temp");
set->view = WindowOpenBufferView(set->window, buffer_name);
set->buffer = GetBuffer(set->view->active_buffer);
set->buffer->garbage = true;
}
void Command_ExecInNewBuffer(BSet set, String cmd, String working_dir) {
CheckpointBeforeGoto(set.window);
Scratch scratch;
String buffer_name = GetUniqueBufferName(working_dir, "shell_command");
View *view = WindowOpenBufferView(set.window, buffer_name);
Buffer *buffer = GetBuffer(view->active_buffer);
buffer->gc = true;
Command_SelectRangeOneCursor(view, Rng(0));
Exec(view->id, true, cmd, working_dir);
BSet main = GetActiveMainSet();
Command_JumpNew(&main);
Exec(main.view->id, true, cmd, working_dir);
ActiveWindow = set.window->id;
}
View *Command_ExecHidden(String buffer_name, String cmd, String working_dir) {
View *view = OpenBufferView(buffer_name);
Buffer *buffer = GetBuffer(view->active_buffer);
buffer->gc = true;
Command_SelectRangeOneCursor(view, Rng(0));
// buffer->garbage = true;
Exec(view->id, true, cmd, working_dir);
return view;
}
@@ -46,14 +46,6 @@ void Command_BeginJump(BSet *set, BufferID buffer_id = NullBufferID) {
set->view = WindowOpenBufferView(set->window, set->buffer->name);
}
void Command_BeginJumpCommandBuffer(BSet *set) {
String current_dir = Command_GetDir();
Command_BeginJump(set, set->window->command_buffer);
set->buffer->name = GetUniqueBufferName(current_dir, WindowBufferName);
Command_SelectEntireBuffer(set->view);
Command_Replace(set->view, u"");
}
void Command_EndJump(BSet set) {
Int pos = XYToPos(set.buffer, {0, set.buffer->line_starts.len - 1});
set.view->carets[0] = MakeCaret(pos);
@@ -62,20 +54,18 @@ void Command_EndJump(BSet set) {
void Command_ListBuffers() {
BSet main = GetActiveMainSet();
Command_BeginJumpCommandBuffer(&main);
ActiveWindow = main.window->id;
Command_JumpNew(&main);
for (Buffer *it = FirstBuffer; it; it = it->next) {
Command_Appendf(main.view, "%.*s\n", FmtString(it->name));
}
Command_EndJump(main);
ActiveWindow = main.window->id;
}
BSet Command_Exec(String cmd, String working_dir) {
BSet set = GetActiveMainSet();
Command_BeginJumpCommandBuffer(&set);
Exec(set.view->id, true, cmd, working_dir);
Command_EndJump(set);
ActiveWindow = set.window->id;
Command_JumpNew(&set);
Exec(set.view->id, true, cmd, working_dir);
return set;
}
@@ -100,14 +90,13 @@ BSet Command_Open(String path) {
String col_string = FieldString(LuaState, "col");
Int col = strtoll(col_string.data, NULL, 10);
ActiveWindow = main.window->id;
if (IsDir(file_path)) {
Command_BeginJumpCommandBuffer(&main);
Command_JumpNew(&main);
Command_Appendf(main.view, "%.*s/..\n", FmtString(file_path));
for (FileIter it = IterateFiles(scratch, file_path); IsValid(it); Advance(&it)) {
Command_Appendf(main.view, "%.*s\n", FmtString(it.absolute_path));
}
Command_EndJump(main);
ActiveWindow = main.window->id;
} else {
CheckpointBeforeGoto(main.window);
View *view = WindowOpenBufferView(main.window, file_path);
@@ -118,7 +107,6 @@ BSet Command_Open(String path) {
}
}
UpdateScroll(main.window, true);
ActiveWindow = main.window->id;
} else if (FieldString(LuaState, "kind") == "exec") {
String cmd = FieldString(LuaState, "cmd");
String working_dir = FieldString(LuaState, "working_dir");
@@ -169,7 +157,7 @@ int Lua_Eval(lua_State *L) {
return 0;
}
int Lua_NewWindowCMD(lua_State *L) {
int Lua_NewBufferCMD(lua_State *L) {
String string = lua_tostring(L, 1);
lua_pop(L, 1);
@@ -204,9 +192,8 @@ int Lua_Cmd(lua_State *L) {
Command_EndJump(set);
} else {
BSet set = GetActiveMainSet();
Command_BeginJumpCommandBuffer(&set);
Command_JumpNew(&set);
Exec(set.view->id, true, cmd, working_dir);
Command_EndJump(set);
ActiveWindow = set.window->id;
}

View File

@@ -1,7 +1,7 @@
luaL_Reg LuaFunctions[] = {
{"C", Lua_C},
{"Eval", Lua_Eval},
{"NewWindowCMD", Lua_NewWindowCMD},
{"NewBufferCMD", Lua_NewBufferCMD},
{"Cmd", Lua_Cmd},
{"Print", Lua_Print},
{"Kill", Lua_Kill},

View File

@@ -130,6 +130,14 @@ void InitBuffer(Allocator allocator, Buffer *buffer, String name, Int size = 409
if (!buffer->no_line_starts) Add(&buffer->line_starts, (Int)0);
}
void DeallocBuffer(Buffer *buffer) {
Dealloc(&buffer->line_starts);
DeallocHistoryArray(&buffer->undo_stack);
DeallocHistoryArray(&buffer->redo_stack);
DeallocEx(buffer->line_starts.allocator, buffer->data);
DeallocEx(buffer->line_starts.allocator, buffer);
}
Buffer *CreateBuffer(Allocator allocator, String name, Int size) {
Buffer *result = AllocType(allocator, Buffer);
InitBuffer(allocator, result, name, size);
@@ -153,9 +161,6 @@ Window *CreateWindow(bool create_command_buffer = true) {
w->draw_scrollbar = StyleDrawScrollbar;
w->draw_line_numbers = StyleDrawLineNumbers;
w->id = AllocWindowID(w);
if (create_command_buffer) {
w->command_buffer = CreateBuffer(GetSystemAllocator(), GetUniqueBufferName(WorkingDir, WindowBufferName), 4096*2)->id;
}
DLL_QUEUE_ADD(FirstWindow, LastWindow, w);
WindowCount += 1;
return w;
@@ -428,47 +433,46 @@ View *WindowOpenBufferView(Window *new_parent_window, String name) {
return result;
}
Window *ViewIsReferenced(ViewID view) {
bool ViewIsReferenced(ViewID view) {
if (view == NullViewID) {
return true;
}
for (Window *it = FirstWindow; it; it = it->next) {
if (it->active_view == view || it->active_goto_list == view) return it;
if (it->active_view == view || it->active_goto_list == view) {
return true;
}
}
return NULL;
return false;
}
View *BufferIsReferenced(BufferID buffer_id) {
return FindView(buffer_id);
}
Window *BufferIsCrumb(BufferID buffer_id) {
bool BufferIsCrumb(BufferID buffer_id) {
for (Window *window = FirstWindow; window; window = window->next) {
For(window->goto_history) if (it.buffer_id == buffer_id) return window;
For(window->goto_redo) if (it.buffer_id == buffer_id) return window;
For(window->goto_history) if (it.buffer_id == buffer_id) return true;
For(window->goto_redo) if (it.buffer_id == buffer_id) return true;
}
return NULL;
return false;
}
bool BufferIsReferenced(BufferID buffer_id) {
if (buffer_id == NullBufferID) {
return true;
}
View *view = FindView(buffer_id);
if (view) {
return true;
}
if (BufferIsCrumb(buffer_id)) {
return true;
}
return false;
}
void GarbageCollect() {
Allocator sys_allocator = GetSystemAllocator();
// IterRemove(Views) {
// IterRemovePrepare(Views);
// View *view = it.o;
// Buffer *buffer = GetBuffer(view->active_buffer);
// bool gc = buffer->gc;
// if (buffer->is_directory) {
// Window *ref = BufferIsCrumb(buffer->id);
// if (!ref) gc = true;
// }
// Window *ref = ViewIsReferenced(view->id);
// if (gc && !ref) {
// Dealloc(&view->carets);
// remove_item = true;
// RawAppendf(GCInfoBuffer, "view %.*s\n", FmtString(buffer->name));
// Dealloc(sys_allocator, &view);
// }
// }
for (Buffer *it = FirstBuffer; it; it = it->next) {
if (it->file_mod_time) {
int64_t new_file_mod_time = GetFileModTime(it->name);
@@ -477,26 +481,38 @@ void GarbageCollect() {
}
}
}
// IterRemove(Buffers) {
// IterRemovePrepare(Buffers);
// Buffer *buffer = it.o;
// bool gc = buffer->gc;
// View *ref = BufferIsReferenced(buffer->id);
// if (gc && !ref) {
// Dealloc(&buffer->line_starts);
// DeallocHistoryArray(&buffer->undo_stack);
// DeallocHistoryArray(&buffer->redo_stack);
// Dealloc(buffer->line_starts.allocator, &buffer->data);
// remove_item = true;
for (View *it = FirstView, *next = NULL; it; it = next) {
next = it->next;
// RawAppendf(GCInfoBuffer, "buffer %.*s\n", FmtString(buffer->name));
// Dealloc(sys_allocator, &buffer);
// } else if (buffer->file_mod_time) {
// int64_t new_file_mod_time = GetFileModTime(buffer->name);
// if (buffer->file_mod_time != new_file_mod_time) {
// buffer->changed_on_disk = true;
// }
// }
// }
Buffer *buffer = GetBuffer(it->active_buffer);
if (!buffer->garbage) {
continue;
}
bool ref = ViewIsReferenced(it->id);
if (ref) {
continue;
}
Dealloc(&it->carets);
DeallocEx(sys_allocator, it);
DLL_QUEUE_REMOVE(FirstView, LastView, it);
}
for (Buffer *it = FirstBuffer, *next = NULL; it; it = next) {
next = it->next;
if (!it->garbage) {
continue;
}
bool ref = BufferIsReferenced(it->id);
if (ref) {
continue;
}
DeallocBuffer(it);
DLL_QUEUE_REMOVE(FirstBuffer, LastBuffer, it);
}
}

View File

@@ -227,7 +227,7 @@ void Windows_SetupVCVarsall(mco_coro *co) {
{
Scratch scratch;
String working_dir = Command_GetDir();
String buffer_name = GetUniqueBufferName(working_dir, "+cmd-");
String buffer_name = GetUniqueBufferName(working_dir, "vcvarsall-");
String cmd = Format(scratch, "\"%.*s\" && set", FmtString(StyleVCVarsall));
view = Command_ExecHidden(buffer_name, cmd, working_dir);
}

View File

@@ -42,7 +42,6 @@ struct Window {
Array<GotoCrumb> goto_redo;
ViewID active_goto_list;
BufferID command_buffer;
double mouse_scroller_offset;
int z;