From 930f28478ca2573b7f7baf29a57a03cfa95a841f Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 2 Dec 2023 01:20:48 -0500 Subject: Add a set of limits to the world The idea here is to remove the burden of passing things around like stack sizes (where most users don't care and will be happy with a reasonably large default) and keeping the call sites to things like serd_reader_new() clean. The cost is a bit more state, so it's both more powerful and more potentially flaky, since changing the limits has action at a distance that isn't clear from the call site. I think it's worth it for the cleaner API in the common case, and the much better forward compatibility. --- src/reader.c | 4 ++-- src/serdi.c | 5 ++++- src/world.c | 19 ++++++++++++++++++- src/world.h | 1 + 4 files changed, 25 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/reader.c b/src/reader.c index 61da89ff..4702131e 100644 --- a/src/reader.c +++ b/src/reader.c @@ -146,9 +146,9 @@ serd_reader_read_document(SerdReader* const reader) SerdReader* serd_reader_new(SerdWorld* const world, const SerdSyntax syntax, - const SerdSink* const sink, - const size_t stack_size) + const SerdSink* const sink) { + const size_t stack_size = world->limits.reader_stack_size; if (stack_size < 3 * sizeof(SerdNode) + 192 + serd_node_align) { return NULL; } diff --git a/src/serdi.c b/src/serdi.c index 0610069d..105bda46 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -245,8 +245,11 @@ main(int argc, char** argv) SerdWriter* const writer = serd_writer_new( world, output_syntax, writer_flags, env, (SerdWriteFunc)fwrite, out_fd); + const SerdLimits limits = {stack_size}; + serd_world_set_limits(world, limits); + SerdReader* const reader = - serd_reader_new(world, input_syntax, serd_writer_sink(writer), stack_size); + serd_reader_new(world, input_syntax, serd_writer_sink(writer)); serd_reader_set_strict(reader, !lax); if (quiet) { diff --git a/src/world.c b/src/world.c index 21cdffab..7fb5eea2 100644 --- a/src/world.c +++ b/src/world.c @@ -16,6 +16,7 @@ # include #endif +#include #include #include #include @@ -102,7 +103,8 @@ serd_world_new(void) return NULL; } - world->blank_node = blank_node; + world->limits.reader_stack_size = 1048576U; + world->blank_node = blank_node; return world; } @@ -116,6 +118,21 @@ serd_world_free(SerdWorld* const world) } } +SerdLimits +serd_world_limits(const SerdWorld* const world) +{ + assert(world); + return world->limits; +} + +SerdStatus +serd_world_set_limits(SerdWorld* const world, const SerdLimits limits) +{ + assert(world); + world->limits = limits; + return SERD_SUCCESS; +} + const SerdNode* serd_world_get_blank(SerdWorld* const world) { diff --git a/src/world.h b/src/world.h index b330d4e4..96252a7a 100644 --- a/src/world.h +++ b/src/world.h @@ -13,6 +13,7 @@ #include struct SerdWorldImpl { + SerdLimits limits; SerdErrorFunc error_func; void* error_handle; uint32_t next_blank_id; -- cgit v1.2.1