diff options
author | David Robillard <d@drobilla.net> | 2021-08-13 20:31:57 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:07 -0500 |
commit | 0e739f34801ff6810064a8fac570f6be2b61ae70 (patch) | |
tree | 4451739f8e9b00d490d2d59aa6b1f370ae99c356 /src/byte_sink.h | |
parent | 63e7e57237a79d0447b0450a7fd3148c43052299 (diff) | |
download | serd-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/byte_sink.h')
-rw-r--r-- | src/byte_sink.h | 66 |
1 files changed, 0 insertions, 66 deletions
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 <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_BYTE_SINK_H -#define SERD_BYTE_SINK_H - -#include "serd/serd.h" - -#include <stddef.h> -#include <string.h> - -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 |