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.
This commit is contained in:
Thiago de Arruda 2014-05-23 15:49:30 -03:00
parent 7dfc7bc2e1
commit ed99198ff1
4 changed files with 13 additions and 0 deletions

View File

@ -26,6 +26,11 @@
map_del(uint64_t)(name##_handles, name->handle); \ map_del(uint64_t)(name##_handles, name->handle); \
} }
static uint64_t next_handle = 1;
HANDLE_IMPL(buf_T, buffer)
void handle_init() void handle_init()
{ {
HANDLE_INIT(buffer);
} }

View File

@ -2,13 +2,17 @@
#define NVIM_API_HANDLE_H #define NVIM_API_HANDLE_H
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/buffer_defs.h"
#define HANDLE_DECLS(type, name) \ #define HANDLE_DECLS(type, name) \
type *handle_get_##name(uint64_t handle); \ type *handle_get_##name(uint64_t handle); \
void handle_register_##name(type *name); \ void handle_register_##name(type *name); \
void handle_unregister_##name(type *name); void handle_unregister_##name(type *name);
HANDLE_DECLS(buf_T, buffer)
void handle_init(void); void handle_init(void);
#endif // NVIM_API_HANDLE_H #endif // NVIM_API_HANDLE_H

View File

@ -26,6 +26,7 @@
#include <string.h> #include <string.h>
#include "nvim/api/private/handle.h"
#include "nvim/vim.h" #include "nvim/vim.h"
#include "nvim/buffer.h" #include "nvim/buffer.h"
#include "nvim/charset.h" #include "nvim/charset.h"
@ -538,6 +539,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);
free_buffer_stuff(buf, TRUE); free_buffer_stuff(buf, TRUE);
unref_var_dict(buf->b_vars); unref_var_dict(buf->b_vars);
aubuflocal_remove(buf); aubuflocal_remove(buf);
@ -1362,6 +1364,7 @@ buflist_new (
} }
if (buf != curbuf || curbuf == NULL) { if (buf != curbuf || curbuf == NULL) {
buf = xcalloc(1, sizeof(buf_T)); buf = xcalloc(1, sizeof(buf_T));
handle_register_buffer(buf);
/* init b: variables */ /* init b: variables */
buf->b_vars = dict_alloc(); buf->b_vars = dict_alloc();
init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE); init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);

View File

@ -401,6 +401,7 @@ typedef struct {
*/ */
struct file_buffer { struct file_buffer {
uint64_t handle; // unique identifier for the buffer
memline_T b_ml; /* associated memline (also contains line memline_T b_ml; /* associated memline (also contains line
count) */ count) */