aboutsummaryrefslogtreecommitdiffstats
path: root/src/byte_sink.c
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/byte_sink.c
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/byte_sink.c')
-rw-r--r--src/byte_sink.c124
1 files changed, 0 insertions, 124 deletions
diff --git a/src/byte_sink.c b/src/byte_sink.c
deleted file mode 100644
index b5b4039e..00000000
--- a/src/byte_sink.c
+++ /dev/null
@@ -1,124 +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.
-*/
-
-#include "byte_sink.h"
-
-#include "serd_config.h"
-#include "system.h"
-
-#include "serd/serd.h"
-
-#include <assert.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-
-#if USE_POSIX_FADVISE && USE_FILENO
-# include <fcntl.h>
-#endif
-
-SerdByteSink*
-serd_byte_sink_new_buffer(SerdBuffer* const buffer)
-{
- assert(buffer);
-
- return serd_byte_sink_new_function(
- serd_buffer_write, serd_buffer_close, buffer, 1u);
-}
-
-SerdByteSink*
-serd_byte_sink_new_function(const SerdWriteFunc write_func,
- const SerdStreamCloseFunc close_func,
- void* const stream,
- const size_t block_size)
-{
- if (!block_size) {
- return NULL;
- }
-
- SerdByteSink* sink = (SerdByteSink*)calloc(1, sizeof(SerdByteSink));
-
- sink->write_func = write_func;
- sink->close_func = close_func;
- sink->stream = stream;
- sink->block_size = block_size;
-
- if (block_size > 1) {
- sink->buf = (char*)serd_allocate_buffer(block_size);
- }
-
- return sink;
-}
-
-SerdByteSink*
-serd_byte_sink_new_filename(const char* const path, const size_t block_size)
-{
- assert(path);
-
- if (!block_size) {
- return NULL;
- }
-
- FILE* const file = fopen(path, "wb");
- if (!file) {
- return NULL;
- }
-
-#if USE_POSIX_FADVISE && USE_FILENO
- posix_fadvise(fileno(file), 0, 0, POSIX_FADV_SEQUENTIAL);
-#endif
-
- return serd_byte_sink_new_function(
- (SerdWriteFunc)fwrite, (SerdStreamCloseFunc)fclose, file, block_size);
-}
-
-void
-serd_byte_sink_flush(SerdByteSink* sink)
-{
- assert(sink);
-
- if (sink->stream && sink->block_size > 1 && sink->size > 0) {
- sink->write_func(sink->buf, 1, sink->size, sink->stream);
- sink->size = 0;
- }
-}
-
-SerdStatus
-serd_byte_sink_close(SerdByteSink* sink)
-{
- assert(sink);
-
- serd_byte_sink_flush(sink);
-
- if (sink->stream && sink->close_func) {
- const int st = sink->close_func(sink->stream);
- sink->stream = NULL;
- return st ? SERD_ERR_UNKNOWN : SERD_SUCCESS;
- }
-
- sink->stream = NULL;
- return SERD_SUCCESS;
-}
-
-void
-serd_byte_sink_free(SerdByteSink* const sink)
-{
- if (sink) {
- serd_byte_sink_close(sink);
- serd_free_aligned(sink->buf);
- free(sink);
- }
-}