aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2017-04-14 19:19:01 +0200
committerDavid Robillard <d@drobilla.net>2017-04-14 20:58:03 +0200
commitf23415e2396d2a1b20b20c937b0632e5e10e7a29 (patch)
tree7fb82f726907e025273a73a1d0564c9ecb7f4940
parentadea993fb14ddfd01191b3008ad5b2675f0fbc06 (diff)
downloadserd-f23415e2396d2a1b20b20c937b0632e5e10e7a29.tar.gz
serd-f23415e2396d2a1b20b20c937b0632e5e10e7a29.tar.bz2
serd-f23415e2396d2a1b20b20c937b0632e5e10e7a29.zip
Make BulkSink general and factor out details from Writer
-rw-r--r--src/serd_internal.h34
-rw-r--r--src/writer.c27
2 files changed, 27 insertions, 34 deletions
diff --git a/src/serd_internal.h b/src/serd_internal.h
index ef83f03c..3ccc7f09 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -201,48 +201,56 @@ serd_stack_pop_aligned(SerdStack* stack, size_t n_bytes)
serd_stack_pop(stack, pad + 1);
}
-/* Bulk Sink */
+/* Byte Sink */
-typedef struct SerdBulkSinkImpl {
+typedef struct SerdByteSinkImpl {
SerdSink sink;
void* stream;
uint8_t* buf;
size_t size;
size_t block_size;
-} SerdBulkSink;
+} SerdByteSink;
-static inline SerdBulkSink
-serd_bulk_sink_new(SerdSink sink, void* stream, size_t block_size)
+static inline SerdByteSink
+serd_byte_sink_new(SerdSink sink, void* stream, size_t block_size)
{
- SerdBulkSink bsink;
+ SerdByteSink bsink;
bsink.sink = sink;
bsink.stream = stream;
bsink.size = 0;
bsink.block_size = block_size;
- bsink.buf = (uint8_t*)serd_bufalloc(block_size);
+ bsink.buf = ((block_size > 1)
+ ? (uint8_t*)serd_bufalloc(block_size)
+ : NULL);
return bsink;
}
static inline void
-serd_bulk_sink_flush(SerdBulkSink* bsink)
+serd_byte_sink_flush(SerdByteSink* bsink)
{
- if (bsink->size > 0) {
+ if (bsink->block_size > 1 && bsink->size > 0) {
bsink->sink(bsink->buf, bsink->size, bsink->stream);
+ bsink->size = 0;
}
- bsink->size = 0;
}
static inline void
-serd_bulk_sink_free(SerdBulkSink* bsink)
+serd_byte_sink_free(SerdByteSink* bsink)
{
- serd_bulk_sink_flush(bsink);
+ serd_byte_sink_flush(bsink);
free(bsink->buf);
bsink->buf = NULL;
}
static inline size_t
-serd_bulk_sink_write(const void* buf, size_t len, SerdBulkSink* bsink)
+serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* bsink)
{
+ if (len == 0) {
+ return 0;
+ } else if (bsink->block_size == 1) {
+ return bsink->sink(buf, len, bsink->stream);
+ }
+
const size_t orig_len = len;
while (len) {
const size_t space = bsink->block_size - bsink->size;
diff --git a/src/writer.c b/src/writer.c
index 1917f8f6..8e57b681 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -85,9 +85,7 @@ struct SerdWriterImpl {
SerdURI root_uri;
SerdURI base_uri;
SerdStack anon_stack;
- SerdBulkSink bulk_sink;
- SerdSink sink;
- void* stream;
+ SerdByteSink byte_sink;
SerdErrorSink error_sink;
void* error_handle;
WriteContext context;
@@ -153,13 +151,7 @@ copy_node(SerdNode* dst, const SerdNode* src)
static inline size_t
sink(const void* buf, size_t len, SerdWriter* writer)
{
- if (len == 0) {
- return 0;
- } else if (writer->style & SERD_STYLE_BULK) {
- return serd_bulk_sink_write(buf, len, &writer->bulk_sink);
- } else {
- return writer->sink(buf, len, writer->stream);
- }
+ return serd_byte_sink_write(buf, len, &writer->byte_sink);
}
// Parse a UTF-8 character, set *size to the length, and return the code point
@@ -828,9 +820,7 @@ serd_writer_finish(SerdWriter* writer)
if (writer->context.graph.type) {
sink("}\n", 2, writer);
}
- if (writer->style & SERD_STYLE_BULK) {
- serd_bulk_sink_flush(&writer->bulk_sink);
- }
+ serd_byte_sink_flush(&writer->byte_sink);
writer->indent = 0;
return free_context(writer);
}
@@ -853,14 +843,11 @@ serd_writer_new(SerdSyntax syntax,
writer->root_uri = SERD_URI_NULL;
writer->base_uri = base_uri ? *base_uri : SERD_URI_NULL;
writer->anon_stack = serd_stack_new(sizeof(WriteContext));
- writer->sink = ssink;
- writer->stream = stream;
writer->context = context;
writer->list_subj = SERD_NODE_NULL;
writer->empty = true;
- if (style & SERD_STYLE_BULK) {
- writer->bulk_sink = serd_bulk_sink_new(ssink, stream, SERD_PAGE_SIZE);
- }
+ writer->byte_sink = serd_byte_sink_new(
+ ssink, stream, (style & SERD_STYLE_BULK) ? SERD_PAGE_SIZE : 1);
return writer;
}
@@ -959,9 +946,7 @@ serd_writer_free(SerdWriter* writer)
serd_writer_finish(writer);
serd_stack_free(&writer->anon_stack);
free(writer->bprefix);
- if (writer->style & SERD_STYLE_BULK) {
- serd_bulk_sink_free(&writer->bulk_sink);
- }
+ serd_byte_sink_free(&writer->byte_sink);
serd_node_free(&writer->root_node);
free(writer);
}