refactor(api): remove unneccesary indirection around handles

These things are just maps to pointers, no need to perform
a huge song and dance around it.
This commit is contained in:
Björn Linse
2021-08-22 11:25:59 +02:00
parent b888018aed
commit c265fd31ab
14 changed files with 19 additions and 84 deletions

View File

@@ -1,40 +0,0 @@
// This is an open source non-commercial project. Dear PVS-Studio, please check
// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include <assert.h>
#include <stdint.h>
#include "nvim/vim.h"
#include "nvim/map.h"
#include "nvim/api/private/handle.h"
#define HANDLE_INIT(name) name##_handles = pmap_new(handle_T)()
#define HANDLE_IMPL(type, name) \
static PMap(handle_T) *name##_handles = NULL; /* NOLINT */ \
\
type *handle_get_##name(handle_T handle) \
{ \
return pmap_get(handle_T)(name##_handles, handle); \
} \
\
void handle_register_##name(type *name) \
{ \
pmap_put(handle_T)(name##_handles, name->handle, name); \
} \
\
void handle_unregister_##name(type *name) \
{ \
pmap_del(handle_T)(name##_handles, name->handle); \
}
HANDLE_IMPL(buf_T, buffer)
HANDLE_IMPL(win_T, window)
HANDLE_IMPL(tabpage_T, tabpage)
void handle_init(void)
{
HANDLE_INIT(buffer);
HANDLE_INIT(window);
HANDLE_INIT(tabpage);
}

View File

@@ -1,24 +0,0 @@
#ifndef NVIM_API_PRIVATE_HANDLE_H
#define NVIM_API_PRIVATE_HANDLE_H
#include "nvim/vim.h"
#include "nvim/buffer_defs.h"
#include "nvim/api/private/defs.h"
#define HANDLE_DECLS(type, name) \
type *handle_get_##name(handle_T handle); \
void handle_register_##name(type *name); \
void handle_unregister_##name(type *name);
// handle_get_buffer handle_register_buffer, handle_unregister_buffer
HANDLE_DECLS(buf_T, buffer)
// handle_get_window handle_register_window, handle_unregister_window
HANDLE_DECLS(win_T, window)
// handle_get_tabpage handle_register_tabpage, handle_unregister_tabpage
HANDLE_DECLS(tabpage_T, tabpage)
void handle_init(void);
#endif // NVIM_API_PRIVATE_HANDLE_H

View File

@@ -9,7 +9,6 @@
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/api/private/defs.h" #include "nvim/api/private/defs.h"
#include "nvim/api/private/handle.h"
#include "nvim/api/vim.h" #include "nvim/api/vim.h"
#include "nvim/msgpack_rpc/helpers.h" #include "nvim/msgpack_rpc/helpers.h"
#include "nvim/lua/executor.h" #include "nvim/lua/executor.h"

View File

@@ -101,6 +101,14 @@
#define api_free_window(value) #define api_free_window(value)
#define api_free_tabpage(value) #define api_free_tabpage(value)
EXTERN PMap(handle_T) buffer_handles INIT(= MAP_INIT);
EXTERN PMap(handle_T) window_handles INIT(= MAP_INIT);
EXTERN PMap(handle_T) tabpage_handles INIT(= MAP_INIT);
#define handle_get_buffer(h) pmap_get(handle_T)(&buffer_handles, (h))
#define handle_get_window(h) pmap_get(handle_T)(&window_handles, (h))
#define handle_get_tabpage(h) pmap_get(handle_T)(&tabpage_handles, (h))
/// Structure used for saving state for :try /// Structure used for saving state for :try
/// ///
/// Used when caller is supposed to be operating when other VimL code is being /// Used when caller is supposed to be operating when other VimL code is being

View File

@@ -5,7 +5,7 @@
#include "nvim/autocmd.h" #include "nvim/autocmd.h"
#include "nvim/api/private/handle.h" #include "nvim/api/private/helpers.h"
#include "nvim/ascii.h" #include "nvim/ascii.h"
#include "nvim/buffer.h" #include "nvim/buffer.h"
#include "nvim/charset.h" #include "nvim/charset.h"
@@ -1150,7 +1150,7 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf)
block_autocmds(); // We don't want BufEnter/WinEnter autocommands. block_autocmds(); // We don't want BufEnter/WinEnter autocommands.
if (need_append) { if (need_append) {
win_append(lastwin, aucmd_win); win_append(lastwin, aucmd_win);
handle_register_window(aucmd_win); pmap_put(handle_T)(&window_handles, aucmd_win->handle, aucmd_win);
win_config_float(aucmd_win, aucmd_win->w_float_config); win_config_float(aucmd_win, aucmd_win->w_float_config);
} }
// Prevent chdir() call in win_enter_ext(), through do_autochdir() // Prevent chdir() call in win_enter_ext(), through do_autochdir()
@@ -1191,7 +1191,7 @@ void aucmd_restbuf(aco_save_T *aco)
win_found: win_found:
win_remove(curwin, NULL); win_remove(curwin, NULL);
handle_unregister_window(curwin); pmap_del(handle_T)(&window_handles, curwin->handle);
if (curwin->w_grid_alloc.chars != NULL) { if (curwin->w_grid_alloc.chars != NULL) {
ui_comp_remove_grid(&curwin->w_grid_alloc); ui_comp_remove_grid(&curwin->w_grid_alloc);
ui_call_win_hide(curwin->w_grid_alloc.handle); ui_call_win_hide(curwin->w_grid_alloc.handle);

View File

@@ -24,7 +24,6 @@
#include <inttypes.h> #include <inttypes.h>
#include <assert.h> #include <assert.h>
#include "nvim/api/private/handle.h"
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/api/vim.h" #include "nvim/api/vim.h"
#include "nvim/ascii.h" #include "nvim/ascii.h"
@@ -757,7 +756,7 @@ void buf_freeall(buf_T *buf, int flags)
*/ */
static void free_buffer(buf_T *buf) static void free_buffer(buf_T *buf)
{ {
handle_unregister_buffer(buf); pmap_del(handle_T)(&buffer_handles, buf->b_fnum);
buf_free_count++; buf_free_count++;
// b:changedtick uses an item in buf_T. // b:changedtick uses an item in buf_T.
free_buffer_stuff(buf, kBffClearWinInfo); free_buffer_stuff(buf, kBffClearWinInfo);
@@ -1841,7 +1840,7 @@ buf_T *buflist_new(char_u *ffname_arg, char_u *sfname_arg, linenr_T lnum,
lastbuf = buf; lastbuf = buf;
buf->b_fnum = top_file_num++; buf->b_fnum = top_file_num++;
handle_register_buffer(buf); pmap_put(handle_T)(&buffer_handles, buf->b_fnum, buf);
if (top_file_num < 0) { // wrap around (may cause duplicates) if (top_file_num < 0) { // wrap around (may cause duplicates)
EMSG(_("W14: Warning: List of file names overflow")); EMSG(_("W14: Warning: List of file names overflow"));
if (emsg_silent == 0) { if (emsg_silent == 0) {

View File

@@ -11,7 +11,7 @@
#include <fcntl.h> #include <fcntl.h>
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/api/private/handle.h" #include "nvim/api/private/helpers.h"
#include "nvim/ascii.h" #include "nvim/ascii.h"
#include "nvim/fileio.h" #include "nvim/fileio.h"
#include "nvim/buffer.h" #include "nvim/buffer.h"

View File

@@ -52,7 +52,6 @@
#include "nvim/os/input.h" #include "nvim/os/input.h"
#include "nvim/os/os.h" #include "nvim/os/os.h"
#include "nvim/os/fileio.h" #include "nvim/os/fileio.h"
#include "nvim/api/private/handle.h"
/// Index in scriptin /// Index in scriptin

View File

@@ -13,7 +13,6 @@
#include "nvim/func_attr.h" #include "nvim/func_attr.h"
#include "nvim/api/private/defs.h" #include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/api/private/handle.h"
#include "nvim/api/vim.h" #include "nvim/api/vim.h"
#include "nvim/msgpack_rpc/channel.h" #include "nvim/msgpack_rpc/channel.h"
#include "nvim/vim.h" #include "nvim/vim.h"

View File

@@ -18,7 +18,7 @@
#include "tree_sitter/api.h" #include "tree_sitter/api.h"
#include "nvim/lua/treesitter.h" #include "nvim/lua/treesitter.h"
#include "nvim/api/private/handle.h" #include "nvim/api/private/helpers.h"
#include "nvim/memline.h" #include "nvim/memline.h"
#include "nvim/buffer.h" #include "nvim/buffer.h"

View File

@@ -78,7 +78,6 @@
#include "nvim/api/ui.h" #include "nvim/api/ui.h"
#include "nvim/api/private/defs.h" #include "nvim/api/private/defs.h"
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/api/private/handle.h"
#include "nvim/api/private/dispatch.h" #include "nvim/api/private/dispatch.h"
#ifndef WIN32 #ifndef WIN32
# include "nvim/os/pty_process_unix.h" # include "nvim/os/pty_process_unix.h"
@@ -158,7 +157,6 @@ void early_init(mparm_T *paramp)
{ {
env_init(); env_init();
fs_init(); fs_init();
handle_init();
eval_init(); // init global variables eval_init(); // init global variables
init_path(argv0 ? argv0 : "nvim"); init_path(argv0 ? argv0 : "nvim");
init_normal_cmds(); // Init the table of Normal mode commands. init_normal_cmds(); // Init the table of Normal mode commands.

View File

@@ -7,7 +7,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include "nvim/api/private/handle.h"
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/ascii.h" #include "nvim/ascii.h"
#include "nvim/os_unix.h" #include "nvim/os_unix.h"

View File

@@ -76,7 +76,6 @@
#include "nvim/event/time.h" #include "nvim/event/time.h"
#include "nvim/os/input.h" #include "nvim/os/input.h"
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/api/private/handle.h"
typedef struct terminal_state { typedef struct terminal_state {
VimState state; VimState state;

View File

@@ -5,7 +5,6 @@
#include <inttypes.h> #include <inttypes.h>
#include <stdbool.h> #include <stdbool.h>
#include "nvim/api/private/handle.h"
#include "nvim/api/private/helpers.h" #include "nvim/api/private/helpers.h"
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/ascii.h" #include "nvim/ascii.h"
@@ -3601,7 +3600,7 @@ static tabpage_T *alloc_tabpage(void)
static int last_tp_handle = 0; static int last_tp_handle = 0;
tabpage_T *tp = xcalloc(1, sizeof(tabpage_T)); tabpage_T *tp = xcalloc(1, sizeof(tabpage_T));
tp->handle = ++last_tp_handle; tp->handle = ++last_tp_handle;
handle_register_tabpage(tp); pmap_put(handle_T)(&tabpage_handles, tp->handle, tp);
// Init t: variables. // Init t: variables.
tp->tp_vars = tv_dict_alloc(); tp->tp_vars = tv_dict_alloc();
@@ -3616,7 +3615,7 @@ void free_tabpage(tabpage_T *tp)
{ {
int idx; int idx;
handle_unregister_tabpage(tp); pmap_del(handle_T)(&tabpage_handles, tp->handle);
diff_clear(tp); diff_clear(tp);
for (idx = 0; idx < SNAP_COUNT; ++idx) for (idx = 0; idx < SNAP_COUNT; ++idx)
clear_snapshot(tp, idx); clear_snapshot(tp, idx);
@@ -4545,7 +4544,7 @@ static win_T *win_alloc(win_T *after, bool hidden)
win_T *new_wp = xcalloc(1, sizeof(win_T)); win_T *new_wp = xcalloc(1, sizeof(win_T));
new_wp->handle = ++last_win_id; new_wp->handle = ++last_win_id;
handle_register_window(new_wp); pmap_put(handle_T)(&window_handles, new_wp->handle, new_wp);
grid_assign_handle(&new_wp->w_grid_alloc); grid_assign_handle(&new_wp->w_grid_alloc);
@@ -4616,7 +4615,7 @@ win_free (
int i; int i;
wininfo_T *wip; wininfo_T *wip;
handle_unregister_window(wp); pmap_del(handle_T)(&window_handles, wp->handle);
clearFolding(wp); clearFolding(wp);
/* reduce the reference count to the argument list. */ /* reduce the reference count to the argument list. */