From ed99198ff1a3c2b6aad88b03e838a1337f83c4dc Mon Sep 17 00:00:00 2001 From: Thiago de Arruda Date: Fri, 23 May 2014 15:49:30 -0300 Subject: [PATCH] API: Refactor: Register/unregister created/destroyed buffers - Add the 'handle' field to `buf_T` - Add declare/implement functions for registering/unregistering/retrieving buffers - Register/unregister buffers when they are created/destroyed. --- src/nvim/api/private/handle.c | 5 +++++ src/nvim/api/private/handle.h | 4 ++++ src/nvim/buffer.c | 3 +++ src/nvim/buffer_defs.h | 1 + 4 files changed, 13 insertions(+) diff --git a/src/nvim/api/private/handle.c b/src/nvim/api/private/handle.c index 3bf519bc58..97f5cd88b5 100644 --- a/src/nvim/api/private/handle.c +++ b/src/nvim/api/private/handle.c @@ -26,6 +26,11 @@ map_del(uint64_t)(name##_handles, name->handle); \ } +static uint64_t next_handle = 1; + +HANDLE_IMPL(buf_T, buffer) + void handle_init() { + HANDLE_INIT(buffer); } diff --git a/src/nvim/api/private/handle.h b/src/nvim/api/private/handle.h index b9ed507ce9..846b20dff2 100644 --- a/src/nvim/api/private/handle.h +++ b/src/nvim/api/private/handle.h @@ -2,13 +2,17 @@ #define NVIM_API_HANDLE_H #include "nvim/vim.h" +#include "nvim/buffer_defs.h" #define HANDLE_DECLS(type, name) \ type *handle_get_##name(uint64_t handle); \ void handle_register_##name(type *name); \ void handle_unregister_##name(type *name); +HANDLE_DECLS(buf_T, buffer) + void handle_init(void); + #endif // NVIM_API_HANDLE_H diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index ffb62add3c..7c50b1721c 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -26,6 +26,7 @@ #include +#include "nvim/api/private/handle.h" #include "nvim/vim.h" #include "nvim/buffer.h" #include "nvim/charset.h" @@ -538,6 +539,7 @@ void buf_freeall(buf_T *buf, int flags) */ static void free_buffer(buf_T *buf) { + handle_unregister_buffer(buf); free_buffer_stuff(buf, TRUE); unref_var_dict(buf->b_vars); aubuflocal_remove(buf); @@ -1362,6 +1364,7 @@ buflist_new ( } if (buf != curbuf || curbuf == NULL) { buf = xcalloc(1, sizeof(buf_T)); + handle_register_buffer(buf); /* init b: variables */ buf->b_vars = dict_alloc(); init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE); diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index d658da9502..6634ba72a6 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -401,6 +401,7 @@ typedef struct { */ struct file_buffer { + uint64_t handle; // unique identifier for the buffer memline_T b_ml; /* associated memline (also contains line count) */