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

@@ -283,25 +283,28 @@ static void dynamic_buffer_ensure(DynamicBuffer *buf, size_t desired)
buf->data = xrealloc(buf->data, buf->cap);
}
static void system_data_cb(RStream *rstream, void *data, bool eof)
static void system_data_cb(RStream *rstream, RBuffer *buf, void *data, bool eof)
{
Job *job = data;
DynamicBuffer *buf = job_data(job);
DynamicBuffer *dbuf = job_data(job);
size_t nread = rstream_pending(rstream);
dynamic_buffer_ensure(buf, buf->len + nread + 1);
rstream_read(rstream, buf->data + buf->len, nread);
buf->len += nread;
size_t nread = buf->size;
dynamic_buffer_ensure(dbuf, dbuf->len + nread + 1);
rbuffer_read(buf, dbuf->data + dbuf->len, nread);
dbuf->len += nread;
}
static void out_data_cb(RStream *rstream, void *data, bool eof)
static void out_data_cb(RStream *rstream, RBuffer *buf, void *data, bool eof)
{
RBuffer *rbuffer = rstream_buffer(rstream);
size_t written = write_output(rbuffer_read_ptr(rbuffer),
rbuffer_pending(rbuffer), false, eof);
rbuffer_consumed(rbuffer, written);
RBUFFER_UNTIL_EMPTY(buf, ptr, len) {
size_t written = write_output(ptr, len, false,
eof && len <= rbuffer_size(buf));
if (written) {
rbuffer_consumed(buf, written);
} else {
break;
}
}
}
/// Parses a command string into a sequence of words, taking quotes into