aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os/uv_helpers.c
blob: 89687bdac7d055b5a871e5a35afd063e163f4b86 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <assert.h>
#include <uv.h>

#include "nvim/os/uv_helpers.h"
#include "nvim/vim.h"
#include "nvim/memory.h"

/// Common structure that will always be assigned to the `data` field of
/// libuv handles. It has fields for many types of pointers, and allow a single
/// handle to contain data from many sources
typedef struct {
  WStream *wstream;
  RStream *rstream;
  Job *job;
} HandleData;


#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/uv_helpers.c.generated.h"
#endif

/// Gets the RStream instance associated with a libuv handle
///
/// @param handle libuv handle
/// @return the RStream pointer
RStream *handle_get_rstream(uv_handle_t *handle)
{
  RStream *rv = init(handle)->rstream;
  assert(rv != NULL);
  return rv;
}

/// Associates a RStream instance with a libuv handle
///
/// @param handle libuv handle
/// @param rstream the RStream pointer
void handle_set_rstream(uv_handle_t *handle, RStream *rstream)
{
  init(handle)->rstream = rstream;
}

/// Gets the WStream instance associated with a libuv handle
///
/// @param handle libuv handle
/// @return the WStream pointer
WStream *handle_get_wstream(uv_handle_t *handle)
{
  WStream *rv = init(handle)->wstream;
  assert(rv != NULL);
  return rv;
}

/// Associates a WStream instance with a libuv handle
///
/// @param handle libuv handle
/// @param wstream the WStream pointer
void handle_set_wstream(uv_handle_t *handle, WStream *wstream)
{
  HandleData *data = init(handle);
  data->wstream = wstream;
}

/// Gets the Job instance associated with a libuv handle
///
/// @param handle libuv handle
/// @return the Job pointer
Job *handle_get_job(uv_handle_t *handle)
{
  Job *rv = init(handle)->job;
  assert(rv != NULL);
  return rv;
}

/// Associates a Job instance with a libuv handle
///
/// @param handle libuv handle
/// @param job the Job pointer
void handle_set_job(uv_handle_t *handle, Job *job)
{
  init(handle)->job = job;
}

static HandleData *init(uv_handle_t *handle)
{
  HandleData *rv;

  if (handle->data == NULL) {
    rv = xmalloc(sizeof(HandleData));
    rv->rstream = NULL;
    rv->wstream = NULL;
    rv->job = NULL;
    handle->data = rv;
  } else {
    rv = handle->data;
  }

  return rv;
}