aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/api/private/handle.c
blob: abbda950736419fdc04b995a9f7fbde2127f8175 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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(uint64_t)()

#define HANDLE_IMPL(type, name)                                               \
  static PMap(uint64_t) *name##_handles = NULL;                               \
                                                                              \
  type *handle_get_##name(uint64_t handle)                                    \
  {                                                                           \
    return pmap_get(uint64_t)(name##_handles, handle);                        \
  }                                                                           \
                                                                              \
  void handle_register_##name(type *name)                                     \
  {                                                                           \
    assert(!name->handle);                                                    \
    name->handle = next_handle++;                                             \
    pmap_put(uint64_t)(name##_handles, name->handle, name);                   \
  }                                                                           \
                                                                              \
  void handle_unregister_##name(type *name)                                   \
  {                                                                           \
    pmap_del(uint64_t)(name##_handles, name->handle);                         \
  }

static uint64_t next_handle = 1;

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);
}