Everywhere: Live patch MAlloc/Free to use RMAlloc/RFree

To make MAlloc/Free operations consistent across multiple processors, we
use a dedicated task on core 5 to service the requests.
This commit is contained in:
Alec Murphy 2025-04-24 15:11:46 -04:00
parent 04a602bb3b
commit fc2b4ba4e5
11 changed files with 668 additions and 362 deletions

View file

@ -8,7 +8,7 @@ U0 @compositor_init_truetype_fonts()
U8* name_ptr; U8* name_ptr;
I32 length; I32 length;
while (de) { while (de) {
info = CAlloc2(sizeof(stbtt_fontinfo), adam_task); info = CAlloc(sizeof(stbtt_fontinfo), adam_task);
if (@stbtt_InitFont(info, FileRead(de->full_name), 0)) { if (@stbtt_InitFont(info, FileRead(de->full_name), 0)) {
MemSet(name_buffer, NULL, 512); MemSet(name_buffer, NULL, 512);
name_ptr = @stbtt_GetFontNameDefault(info, &length); name_ptr = @stbtt_GetFontNameDefault(info, &length);
@ -122,7 +122,7 @@ class @compositor
U0 @compositor_add_window_to_list(Window* win) U0 @compositor_add_window_to_list(Window* win)
{ {
@compositor_windows_list* win_list = Compositor.windows; @compositor_windows_list* win_list = Compositor.windows;
@compositor_windows_list* win_next = CAlloc2(sizeof(@compositor_windows_list)); @compositor_windows_list* win_next = CAlloc(sizeof(@compositor_windows_list));
while (win_list->next) { while (win_list->next) {
win_list = win_list->next; win_list = win_list->next;
} }
@ -134,7 +134,7 @@ U0 @compositor_add_window_to_list(Window* win)
U0 @compositor_add_global_input_event_listener_to_list(Window* win) U0 @compositor_add_global_input_event_listener_to_list(Window* win)
{ {
@compositor_windows_list* win_list = Compositor.global_input_event_listeners; @compositor_windows_list* win_list = Compositor.global_input_event_listeners;
@compositor_windows_list* win_next = CAlloc2(sizeof(@compositor_windows_list)); @compositor_windows_list* win_next = CAlloc(sizeof(@compositor_windows_list));
while (win_list->next) { while (win_list->next) {
win_list = win_list->next; win_list = win_list->next;
} }
@ -164,7 +164,7 @@ U0 @compositor_remove_global_input_event_listener_from_list(Window* win)
U0 @compositor_refresh(Window* win) U0 @compositor_refresh(Window* win)
{ {
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
msg->client = win->client; msg->client = win->client;
msg->type = CPZ_MSG_WIN_REPAINT; msg->type = CPZ_MSG_WIN_REPAINT;
msg->payload = win; msg->payload = win;
@ -223,7 +223,7 @@ U0 @compositor_set_z_index_send_msg(Window* win, I64 index)
{ {
if (!win) if (!win)
return; return;
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
msg->client = win->client; msg->client = win->client;
msg->type = CPZ_MSG_WIN_SET_Z_INDEX; msg->type = CPZ_MSG_WIN_SET_Z_INDEX;
msg->payload = win; msg->payload = win;
@ -290,7 +290,7 @@ U0 @compositor_set_wallpaper_send_msg(Context2D* ctx, U32 mode = CPZ_WALLPAPER_A
{ {
if (!ctx) if (!ctx)
return; return;
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
msg->client = Fs; msg->client = Fs;
msg->type = CPZ_MSG_SET_WALLPAPER; msg->type = CPZ_MSG_SET_WALLPAPER;
msg->payload = ctx; msg->payload = ctx;
@ -320,7 +320,7 @@ U0 @compositor_ipc_queue_process()
System.Log(Fs, "Received message ← CreateWindow (%dx%d at %d, %d)", System.Log(Fs, "Received message ← CreateWindow (%dx%d at %d, %d)",
win->width, win->height, win->x, win->y); win->width, win->height, win->x, win->y);
Free(msg); Free(msg);
msg = CAlloc2(sizeof(IpcMessage)); msg = CAlloc(sizeof(IpcMessage));
msg->client = win->client; msg->client = win->client;
msg->type = CPZ_MSG_WIN_REPAINT; msg->type = CPZ_MSG_WIN_REPAINT;
msg->payload = win; msg->payload = win;
@ -518,7 +518,7 @@ U0 @compositor_set_active_window(Window* win)
win_list = win_list->next; win_list = win_list->next;
} }
if (Compositor.active_win && Compositor.active_win != win) { if (Compositor.active_win && Compositor.active_win != win) {
msg = CAlloc2(sizeof(IpcMessage)); msg = CAlloc(sizeof(IpcMessage));
msg->client = Compositor.active_win->client; msg->client = Compositor.active_win->client;
msg->type = CPZ_MSG_WIN_REPAINT; msg->type = CPZ_MSG_WIN_REPAINT;
msg->payload = Compositor.active_win; msg->payload = Compositor.active_win;
@ -531,7 +531,7 @@ U0 @compositor_set_active_window(Window* win)
} }
System.Log(Fs, "SetActiveWindow (%dx%d at %d, %d)", win->width, win->height, System.Log(Fs, "SetActiveWindow (%dx%d at %d, %d)", win->width, win->height,
win->x, win->y); win->x, win->y);
msg = CAlloc2(sizeof(IpcMessage)); msg = CAlloc(sizeof(IpcMessage));
msg->client = win->client; msg->client = win->client;
msg->type = CPZ_MSG_WIN_REPAINT; msg->type = CPZ_MSG_WIN_REPAINT;
msg->payload = win; msg->payload = win;
@ -583,7 +583,7 @@ U0 @compositor_handle_window_resize()
new_height = Max(Compositor.theme.window.min_height, new_height); new_height = Max(Compositor.theme.window.min_height, new_height);
if (Compositor.active_win->width != new_width || Compositor.active_win->height != new_height && !Compositor.active_win->repainting) { if (Compositor.active_win->width != new_width || Compositor.active_win->height != new_height && !Compositor.active_win->repainting) {
Window* win = Compositor.active_win; Window* win = Compositor.active_win;
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
System.Log(Fs, System.Log(Fs,
"Sent message → WindowResizeTo (%dx%d at %d, %d) to (%dx%d)", "Sent message → WindowResizeTo (%dx%d at %d, %d) to (%dx%d)",
Compositor.active_win->width, Compositor.active_win->height, Compositor.active_win->width, Compositor.active_win->height,
@ -673,7 +673,7 @@ U0 @compositor_handle_window_drag()
new_y = Compositor.active_win->origin.y + (Mouse.y - Compositor.active_win->origin.mouse_y); new_y = Compositor.active_win->origin.y + (Mouse.y - Compositor.active_win->origin.mouse_y);
if (Compositor.active_win->x != new_x || Compositor.active_win->y != new_y) { if (Compositor.active_win->x != new_x || Compositor.active_win->y != new_y) {
Window* win = Compositor.active_win; Window* win = Compositor.active_win;
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
System.Log( System.Log(
Fs, "Sent message → WindowMoveTo (%dx%d at %d, %d) to (%d, %d)", Fs, "Sent message → WindowMoveTo (%dx%d at %d, %d) to (%d, %d)",
Compositor.active_win->width, Compositor.active_win->height, Compositor.active_win->width, Compositor.active_win->height,
@ -729,7 +729,7 @@ U0 @compositor_handle_global_input_events()
@compositor_windows_list* win_list = Compositor.global_input_event_listeners->next; @compositor_windows_list* win_list = Compositor.global_input_event_listeners->next;
while (win_list) { while (win_list) {
msg = CAlloc2(sizeof(IpcMessage)); msg = CAlloc(sizeof(IpcMessage));
System.Log(Fs, "Sent message → WinKeyPress [%08x] to window 0x%08x", key, System.Log(Fs, "Sent message → WinKeyPress [%08x] to window 0x%08x", key,
win_list->window); win_list->window);
msg->client = win_list->window->client; msg->client = win_list->window->client;
@ -767,7 +767,7 @@ U0 @compositor_handle_window_input_events(Window* win)
if (win->focused_widget) { if (win->focused_widget) {
if (win->focused_widget->type == WIDGET_TYPE_INPUT) { if (win->focused_widget->type == WIDGET_TYPE_INPUT) {
if (@widget_input_handle_key(win->focused_widget)) { if (@widget_input_handle_key(win->focused_widget)) {
msg = CAlloc2(sizeof(IpcMessage)); msg = CAlloc(sizeof(IpcMessage));
System.Log(Fs, "Sent message → WinKeyPress"); System.Log(Fs, "Sent message → WinKeyPress");
msg->client = win->client; msg->client = win->client;
msg->type = CPZ_MSG_WIN_KEY_PRESS; msg->type = CPZ_MSG_WIN_KEY_PRESS;
@ -789,7 +789,7 @@ U0 @compositor_handle_window_input_events(Window* win)
} }
if (Mouse.z != Compositor.mouse.delta_z) { if (Mouse.z != Compositor.mouse.delta_z) {
msg = CAlloc2(sizeof(IpcMessage)); msg = CAlloc(sizeof(IpcMessage));
System.Log(Fs, "Sent message → WindowMouseWheel"); System.Log(Fs, "Sent message → WindowMouseWheel");
msg->client = win->client; msg->client = win->client;
msg->type = CPZ_MSG_WIN_MOUSE_WHEEL; msg->type = CPZ_MSG_WIN_MOUSE_WHEEL;
@ -804,7 +804,7 @@ U0 @compositor_handle_window_input_events(Window* win)
if (Mouse.x >= win->x && Mouse.x <= win->x + win->width && Mouse.y >= win->y && Mouse.y <= win->y + win->height && (mouse_x != win->mouse.x || mouse_y != win->mouse.y)) { if (Mouse.x >= win->x && Mouse.x <= win->x + win->width && Mouse.y >= win->y && Mouse.y <= win->y + win->height && (mouse_x != win->mouse.x || mouse_y != win->mouse.y)) {
win->mouse.x = mouse_x; win->mouse.x = mouse_x;
win->mouse.y = mouse_y; win->mouse.y = mouse_y;
msg = CAlloc2(sizeof(IpcMessage)); msg = CAlloc(sizeof(IpcMessage));
System.Log(Fs, "Sent message → WindowMouseAt (%dx%d)", mouse_x, System.Log(Fs, "Sent message → WindowMouseAt (%dx%d)", mouse_x,
mouse_y); mouse_y);
msg->client = win->client; msg->client = win->client;
@ -829,7 +829,7 @@ U0 @compositor_handle_window_input_events(Window* win)
win->mouse.left = mouse_left; win->mouse.left = mouse_left;
win->left_btn_down.x = Mouse.x - win->x; win->left_btn_down.x = Mouse.x - win->x;
win->left_btn_down.y = Mouse.y - win->y; win->left_btn_down.y = Mouse.y - win->y;
msg = CAlloc2(sizeof(IpcMessage)); msg = CAlloc(sizeof(IpcMessage));
msg->client = win->client; msg->client = win->client;
msg->type = type; msg->type = type;
msg->payload = win; msg->payload = win;
@ -851,7 +851,7 @@ U0 @compositor_handle_window_input_events(Window* win)
win->mouse.right = mouse_right; win->mouse.right = mouse_right;
win->right_btn_down.x = Mouse.x - win->x; win->right_btn_down.x = Mouse.x - win->x;
win->right_btn_down.y = Mouse.y - win->y; win->right_btn_down.y = Mouse.y - win->y;
msg = CAlloc2(sizeof(IpcMessage)); msg = CAlloc(sizeof(IpcMessage));
msg->client = win->client; msg->client = win->client;
msg->type = type; msg->type = type;
msg->payload = win; msg->payload = win;
@ -885,8 +885,8 @@ Window* @compositor_create_window(I64 x, I64 y, I64 width, I64 height,
I64 flags = WIN_FLAGS_DEFAULT, I64 flags = WIN_FLAGS_DEFAULT,
U8* title = NULL, Context2D* icon = NULL) U8* title = NULL, Context2D* icon = NULL)
{ {
Window* win = CAlloc2(sizeof(Window)); Window* win = CAlloc(sizeof(Window));
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
win->x = x; win->x = x;
win->y = y; win->y = y;
win->width = width; win->width = width;
@ -896,7 +896,7 @@ Window* @compositor_create_window(I64 x, I64 y, I64 width, I64 height,
win->icon = icon; win->icon = icon;
win->client = Fs; win->client = Fs;
win->signature = WIN_SIGNATURE; win->signature = WIN_SIGNATURE;
win->widget = CAlloc2(sizeof(@window_widgets_list)); win->widget = CAlloc(sizeof(@window_widgets_list));
win->callback.close = &@gui_window_callback_close; win->callback.close = &@gui_window_callback_close;
win->callback.maximize = &@gui_window_callback_maximize; win->callback.maximize = &@gui_window_callback_maximize;
win->callback.minimize = &@gui_window_callback_minimize; win->callback.minimize = &@gui_window_callback_minimize;
@ -918,7 +918,7 @@ Window* @compositor_create_window(I64 x, I64 y, I64 width, I64 height,
U0 @compositor_destroy_window(Window* win) U0 @compositor_destroy_window(Window* win)
{ {
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
msg->client = Fs; msg->client = Fs;
msg->type = CPZ_MSG_WIN_DESTROY; msg->type = CPZ_MSG_WIN_DESTROY;
msg->payload = win; msg->payload = win;
@ -1017,8 +1017,8 @@ U0 @compositor_init()
{ {
Compositor.blend_ctx = NewContext2D(Display.Width(), Display.Height()); Compositor.blend_ctx = NewContext2D(Display.Width(), Display.Height());
Compositor.ctx = NewContext2D(Display.Width(), Display.Height()); Compositor.ctx = NewContext2D(Display.Width(), Display.Height());
Compositor.windows = CAlloc2(sizeof(@compositor_windows_list)); Compositor.windows = CAlloc(sizeof(@compositor_windows_list));
Compositor.global_input_event_listeners = CAlloc2(sizeof(@compositor_windows_list)); Compositor.global_input_event_listeners = CAlloc(sizeof(@compositor_windows_list));
Compositor.bounds.x1 = -Display.Width(); Compositor.bounds.x1 = -Display.Width();
Compositor.bounds.y1 = 33; Compositor.bounds.y1 = 33;
Compositor.bounds.x2 = Display.Width(); Compositor.bounds.x2 = Display.Width();

View file

@ -16,7 +16,7 @@ U8** @shell_parse_args(@shell* sh, U8* str,
Bool quoted = FALSE; Bool quoted = FALSE;
I64 _argc = 0; I64 _argc = 0;
U8** _argv = NULL; U8** _argv = NULL;
U8** _tmp = CAlloc2(sizeof(U64) * StrLen(str)); U8** _tmp = CAlloc(sizeof(U64) * StrLen(str));
I64 i = 0; I64 i = 0;
I64 s = 0; I64 s = 0;
I64 len; I64 len;
@ -29,7 +29,7 @@ U8** @shell_parse_args(@shell* sh, U8* str,
if (str[i - 1] == '"') if (str[i - 1] == '"')
len--; len--;
if (len - 1) { if (len - 1) {
_tmp[_argc] = CAlloc2(len); _tmp[_argc] = CAlloc(len);
MemCpy(_tmp[_argc], str + s, len - 1); MemCpy(_tmp[_argc], str + s, len - 1);
_argc++; _argc++;
} }
@ -47,7 +47,7 @@ U8** @shell_parse_args(@shell* sh, U8* str,
i++; i++;
} }
*argc = _argc; *argc = _argc;
_argv = CAlloc2(sizeof(U64) * _argc); _argv = CAlloc(sizeof(U64) * _argc);
MemCpy(_argv, _tmp, sizeof(U64) * _argc); MemCpy(_argv, _tmp, sizeof(U64) * _argc);
Free(_tmp); Free(_tmp);
return _argv; return _argv;
@ -90,7 +90,7 @@ U8* @shell_expand_relative_path(@shell* sh, U8* path)
return StrNew(path); return StrNew(path);
break; break;
default: default:
U8* abs_path = CAlloc2(StrLen(path) + StrLen(&sh->cwd) + 4); U8* abs_path = CAlloc(StrLen(path) + StrLen(&sh->cwd) + 4);
StrPrint(abs_path, "%s/%s", &sh->cwd, path); StrPrint(abs_path, "%s/%s", &sh->cwd, path);
return abs_path; return abs_path;
break; break;
@ -121,7 +121,7 @@ U0 @shell_set_env_var(@shell* sh, U8* key, U8* value)
} }
var = var->next; var = var->next;
} }
@shell_env_var* new = CAlloc2(sizeof(@shell_env_var)); @shell_env_var* new = CAlloc(sizeof(@shell_env_var));
StrCpy(&new->key, key); StrCpy(&new->key, key);
StrCpy(&new->value, value); StrCpy(&new->value, value);
new->prev = var; new->prev = var;
@ -261,17 +261,17 @@ U0 @shell_instance(@shell* sh)
U0 @shell_init(@shell* sh) U0 @shell_init(@shell* sh)
{ {
sh->env = CAlloc2(sizeof(@shell_env_var)); sh->env = CAlloc(sizeof(@shell_env_var));
sh->history.limit = SHELL_HISTORY_LIMIT; sh->history.limit = SHELL_HISTORY_LIMIT;
sh->history.pos = 0; sh->history.pos = 0;
sh->history.entries = CAlloc2(sizeof(U64) * SHELL_HISTORY_LIMIT); sh->history.entries = CAlloc(sizeof(U64) * SHELL_HISTORY_LIMIT);
sh->input = FifoU8New(SHELL_INPUT_FIFO_SIZE); sh->input = FifoU8New(SHELL_INPUT_FIFO_SIZE);
sh->task = Spawn(&@shell_instance, sh); sh->task = Spawn(&@shell_instance, sh);
} }
@shell* @shell_new(Bool headless = FALSE) @shell* @shell_new(Bool headless = FALSE)
{ {
@shell* sh = CAlloc2(sizeof(@shell)); @shell* sh = CAlloc(sizeof(@shell));
if (!headless) if (!headless)
@shell_init(sh); @shell_init(sh);
StrCpy(&sh->cwd, &Compositor.session.home); StrCpy(&sh->cwd, &Compositor.session.home);

View file

@ -68,14 +68,12 @@ I64 @systemstarter_open(@shell* sh, I64 argc, U8** argv)
U0 @systemstarter_spawn(U8* path, U8* name) U0 @systemstarter_spawn(U8* path, U8* name)
{ {
CTask* task = Spawn(&UserCmdLine); CTask* task = Spawn(&UserCmdLine);
Sleep(Rand * 100);
U8 change_path_str[512]; U8 change_path_str[512];
StrPrint(task->task_name, name); StrPrint(task->task_name, name);
StrPrint(change_path_str, "Cd(\"%s\");\n", path); StrPrint(change_path_str, "Cd(\"%s\");\n", path);
TaskExe(task, NULL, "Raw(ON);\n", 0); TaskExe(task, NULL, "Raw(ON);\n", 0);
TaskExe(task, NULL, change_path_str, 0); TaskExe(task, NULL, change_path_str, 0);
TaskExe(task, NULL, "ExeFile(\"Run.HC\");\n", 0); TaskExe(task, NULL, "ExeFile(\"Run.HC\");\n", 0);
Sleep(Rand * 100);
} }
U0 @systemstarter_load_applets() U0 @systemstarter_load_applets()
@ -97,12 +95,12 @@ U0 @systemstarter_play_user_startup_sound()
U8 path[512]; U8 path[512];
StrPrint(&path, "/home/%s/.sounds/startup.wav", StrPrint(&path, "/home/%s/.sounds/startup.wav",
&Compositor.session.user.name); &Compositor.session.user.name);
U8** argv = CAlloc2(sizeof(U64) * 2); U8** argv = CAlloc(sizeof(U64) * 2);
argv[0] = "aplay"; argv[0] = "aplay";
argv[1] = &path; argv[1] = &path;
@shell* sh = @shell_new(TRUE); @shell* sh = @shell_new(TRUE);
sh->session = &Compositor.session; sh->session = &Compositor.session;
//@shell_cmd_aplay(sh, 2, argv); @shell_cmd_aplay(sh, 2, argv);
Free(sh); Free(sh);
} }
@ -111,7 +109,7 @@ U0 @systemstarter_set_user_wallpaper()
U8 path[512]; U8 path[512];
StrPrint(&path, "/home/%s/.wallpaper/wallpaper.png", StrPrint(&path, "/home/%s/.wallpaper/wallpaper.png",
&Compositor.session.user.name); &Compositor.session.user.name);
U8** argv = CAlloc2(sizeof(U64) * 2); U8** argv = CAlloc(sizeof(U64) * 2);
argv[0] = "wpset"; argv[0] = "wpset";
argv[1] = &path; argv[1] = &path;
//@shell_cmd_wpset(NULL, 2, argv); //@shell_cmd_wpset(NULL, 2, argv);
@ -196,8 +194,8 @@ U0 @systemstarter_task()
U0 @systemstarter_create_task(U8* path, U8* name) U0 @systemstarter_create_task(U8* path, U8* name)
{ {
@systemtask* st = CAlloc2(sizeof(@systemtask)); @systemtask* st = CAlloc(sizeof(@systemtask));
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
st->path = StrNew(path); st->path = StrNew(path);
st->name = StrNew(name); st->name = StrNew(name);
msg->client = NULL; msg->client = NULL;

View file

@ -26,7 +26,7 @@ U0 @systemtray_register_item(I64 addr)
U0 @systemtray_unregister_item(Widget* item) U0 @systemtray_unregister_item(Widget* item)
{ {
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
msg->client = NULL; msg->client = NULL;
msg->type = CPZ_MSG_WIN_WIDGET_DESTROY; msg->type = CPZ_MSG_WIN_WIDGET_DESTROY;
msg->payload = item; msg->payload = item;
@ -87,7 +87,7 @@ U0 @systemtray_task()
Context2DWidget* @systemtray_client_register_item() Context2DWidget* @systemtray_client_register_item()
{ {
Context2DWidget* item = NULL; Context2DWidget* item = NULL;
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
msg->client = NULL; msg->client = NULL;
msg->type = SYSTRAY_MSG_REGISTER; msg->type = SYSTRAY_MSG_REGISTER;
msg->payload = &item; msg->payload = &item;
@ -100,7 +100,7 @@ Context2DWidget* @systemtray_client_register_item()
U0 @systemtray_client_unregister_item(Widget* item) U0 @systemtray_client_unregister_item(Widget* item)
{ {
IpcMessage* msg = CAlloc2(sizeof(IpcMessage)); IpcMessage* msg = CAlloc(sizeof(IpcMessage));
msg->client = NULL; msg->client = NULL;
msg->type = SYSTRAY_MSG_UNREGISTER; msg->type = SYSTRAY_MSG_UNREGISTER;
msg->payload = item; msg->payload = item;

View file

@ -12,9 +12,7 @@ U0 bcmp()
U64 @calloc(I64 size) U64 @calloc(I64 size)
{ {
U64 res = CAlloc(size, calloc_mem_task[calloc_current_mem_task % CALLOC_MEM_TASK_COUNT]->code_heap); return CAlloc(size, erythros_mem_task->code_heap);
calloc_current_mem_task++;
return res;
} }
U0 calloc() U0 calloc()
@ -127,9 +125,7 @@ U0 ntohs()
U64 @malloc(I64 size) U64 @malloc(I64 size)
{ {
U64 res = MAlloc(size, malloc_mem_task[malloc_current_mem_task % MALLOC_MEM_TASK_COUNT]->code_heap); return MAlloc(size, erythros_mem_task->code_heap);
malloc_current_mem_task++;
return res;
} }
U0 malloc() U0 malloc()

View file

@ -669,10 +669,10 @@ Context2D* NewContext2D(I64 width, I64 height,
return NULL; return NULL;
break; break;
} }
Context2D* ctx = CAlloc2(sizeof(Context2D)); Context2D* ctx = CAlloc(sizeof(Context2D));
ctx->width = width; ctx->width = width;
ctx->height = height; ctx->height = height;
ctx->fb = CAlloc2((width * height) * bpp / 8); ctx->fb = CAlloc((width * height) * bpp / 8);
ctx->opacity = -1; ctx->opacity = -1;
return @create_callable_context2d(ctx); return @create_callable_context2d(ctx);
} }
@ -1563,7 +1563,7 @@ class @graphics2d
U0 @graphics2d_init() U0 @graphics2d_init()
{ {
Graphics2D.fb = CAlloc2(sizeof(Context2D)); Graphics2D.fb = CAlloc(sizeof(Context2D));
Graphics2D.fb->width = Display.width; Graphics2D.fb->width = Display.width;
Graphics2D.fb->height = Display.height; Graphics2D.fb->height = Display.height;
Graphics2D.fb->fb = Display.fb; Graphics2D.fb->fb = Display.fb;
@ -1665,7 +1665,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
@callable_context2d* @create_callable_context2d(@context2d* ctx) @callable_context2d* @create_callable_context2d(@context2d* ctx)
{ {
@callable_context2d* res = CAlloc2(sizeof(@callable_context2d)); @callable_context2d* res = CAlloc(sizeof(@callable_context2d));
MemCpy(res, ctx, sizeof(@context2d)); MemCpy(res, ctx, sizeof(@context2d));
Free(ctx); Free(ctx);
@ -1674,7 +1674,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// blot // blot
code_size = MSize(&@c2d_blot_wrapper_function); code_size = MSize(&@c2d_blot_wrapper_function);
res->blot = CAlloc2(code_size, adam_task->code_heap); res->blot = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->blot, &@c2d_blot_wrapper_function, code_size); MemCpy(res->blot, &@c2d_blot_wrapper_function, code_size);
a = res->blot; a = res->blot;
@ -1687,7 +1687,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// blur // blur
code_size = MSize(&@c2d_blur_wrapper_function); code_size = MSize(&@c2d_blur_wrapper_function);
res->blur = CAlloc2(code_size, adam_task->code_heap); res->blur = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->blur, &@c2d_blur_wrapper_function, code_size); MemCpy(res->blur, &@c2d_blur_wrapper_function, code_size);
a = res->blur; a = res->blur;
@ -1700,7 +1700,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// copy_rect // copy_rect
code_size = MSize(&@c2d_copy_rect_wrapper_function); code_size = MSize(&@c2d_copy_rect_wrapper_function);
res->copy_rect = CAlloc2(code_size, adam_task->code_heap); res->copy_rect = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->copy_rect, &@c2d_copy_rect_wrapper_function, code_size); MemCpy(res->copy_rect, &@c2d_copy_rect_wrapper_function, code_size);
a = res->copy_rect; a = res->copy_rect;
@ -1713,7 +1713,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// fill // fill
code_size = MSize(&@c2d_fill_wrapper_function); code_size = MSize(&@c2d_fill_wrapper_function);
res->fill = CAlloc2(code_size, adam_task->code_heap); res->fill = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->fill, &@c2d_fill_wrapper_function, code_size); MemCpy(res->fill, &@c2d_fill_wrapper_function, code_size);
a = res->fill; a = res->fill;
@ -1726,7 +1726,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// fill_rect // fill_rect
code_size = MSize(&@c2d_fill_rect_wrapper_function); code_size = MSize(&@c2d_fill_rect_wrapper_function);
res->fill_rect = CAlloc2(code_size, adam_task->code_heap); res->fill_rect = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->fill_rect, &@c2d_fill_rect_wrapper_function, code_size); MemCpy(res->fill_rect, &@c2d_fill_rect_wrapper_function, code_size);
a = res->fill_rect; a = res->fill_rect;
@ -1739,7 +1739,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// peek // peek
code_size = MSize(&@c2d_peek_wrapper_function); code_size = MSize(&@c2d_peek_wrapper_function);
res->peek = CAlloc2(code_size, adam_task->code_heap); res->peek = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->peek, &@c2d_peek_wrapper_function, code_size); MemCpy(res->peek, &@c2d_peek_wrapper_function, code_size);
a = res->peek; a = res->peek;
@ -1752,7 +1752,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// plot // plot
code_size = MSize(&@c2d_plot_wrapper_function); code_size = MSize(&@c2d_plot_wrapper_function);
res->plot = CAlloc2(code_size, adam_task->code_heap); res->plot = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->plot, &@c2d_plot_wrapper_function, code_size); MemCpy(res->plot, &@c2d_plot_wrapper_function, code_size);
a = res->plot; a = res->plot;
@ -1765,7 +1765,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// line // line
code_size = MSize(&@c2d_line_wrapper_function); code_size = MSize(&@c2d_line_wrapper_function);
res->line = CAlloc2(code_size, adam_task->code_heap); res->line = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->line, &@c2d_line_wrapper_function, code_size); MemCpy(res->line, &@c2d_line_wrapper_function, code_size);
a = res->line; a = res->line;
@ -1778,7 +1778,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// text // text
code_size = MSize(&@c2d_text_wrapper_function); code_size = MSize(&@c2d_text_wrapper_function);
res->text = CAlloc2(code_size, adam_task->code_heap); res->text = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->text, &@c2d_text_wrapper_function, code_size); MemCpy(res->text, &@c2d_text_wrapper_function, code_size);
a = res->text; a = res->text;
@ -1791,7 +1791,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// clipped // clipped
code_size = MSize(&@c2d_clipped_wrapper_function); code_size = MSize(&@c2d_clipped_wrapper_function);
res->clipped = CAlloc2(code_size, adam_task->code_heap); res->clipped = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->clipped, &@c2d_clipped_wrapper_function, code_size); MemCpy(res->clipped, &@c2d_clipped_wrapper_function, code_size);
a = res->clipped; a = res->clipped;
@ -1804,7 +1804,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// rotated // rotated
code_size = MSize(&@c2d_rotated_wrapper_function); code_size = MSize(&@c2d_rotated_wrapper_function);
res->rotated = CAlloc2(code_size, adam_task->code_heap); res->rotated = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->rotated, &@c2d_rotated_wrapper_function, code_size); MemCpy(res->rotated, &@c2d_rotated_wrapper_function, code_size);
a = res->rotated; a = res->rotated;
@ -1817,7 +1817,7 @@ U32 @c2d_peek_wrapper_function(I64 x, I64 y)
// scaled // scaled
code_size = MSize(&@c2d_scaled_wrapper_function); code_size = MSize(&@c2d_scaled_wrapper_function);
res->scaled = CAlloc2(code_size, adam_task->code_heap); res->scaled = CAlloc(code_size, adam_task->code_heap);
MemCpy(res->scaled, &@c2d_scaled_wrapper_function, code_size); MemCpy(res->scaled, &@c2d_scaled_wrapper_function, code_size);
a = res->scaled; a = res->scaled;

View file

@ -333,7 +333,7 @@ U0 @widget_add_widget_to_list(Window* win, Widget* widget)
while (widgets_list->next) { while (widgets_list->next) {
widgets_list = widgets_list->next; widgets_list = widgets_list->next;
} }
@window_widgets_list* widget_list_item = CAlloc2(sizeof(@window_widgets_list)); @window_widgets_list* widget_list_item = CAlloc(sizeof(@window_widgets_list));
widget_list_item->widget = widget; widget_list_item->widget = widget;
widget_list_item->prev = widgets_list; widget_list_item->prev = widgets_list;
widgets_list->next = widget_list_item; widgets_list->next = widget_list_item;
@ -666,7 +666,7 @@ Widget* @widget_create_widget(Window* win, I64 type, I64 x, I64 y, I64 width,
default: default:
return NULL; return NULL;
} }
@widget_create_set_values : widget = CAlloc2(size_of_widget); @widget_create_set_values : widget = CAlloc(size_of_widget);
widget->type = type; widget->type = type;
widget->x = x; widget->x = x;
widget->y = y; widget->y = y;
@ -681,7 +681,7 @@ Widget* @widget_create_widget(Window* win, I64 type, I64 x, I64 y, I64 width,
widget(TerminalWidget*)->color.background = Color(0, 0, 0); widget(TerminalWidget*)->color.background = Color(0, 0, 0);
widget(TerminalWidget*)->color.foreground = Color(217, 217, 217); widget(TerminalWidget*)->color.foreground = Color(217, 217, 217);
widget(TerminalWidget*)->color.cursor = Color(217, 217, 0); widget(TerminalWidget*)->color.cursor = Color(217, 217, 0);
widget(TerminalWidget*)->row = CAlloc2(sizeof(@terminal_widget_row) * 2000); widget(TerminalWidget*)->row = CAlloc(sizeof(@terminal_widget_row) * 2000);
break; break;
case WIDGET_TYPE_INPUT: case WIDGET_TYPE_INPUT:
widget(TextInputWidget*)->color = Color(0, 0, 0); widget(TextInputWidget*)->color = Color(0, 0, 0);
@ -692,7 +692,7 @@ Widget* @widget_create_widget(Window* win, I64 type, I64 x, I64 y, I64 width,
break; break;
case WIDGET_TYPE_LABEL: case WIDGET_TYPE_LABEL:
widget(TextLabelWidget*)->color = Color(0, 0, 0); widget(TextLabelWidget*)->color = Color(0, 0, 0);
widget(TextLabelWidget*)->SetText = CAlloc2(0x1F, Fs->code_heap); widget(TextLabelWidget*)->SetText = CAlloc(0x1F, Fs->code_heap);
I32 addr = widget(TextLabelWidget*)->SetText; I32 addr = widget(TextLabelWidget*)->SetText;
MemCpy(addr, widget_self_set1, 0x1F); MemCpy(addr, widget_self_set1, 0x1F);
MemCpy(addr + 12, &widget, 8); MemCpy(addr + 12, &widget, 8);
@ -700,7 +700,7 @@ Widget* @widget_create_widget(Window* win, I64 type, I64 x, I64 y, I64 width,
break; break;
case WIDGET_TYPE_LISTVIEW: case WIDGET_TYPE_LISTVIEW:
widget(ListViewWidget*)->color = Color(0, 0, 0); widget(ListViewWidget*)->color = Color(0, 0, 0);
widget(ListViewWidget*)->items = CAlloc2(sizeof(@list_view_item)); widget(ListViewWidget*)->items = CAlloc(sizeof(@list_view_item));
break; break;
} }
@ -782,7 +782,7 @@ U0 @widget_init_widget(Widget* widget, Window* win, I64 type, I64 x, I64 y,
widget(TerminalWidget*)->color.background = Color(0, 0, 0); widget(TerminalWidget*)->color.background = Color(0, 0, 0);
widget(TerminalWidget*)->color.foreground = Color(217, 217, 217); widget(TerminalWidget*)->color.foreground = Color(217, 217, 217);
widget(TerminalWidget*)->color.cursor = Color(217, 217, 0); widget(TerminalWidget*)->color.cursor = Color(217, 217, 0);
widget(TerminalWidget*)->row = CAlloc2(sizeof(@terminal_widget_row) * 2000); widget(TerminalWidget*)->row = CAlloc(sizeof(@terminal_widget_row) * 2000);
break; break;
case WIDGET_TYPE_INPUT: case WIDGET_TYPE_INPUT:
widget(TextInputWidget*)->color = Color(0, 0, 0); widget(TextInputWidget*)->color = Color(0, 0, 0);
@ -791,7 +791,7 @@ U0 @widget_init_widget(Widget* widget, Window* win, I64 type, I64 x, I64 y,
widget(TextInputWidget*)->selected_region_start = -1; widget(TextInputWidget*)->selected_region_start = -1;
widget(TextInputWidget*)->selected_region_end = -1; widget(TextInputWidget*)->selected_region_end = -1;
widget(TextInputWidget*)->color = Color(0, 0, 0); widget(TextInputWidget*)->color = Color(0, 0, 0);
widget(TextInputWidget*)->SetText = CAlloc2(0x1F, Fs->code_heap); widget(TextInputWidget*)->SetText = CAlloc(0x1F, Fs->code_heap);
addr = widget(TextInputWidget*)->SetFont; addr = widget(TextInputWidget*)->SetFont;
MemCpy(addr, widget_self_set1, 0x1F); MemCpy(addr, widget_self_set1, 0x1F);
MemCpy(addr + 12, &widget, 8); MemCpy(addr + 12, &widget, 8);
@ -803,7 +803,7 @@ U0 @widget_init_widget(Widget* widget, Window* win, I64 type, I64 x, I64 y,
break; break;
case WIDGET_TYPE_LABEL: case WIDGET_TYPE_LABEL:
widget(TextLabelWidget*)->color = Color(0, 0, 0); widget(TextLabelWidget*)->color = Color(0, 0, 0);
widget(TextLabelWidget*)->SetText = CAlloc2(0x1F, Fs->code_heap); widget(TextLabelWidget*)->SetText = CAlloc(0x1F, Fs->code_heap);
addr = widget(TextLabelWidget*)->SetFont; addr = widget(TextLabelWidget*)->SetFont;
MemCpy(addr, widget_self_set1, 0x1F); MemCpy(addr, widget_self_set1, 0x1F);
MemCpy(addr + 12, &widget, 8); MemCpy(addr + 12, &widget, 8);
@ -815,7 +815,7 @@ U0 @widget_init_widget(Widget* widget, Window* win, I64 type, I64 x, I64 y,
break; break;
case WIDGET_TYPE_LISTVIEW: case WIDGET_TYPE_LISTVIEW:
widget(ListViewWidget*)->color = Color(0, 0, 0); widget(ListViewWidget*)->color = Color(0, 0, 0);
widget(ListViewWidget*)->items = CAlloc2(sizeof(@list_view_item)); widget(ListViewWidget*)->items = CAlloc(sizeof(@list_view_item));
break; break;
} }

View file

@ -4,6 +4,8 @@ DocMax(adam_task);
WinMax(adam_task); WinMax(adam_task);
WinToTop(adam_task); WinToTop(adam_task);
#include "Setup/Util";
#include "Setup/MAlloc";
#include "Setup/Environment"; #include "Setup/Environment";
// Erythros system drivers // Erythros system drivers

View file

@ -1,159 +1,4 @@
AutoComplete(0); // Before continuing, we:
#define include_noreindex #include
I64 tos_nist_offset = 5603; // UTC -4
#define NIST_TIME_OFFSET (tos_nist_offset - local_time_offset / CDATE_FREQ)
public
I64 CDate2Unix(CDate dt)
{ // TempleOS datetime to Unix timestamp.
return ToI64((dt - Str2Date("1/1/1970")) / CDATE_FREQ + NIST_TIME_OFFSET);
}
public
CDate Unix2CDate(I64 timestamp)
{ // Unix timestamp to TempleOS datetime.
return (timestamp - NIST_TIME_OFFSET) * CDATE_FREQ + Str2Date("1/1/1970");
}
// FIXME: Put these in a "Builtin" library?
U0 FifoU8Cpy(CFifoU8* f, U8* s)
{
if (!f || !s)
return;
while (*s)
FifoU8Ins(f, *s++);
}
Bool KeyDown(I64 sc) return Bt(kbd.down_bitmap, sc);
I64 T(Bool _condition, I64 _true, I64 _false)
{
if (_condition)
return _true;
return _false;
}
asm
{
_MEMCPY_U16::
PUSH RBP
MOV RBP,RSP
PUSH RSI
PUSH RDI
CLD
MOV RDI,U64 SF_ARG1[RBP]
MOV RSI,U64 SF_ARG2[RBP]
MOV RCX,U64 SF_ARG3[RBP]
REP_MOVSW
MOV RAX,RDI
POP RDI
POP RSI
POP RBP
RET1 24
_MEMCPY_U32::
PUSH RBP
MOV RBP,RSP
PUSH RSI
PUSH RDI
CLD
MOV RDI,U64 SF_ARG1[RBP]
MOV RSI,U64 SF_ARG2[RBP]
MOV RCX,U64 SF_ARG3[RBP]
REP_MOVSD
MOV RAX,RDI
POP RDI
POP RSI
POP RBP
RET1 24
_MEMCPY_U64::
PUSH RBP
MOV RBP,RSP
PUSH RSI
PUSH RDI
CLD
MOV RDI,U64 SF_ARG1[RBP]
MOV RSI,U64 SF_ARG2[RBP]
MOV RCX,U64 SF_ARG3[RBP]
REP_MOVSQ
MOV RAX,RDI
POP RDI
POP RSI
POP RBP
RET1 24
}
public _extern _MEMCPY_U16 U16* MemCpyU16(U16* dst, U16* src, I64 cnt);
public
_extern _MEMCPY_U32 U32* MemCpyU32(U32* dst, U32* src, I64 cnt);
public
_extern _MEMCPY_U64 U64* MemCpyU64(U64* dst, U64* src, I64 cnt);
I64 @lerp(U32 val, U32 mx1, U32 mx2)
{
F64 r = (val & mx1) / ToF64(mx1);
return ToI64(r * mx2);
}
U0 @patch_call_rel32(U32 from, U32 to)
{
*(from(U8*)) = 0xE8;
*((from + 1)(I32*)) = to - from - 5;
}
U0 @patch_jmp_rel32(U32 from, U32 to)
{
*(from(U8*)) = 0xE9;
*((from + 1)(I32*)) = to - from - 5;
}
CMemBlk* ShrinkMemBlkByPags(CMemBlk* from, I64 count)
{
from->pags -= count;
U64 to = from;
to += count * MEM_PAG_SIZE;
MemCpy(to, from, MEM_PAG_SIZE);
return to;
}
U0 @sse_enable()
{
/* clang-format off */
asm
{
MOV_EAX_CR0
AND AX, 0xFFFB // clear coprocessor emulation CR0.EM
OR AX, 0x2 // set coprocessor monitoring CR0.MP
MOV_CR0_EAX
MOV_EAX_CR4
OR AX, 3 << 9 // set CR4.OSFXSR and CR4.OSXMMEXCPT at the same time
MOV_CR4_EAX
}
/* clang-format on */
}
U0 @sse_enable_on_all_cores()
{
I64 i;
for (i = 1; i < mp_cnt; i++)
Spawn(&@sse_enable, , , i);
}
I64 @t(Bool _condition, I64 _true, I64 _false)
{
if (_condition)
return _true;
return _false;
}
U0 @erythros_mem_task_loop()
{
while (1) {
Sleep(1);
};
}
// Before doing anything else, we:
// 1. Mark memory in code heap below 0x1000000 as used. // 1. Mark memory in code heap below 0x1000000 as used.
sys_code_bp->mem_free_lst->next->pags = 0; sys_code_bp->mem_free_lst->next->pags = 0;
@ -161,50 +6,13 @@ sys_code_bp->mem_free_lst->next->pags = 0;
// 2. Free up 64MB at bottom of code heap for non-HolyC programs // 2. Free up 64MB at bottom of code heap for non-HolyC programs
sys_code_bp->mem_free_lst = ShrinkMemBlkByPags(sys_code_bp->mem_free_lst, 131072); sys_code_bp->mem_free_lst = ShrinkMemBlkByPags(sys_code_bp->mem_free_lst, 131072);
// 3. Enable SSE // 3. Set mem_task
@sse_enable;
@sse_enable_on_all_cores;
// 4. Init mem_tasks CTask* erythros_mem_task = sys_malloc_task;
CTask* erythros_mem_task = Spawn(&@erythros_mem_task_loop, , "ErythrosMemTask");
#define MALLOC_MEM_TASK_COUNT 16
CTask** malloc_mem_task = CAlloc(sizeof(CTask*) * MALLOC_MEM_TASK_COUNT, adam_task);
I64 malloc_current_mem_task = 0;
U0 @malloc_mem_tasks_init()
{
U8* scratch_buffer[64];
I64 i;
for (i = 0; i < MALLOC_MEM_TASK_COUNT; i++) {
StrPrint(scratch_buffer, "ErythrosMallocTask%d", i);
malloc_mem_task[i] = Spawn(&@erythros_mem_task_loop, , scratch_buffer);
}
}
@malloc_mem_tasks_init;
#define CALLOC_MEM_TASK_COUNT 16
CTask** calloc_mem_task = CAlloc(sizeof(CTask*) * CALLOC_MEM_TASK_COUNT, adam_task);
I64 calloc_current_mem_task = 0;
U0 @calloc_mem_tasks_init()
{
U8* scratch_buffer[64];
I64 i;
for (i = 0; i < CALLOC_MEM_TASK_COUNT; i++) {
StrPrint(scratch_buffer, "ErythrosCallocTask%d", i);
calloc_mem_task[i] = Spawn(&@erythros_mem_task_loop, , scratch_buffer);
}
}
@calloc_mem_tasks_init;
U0 dd() { DocDump(adam_task->put_doc); }
//@patch_jmp_rel32(&Fault2, &Reboot); // Reboot instead of crashing to the debugger
U0 NoBeep(I8, Bool) {}; U0 NoBeep(I8, Bool) {};
@patch_jmp_rel32(&Beep, &NoBeep); // Don't delay on beep when entering debugger @patch_jmp_rel32(&Beep, &NoBeep); // Don't delay on beep when entering debugger
//@patch_jmp_rel32(&Fault2, &Reboot); // Reboot instead of crashing to the debugger
Bool BlkDevLock2(CBlkDev* bd) Bool BlkDevLock2(CBlkDev* bd)
{ {
@ -233,99 +41,5 @@ Bool DrvLock2(CDrv* dv)
return FALSE; return FALSE;
} }
CTask* SpawnQue2(U0 (*fp_addr)(U8* data), U8* data = NULL, U8* task_name = NULL,
I64 target_cpu, CTask* parent = NULL, // NULL means adam
I64 stk_size = 0, I64 flags = 1 << JOBf_ADD_TO_QUE)
{
CTask* res;
CJob* tmpc = JobQue(fp_addr, data, target_cpu,
flags, JOBT_SPAWN_TASK, task_name, parent, stk_size);
CJobCtrl* ctrl;
while (!Bt(&tmpc->flags, JOBf_DONE)) {
LBts(&Fs->task_flags, TASKf_IDLE);
Sleep(1);
}
LBtr(&Fs->task_flags, TASKf_IDLE);
res = tmpc->spawned_task;
ctrl = tmpc->ctrl;
PUSHFD
CLI while (LBts(&ctrl->flags, JOBCf_LOCKED))
Sleep(1);
QueRem(tmpc);
LBtr(&ctrl->flags, JOBCf_LOCKED);
POPFD
JobDel(tmpc);
return res;
}
@patch_jmp_rel32(&BlkDevLock, &BlkDevLock2); // Patch BlkDevLock so we don't deadlock on multiple tasks reading from virtio disk @patch_jmp_rel32(&BlkDevLock, &BlkDevLock2); // Patch BlkDevLock so we don't deadlock on multiple tasks reading from virtio disk
@patch_jmp_rel32(&DrvLock, &DrvLock2); // Patch DrvLock so we don't deadlock on multiple tasks reading from virtio disk @patch_jmp_rel32(&DrvLock, &DrvLock2); // Patch DrvLock so we don't deadlock on multiple tasks reading from virtio disk
@patch_jmp_rel32(&SpawnQue, &SpawnQue2); // Patch SpawnQue so we don't deadlock on spawning multicore tasks simultaneously
#define MALLOC2_REQUEST_PTR 0x280000
#define MALLOC2_REQUEST_TIMEOUT 4096
class @malloc2_request
{
U64 res_addr;
CTask* task;
U64 bytes;
Bool zero;
Bool kick;
};
U0 @malloc2_task()
{
Sleep(50);
U64* request_ptr = MALLOC2_REQUEST_PTR;
@malloc2_request* request = NULL;
while (1) {
request = *request_ptr;
if (request) {
if (request->zero) {
LXchgI64(request->res_addr, CAlloc(request->bytes, request->task));
} else {
LXchgI64(request->res_addr, MAlloc(request->bytes, request->task));
}
LXchgU8(&request->kick, 1);
*request_ptr = NULL;
}
}
}
Spawn(&@malloc2_task, , "MAlloc2 Task", 5);
U64 MAlloc2(I64 size, CTask* task = NULL, Bool zero = FALSE)
{
I64 count = 0;
U64 res = NULL;
U64* request_ptr = MALLOC2_REQUEST_PTR;
@malloc2_request request;
request.res_addr = &res;
request.bytes = size;
request.task = task;
request.zero = zero;
request.kick = FALSE;
retry_malloc2:
count = 0;
while (*request_ptr)
Busy(5);
LXchgI64(request_ptr, &request);
while (!request.kick) {
++count;
if (count > MALLOC2_REQUEST_TIMEOUT)
goto retry_malloc2;
Busy(5);
}
return res;
}
U64 CAlloc2(I64 size, CTask* task = NULL)
{
return MAlloc2(size, task, TRUE);
}

446
System/Setup/MAlloc.HC Normal file
View file

@ -0,0 +1,446 @@
#define RMALLOC_REQUEST_PTR 0x280000
#define RMALLOC_BUSY_WAIT 5
#define RMALLOC_TASK_CPU 5
#define RMALLOC_TIMEOUT 1024
#define RMALLOC_TYPE_MALLOC 0
#define RMALLOC_TYPE_CALLOC 1
#define RMALLOC_TYPE_FREE 2
asm {
//************************************
// See ::/Doc/Credits.DD.
_RMALLOC::
// Throws 'OutMem'
PUSH RBP
MOV RBP,RSP
PUSH RSI
PUSH RDI
XOR RBX,RBX
MOV RDX,U64 SF_ARG2[RBP]
TEST RDX,RDX
JNZ @@05
MOV RDX,U64 FS:CTask.addr[RBX]
@@05: CMP U32 CTask.task_signature[RDX],TASK_SIGNATURE_VAL
#assert CTask.task_signature == CHeapCtrl.hc_signature // location signature same
JNE @@10
MOV RDX,U64 CTask.data_heap[RDX]
@@10: CMP U32 CHeapCtrl.hc_signature[RDX],HEAP_CTRL_SIGNATURE_VAL
JE @@15
PUSH RDX
CALL &SysBadMAlloc
JMP I32 _SYS_HLT
@@15: MOV RAX,U64 SF_ARG1[RBP]
PUSHFD
ADD RAX,CMemUsed.start+7 // round-up to I64
AND AL,0xF8
#assert CMemUsed.start >= sizeof(CMemUnused)
CMP RAX,CMemUsed.start
JAE @@20
MOV RAX,CMemUsed.start
@@20:
CLI
@@25: LOCK
BTS U32 CHeapCtrl.locked_flags[RDX],HClf_LOCKED
PAUSE // don't know if this inst helps
JC @@25
CMP RAX,MEM_HEAP_HASH_SIZE
JAE @@30
MOV RSI,U64 CHeapCtrl.heap_hash[RAX+RDX]
TEST RSI,RSI
JZ @@35
MOV RCX,U64 CMemUnused.next[RSI]
MOV U64 CHeapCtrl.heap_hash[RAX+RDX],RCX
JMP I32 RMALLOC_ALMOST_DONE
// Big allocation
@@30: ADD RAX,sizeof(CMemBlk)+MEM_PAG_SIZE-1
SHR RAX,MEM_PAG_BITS
PUSH RDX // preserve HeapCtrl
PUSH RDX
PUSH RAX
CALL &MemPagTaskAlloc
POP RDX
TEST RAX,RAX
JZ @@45 // Out of memory
MOV RSI,RAX
MOV EAX,U32 CMemBlk.pags[RSI]
SHL RAX,MEM_PAG_BITS
SUB RAX,sizeof(CMemBlk)
ADD RSI,sizeof(CMemBlk)
JMP I32 RMALLOC_ALMOST_DONE
// Little allocation, chunk-off piece from free lst chunks
@@35: LEA RSI,U64 CHeapCtrl.malloc_free_lst-CMemUnused.next[RDX]
@@40: MOV RBX,RSI
MOV RSI,U64 CMemUnused.next[RBX]
TEST RSI,RSI
JNZ I32 @@60
PUSH RAX //-**** save byte size
ADD RAX,16*MEM_PAG_SIZE-1
SHR RAX,MEM_PAG_BITS
PUSH RDX // preserve HeapCtrl
PUSH RDX
PUSH RAX
CALL &MemPagTaskAlloc
POP RDX
TEST RAX,RAX
JNZ @@50
// Out of memory
@@45: LOCK
BTR U32 CHeapCtrl.locked_flags[RDX],HClf_LOCKED
POPFD
PUSH TRUE
MOV RAX,'OutMem'
PUSH RAX
CALL I32 &throw
JMP I32 RMALLOC_FINAL_EXIT // Never gets here, hopefully.
@@50: MOV RSI,RAX
MOV EAX,U32 CMemBlk.pags[RSI]
SHL RAX,MEM_PAG_BITS
// Can it be combined with last chunk? (Never Free these chunks.)
MOV RDI,U64 CHeapCtrl.last_mergable[RDX]
LEA RBX,U64 [RSI+RAX]
CMP RDI,RBX
JNE @@55
PUSH RAX
MOV EAX,U32 CMemBlk.pags[RDI]
ADD U32 CMemBlk.pags[RSI],EAX
// QueRem
MOV RAX,U64 CMemBlk.next[RDI]
MOV RBX,U64 CMemBlk.last[RDI]
MOV U64 CMemBlk.last[RAX],RBX
MOV U64 CMemBlk.next[RBX],RAX
POP RAX
@@55: MOV U64 CHeapCtrl.last_mergable[RDX],RSI
LEA RSI,U64 sizeof(CMemBlk)[RSI]
SUB RAX,sizeof(CMemBlk)
LEA RBX,U64 CHeapCtrl.malloc_free_lst-CMemUnused.next[RDX]
MOV RDI,U64 CMemUnused.next[RBX]
MOV U64 CMemUnused.next[RSI],RDI
MOV U64 CMemUnused.size[RSI],RAX
MOV U64 CMemUnused.next[RBX],RSI
POP RAX //+****
JMP @@70
@@60: CMP U64 CMemUnused.size[RSI],RAX
JB I32 @@40
JNE @@70
@@65: MOV RDI,U64 CMemUnused.next[RSI]
MOV U64 CMemUnused.next[RBX],RDI
JMP RMALLOC_ALMOST_DONE
@@70: SUB U64 CMemUnused.size[RSI],RAX // UPDATE FREE ENTRY
CMP U64 CMemUnused.size[RSI],sizeof(CMemUnused)
JAE @@75 // take from top of block
ADD U64 CMemUnused.size[RSI],RAX // doesn't fit, undo
JMP I32 @@40
@@75: ADD RSI,U64 CMemUnused.size[RSI]
RMALLOC_ALMOST_DONE:
// RSI=res-CMemUsed.size
// RAX=size+CMemUsed.size
// RDX=HeapCtrl
ADD U64 CHeapCtrl.used_u8s[RDX],RAX
#if _CFG_HEAP_DBG
// QueIns
MOV RDI,U64 CHeapCtrl.last_um[RDX]
MOV U64 CMemUsed.next[RDI],RSI
MOV U64 CHeapCtrl.last_um[RDX],RSI
MOV U64 CMemUsed.last[RSI],RDI
LEA RDI,U64 CHeapCtrl.next_um-CMemUsed.next[RDX]
MOV U64 CMemUsed.next[RSI],RDI
// Caller1/Caller2
PUSH RDX
MOV RDX,U64 [MEM_HEAP_LIMIT]
MOV RDI,U64 SF_RIP[RBP]
CMP RDI,RDX
JB @@80
XOR RDI,RDI
MOV U64 CMemUsed.caller1[RSI],RDI
JMP @@90
@@80: MOV U64 CMemUsed.caller1[RSI],RDI
MOV RDI,U64 SF_RBP[RBP]
CMP RDI,RDX
JB @@85
XOR RDI,RDI
JMP @@90
@@85: MOV RDI,U64 SF_RIP[RDI]
CMP RDI,RDX
JB @@90
XOR RDI,RDI
@@90: MOV U64 CMemUsed.caller2[RSI],RDI
POP RDX
#endif
LOCK
BTR U32 CHeapCtrl.locked_flags[RDX],HClf_LOCKED
POPFD
MOV U64 CMemUsed.size[RSI],RAX
MOV U64 CMemUsed.hc[RSI],RDX
LEA RAX,U64 CMemUsed.start[RSI]
TEST U8 [SYS_SEMAS+SEMA_HEAPLOG_ACTIVE*DFT_CACHE_LINE_WIDTH],1
JZ @@105
PUSH RAX
PUSH RAX
MOV RAX,U64 [SYS_EXTERN_TABLE]
MOV RAX,U64 EXT_HEAPLOG_MALLOC*8[RAX]
TEST RAX,RAX
JZ @@95
CALL RAX
JMP @@100
@@95: ADD RSP,8
@@100: POP RAX
@@105: TEST U8 [SYS_HEAP_INIT_FLAG],1
JZ RMALLOC_FINAL_EXIT
PUSH RAX
MOV RCX,U64 CMemUsed.size-CMemUsed.start[RAX]
SUB RCX,CMemUsed.start
MOV RDI,RAX
MOV AL,U8 [SYS_HEAP_INIT_VAL]
REP_STOSB
POP RAX
RMALLOC_FINAL_EXIT:
POP RDI
POP RSI
POP RBP
RET1 16
//************************************
_RFREE::
// Be aware of heap_hash in MemPagTaskAlloc().
PUSH RBP
MOV RBP,RSP
PUSH RSI
PUSH RDI
TEST U8 [SYS_SEMAS+SEMA_HEAPLOG_ACTIVE*DFT_CACHE_LINE_WIDTH],1
JZ @@15
MOV RBX,U64 SF_ARG1[RBP]
TEST RBX,RBX
JZ @@05
MOV RAX,U64 CMemUsed.size-CMemUsed.start[RBX]
TEST RAX,RAX
JGE @@05 // Aligned alloced chunks have neg size
ADD RBX,RAX
@@05: PUSH RBX
MOV RAX,U64 [SYS_EXTERN_TABLE]
MOV RAX,U64 EXT_HEAPLOG_FREE*8[RAX]
TEST RAX,RAX
JZ @@10
CALL RAX
JMP @@15
@@10: ADD RSP,8
@@15: MOV RSI,U64 SF_ARG1[RBP]
TEST RSI,RSI
#if _CFG_HEAP_DBG
JZ I32 RFREE_DONE
#else
JZ RFREE_DONE
#endif
MOV RAX,U64 CMemUsed.size-CMemUsed.start[RSI]
TEST RAX,RAX
JGE @@20 // Aligned alloced chunks have neg size.
// The neg size is offset to start of CMemUsed struct.
ADD RSI,RAX
@@20: PUSHFD
SUB RSI,CMemUsed.start
MOV RDX,U64 CMemUsed.hc[RSI]
CMP U32 CHeapCtrl.hc_signature[RDX],HEAP_CTRL_SIGNATURE_VAL
JE @@25
ADD RSI,CMemUsed.start
PUSH RSI
CALL &SysBadFree
JMP I32 _SYS_HLT
@@25: MOV RAX,U64 CMemUsed.size[RSI]
SUB U64 CHeapCtrl.used_u8s[RDX],RAX
CLI
@@30: LOCK
BTS U32 CHeapCtrl.locked_flags[RDX],HClf_LOCKED
PAUSE
JC @@30
#if _CFG_HEAP_DBG
// QueRem
MOV RDX,U64 CMemUsed.next[RSI]
MOV RDI,U64 CMemUsed.last[RSI]
MOV U64 CMemUsed.last[RDX],RDI
MOV U64 CMemUsed.next[RDI],RDX
// Caller1/Caller2
MOV RDX,U64 [MEM_HEAP_LIMIT]
MOV RDI,U64 SF_RIP[RBP]
CMP RDI,RDX
JB @@35
XOR RDI,RDI
MOV U64 CMemUnused.caller1[RSI],RDI
JMP @@45
@@35: MOV U64 CMemUnused.caller1[RSI],RDI
MOV RDI,U64 SF_RBP[RBP]
CMP RDI,RDX
JB @@40
XOR RDI,RDI
JMP @@45
@@40: MOV RDI,U64 SF_RIP[RDI]
CMP RDI,RDX
JB @@45
XOR RDI,RDI
@@45: MOV U64 CMemUnused.caller2[RSI],RDI
MOV RDX,U64 CMemUsed.hc[RSI]
#endif
CMP RAX,MEM_HEAP_HASH_SIZE
JAE @@50
#assert CMemUnused.size == CMemUsed.size
// MOV U64 CMemUnused.size[RSI],RAX
MOV RBX,U64 CHeapCtrl.heap_hash[RAX+RDX]
MOV U64 CMemUnused.next[RSI],RBX
MOV U64 CHeapCtrl.heap_hash[RAX+RDX],RSI
JMP @@55
@@50: SUB RSI,sizeof(CMemBlk)
PUSH RDX
PUSH RDX
PUSH RSI
CALL &MemPagTaskFree
POP RDX
@@55: LOCK
BTR U32 CHeapCtrl.locked_flags[RDX],HClf_LOCKED
POPFD
RFREE_DONE:
POP RDI
POP RSI
POP RBP
RET1 8
//************************************
}
_extern _RMALLOC U8* RMAlloc(I64 size, CTask* mem_task = NULL);
_extern _RFREE U0 RFree(U8* addr);
U8* RCAlloc(I64 size, CTask* mem_task = NULL)
{
U8* res = RMAlloc(size, mem_task);
if (res) {
MemSet(res, NULL, size);
}
return res;
}
class @rmalloc_request
{
U64 res_addr;
CTask* task;
U64 bytes;
U64 ptr;
Bool type;
Bool kick;
};
U0 @rmalloc_task()
{
U64* ptr = RMALLOC_REQUEST_PTR;
@rmalloc_request* r = NULL;
while (1) {
r = *ptr;
if (r) {
switch (r->type) {
case RMALLOC_TYPE_MALLOC:
LXchgI64(r->res_addr, RMAlloc(r->bytes, r->task));
break;
case RMALLOC_TYPE_CALLOC:
LXchgI64(r->res_addr, RCAlloc(r->bytes, r->task));
break;
case RMALLOC_TYPE_FREE:
RFree(r->ptr);
break;
default:
break;
}
LXchgU8(&r->kick, 1);
*ptr = NULL;
}
Yield;
}
}
U64 RMAllocOper(I64 size, CTask* task = NULL, Bool type = RMALLOC_TYPE_MALLOC, U64 addr = NULL)
{
I64 count = 0;
U64 res = NULL;
U64* ptr = RMALLOC_REQUEST_PTR;
@rmalloc_request r;
r.res_addr = &res;
r.bytes = size;
r.task = task;
r.ptr = addr;
r.type = type;
r.kick = FALSE;
retry_rmalloc:
count = 0;
while (*ptr)
Busy(RMALLOC_BUSY_WAIT);
LXchgI64(ptr, &r);
while (!r.kick) {
++count;
if (count > RMALLOC_TIMEOUT)
goto retry_rmalloc;
Busy(RMALLOC_BUSY_WAIT);
}
return res;
}
U64 MAlloc2(I64 size, CTask* task = NULL)
{
return RMAllocOper(size, task);
}
U64 CAlloc2(I64 size, CTask* task = NULL)
{
return RMAllocOper(size, task, RMALLOC_TYPE_CALLOC);
}
U0 Free2(U8* addr)
{
RMAllocOper(NULL, NULL, RMALLOC_TYPE_FREE, addr);
}
CTask* sys_malloc_task = Spawn(&@rmalloc_task, , "RMAlloc Task", RMALLOC_TASK_CPU);
Suspend(sys_winmgr_task);
@patch_jmp_rel32(&MAlloc, &MAlloc2);
@patch_jmp_rel32(&CAlloc, &CAlloc2);
@patch_jmp_rel32(&Free, &Free2);
Suspend(sys_winmgr_task, FALSE);

150
System/Setup/Util.HC Normal file
View file

@ -0,0 +1,150 @@
AutoComplete(0);
U0 @sse_enable()
{
/* clang-format off */
asm
{
MOV_EAX_CR0
AND AX, 0xFFFB // clear coprocessor emulation CR0.EM
OR AX, 0x2 // set coprocessor monitoring CR0.MP
MOV_CR0_EAX
MOV_EAX_CR4
OR AX, 3 << 9 // set CR4.OSFXSR and CR4.OSXMMEXCPT at the same time
MOV_CR4_EAX
}
/* clang-format on */
}
U0 @sse_enable_on_all_cores()
{
I64 i;
for (i = 1; i < mp_cnt; i++)
Spawn(&@sse_enable, , , i);
}
// Enable SSE
@sse_enable;
@sse_enable_on_all_cores;
U0 @patch_call_rel32(U32 from, U32 to)
{
*(from(U8*)) = 0xE8;
*((from + 1)(I32*)) = to - from - 5;
}
U0 @patch_jmp_rel32(U32 from, U32 to)
{
*(from(U8*)) = 0xE9;
*((from + 1)(I32*)) = to - from - 5;
}
I64 tos_nist_offset = 5603; // UTC -4
#define NIST_TIME_OFFSET (tos_nist_offset - local_time_offset / CDATE_FREQ)
public
I64 CDate2Unix(CDate dt)
{ // TempleOS datetime to Unix timestamp.
return ToI64((dt - Str2Date("1/1/1970")) / CDATE_FREQ + NIST_TIME_OFFSET);
}
public
CDate Unix2CDate(I64 timestamp)
{ // Unix timestamp to TempleOS datetime.
return (timestamp - NIST_TIME_OFFSET) * CDATE_FREQ + Str2Date("1/1/1970");
}
// FIXME: Put these in a "Builtin" library?
U0 FifoU8Cpy(CFifoU8* f, U8* s)
{
if (!f || !s)
return;
while (*s)
FifoU8Ins(f, *s++);
}
Bool KeyDown(I64 sc) return Bt(kbd.down_bitmap, sc);
I64 T(Bool _condition, I64 _true, I64 _false)
{
if (_condition)
return _true;
return _false;
}
asm
{
_MEMCPY_U16::
PUSH RBP
MOV RBP,RSP
PUSH RSI
PUSH RDI
CLD
MOV RDI,U64 SF_ARG1[RBP]
MOV RSI,U64 SF_ARG2[RBP]
MOV RCX,U64 SF_ARG3[RBP]
REP_MOVSW
MOV RAX,RDI
POP RDI
POP RSI
POP RBP
RET1 24
_MEMCPY_U32::
PUSH RBP
MOV RBP,RSP
PUSH RSI
PUSH RDI
CLD
MOV RDI,U64 SF_ARG1[RBP]
MOV RSI,U64 SF_ARG2[RBP]
MOV RCX,U64 SF_ARG3[RBP]
REP_MOVSD
MOV RAX,RDI
POP RDI
POP RSI
POP RBP
RET1 24
_MEMCPY_U64::
PUSH RBP
MOV RBP,RSP
PUSH RSI
PUSH RDI
CLD
MOV RDI,U64 SF_ARG1[RBP]
MOV RSI,U64 SF_ARG2[RBP]
MOV RCX,U64 SF_ARG3[RBP]
REP_MOVSQ
MOV RAX,RDI
POP RDI
POP RSI
POP RBP
RET1 24
}
public _extern _MEMCPY_U16 U16* MemCpyU16(U16* dst, U16* src, I64 cnt);
public
_extern _MEMCPY_U32 U32* MemCpyU32(U32* dst, U32* src, I64 cnt);
public
_extern _MEMCPY_U64 U64* MemCpyU64(U64* dst, U64* src, I64 cnt);
I64 @lerp(U32 val, U32 mx1, U32 mx2)
{
F64 r = (val & mx1) / ToF64(mx1);
return ToI64(r * mx2);
}
CMemBlk* ShrinkMemBlkByPags(CMemBlk* from, I64 count)
{
from->pags -= count;
U64 to = from;
to += count * MEM_PAG_SIZE;
MemCpy(to, from, MEM_PAG_SIZE);
return to;
}
I64 @t(Bool _condition, I64 _true, I64 _false)
{
if (_condition)
return _true;
return _false;
}
U0 dd() { DocDump(adam_task->put_doc); }