Refactor modules to use handle data accessors

The job, rstream and wstream modules now use handle data accessors
This commit is contained in:
Thiago de Arruda 2014-05-07 17:57:45 -03:00
parent 7f00608d57
commit 519fd1deb6
4 changed files with 28 additions and 12 deletions

View File

@ -3,6 +3,7 @@
#include <uv.h> #include <uv.h>
#include "os/uv_helpers.h"
#include "os/job.h" #include "os/job.h"
#include "os/job_defs.h" #include "os/job_defs.h"
#include "os/rstream.h" #include "os/rstream.h"
@ -160,10 +161,13 @@ int job_start(char **argv,
job->proc_opts.exit_cb = exit_cb; job->proc_opts.exit_cb = exit_cb;
job->proc_opts.cwd = NULL; job->proc_opts.cwd = NULL;
job->proc_opts.env = NULL; job->proc_opts.env = NULL;
job->proc.data = NULL;
job->proc_stdin.data = NULL;
job->proc_stdout.data = NULL;
job->proc_stderr.data = NULL;
// Initialize the job std{in,out,err} // Initialize the job std{in,out,err}
uv_pipe_init(uv_default_loop(), &job->proc_stdin, 0); uv_pipe_init(uv_default_loop(), &job->proc_stdin, 0);
job->proc_stdin.data = job;
job->stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE; job->stdio[0].flags = UV_CREATE_PIPE | UV_READABLE_PIPE;
job->stdio[0].data.stream = (uv_stream_t *)&job->proc_stdin; job->stdio[0].data.stream = (uv_stream_t *)&job->proc_stdin;
@ -181,6 +185,12 @@ int job_start(char **argv,
return -1; return -1;
} }
// Give all handles a reference to the job
handle_set_job((uv_handle_t *)&job->proc, job);
handle_set_job((uv_handle_t *)&job->proc_stdin, job);
handle_set_job((uv_handle_t *)&job->proc_stdout, job);
handle_set_job((uv_handle_t *)&job->proc_stderr, job);
job->in = wstream_new(JOB_WRITE_MAXMEM); job->in = wstream_new(JOB_WRITE_MAXMEM);
wstream_set_stream(job->in, (uv_stream_t *)&job->proc_stdin); wstream_set_stream(job->in, (uv_stream_t *)&job->proc_stdin);
// Start the readable streams // Start the readable streams
@ -190,8 +200,6 @@ int job_start(char **argv,
rstream_set_stream(job->err, (uv_stream_t *)&job->proc_stderr); rstream_set_stream(job->err, (uv_stream_t *)&job->proc_stderr);
rstream_start(job->out); rstream_start(job->out);
rstream_start(job->err); rstream_start(job->err);
// Give the callback a reference to the job
job->proc.data = job;
// Save the job to the table // Save the job to the table
table[i] = job; table[i] = job;
@ -328,7 +336,7 @@ static void read_cb(RStream *rstream, void *data, bool eof)
// Emits a JobExit event if both rstreams are closed // Emits a JobExit event if both rstreams are closed
static void exit_cb(uv_process_t *proc, int64_t status, int term_signal) static void exit_cb(uv_process_t *proc, int64_t status, int term_signal)
{ {
Job *job = proc->data; Job *job = handle_get_job((uv_handle_t *)proc);
if (--job->pending_refs == 0) { if (--job->pending_refs == 0) {
emit_exit_event(job); emit_exit_event(job);
@ -345,7 +353,7 @@ static void emit_exit_event(Job *job)
static void close_cb(uv_handle_t *handle) static void close_cb(uv_handle_t *handle)
{ {
Job *job = handle->data; Job *job = handle_get_job(handle);
if (--job->pending_closes == 0) { if (--job->pending_closes == 0) {
// Only free the job memory after all the associated handles are properly // Only free the job memory after all the associated handles are properly

View File

@ -4,6 +4,7 @@
#include <uv.h> #include <uv.h>
#include "os/uv_helpers.h"
#include "os/rstream_defs.h" #include "os/rstream_defs.h"
#include "os/rstream.h" #include "os/rstream.h"
#include "os/event_defs.h" #include "os/event_defs.h"
@ -66,7 +67,7 @@ void rstream_free(RStream *rstream)
void rstream_set_stream(RStream *rstream, uv_stream_t *stream) void rstream_set_stream(RStream *rstream, uv_stream_t *stream)
{ {
stream->data = rstream; handle_set_rstream((uv_handle_t *)stream, rstream);
rstream->stream = stream; rstream->stream = stream;
} }
@ -90,7 +91,8 @@ void rstream_set_file(RStream *rstream, uv_file file)
// be processed between reads. // be processed between reads.
rstream->fread_idle = xmalloc(sizeof(uv_idle_t)); rstream->fread_idle = xmalloc(sizeof(uv_idle_t));
uv_idle_init(uv_default_loop(), rstream->fread_idle); uv_idle_init(uv_default_loop(), rstream->fread_idle);
rstream->fread_idle->data = rstream; rstream->fread_idle->data = NULL;
handle_set_rstream((uv_handle_t *)rstream->fread_idle, rstream);
} else { } else {
// Only pipes are supported for now // Only pipes are supported for now
assert(rstream->file_type == UV_NAMED_PIPE assert(rstream->file_type == UV_NAMED_PIPE
@ -98,7 +100,8 @@ void rstream_set_file(RStream *rstream, uv_file file)
rstream->stream = xmalloc(sizeof(uv_pipe_t)); rstream->stream = xmalloc(sizeof(uv_pipe_t));
uv_pipe_init(uv_default_loop(), (uv_pipe_t *)rstream->stream, 0); uv_pipe_init(uv_default_loop(), (uv_pipe_t *)rstream->stream, 0);
uv_pipe_open((uv_pipe_t *)rstream->stream, file); uv_pipe_open((uv_pipe_t *)rstream->stream, file);
rstream->stream->data = rstream; rstream->stream->data = NULL;
handle_set_rstream((uv_handle_t *)rstream->stream, rstream);
} }
rstream->fd = file; rstream->fd = file;
@ -176,7 +179,7 @@ void rstream_read_event(Event event)
// Called by libuv to allocate memory for reading. // Called by libuv to allocate memory for reading.
static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf) static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf)
{ {
RStream *rstream = handle->data; RStream *rstream = handle_get_rstream(handle);
if (rstream->reading) { if (rstream->reading) {
buf->len = 0; buf->len = 0;
@ -195,7 +198,7 @@ static void alloc_cb(uv_handle_t *handle, size_t suggested, uv_buf_t *buf)
// 0-length buffer. // 0-length buffer.
static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf) static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
{ {
RStream *rstream = stream->data; RStream *rstream = handle_get_rstream((uv_handle_t *)stream);
if (cnt <= 0) { if (cnt <= 0) {
if (cnt != UV_ENOBUFS) { if (cnt != UV_ENOBUFS) {
@ -227,7 +230,7 @@ static void read_cb(uv_stream_t *stream, ssize_t cnt, const uv_buf_t *buf)
static void fread_idle_cb(uv_idle_t *handle) static void fread_idle_cb(uv_idle_t *handle)
{ {
uv_fs_t req; uv_fs_t req;
RStream *rstream = handle->data; RStream *rstream = handle_get_rstream((uv_handle_t *)handle);
rstream->uvbuf.base = rstream->buffer + rstream->wpos; rstream->uvbuf.base = rstream->buffer + rstream->wpos;
rstream->uvbuf.len = rstream->buffer_size - rstream->wpos; rstream->uvbuf.len = rstream->buffer_size - rstream->wpos;
@ -272,6 +275,7 @@ static void fread_idle_cb(uv_idle_t *handle)
static void close_cb(uv_handle_t *handle) static void close_cb(uv_handle_t *handle)
{ {
free(handle->data);
free(handle); free(handle);
} }

View File

@ -1,6 +1,8 @@
#ifndef NEOVIM_OS_RSTREAM_DEFS_H #ifndef NEOVIM_OS_RSTREAM_DEFS_H
#define NEOVIM_OS_RSTREAM_DEFS_H #define NEOVIM_OS_RSTREAM_DEFS_H
#include <stdbool.h>
typedef struct rstream RStream; typedef struct rstream RStream;
/// Type of function called when the RStream receives data /// Type of function called when the RStream receives data

View File

@ -3,6 +3,7 @@
#include <uv.h> #include <uv.h>
#include "os/uv_helpers.h"
#include "os/wstream.h" #include "os/wstream.h"
#include "os/wstream_defs.h" #include "os/wstream_defs.h"
#include "vim.h" #include "vim.h"
@ -54,7 +55,7 @@ void wstream_free(WStream *wstream)
void wstream_set_stream(WStream *wstream, uv_stream_t *stream) void wstream_set_stream(WStream *wstream, uv_stream_t *stream)
{ {
stream->data = wstream; handle_set_wstream((uv_handle_t *)stream, wstream);
wstream->stream = stream; wstream->stream = stream;
} }
@ -112,3 +113,4 @@ static void write_cb(uv_write_t *req, int status)
free(data); free(data);
} }