From 0e739f34801ff6810064a8fac570f6be2b61ae70 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 13 Aug 2021 20:31:57 -0400 Subject: 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. --- src/byte_sink.h | 66 --------------------------------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 src/byte_sink.h (limited to 'src/byte_sink.h') diff --git a/src/byte_sink.h b/src/byte_sink.h deleted file mode 100644 index d117a589..00000000 --- a/src/byte_sink.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - Copyright 2011-2020 David Robillard - - 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_BYTE_SINK_H -#define SERD_BYTE_SINK_H - -#include "serd/serd.h" - -#include -#include - -struct SerdByteSinkImpl { - SerdWriteFunc write_func; ///< User sink for TO_FUNCTION - SerdStreamCloseFunc close_func; ///< Optional function to close stream - void* stream; ///< User data for write_func - char* buf; ///< Local buffer iff block_size > 1 - size_t size; ///< Bytes written so far in this chunk - size_t block_size; ///< Size of chunks to write -}; - -static inline size_t -serd_byte_sink_write(const void* buf, size_t len, SerdByteSink* const sink) -{ - if (len == 0) { - return 0; - } - - if (sink->block_size == 1) { - return sink->write_func(buf, 1, len, sink->stream); - } - - const size_t orig_len = len; - while (len) { - const size_t space = sink->block_size - sink->size; - const size_t n = space < len ? space : len; - - // Write as much as possible into the remaining buffer space - memcpy(sink->buf + sink->size, buf, n); - sink->size += n; - buf = (const char*)buf + n; - len -= n; - - // Flush page if buffer is full - if (sink->size == sink->block_size) { - sink->write_func(sink->buf, 1, sink->block_size, sink->stream); - sink->size = 0; - } - } - - return orig_len; -} - -#endif // SERD_BYTE_SINK_H -- cgit v1.2.1