From 12bd9a523d88bc94cda1f7e08b144e167a69762a Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Tue, 18 Jul 2017 19:17:40 -0400 Subject: [PATCH] zipped memory together --- platform_all/4ed_shared_init_logic.cpp | 44 ++++++++++++++++++ platform_linux/linux_4ed.cpp | 44 +++++++----------- platform_unix/unix_4ed_functions.cpp | 16 +++++-- platform_win32/win32_4ed.cpp | 63 +++++++------------------- platform_win32/win32_4ed_functions.cpp | 8 +++- 5 files changed, 95 insertions(+), 80 deletions(-) create mode 100644 platform_all/4ed_shared_init_logic.cpp diff --git a/platform_all/4ed_shared_init_logic.cpp b/platform_all/4ed_shared_init_logic.cpp new file mode 100644 index 00000000..85aade5f --- /dev/null +++ b/platform_all/4ed_shared_init_logic.cpp @@ -0,0 +1,44 @@ +/* + * Mr. 4th Dimention - Allen Webster + * + * 18.07.2017 + * + * Shared logic for 4coder initialization. + * + */ + +// TOP + +internal b32 +system_memory_init(){ +#if defined(FRED_INTERNAL) +# if defined(BUILD_X64) + void *bases[] = { (void*)TB(1), (void*)TB(2), }; +# elif defined(BUILD_X86) + void *bases[] = { (void*)MB(96), (void*)MB(98), }; +# endif +#else + void *bases[] = { (void*)0, (void*)0, }; +#endif + + memory_vars.vars_memory_size = MB(2); + memory_vars.vars_memory = system_memory_allocate_extended(bases[0], memory_vars.vars_memory_size); + memory_vars.target_memory_size = MB(512); + memory_vars.target_memory = system_memory_allocate_extended(bases[1], memory_vars.target_memory_size); + memory_vars.user_memory_size = MB(2); + memory_vars.user_memory = system_memory_allocate_extended(0, memory_vars.user_memory_size); + memory_vars.debug_memory_size = MB(512); + memory_vars.debug_memory = system_memory_allocate_extended(0, memory_vars.debug_memory_size); + target.max = MB(1); + target.push_buffer = (char*)system_memory_allocate(target.max); + + b32 alloc_success = true; + if (memory_vars.vars_memory == 0 || memory_vars.target_memory == 0 || memory_vars.user_memory == 0 || target.push_buffer == 0){ + alloc_success = false; + } + + return(alloc_success); +} + +// BOTTOM + diff --git a/platform_linux/linux_4ed.cpp b/platform_linux/linux_4ed.cpp index 4330eca8..b781a5c2 100644 --- a/platform_linux/linux_4ed.cpp +++ b/platform_linux/linux_4ed.cpp @@ -120,12 +120,10 @@ struct Linux_Coroutine { internal void LinuxStringDup(String*, void*, size_t); internal void LinuxToggleFullscreen(Display*, Window); -internal void LinuxFatalErrorMsg(const char* msg); struct Linux_Vars{ Display *XDisplay; Window XWindow; - Render_Target target; XIM input_method; XIMStyle input_style; @@ -188,6 +186,7 @@ struct Linux_Vars{ //////////////////////////////// global Linux_Vars linuxvars; +global Render_Target target; global System_Functions sysfunc; global Application_Memory memory_vars; global Plat_Settings plat_settings; @@ -551,9 +550,9 @@ LinuxLoadAppCode(String* base_dir){ internal void LinuxLoadRenderCode(){ - linuxvars.target.push_clip = draw_push_clip; - linuxvars.target.pop_clip = draw_pop_clip; - linuxvars.target.push_piece = draw_push_piece; + target.push_clip = draw_push_clip; + target.pop_clip = draw_pop_clip; + target.push_piece = draw_push_piece; } // @@ -562,7 +561,7 @@ LinuxLoadRenderCode(){ internal void LinuxRedrawTarget(){ - launch_rendering(&sysfunc, &linuxvars.target); + launch_rendering(&sysfunc, &target); //glFlush(); glXSwapBuffers(linuxvars.XDisplay, linuxvars.XWindow); } @@ -576,8 +575,8 @@ LinuxResizeTarget(i32 width, i32 height){ glOrtho(0, width, height, 0, -1, 1); glScissor(0, 0, width, height); - linuxvars.target.width = width; - linuxvars.target.height = height; + target.width = width; + target.height = height; } } @@ -1682,7 +1681,7 @@ LinuxHandleX11Events(void) i32 w = Event.xconfigure.width; i32 h = Event.xconfigure.height; - if (w != linuxvars.target.width || h != linuxvars.target.height){ + if (w != target.width || h != target.height){ LinuxResizeTarget(w, h); } }break; @@ -1831,11 +1830,8 @@ LinuxHandleX11Events(void) } } -// -// Entry point -// - #include "4ed_link_system_functions.cpp" +#include "4ed_shared_init_logic.cpp" int main(int argc, char **argv){ @@ -1847,25 +1843,19 @@ main(int argc, char **argv){ String base_dir = make_fixed_width_string(base_dir_mem); if (!LinuxLoadAppCode(&base_dir)){ - LinuxFatalErrorMsg("Could not load '4ed_app.so'. This file should be in the same directory as the main '4ed' executable."); + char msg[] = "Could not load '4ed_app.so'. This file should be in the same directory as the main '4ed' executable."; + LinuxFatalErrorMsg(msg); return 99; } link_system_code(&sysfunc); LinuxLoadRenderCode(); - memory_vars.vars_memory_size = MB(2); - memory_vars.vars_memory = system_memory_allocate(memory_vars.vars_memory_size); - memory_vars.target_memory_size = MB(512); - memory_vars.target_memory = system_memory_allocate(memory_vars.target_memory_size); - memory_vars.user_memory_size = MB(2); - memory_vars.user_memory = system_memory_allocate(memory_vars.user_memory_size); + b32 alloc_success = system_memory_init(); - linuxvars.target.max = MB(1); - linuxvars.target.push_buffer = (char*)system_memory_allocate(linuxvars.target.max); - - if (memory_vars.vars_memory == NULL || memory_vars.target_memory == NULL || memory_vars.user_memory == NULL || linuxvars.target.push_buffer == NULL){ - LinuxFatalErrorMsg("Could not allocate sufficient memory. Please make sure you have atleast 512Mb of RAM free. (This requirement will be relaxed in the future)."); + if (!alloc_success){ + char msg[] = "Could not allocate sufficient memory. Please make sure you have atleast 512Mb of RAM free. (This requirement will be relaxed in the future)."; + LinuxFatalErrorMsg(msg); exit(1); } @@ -2115,7 +2105,7 @@ main(int argc, char **argv){ XAddConnectionWatch(linuxvars.XDisplay, &LinuxX11ConnectionWatch, NULL); - linuxvars.app.init(&sysfunc, &linuxvars.target, &memory_vars, linuxvars.clipboard_contents, current_directory, linuxvars.custom_api); + linuxvars.app.init(&sysfunc, &target, &memory_vars, linuxvars.clipboard_contents, current_directory, linuxvars.custom_api); LinuxResizeTarget(window_width, window_height); @@ -2209,7 +2199,7 @@ main(int argc, char **argv){ b32 keep_running = linuxvars.keep_running; - linuxvars.app.step(&sysfunc, &linuxvars.target, &memory_vars, &linuxvars.input, &result, clparams); + linuxvars.app.step(&sysfunc, &target, &memory_vars, &linuxvars.input, &result, clparams); if (result.perform_kill){ break; diff --git a/platform_unix/unix_4ed_functions.cpp b/platform_unix/unix_4ed_functions.cpp index 0c67c817..c8f433b6 100644 --- a/platform_unix/unix_4ed_functions.cpp +++ b/platform_unix/unix_4ed_functions.cpp @@ -86,18 +86,24 @@ Sys_File_Can_Be_Made_Sig(system_file_can_be_made){ // Memory // -internal -Sys_Memory_Allocate_Sig(system_memory_allocate){ +internal void* +system_memory_allocate_extended(void *base, umem size){ // NOTE(allen): This must return the exact base of the vpage. // We will count on the user to keep track of size themselves. - void *result = mmap(0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if(result == MAP_FAILED){ + void *result = mmap(base, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (result == MAP_FAILED){ LOG("error: mmap failed\n"); - result = NULL; + result = 0; } return(result); } +internal +Sys_Memory_Allocate_Sig(system_memory_allocate){ + void *result = system_memory_allocate_extended(0, size); + return(result); +} + internal Sys_Memory_Set_Protection_Sig(system_memory_set_protection){ bool32 result = true; diff --git a/platform_win32/win32_4ed.cpp b/platform_win32/win32_4ed.cpp index 01933e3c..7657c9bb 100644 --- a/platform_win32/win32_4ed.cpp +++ b/platform_win32/win32_4ed.cpp @@ -142,7 +142,6 @@ struct Win32_Vars{ DWORD clipboard_sequence; HWND window_handle; - Render_Target target; i32 dpi_x, dpi_y; f64 count_per_usecond; @@ -155,6 +154,7 @@ struct Win32_Vars{ //////////////////////////////// global Win32_Vars win32vars; +global Render_Target target; global System_Functions sysfunc; global Application_Memory memory_vars; global Plat_Settings plat_settings; @@ -585,9 +585,9 @@ Win32LoadAppCode(){ internal void Win32LoadRenderCode(){ - win32vars.target.push_clip = draw_push_clip; - win32vars.target.pop_clip = draw_pop_clip; - win32vars.target.push_piece = draw_push_piece; + target.push_clip = draw_push_clip; + target.pop_clip = draw_pop_clip; + target.push_piece = draw_push_piece; } // @@ -632,7 +632,7 @@ Win32KeycodeInit(){ internal void Win32RedrawScreen(HDC hdc){ - launch_rendering(&sysfunc, &win32vars.target); + launch_rendering(&sysfunc, &target); glFlush(); SwapBuffers(hdc); } @@ -646,8 +646,8 @@ Win32Resize(i32 width, i32 height){ glOrtho(0, width, height, 0, -1, 1); glScissor(0, 0, width, height); - win32vars.target.width = width; - win32vars.target.height = height; + target.width = width; + target.height = height; } } @@ -1070,6 +1070,7 @@ Win32Callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){ } #include "4ed_link_system_functions.cpp" +#include "4ed_shared_init_logic.cpp" int CALL_CONVENTION WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ @@ -1096,42 +1097,10 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS // Memory Initialization // - LPVOID base; -#if defined(FRED_INTERNAL) -#if defined(BUILD_X64) - base = (LPVOID)TB(1); -#elif defined(BUILD_X86) - base = (LPVOID)MB(96); -#endif -#else - base = (LPVOID)0; -#endif - - memory_vars.vars_memory_size = MB(2); - memory_vars.vars_memory = VirtualAlloc(base, memory_vars.vars_memory_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); - -#if defined(FRED_INTERNAL) -#if defined(BUILD_X64) - base = (LPVOID)TB(2); -#elif defined(BUILD_X86) - base = (LPVOID)MB(98); -#endif -#else - base = (LPVOID)0; -#endif - memory_vars.target_memory_size = MB(512); - memory_vars.target_memory = VirtualAlloc(base, memory_vars.target_memory_size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); - - memory_vars.user_memory_size = MB(2); - memory_vars.user_memory = system_memory_allocate(memory_vars.user_memory_size); - - memory_vars.debug_memory_size = MB(512); - memory_vars.debug_memory = system_memory_allocate(memory_vars.debug_memory_size); - - win32vars.target.max = MB(1); - win32vars.target.push_buffer = (char*)system_memory_allocate(win32vars.target.max); - - if (memory_vars.vars_memory == 0 || memory_vars.target_memory == 0 || memory_vars.user_memory == 0 || win32vars.target.push_buffer == 0){ + b32 alloc_success = system_memory_init(); + if (!alloc_success){ + // HACK(allen): + LOGF("Failed thingy"); exit(1); } @@ -1343,7 +1312,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS // LOG("Initializing application variables\n"); - win32vars.app.init(&sysfunc, &win32vars.target, &memory_vars, win32vars.clipboard_contents, current_directory, win32vars.custom_api); + win32vars.app.init(&sysfunc, &target, &memory_vars, win32vars.clipboard_contents, current_directory, win32vars.custom_api); system_memory_free(current_directory.str, 0); @@ -1460,8 +1429,8 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS i32_Rect screen; screen.x0 = 0; screen.y0 = 0; - screen.x1 = win32vars.target.width; - screen.y1 = win32vars.target.height; + screen.x1 = target.width; + screen.y1 = target.height; i32 mx = mouse_point.x; i32 my = mouse_point.y; @@ -1538,7 +1507,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdS win32vars.send_exit_signal = false; } - win32vars.app.step(&sysfunc, &win32vars.target, &memory_vars, &input, &result, clparams); + win32vars.app.step(&sysfunc, &target, &memory_vars, &input, &result, clparams); if (result.perform_kill){ keep_running = false; diff --git a/platform_win32/win32_4ed_functions.cpp b/platform_win32/win32_4ed_functions.cpp index dd411cd2..35371350 100644 --- a/platform_win32/win32_4ed_functions.cpp +++ b/platform_win32/win32_4ed_functions.cpp @@ -71,9 +71,15 @@ Sys_File_Can_Be_Made_Sig(system_file_can_be_made){ // Memory // +internal void* +system_memory_allocate_extended(void *base, umem size){ + void *result = VirtualAlloc(base, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + return(result); +} + internal Sys_Memory_Allocate_Sig(system_memory_allocate){ - void *result = VirtualAlloc(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + void *result = system_memory_allocate_extended(0, size); return(result); }