aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiago de Arruda <tpadilha84@gmail.com>2014-05-07 17:57:23 -0300
committerThiago de Arruda <tpadilha84@gmail.com>2014-05-12 19:28:30 -0300
commit7f00608d57998858fd6a3e0720cbdbfeb92bd567 (patch)
tree09753d4dcddc8b77e91046170218cc79b34a6258
parent51ee26fe687facb730a5d96c71bb169dafc1d67e (diff)
downloadrneovim-7f00608d57998858fd6a3e0720cbdbfeb92bd567.tar.gz
rneovim-7f00608d57998858fd6a3e0720cbdbfeb92bd567.tar.bz2
rneovim-7f00608d57998858fd6a3e0720cbdbfeb92bd567.zip
Add helpers for accessing data in libuv handles
Libuv handles have a single generic pointer for storing user data, this adds some functions for adding/retrieving pointers to "slots" in the new `HandleData` structure, which increase flexibility when using shared handles(job streams for example)
-rw-r--r--src/os/uv_helpers.c70
-rw-r--r--src/os/uv_helpers.h47
2 files changed, 117 insertions, 0 deletions
diff --git a/src/os/uv_helpers.c b/src/os/uv_helpers.c
new file mode 100644
index 0000000000..62b021de5e
--- /dev/null
+++ b/src/os/uv_helpers.c
@@ -0,0 +1,70 @@
+#include <uv.h>
+
+#include "os/uv_helpers.h"
+#include "vim.h"
+#include "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;
+
+static HandleData *init(uv_handle_t *handle);
+
+RStream *handle_get_rstream(uv_handle_t *handle)
+{
+ RStream *rv = init(handle)->rstream;
+ assert(rv != NULL);
+ return rv;
+}
+
+void handle_set_rstream(uv_handle_t *handle, RStream *rstream)
+{
+ init(handle)->rstream = rstream;
+}
+
+WStream *handle_get_wstream(uv_handle_t *handle)
+{
+ WStream *rv = init(handle)->wstream;
+ assert(rv != NULL);
+ return rv;
+}
+
+void handle_set_wstream(uv_handle_t *handle, WStream *wstream)
+{
+ HandleData *data = init(handle);
+ data->wstream = wstream;
+}
+
+Job *handle_get_job(uv_handle_t *handle)
+{
+ Job *rv = init(handle)->job;
+ assert(rv != NULL);
+ return rv;
+}
+
+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;
+}
diff --git a/src/os/uv_helpers.h b/src/os/uv_helpers.h
new file mode 100644
index 0000000000..9d4cea30b2
--- /dev/null
+++ b/src/os/uv_helpers.h
@@ -0,0 +1,47 @@
+#ifndef NEOVIM_OS_UV_HELPERS_H
+#define NEOVIM_OS_UV_HELPERS_H
+
+#include <uv.h>
+
+#include "os/wstream_defs.h"
+#include "os/rstream_defs.h"
+#include "os/job_defs.h"
+
+/// 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);
+
+/// 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);
+
+/// 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);
+
+/// 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);
+
+/// 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);
+
+/// 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);
+
+#endif // NEOVIM_OS_UV_HELPERS_H
+