rbuffer: Reimplement as a ring buffer and decouple from rstream

Extract the RBuffer class from rstream.c and reimplement it as a ring buffer,
a more efficient version that doesn't need to relocate memory.

The old rbuffer_read/rbuffer_write interfaces are kept for simple
reading/writing, and the RBUFFER_UNTIL_{FULL,EMPTY} macros are introduced to
hide wrapping logic when more control is required(such as passing the buffer
pointer to a library function that writes directly to the pointer)

Also add a basic infrastructure for writing helper C files that are only
compiled in the unit test library, and use this to write unit tests for RBuffer
which contains some macros that can't be accessed directly by luajit.

Helped-by: oni-link <knil.ino@gmail.com>
Reviewed-by: oni-link <knil.ino@gmail.com>
Reviewed-by: Scott Prager <splinterofchaos@gmail.com>
Reviewed-by: Justin M. Keyes <justinkz@gmail.com>
Reviewed-by: Michael Reed <m.reed@mykolab.com>
This commit is contained in:
Thiago de Arruda
2015-06-30 13:37:19 -03:00
parent dcaf9c6bc3
commit 0ef80b9c2b
16 changed files with 790 additions and 272 deletions

View File

@@ -20341,19 +20341,19 @@ static inline void push_job_event(Job *job, ufunc_T *callback,
}, !disable_job_defer);
}
static void on_job_stdout(RStream *rstream, void *job, bool eof)
static void on_job_stdout(RStream *rstream, RBuffer *buf, void *job, bool eof)
{
TerminalJobData *data = job_data(job);
on_job_output(rstream, job, eof, data->on_stdout, "stdout");
on_job_output(rstream, job, buf, eof, data->on_stdout, "stdout");
}
static void on_job_stderr(RStream *rstream, void *job, bool eof)
static void on_job_stderr(RStream *rstream, RBuffer *buf, void *job, bool eof)
{
TerminalJobData *data = job_data(job);
on_job_output(rstream, job, eof, data->on_stderr, "stderr");
on_job_output(rstream, job, buf, eof, data->on_stderr, "stderr");
}
static void on_job_output(RStream *rstream, Job *job, bool eof,
static void on_job_output(RStream *rstream, Job *job, RBuffer *buf, bool eof,
ufunc_T *callback, const char *type)
{
if (eof) {
@@ -20361,20 +20361,19 @@ static void on_job_output(RStream *rstream, Job *job, bool eof,
}
TerminalJobData *data = job_data(job);
char *ptr = rstream_read_ptr(rstream);
size_t len = rstream_pending(rstream);
RBUFFER_UNTIL_EMPTY(buf, ptr, len) {
// The order here matters, the terminal must receive the data first because
// push_job_event will modify the read buffer(convert NULs into NLs)
if (data->term) {
terminal_receive(data->term, ptr, len);
}
// The order here matters, the terminal must receive the data first because
// push_job_event will modify the read buffer(convert NULs into NLs)
if (data->term) {
terminal_receive(data->term, ptr, len);
if (callback) {
push_job_event(job, callback, type, ptr, len, 0);
}
rbuffer_consumed(buf, len);
}
if (callback) {
push_job_event(job, callback, type, ptr, len, 0);
}
rbuffer_consumed(rstream_buffer(rstream), len);
}
static void on_job_exit(Job *job, int status, void *d)