aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-10-14 19:18:17 +0200
committerDavid Robillard <d@drobilla.net>2020-10-27 13:13:58 +0100
commit0e8a72aae08b64beb11d97f020c14126178d7545 (patch)
tree0cf783d174f2ddd95453c34aba3ef46fc3a5b9d7
parent3ff6565bdf0861f9db3527b58919ca7481deca6c (diff)
downloadserd-0e8a72aae08b64beb11d97f020c14126178d7545.tar.gz
serd-0e8a72aae08b64beb11d97f020c14126178d7545.tar.bz2
serd-0e8a72aae08b64beb11d97f020c14126178d7545.zip
Add function to SerdSink for freeing opaque handle
This can be used to associate dynamically allocated data with a sink and have it managed along with the sink's lifetime.
-rw-r--r--serd/serd.h6
-rw-r--r--src/sink.c14
-rw-r--r--src/sink.h1
-rw-r--r--tests/env_test.c2
-rw-r--r--tests/overflow_test.c2
-rw-r--r--tests/read_chunk_test.c2
-rw-r--r--tests/serd_test.c6
7 files changed, 23 insertions, 10 deletions
diff --git a/serd/serd.h b/serd/serd.h
index e471988c..72e64c20 100644
--- a/serd/serd.h
+++ b/serd/serd.h
@@ -1014,6 +1014,9 @@ serd_env_write_prefixes(const SerdEnv* env, const SerdSink* sink);
@{
*/
+/// Function to free an opaque handle
+typedef void (*SerdFreeFunc)(void* ptr);
+
/**
Create a new sink
@@ -1021,10 +1024,11 @@ serd_env_write_prefixes(const SerdEnv* env, const SerdSink* sink);
serd_sink_set_*_func functions to set handlers for various events.
@param handle Opaque handle that will be passed to sink functions.
+ @param free_handle Free function to call on handle in serd_sink_free().
*/
SERD_API
SerdSink*
-serd_sink_new(void* handle);
+serd_sink_new(void* handle, SerdFreeFunc free_handle);
/// Free `sink`
SERD_API
diff --git a/src/sink.c b/src/sink.c
index edaf271f..498935a0 100644
--- a/src/sink.c
+++ b/src/sink.c
@@ -23,18 +23,26 @@
#include <stdlib.h>
SerdSink*
-serd_sink_new(void* handle)
+serd_sink_new(void* handle, SerdFreeFunc free_handle)
{
SerdSink* sink = (SerdSink*)calloc(1, sizeof(SerdSink));
- sink->handle = handle;
+ sink->handle = handle;
+ sink->free_handle = free_handle;
+
return sink;
}
void
serd_sink_free(SerdSink* sink)
{
- free(sink);
+ if (sink) {
+ if (sink->free_handle) {
+ sink->free_handle(sink->handle);
+ }
+
+ free(sink);
+ }
}
SerdStatus
diff --git a/src/sink.h b/src/sink.h
index 57c52021..d9b5c59c 100644
--- a/src/sink.h
+++ b/src/sink.h
@@ -24,6 +24,7 @@
*/
struct SerdSinkImpl {
void* handle;
+ SerdFreeFunc free_handle;
SerdBaseFunc base;
SerdPrefixFunc prefix;
SerdStatementFunc statement;
diff --git a/tests/env_test.c b/tests/env_test.c
index c0629da5..ee172fff 100644
--- a/tests/env_test.c
+++ b/tests/env_test.c
@@ -86,7 +86,7 @@ test_env(void)
serd_node_free(blank);
size_t n_prefixes = 0;
- SerdSink* count_prefixes_sink = serd_sink_new(&n_prefixes);
+ SerdSink* count_prefixes_sink = serd_sink_new(&n_prefixes, NULL);
serd_sink_set_prefix_func(count_prefixes_sink, count_prefixes);
serd_env_set_prefix(env, pre, eg);
serd_env_write_prefixes(env, count_prefixes_sink);
diff --git a/tests/overflow_test.c b/tests/overflow_test.c
index 4a942f58..7e545793 100644
--- a/tests/overflow_test.c
+++ b/tests/overflow_test.c
@@ -56,7 +56,7 @@ main(void)
{NULL, 0}};
SerdWorld* world = serd_world_new();
- SerdSink* sink = serd_sink_new(NULL);
+ SerdSink* sink = serd_sink_new(NULL, NULL);
for (const Test* t = tests; t->str; ++t) {
const SerdStatus st = test(world, sink, t->str, t->stack_size);
diff --git a/tests/read_chunk_test.c b/tests/read_chunk_test.c
index 8ff3e03e..56c6abdc 100644
--- a/tests/read_chunk_test.c
+++ b/tests/read_chunk_test.c
@@ -74,7 +74,7 @@ int
main(void)
{
SerdWorld* world = serd_world_new();
- SerdSink* sink = serd_sink_new(NULL);
+ SerdSink* sink = serd_sink_new(NULL, NULL);
serd_sink_set_base_func(sink, on_base);
serd_sink_set_prefix_func(sink, on_prefix);
serd_sink_set_statement_func(sink, on_statement);
diff --git a/tests/serd_test.c b/tests/serd_test.c
index da3780f7..3b622c7a 100644
--- a/tests/serd_test.c
+++ b/tests/serd_test.c
@@ -119,7 +119,7 @@ test_read_chunks(void)
size_t n_statements = 0;
FILE* const f = tmpfile();
static const char null = 0;
- SerdSink* sink = serd_sink_new(&n_statements);
+ SerdSink* sink = serd_sink_new(&n_statements, NULL);
assert(sink);
serd_sink_set_statement_func(sink, count_statements);
@@ -202,7 +202,7 @@ test_read_string(void)
{
SerdWorld* world = serd_world_new();
size_t n_statements = 0;
- SerdSink* sink = serd_sink_new(&n_statements);
+ SerdSink* sink = serd_sink_new(&n_statements, NULL);
SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096);
assert(reader);
@@ -703,7 +703,7 @@ test_reader(const char* path)
SerdWorld* world = serd_world_new();
size_t n_statements = 0;
- SerdSink* sink = serd_sink_new(&n_statements);
+ SerdSink* sink = serd_sink_new(&n_statements, NULL);
serd_sink_set_statement_func(sink, count_statements);
SerdReader* reader = serd_reader_new(world, SERD_TURTLE, sink, 4096);