aboutsummaryrefslogtreecommitdiffstats
path: root/src/block_dumper.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-08-13 20:31:57 -0400
committerDavid Robillard <d@drobilla.net>2022-01-28 21:57:07 -0500
commit0e739f34801ff6810064a8fac570f6be2b61ae70 (patch)
tree4451739f8e9b00d490d2d59aa6b1f370ae99c356 /src/block_dumper.h
parent63e7e57237a79d0447b0450a7fd3148c43052299 (diff)
downloadserd-0e739f34801ff6810064a8fac570f6be2b61ae70.tar.gz
serd-0e739f34801ff6810064a8fac570f6be2b61ae70.tar.bz2
serd-0e739f34801ff6810064a8fac570f6be2b61ae70.zip
Simplify output stream API
This makes the paging mechanism an internal detail once again. While it's conceptually elegant to simply have a single write interface and have the block dumper just be another implementation of that, unfortunately it is not practical. The inlining of serd_block_dumper_write() is a significant performance boost, because it avoids a non-inlinable function call of overhead per character. Compared to the SerdByteSink approach, this removes the burden and overhead of needing to dynamically allocate the structure itself.
Diffstat (limited to 'src/block_dumper.h')
-rw-r--r--src/block_dumper.h88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/block_dumper.h b/src/block_dumper.h
new file mode 100644
index 00000000..7c718566
--- /dev/null
+++ b/src/block_dumper.h
@@ -0,0 +1,88 @@
+/*
+ Copyright 2011-2021 David Robillard <d@drobilla.net>
+
+ Permission to use, copy, modify, and/or distribute this software for any
+ purpose with or without fee is hereby granted, provided that the above
+ copyright notice and this permission notice appear in all copies.
+
+ THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+*/
+
+#ifndef SERD_BLOCK_DUMPER_H
+#define SERD_BLOCK_DUMPER_H
+
+#include "serd/serd.h"
+
+#include <stddef.h>
+#include <string.h>
+
+typedef struct {
+ SerdOutputStream* SERD_ALLOCATED out; ///< Output stream to write to
+ char* SERD_ALLOCATED buf; ///< Local buffer if needed
+ size_t size; ///< Bytes pending for this block
+ size_t block_size; ///< Block size to write in bytes
+} SerdBlockDumper;
+
+/**
+ Set up a new output stream wrapper that writes in blocks.
+
+ This allocates a buffer internally, which must be eventually freed by
+ calling serd_block_dumper_close().
+*/
+SerdStatus
+serd_block_dumper_open(SerdBlockDumper* SERD_NONNULL dumper,
+ SerdOutputStream* SERD_NONNULL output,
+ size_t block_size);
+
+void
+serd_block_dumper_flush(SerdBlockDumper* SERD_NONNULL dumper);
+
+void
+serd_block_dumper_close(SerdBlockDumper* SERD_NONNULL dumper);
+
+/**
+ Write some bytes to the page writer.
+
+ This works like any other SerdWriteFunc, but will append to an internal
+ buffer and only actually write to the output when a whole block is ready.
+*/
+static inline size_t
+serd_block_dumper_write(const void* SERD_NONNULL buf,
+ const size_t size,
+ const size_t nmemb,
+ SerdBlockDumper* SERD_NONNULL const dumper)
+{
+ if (dumper->block_size == 1) {
+ return dumper->out->write(buf, size, nmemb, dumper->out->stream);
+ }
+
+ size_t len = size * nmemb;
+ const size_t orig_len = len;
+ while (len) {
+ const size_t space = dumper->block_size - dumper->size;
+ const size_t n = space < len ? space : len;
+
+ // Write as much as possible into the remaining buffer space
+ memcpy(dumper->buf + dumper->size, buf, n);
+ dumper->size += n;
+ buf = (const char*)buf + n;
+ len -= n;
+
+ // Flush page if buffer is full
+ if (dumper->size == dumper->block_size) {
+ dumper->out->write(
+ dumper->buf, 1, dumper->block_size, dumper->out->stream);
+ dumper->size = 0;
+ }
+ }
+
+ return orig_len;
+}
+
+#endif // SERD_DUMPER_H