diff options
author | David Robillard <d@drobilla.net> | 2021-09-10 13:20:47 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-01-28 21:57:07 -0500 |
commit | eb804125430e3445e85c423b28e1c41346772ed0 (patch) | |
tree | 532f6995ace537170fbbfde2d0d8226d1a48279b /src | |
parent | cbf01be4126cbc0f6d80364a7e0b6ad777a7d8ae (diff) | |
download | serd-eb804125430e3445e85c423b28e1c41346772ed0.tar.gz serd-eb804125430e3445e85c423b28e1c41346772ed0.tar.bz2 serd-eb804125430e3445e85c423b28e1c41346772ed0.zip |
Make environments and sinks part of the world
Although functions/components that require minimal pre-existing state are nice,
these allocate memory and could potentially share resources. So, give them a
pointer to a world which can be used to configure such things. In particular,
this is working towards supporting custom allocators everywhere.
Diffstat (limited to 'src')
-rw-r--r-- | src/canon.c | 2 | ||||
-rw-r--r-- | src/env.c | 13 | ||||
-rw-r--r-- | src/env.h | 4 | ||||
-rw-r--r-- | src/filter.c | 16 | ||||
-rw-r--r-- | src/inserter.c | 4 | ||||
-rw-r--r-- | src/node_syntax.c | 51 | ||||
-rw-r--r-- | src/sink.c | 9 |
7 files changed, 69 insertions, 30 deletions
diff --git a/src/canon.c b/src/canon.c index ac0c91c2..f8f1d8f0 100644 --- a/src/canon.c +++ b/src/canon.c @@ -191,5 +191,5 @@ serd_canon_new(const SerdWorld* const world, data->target = target; data->flags = flags; - return serd_sink_new(data, (SerdEventFunc)serd_canon_on_event, free); + return serd_sink_new(world, data, (SerdEventFunc)serd_canon_on_event, free); } @@ -32,6 +32,7 @@ typedef struct { } SerdPrefix; struct SerdEnvImpl { + SerdWorld* world; SerdNodes* nodes; SerdPrefix* prefixes; size_t n_prefixes; @@ -40,10 +41,13 @@ struct SerdEnvImpl { }; SerdEnv* -serd_env_new(const SerdStringView base_uri) +serd_env_new(SerdWorld* const world, const SerdStringView base_uri) { + assert(world); + SerdEnv* env = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl)); + env->world = world; env->nodes = serd_nodes_new(); if (env && base_uri.len) { @@ -62,6 +66,7 @@ serd_env_copy(const SerdEnv* const env) SerdEnv* copy = (SerdEnv*)calloc(1, sizeof(struct SerdEnvImpl)); if (copy) { + copy->world = env->world; copy->nodes = serd_nodes_new(); copy->n_prefixes = env->n_prefixes; @@ -115,6 +120,12 @@ serd_env_equals(const SerdEnv* const a, const SerdEnv* const b) return true; } +SerdWorld* +serd_env_world(const SerdEnv* const env) +{ + return env->world; +} + SerdURIView serd_env_base_uri_view(const SerdEnv* const env) { @@ -33,6 +33,10 @@ serd_env_expand_in_place(const SerdEnv* env, SerdStringView* uri_prefix, SerdStringView* uri_suffix); +SERD_PURE_FUNC +SerdWorld* +serd_env_world(const SerdEnv* env); + SERD_CONST_FUNC SerdURIView serd_env_base_uri_view(const SerdEnv* env); diff --git a/src/filter.c b/src/filter.c index e5e8fa29..8efe1e91 100644 --- a/src/filter.c +++ b/src/filter.c @@ -70,13 +70,15 @@ serd_filter_on_event(void* const handle, const SerdEvent* const event) } SerdSink* -serd_filter_new(const SerdSink* const target, - const SerdNode* const subject, - const SerdNode* const predicate, - const SerdNode* const object, - const SerdNode* const graph, - const bool inclusive) +serd_filter_new(const SerdWorld* const world, + const SerdSink* const target, + const SerdNode* const subject, + const SerdNode* const predicate, + const SerdNode* const object, + const SerdNode* const graph, + const bool inclusive) { + assert(world); assert(target); SerdFilterData* const data = @@ -101,5 +103,5 @@ serd_filter_new(const SerdSink* const target, data->graph = serd_node_copy(graph); } - return serd_sink_new(data, serd_filter_on_event, free_data); + return serd_sink_new(world, data, serd_filter_on_event, free_data); } diff --git a/src/inserter.c b/src/inserter.c index 96caf216..9f388921 100644 --- a/src/inserter.c +++ b/src/inserter.c @@ -118,8 +118,8 @@ serd_inserter_new(SerdModel* const model, const SerdNode* const default_graph) data->model = model; data->default_graph = serd_node_copy(default_graph); - SerdSink* const sink = - serd_sink_new(data, (SerdEventFunc)serd_inserter_on_event, free); + SerdSink* const sink = serd_sink_new( + model->world, data, (SerdEventFunc)serd_inserter_on_event, free); return sink; } diff --git a/src/node_syntax.c b/src/node_syntax.c index 5c579b84..261d4b03 100644 --- a/src/node_syntax.c +++ b/src/node_syntax.c @@ -14,6 +14,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "env.h" #include "writer.h" #include "serd/serd.h" @@ -35,7 +36,8 @@ on_node_string_event(void* const handle, const SerdEvent* const event) } static SerdNode* -serd_node_from_syntax_in(const char* const str, +serd_node_from_syntax_in(SerdWorld* const world, + const char* const str, const SerdSyntax syntax, SerdEnv* const env) { @@ -50,9 +52,9 @@ serd_node_from_syntax_in(const char* const str, snprintf(doc, doc_len + 1, "%s %s .", prelude, str); - SerdNode* object = NULL; - SerdWorld* const world = serd_world_new(); - SerdSink* const sink = serd_sink_new(&object, on_node_string_event, NULL); + SerdNode* object = NULL; + SerdSink* const sink = + serd_sink_new(world, &object, on_node_string_event, NULL); SerdReader* const reader = serd_reader_new(world, @@ -73,7 +75,6 @@ serd_node_from_syntax_in(const char* const str, serd_close_input(&in); serd_reader_free(reader); serd_sink_free(sink); - serd_world_free(world); free(doc); return object; @@ -87,22 +88,34 @@ serd_node_from_syntax(const char* const str, assert(str); if (env) { - return serd_node_from_syntax_in(str, syntax, env); + return serd_node_from_syntax_in(serd_env_world(env), str, syntax, env); } - SerdEnv* const temp_env = serd_env_new(SERD_EMPTY_STRING()); - SerdNode* const node = serd_node_from_syntax_in(str, syntax, temp_env); + SerdWorld* const temp_world = serd_world_new(); + if (!temp_world) { + return NULL; + } + + SerdEnv* const temp_env = serd_env_new(temp_world, SERD_EMPTY_STRING()); + if (!temp_env) { + serd_world_free(temp_world); + return NULL; + } + + SerdNode* const node = + serd_node_from_syntax_in(temp_world, str, syntax, temp_env); serd_env_free(temp_env); + serd_world_free(temp_world); return node; } static char* -serd_node_to_syntax_in(const SerdNode* const node, +serd_node_to_syntax_in(SerdWorld* const world, + const SerdNode* const node, const SerdSyntax syntax, const SerdEnv* const env) { - SerdWorld* const world = serd_world_new(); SerdBuffer buffer = {NULL, 0}; SerdOutputStream out = serd_open_output_buffer(&buffer); SerdWriter* const writer = serd_writer_new(world, syntax, 0, env, &out, 1); @@ -118,7 +131,6 @@ serd_node_to_syntax_in(const SerdNode* const node, serd_writer_free(writer); serd_close_output(&out); - serd_world_free(world); return result; } @@ -131,12 +143,19 @@ serd_node_to_syntax(const SerdNode* const node, assert(node); if (env) { - return serd_node_to_syntax_in(node, syntax, env); + return serd_node_to_syntax_in(serd_env_world(env), node, syntax, env); } - SerdEnv* const temp_env = serd_env_new(SERD_EMPTY_STRING()); - char* const string = serd_node_to_syntax_in(node, syntax, temp_env); + SerdWorld* const temp_world = serd_world_new(); + SerdEnv* const temp_env = serd_env_new(temp_world, SERD_EMPTY_STRING()); + if (temp_env) { + char* const string = + serd_node_to_syntax_in(temp_world, node, syntax, temp_env); - serd_env_free(temp_env); - return string; + serd_env_free(temp_env); + serd_world_free(temp_world); + return string; + } + + return NULL; } @@ -24,10 +24,13 @@ #include <stdlib.h> SerdSink* -serd_sink_new(void* const handle, - SerdEventFunc event_func, - SerdFreeFunc free_handle) +serd_sink_new(const SerdWorld* const world, + void* const handle, + SerdEventFunc event_func, + SerdFreeFunc free_handle) { + (void)world; + SerdSink* sink = (SerdSink*)calloc(1, sizeof(SerdSink)); sink->handle = handle; |