aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2021-01-13 19:30:45 +0100
committerDavid Robillard <d@drobilla.net>2021-03-08 23:23:05 -0500
commit10c706a040abeaf5c82db54086d4edb03a995cf3 (patch)
treea9eec56499e1f2cb90a623267b9be4d1453ce99e /src
parent024553949ab66abdea597bed386b2f36018df337 (diff)
downloadserd-10c706a040abeaf5c82db54086d4edb03a995cf3.tar.gz
serd-10c706a040abeaf5c82db54086d4edb03a995cf3.tar.bz2
serd-10c706a040abeaf5c82db54086d4edb03a995cf3.zip
Add SerdWorld for shared library state
Diffstat (limited to 'src')
-rw-r--r--src/attributes.h4
-rw-r--r--src/reader.c17
-rw-r--r--src/reader.h1
-rw-r--r--src/serd_internal.h14
-rw-r--r--src/serdi.c17
-rw-r--r--src/system.h6
-rw-r--r--src/world.c40
-rw-r--r--src/world.h27
-rw-r--r--src/writer.c18
9 files changed, 105 insertions, 39 deletions
diff --git a/src/attributes.h b/src/attributes.h
index 8628a868..25e5b505 100644
--- a/src/attributes.h
+++ b/src/attributes.h
@@ -18,9 +18,9 @@
#define SERD_ATTRIBUTES_H
#ifdef __GNUC__
-# define SERD_MALLOC_FUNC __attribute__((malloc))
+# define SERD_I_MALLOC_FUNC __attribute__((malloc))
#else
-# define SERD_MALLOC_FUNC
+# define SERD_I_MALLOC_FUNC
#endif
#endif // SERD_ATTRIBUTES_H
diff --git a/src/reader.c b/src/reader.c
index f375be42..640fb12b 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -36,7 +36,7 @@ r_err(SerdReader* reader, SerdStatus st, const char* fmt, ...)
va_start(args, fmt);
const Cursor* const cur = &reader->source.cur;
const SerdError e = {st, cur->filename, cur->line, cur->col, fmt, &args};
- serd_error(reader->error_func, reader->error_handle, &e);
+ serd_error(reader->world, &e);
va_end(args);
return st;
}
@@ -154,10 +154,14 @@ serd_reader_read_document(SerdReader* reader)
}
SerdReader*
-serd_reader_new(SerdSyntax syntax, const SerdSink* sink, size_t stack_size)
+serd_reader_new(SerdWorld* const world,
+ const SerdSyntax syntax,
+ const SerdSink* const sink,
+ const size_t stack_size)
{
SerdReader* me = (SerdReader*)calloc(1, sizeof(SerdReader));
+ me->world = world;
me->sink = sink;
me->default_graph = NULL;
me->stack = serd_stack_new(stack_size);
@@ -179,15 +183,6 @@ serd_reader_set_strict(SerdReader* reader, bool strict)
}
void
-serd_reader_set_error_sink(SerdReader* reader,
- SerdErrorFunc error_func,
- void* error_handle)
-{
- reader->error_func = error_func;
- reader->error_handle = error_handle;
-}
-
-void
serd_reader_free(SerdReader* reader)
{
if (!reader) {
diff --git a/src/reader.h b/src/reader.h
index 8c8d45d5..ce377fd3 100644
--- a/src/reader.h
+++ b/src/reader.h
@@ -50,6 +50,7 @@ typedef struct {
} ReadContext;
struct SerdReaderImpl {
+ SerdWorld* world;
const SerdSink* sink;
SerdErrorFunc error_func;
void* error_handle;
diff --git a/src/serd_internal.h b/src/serd_internal.h
index e16a2c12..1bb2a3bc 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -17,6 +17,8 @@
#ifndef SERD_INTERNAL_H
#define SERD_INTERNAL_H
+#include "world.h"
+
#include "serd/serd.h"
#include <stdio.h>
@@ -33,12 +35,16 @@
/* Error reporting */
static inline void
-serd_error(SerdErrorFunc error_func, void* handle, const SerdError* e)
+serd_error(const SerdWorld* world, const SerdError* e)
{
- if (error_func) {
- error_func(handle, e);
+ if (world->error_func) {
+ world->error_func(world->error_handle, e);
} else {
- fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col);
+ if (e->filename) {
+ fprintf(stderr, "error: %s:%u:%u: ", e->filename, e->line, e->col);
+ } else {
+ fprintf(stderr, "error: ");
+ }
vfprintf(stderr, e->fmt, *e->args);
}
}
diff --git a/src/serdi.c b/src/serdi.c
index 628ae004..3f0185ec 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -80,7 +80,7 @@ missing_arg(const char* name, char opt)
}
static SerdStatus
-quiet_error_sink(void* handle, const SerdError* e)
+quiet_error_func(void* const handle, const SerdError* const e)
{
(void)handle;
(void)e;
@@ -247,19 +247,19 @@ main(int argc, char** argv)
base = serd_new_file_uri(SERD_MEASURE_STRING(input), SERD_EMPTY_STRING());
}
- FILE* const out_fd = stdout;
- SerdEnv* const env = serd_env_new(serd_node_string_view(base));
+ FILE* const out_fd = stdout;
+ SerdWorld* const world = serd_world_new();
+ SerdEnv* const env = serd_env_new(serd_node_string_view(base));
- SerdWriter* writer = serd_writer_new(
- output_syntax, writer_flags, env, (SerdWriteFunc)fwrite, out_fd);
+ SerdWriter* const writer = serd_writer_new(
+ world, output_syntax, writer_flags, env, (SerdWriteFunc)fwrite, out_fd);
SerdReader* const reader =
- serd_reader_new(input_syntax, serd_writer_sink(writer), stack_size);
+ serd_reader_new(world, input_syntax, serd_writer_sink(writer), stack_size);
serd_reader_set_strict(reader, !lax);
if (quiet) {
- serd_reader_set_error_sink(reader, quiet_error_sink, NULL);
- serd_writer_set_error_sink(writer, quiet_error_sink, NULL);
+ serd_world_set_error_func(world, quiet_error_func, NULL);
}
SerdNode* root = serd_new_uri(SERD_MEASURE_STRING(root_uri));
@@ -292,6 +292,7 @@ main(int argc, char** argv)
serd_writer_free(writer);
serd_env_free(env);
serd_node_free(base);
+ serd_world_free(world);
if (fclose(stdout)) {
perror("serdi: write error");
diff --git a/src/system.h b/src/system.h
index df5827de..66546d6e 100644
--- a/src/system.h
+++ b/src/system.h
@@ -27,11 +27,13 @@ FILE*
serd_fopen(const char* path, const char* mode);
/// Allocate a buffer aligned to `alignment` bytes
-SERD_MALLOC_FUNC void*
+SERD_I_MALLOC_FUNC
+void*
serd_malloc_aligned(size_t alignment, size_t size);
/// Allocate an aligned buffer for I/O
-SERD_MALLOC_FUNC void*
+SERD_I_MALLOC_FUNC
+void*
serd_allocate_buffer(size_t size);
/// Free a buffer allocated with an aligned allocation function
diff --git a/src/world.c b/src/world.c
new file mode 100644
index 00000000..6f5a26c1
--- /dev/null
+++ b/src/world.c
@@ -0,0 +1,40 @@
+/*
+ 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 "world.h"
+
+#include <stdlib.h>
+
+SerdWorld*
+serd_world_new(void)
+{
+ return (SerdWorld*)calloc(1, sizeof(SerdWorld));
+}
+
+void
+serd_world_free(SerdWorld* world)
+{
+ free(world);
+}
+
+void
+serd_world_set_error_func(SerdWorld* world,
+ SerdErrorFunc error_func,
+ void* handle)
+{
+ world->error_func = error_func;
+ world->error_handle = handle;
+}
diff --git a/src/world.h b/src/world.h
new file mode 100644
index 00000000..3d994df9
--- /dev/null
+++ b/src/world.h
@@ -0,0 +1,27 @@
+/*
+ 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_WORLD_H
+#define SERD_WORLD_H
+
+#include "serd/serd.h"
+
+struct SerdWorldImpl {
+ SerdErrorFunc error_func;
+ void* error_handle;
+};
+
+#endif // SERD_WORLD_H
diff --git a/src/writer.c b/src/writer.c
index 5a934e41..de0e80a7 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -93,6 +93,7 @@ static const SepRule rules[] = {{NULL, 0, 0, 0, 0},
{"\n", 1, 0, 1, 0}};
struct SerdWriterImpl {
+ SerdWorld* world;
SerdSink iface;
SerdSyntax syntax;
SerdWriterFlags flags;
@@ -149,8 +150,8 @@ w_err(SerdWriter* writer, SerdStatus st, const char* fmt, ...)
va_list args;
va_start(args, fmt);
- const SerdError e = {st, "", 0, 0, fmt, &args};
- serd_error(writer->error_func, writer->error_handle, &e);
+ const SerdError e = {st, NULL, 0, 0, fmt, &args};
+ serd_error(writer->world, &e);
va_end(args);
}
@@ -966,7 +967,8 @@ serd_writer_finish(SerdWriter* writer)
}
SerdWriter*
-serd_writer_new(SerdSyntax syntax,
+serd_writer_new(SerdWorld* world,
+ SerdSyntax syntax,
SerdWriterFlags flags,
SerdEnv* env,
SerdWriteFunc ssink,
@@ -975,6 +977,7 @@ serd_writer_new(SerdSyntax syntax,
const WriteContext context = WRITE_CONTEXT_NULL;
SerdWriter* writer = (SerdWriter*)calloc(1, sizeof(SerdWriter));
+ writer->world = world;
writer->syntax = syntax;
writer->flags = flags;
writer->env = env;
@@ -997,15 +1000,6 @@ serd_writer_new(SerdSyntax syntax,
}
void
-serd_writer_set_error_sink(SerdWriter* writer,
- SerdErrorFunc error_func,
- void* error_handle)
-{
- writer->error_func = error_func;
- writer->error_handle = error_handle;
-}
-
-void
serd_writer_chop_blank_prefix(SerdWriter* writer, const char* prefix)
{
free(writer->bprefix);