diff options
| author | Randy Palamar <randy@rnpnr.xyz> | 2025-12-01 22:08:13 -0700 |
|---|---|---|
| committer | Randy Palamar <randy@rnpnr.xyz> | 2025-12-16 11:28:44 -0700 |
| commit | 65dd46e0bba74948c824370a06e509cba462cd72 (patch) | |
| tree | b6055982318341cffa5d357b8e252f4a3661da6c | |
| parent | a98a1850640ca709ed68df3ca57f0404bbc4d3fc (diff) | |
| download | vis-65dd46e0bba74948c824370a06e509cba462cd72.tar.gz vis-65dd46e0bba74948c824370a06e509cba462cd72.tar.xz | |
main: make vis instance into a global
This might be controversial to some but for the purposes of the
editor there will never be more than one instance of vis. This
allows the compiler to use more efficient rip relative addressing
in the places where it knows the code is referring to the global
instance.
Once the code is compiling in a single translation unit with all
functions declared static this will allow for much better
optimization.
From the perspective that vis is meant to be a library, which is
what Marc clearly intended, changing vis_new() to vis_init() and
vis_free() to vis_cleanup() is still better design. The library
user should be allowed to place the vis instance wherever they
want in memory.
| -rw-r--r-- | main.c | 16 | ||||
| -rw-r--r-- | vis.c | 22 | ||||
| -rw-r--r-- | vis.h | 11 |
3 files changed, 26 insertions, 23 deletions
@@ -21,6 +21,8 @@ #include "array.h" #include "buffer.h" +static Vis vis[1]; + #define PAGE INT_MAX #define PAGE_HALF (INT_MAX-1) @@ -207,6 +209,10 @@ typedef enum { KEY_ACTION_LIST(ENUM) } VisActionKind; #undef ENUM +/* NOTE: must conform to the vis library signature, but we can rename the parameters */ +#undef KEY_ACTION_FN +#define KEY_ACTION_FN(name) const char *name(Vis *_unused, const char *keys, const Arg *arg) + /** key bindings functions */ static KEY_ACTION_FN(ka_nop) @@ -1271,8 +1277,6 @@ static KEY_ACTION_FN(ka_jumplist) return keys; } -static Vis *vis; - static void signal_handler(int signum, siginfo_t *siginfo, void *context) { vis_signal_handler(vis, signum, siginfo, context); } @@ -1283,7 +1287,8 @@ static const KeyAction vis_action[] = { KEY_ACTION_LIST(KEY_ACTION_STRUCT) }; #include "config.h" -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ for (int i = 1; i < argc; i++) { if (argv[i][0] != '-') { continue; @@ -1306,8 +1311,7 @@ int main(int argc, char *argv[]) { } } - vis = vis_new(); - if (!vis) + if (!vis_init(vis)) return EXIT_FAILURE; vis_event_emit(vis, VIS_EVENT_INIT); @@ -1403,6 +1407,6 @@ int main(int argc, char *argv[]) { } int status = vis_run(vis); - vis_free(vis); + vis_cleanup(vis); return status; } @@ -564,15 +564,11 @@ void vis_window_close(Win *win) { vis_draw(vis); } -Vis *vis_new(void) { - Vis *vis = calloc(1, sizeof(Vis)); - if (!vis) - return NULL; +bool vis_init(Vis *vis) +{ vis->exit_status = -1; - if (!ui_terminal_init(&vis->ui)) { - free(vis); - return NULL; - } + if (!ui_terminal_init(&vis->ui)) + return false; ui_init(&vis->ui, vis); vis->change_colors = true; for (size_t i = 0; i < LENGTH(vis->registers); i++) @@ -611,13 +607,14 @@ Vis *vis_new(void) { vis->mode_prev = vis->mode = &vis_modes[VIS_MODE_NORMAL]; vis_modes[VIS_MODE_INSERT].input = vis_event_mode_insert_input; vis_modes[VIS_MODE_REPLACE].input = vis_event_mode_replace_input; - return vis; + return true; err: - vis_free(vis); - return NULL; + vis_cleanup(vis); + return false; } -void vis_free(Vis *vis) { +void vis_cleanup(Vis *vis) +{ if (!vis) return; while (vis->windows) @@ -656,7 +653,6 @@ void vis_free(Vis *vis) { vis_action_free(vis, array_get_ptr(&vis->actions_user, 0)); array_release(&vis->actions_user); free(vis->shell); - free(vis); } void vis_insert(Vis *vis, size_t pos, const char *data, size_t len) { @@ -99,10 +99,13 @@ typedef struct { * @defgroup vis_lifecycle Vis Lifecycle * @{ */ -/** Create a new editor instance. */ -Vis *vis_new(void); -/** Free all resources associated with this editor instance, terminates UI. */ -void vis_free(Vis*); +/** + * Initializes a new editor instance. + * @param vis The editor instance. + */ +bool vis_init(Vis*); +/** Release all resources associated with this editor instance, terminates UI. */ +void vis_cleanup(Vis*); /** * Enter main loop, start processing user input. * @param vis The editor instance. |
