From fc811b2211a63ca434b80592c4f5270d0c9f4262 Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Wed, 26 Sep 2018 11:06:57 -0700 Subject: [PATCH] User managed scopes --- 4coder_default_hooks.cpp | 38 ++++++++---------------- 4coder_generated/app_functions.h | 14 +++++++++ 4ed_api_implementation.cpp | 51 +++++++++++++++++++++++++++----- 4ed_app_models.h | 7 +++-- 4ed_dynamic_variables.cpp | 7 ++--- 5 files changed, 75 insertions(+), 42 deletions(-) diff --git a/4coder_default_hooks.cpp b/4coder_default_hooks.cpp index 266f397b..05a57e86 100644 --- a/4coder_default_hooks.cpp +++ b/4coder_default_hooks.cpp @@ -55,25 +55,23 @@ RENDER_CALLER_SIG(default_render_caller){ View_Summary view = get_view(app, view_id, AccessAll); Buffer_Summary buffer = get_buffer(app, view.buffer_id, AccessAll); - // NOTE(allen): Line highlight setup - Managed_Object line_highlight = 0; + Managed_Scope render_scope = create_user_managed_scope(app); + // NOTE(allen): Line highlight setup if (highlight_line_at_cursor){ Theme_Color color = {0}; color.tag = Stag_Highlight_Cursor_Line; get_theme_colors(app, &color, 1); uint32_t line_color = color.color; - line_highlight = alloc_buffer_markers_on_buffer(app, buffer.buffer_id, 1, 0); - buffer_markers_set_visuals(app, line_highlight, BufferMarkersType_LineHighlights, line_color, 0, 0); + Managed_Object o = alloc_buffer_markers_on_buffer(app, buffer.buffer_id, 1, &render_scope); + buffer_markers_set_visuals(app, o, BufferMarkersType_LineHighlights, line_color, 0, 0); Marker marker = {0}; marker.pos = view.cursor.pos; - managed_object_store_data(app, line_highlight, 0, 1, &marker); + managed_object_store_data(app, o, 0, 1, &marker); } // NOTE(allen): Token highlight setup bool32 do_token_highlight = false; - Managed_Object token_highlight = 0; - if (do_token_highlight){ Theme_Color color = {0}; color.tag = Stag_Cursor; @@ -85,7 +83,7 @@ RENDER_CALLER_SIG(default_render_caller){ int32_t pos1 = buffer_boundary_seek(app, &buffer, pos0, DirLeft , token_flags); int32_t pos2 = buffer_boundary_seek(app, &buffer, pos1, DirRight, token_flags); - token_highlight = alloc_buffer_markers_on_buffer(app, buffer.buffer_id, 2, 0); + Managed_Object token_highlight = alloc_buffer_markers_on_buffer(app, buffer.buffer_id, 2, &render_scope); buffer_markers_set_visuals(app, token_highlight, BufferMarkersType_CharacterHighlightRanges, token_color, 0, 0); Marker range_markers[2] = {0}; range_markers[0].pos = pos1; @@ -101,7 +99,7 @@ RENDER_CALLER_SIG(default_render_caller){ 0x700000A0, 0x70A0A000, }; - Managed_Object matching_brace_highlights[ArrayCount(enclosure_colors)] = {0}; + int32_t color_count = ArrayCount(enclosure_colors); if (do_matching_enclosure_highlight){ Partition *scratch = &global_part; @@ -121,10 +119,10 @@ RENDER_CALLER_SIG(default_render_caller){ } int32_t count = (int32_t)(push_array(scratch, Range, 0) - ranges); - int32_t bucket_count = count/ArrayCount(matching_brace_highlights); - int32_t left_over_count = count%ArrayCount(matching_brace_highlights); + int32_t bucket_count = count/color_count; + int32_t left_over_count = count%color_count; - for (int32_t i = 0; i < ArrayCount(matching_brace_highlights); i += 1){ + for (int32_t i = 0; i < color_count; i += 1){ int32_t sub_count = bucket_count + (i < left_over_count?1:0); if (sub_count > 0){ int32_t marker_count = sub_count*2; @@ -136,8 +134,7 @@ RENDER_CALLER_SIG(default_render_caller){ markers[j + 0].pos = range_ptr->first; markers[j + 1].pos = range_ptr->one_past_last - 1; } - Managed_Object m = alloc_buffer_markers_on_buffer(app, buffer.buffer_id, marker_count, 0); - matching_brace_highlights[i] = m; + Managed_Object m = alloc_buffer_markers_on_buffer(app, buffer.buffer_id, marker_count, &render_scope); buffer_markers_set_visuals(app, m, BufferMarkersType_CharacterBlocks, enclosure_colors[i], 0, 0); managed_object_store_data(app, m, 0, marker_count, markers); end_temp_memory(marker_temp); @@ -149,18 +146,7 @@ RENDER_CALLER_SIG(default_render_caller){ do_core_render(app); - if (line_highlight != 0){ - managed_object_free(app, line_highlight); - } - if (token_highlight != 0){ - managed_object_free(app, token_highlight); - } - for (int32_t i = 0; i < ArrayCount(matching_brace_highlights); i += 1){ - if (matching_brace_highlights[i] != 0){ - managed_object_free(app, matching_brace_highlights[i]); - } - } - + destroy_user_managed_scope(app, render_scope); } HOOK_SIG(default_exit){ diff --git a/4coder_generated/app_functions.h b/4coder_generated/app_functions.h index fcd39ebd..857ec49f 100644 --- a/4coder_generated/app_functions.h +++ b/4coder_generated/app_functions.h @@ -49,6 +49,8 @@ struct Application_Links; #define VIEW_END_UI_MODE_SIG(n) int32_t n(Application_Links *app, View_Summary *view) #define VIEW_SET_UI_SIG(n) bool32 n(Application_Links *app, View_Summary *view, UI_Control *control, UI_Quit_Function_Type *quit_function) #define VIEW_GET_UI_COPY_SIG(n) UI_Control n(Application_Links *app, View_Summary *view, struct Partition *part) +#define CREATE_USER_MANAGED_SCOPE_SIG(n) Managed_Scope n(Application_Links *app) +#define DESTROY_USER_MANAGED_SCOPE_SIG(n) bool32 n(Application_Links *app, Managed_Scope scope) #define GET_GLOBAL_MANAGED_SCOPE_SIG(n) Managed_Scope n(Application_Links *app) #define GET_MANAGED_SCOPE_WITH_MULTIPLE_DEPENDENCIES_SIG(n) Managed_Scope n(Application_Links *app, Managed_Scope *intersected_scopes, int32_t count) #define MANAGED_VARIABLE_CREATE_SIG(n) Managed_Variable_ID n(Application_Links *app, char *null_terminated_name, uint64_t default_value) @@ -155,6 +157,8 @@ typedef VIEW_START_UI_MODE_SIG(View_Start_UI_Mode_Function); typedef VIEW_END_UI_MODE_SIG(View_End_UI_Mode_Function); typedef VIEW_SET_UI_SIG(View_Set_UI_Function); typedef VIEW_GET_UI_COPY_SIG(View_Get_UI_Copy_Function); +typedef CREATE_USER_MANAGED_SCOPE_SIG(Create_User_Managed_Scope_Function); +typedef DESTROY_USER_MANAGED_SCOPE_SIG(Destroy_User_Managed_Scope_Function); typedef GET_GLOBAL_MANAGED_SCOPE_SIG(Get_Global_Managed_Scope_Function); typedef GET_MANAGED_SCOPE_WITH_MULTIPLE_DEPENDENCIES_SIG(Get_Managed_Scope_With_Multiple_Dependencies_Function); typedef MANAGED_VARIABLE_CREATE_SIG(Managed_Variable_Create_Function); @@ -263,6 +267,8 @@ View_Start_UI_Mode_Function *view_start_ui_mode; View_End_UI_Mode_Function *view_end_ui_mode; View_Set_UI_Function *view_set_ui; View_Get_UI_Copy_Function *view_get_ui_copy; +Create_User_Managed_Scope_Function *create_user_managed_scope; +Destroy_User_Managed_Scope_Function *destroy_user_managed_scope; Get_Global_Managed_Scope_Function *get_global_managed_scope; Get_Managed_Scope_With_Multiple_Dependencies_Function *get_managed_scope_with_multiple_dependencies; Managed_Variable_Create_Function *managed_variable_create; @@ -370,6 +376,8 @@ View_Start_UI_Mode_Function *view_start_ui_mode_; View_End_UI_Mode_Function *view_end_ui_mode_; View_Set_UI_Function *view_set_ui_; View_Get_UI_Copy_Function *view_get_ui_copy_; +Create_User_Managed_Scope_Function *create_user_managed_scope_; +Destroy_User_Managed_Scope_Function *destroy_user_managed_scope_; Get_Global_Managed_Scope_Function *get_global_managed_scope_; Get_Managed_Scope_With_Multiple_Dependencies_Function *get_managed_scope_with_multiple_dependencies_; Managed_Variable_Create_Function *managed_variable_create_; @@ -485,6 +493,8 @@ app_links->view_start_ui_mode_ = View_Start_UI_Mode;\ app_links->view_end_ui_mode_ = View_End_UI_Mode;\ app_links->view_set_ui_ = View_Set_UI;\ app_links->view_get_ui_copy_ = View_Get_UI_Copy;\ +app_links->create_user_managed_scope_ = Create_User_Managed_Scope;\ +app_links->destroy_user_managed_scope_ = Destroy_User_Managed_Scope;\ app_links->get_global_managed_scope_ = Get_Global_Managed_Scope;\ app_links->get_managed_scope_with_multiple_dependencies_ = Get_Managed_Scope_With_Multiple_Dependencies;\ app_links->managed_variable_create_ = Managed_Variable_Create;\ @@ -592,6 +602,8 @@ static inline bool32 view_start_ui_mode(Application_Links *app, View_Summary *vi static inline int32_t view_end_ui_mode(Application_Links *app, View_Summary *view){return(app->view_end_ui_mode(app, view));} static inline bool32 view_set_ui(Application_Links *app, View_Summary *view, UI_Control *control, UI_Quit_Function_Type *quit_function){return(app->view_set_ui(app, view, control, quit_function));} static inline UI_Control view_get_ui_copy(Application_Links *app, View_Summary *view, struct Partition *part){return(app->view_get_ui_copy(app, view, part));} +static inline Managed_Scope create_user_managed_scope(Application_Links *app){return(app->create_user_managed_scope(app));} +static inline bool32 destroy_user_managed_scope(Application_Links *app, Managed_Scope scope){return(app->destroy_user_managed_scope(app, scope));} static inline Managed_Scope get_global_managed_scope(Application_Links *app){return(app->get_global_managed_scope(app));} static inline Managed_Scope get_managed_scope_with_multiple_dependencies(Application_Links *app, Managed_Scope *intersected_scopes, int32_t count){return(app->get_managed_scope_with_multiple_dependencies(app, intersected_scopes, count));} static inline Managed_Variable_ID managed_variable_create(Application_Links *app, char *null_terminated_name, uint64_t default_value){return(app->managed_variable_create(app, null_terminated_name, default_value));} @@ -699,6 +711,8 @@ static inline bool32 view_start_ui_mode(Application_Links *app, View_Summary *vi static inline int32_t view_end_ui_mode(Application_Links *app, View_Summary *view){return(app->view_end_ui_mode_(app, view));} static inline bool32 view_set_ui(Application_Links *app, View_Summary *view, UI_Control *control, UI_Quit_Function_Type *quit_function){return(app->view_set_ui_(app, view, control, quit_function));} static inline UI_Control view_get_ui_copy(Application_Links *app, View_Summary *view, struct Partition *part){return(app->view_get_ui_copy_(app, view, part));} +static inline Managed_Scope create_user_managed_scope(Application_Links *app){return(app->create_user_managed_scope_(app));} +static inline bool32 destroy_user_managed_scope(Application_Links *app, Managed_Scope scope){return(app->destroy_user_managed_scope_(app, scope));} static inline Managed_Scope get_global_managed_scope(Application_Links *app){return(app->get_global_managed_scope_(app));} static inline Managed_Scope get_managed_scope_with_multiple_dependencies(Application_Links *app, Managed_Scope *intersected_scopes, int32_t count){return(app->get_managed_scope_with_multiple_dependencies_(app, intersected_scopes, count));} static inline Managed_Variable_ID managed_variable_create(Application_Links *app, char *null_terminated_name, uint64_t default_value){return(app->managed_variable_create_(app, null_terminated_name, default_value));} diff --git a/4ed_api_implementation.cpp b/4ed_api_implementation.cpp index a36b15ec..9d477d28 100644 --- a/4ed_api_implementation.cpp +++ b/4ed_api_implementation.cpp @@ -2248,14 +2248,6 @@ View_Get_UI_Copy(Application_Links *app, View_Summary *view, struct Partition *p return(result); } -API_EXPORT Managed_Scope -Get_Global_Managed_Scope(Application_Links *app) -{ - Command_Data *cmd = (Command_Data*)app->cmd_context; - Models *models = cmd->models; - return((Managed_Scope)models->dynamic_workspace.scope_id); -} - internal Dynamic_Workspace* get_dynamic_workspace(Models *models, Managed_Scope handle){ u32_Ptr_Lookup_Result lookup_result = lookup_u32_Ptr_table(&models->lifetime_allocator.scope_id_to_scope_ptr_table, (u32)handle); @@ -2265,6 +2257,38 @@ get_dynamic_workspace(Models *models, Managed_Scope handle){ return((Dynamic_Workspace*)*lookup_result.val); } +API_EXPORT Managed_Scope +Create_User_Managed_Scope(Application_Links *app){ + Command_Data *cmd = (Command_Data*)app->cmd_context; + Models *models = cmd->models; + Heap *heap = &models->mem.heap; + Lifetime_Object *object = lifetime_alloc_object(heap, &models->lifetime_allocator, DynamicWorkspace_Unassociated, 0); + object->workspace.user_back_ptr = object; + Managed_Scope scope = (Managed_Scope)object->workspace.scope_id; + return(scope); +} + +API_EXPORT bool32 +Destroy_User_Managed_Scope(Application_Links *app, Managed_Scope scope){ + Command_Data *cmd = (Command_Data*)app->cmd_context; + Models *models = cmd->models; + Dynamic_Workspace *workspace = get_dynamic_workspace(models, scope); + if (workspace != 0){ + Lifetime_Object *lifetime_object = (Lifetime_Object*)workspace->user_back_ptr; + lifetime_free_object(&models->mem.heap, &models->lifetime_allocator, lifetime_object); + return(true); + } + return(false); +} + +API_EXPORT Managed_Scope +Get_Global_Managed_Scope(Application_Links *app) +{ + Command_Data *cmd = (Command_Data*)app->cmd_context; + Models *models = cmd->models; + return((Managed_Scope)models->dynamic_workspace.scope_id); +} + API_EXPORT Managed_Scope Get_Managed_Scope_With_Multiple_Dependencies(Application_Links *app, Managed_Scope *intersected_scopes, int32_t count) { @@ -2290,6 +2314,12 @@ Get_Managed_Scope_With_Multiple_Dependencies(Application_Links *app, Managed_Sco // NOTE(allen): (global_scope INTERSECT X) == X for all X, therefore we emit nothing when a global group is in the key list. }break; + case DynamicWorkspace_Unassociated: + { + Lifetime_Object **new_object_ptr = push_array(scratch, Lifetime_Object*, 1); + *new_object_ptr = (Lifetime_Object*)workspace->user_back_ptr; + }break; + case DynamicWorkspace_Buffer: { Editing_File *file = (Editing_File*)workspace->user_back_ptr; @@ -2316,6 +2346,11 @@ Get_Managed_Scope_With_Multiple_Dependencies(Application_Links *app, Managed_Sco } } }break; + + default: + { + InvalidCodePath; + }break; } } diff --git a/4ed_app_models.h b/4ed_app_models.h index 44d3efb5..de95a90d 100644 --- a/4ed_app_models.h +++ b/4ed_app_models.h @@ -121,9 +121,10 @@ struct Models{ typedef i32 Dynamic_Workspace_Type; enum{ DynamicWorkspace_Global = 0, - DynamicWorkspace_Buffer = 1, - DynamicWorkspace_View = 2, - DynamicWorkspace_Intersected = 3, + DynamicWorkspace_Unassociated = 1, + DynamicWorkspace_Buffer = 2, + DynamicWorkspace_View = 3, + DynamicWorkspace_Intersected = 4, }; enum App_State{ diff --git a/4ed_dynamic_variables.cpp b/4ed_dynamic_variables.cpp index f55d7bf6..6f2d22fa 100644 --- a/4ed_dynamic_variables.cpp +++ b/4ed_dynamic_variables.cpp @@ -163,9 +163,7 @@ dynamic_memory_bank_free_all(Heap *heap, Dynamic_Memory_Bank *mem_bank){ //////////////////////////////// internal void -dynamic_workspace_init(Heap *heap, Lifetime_Allocator *lifetime_allocator, - i32 user_type, void *user_back_ptr, - Dynamic_Workspace *workspace){ +dynamic_workspace_init(Heap *heap, Lifetime_Allocator *lifetime_allocator, i32 user_type, void *user_back_ptr, Dynamic_Workspace *workspace){ dynamic_variables_block_init(heap, &workspace->var_block); dynamic_memory_bank_init(heap, &workspace->mem_bank); if (lifetime_allocator->scope_id_counter == 0){ @@ -466,8 +464,7 @@ lifetime_alloc_object(Heap *heap, Lifetime_Allocator *lifetime_allocator, i32 us } internal void -lifetime_free_object(Heap *heap, Lifetime_Allocator *lifetime_allocator, - Lifetime_Object *lifetime_object){ +lifetime_free_object(Heap *heap, Lifetime_Allocator *lifetime_allocator, Lifetime_Object *lifetime_object){ dynamic_workspace_free(heap, lifetime_allocator, &lifetime_object->workspace); i32 key_i = 0;