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

@@ -328,19 +328,17 @@ static void channel_from_stdio(void)
channel->data.streams.uv = NULL;
}
static void job_out(RStream *rstream, void *data, bool eof)
static void job_out(RStream *rstream, RBuffer *buf, void *data, bool eof)
{
Job *job = data;
parse_msgpack(rstream, job_data(job), eof);
parse_msgpack(rstream, buf, job_data(job), eof);
}
static void job_err(RStream *rstream, void *data, bool eof)
static void job_err(RStream *rstream, RBuffer *rbuf, void *data, bool eof)
{
size_t count;
char buf[256];
while ((count = rstream_pending(rstream))) {
size_t read = rstream_read(rstream, buf, sizeof(buf) - 1);
while (rbuffer_size(rbuf)) {
char buf[256];
size_t read = rbuffer_read(rbuf, buf, sizeof(buf) - 1);
buf[read] = NUL;
ELOG("Channel %" PRIu64 " stderr: %s",
((Channel *)job_data(data))->id, buf);
@@ -352,7 +350,7 @@ static void job_exit(Job *job, int status, void *data)
decref(data);
}
static void parse_msgpack(RStream *rstream, void *data, bool eof)
static void parse_msgpack(RStream *rstream, RBuffer *rbuf, void *data, bool eof)
{
Channel *channel = data;
incref(channel);
@@ -363,14 +361,14 @@ static void parse_msgpack(RStream *rstream, void *data, bool eof)
goto end;
}
size_t count = rstream_pending(rstream);
size_t count = rbuffer_size(rbuf);
DLOG("Feeding the msgpack parser with %u bytes of data from RStream(%p)",
count,
rstream);
// Feed the unpacker with data
msgpack_unpacker_reserve_buffer(channel->unpacker, count);
rstream_read(rstream, msgpack_unpacker_buffer(channel->unpacker), count);
rbuffer_read(rbuf, msgpack_unpacker_buffer(channel->unpacker), count);
msgpack_unpacker_buffer_consumed(channel->unpacker, count);
msgpack_unpacked unpacked;