aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-05-11 11:09:23 +0200
committerDavid Robillard <d@drobilla.net>2019-04-13 19:15:32 +0200
commit583af959cc8b804914c887b9dc2b1c821bdf41ab (patch)
tree01a6ae403f3adaa65a79b66a80a3ad551731d559 /src
parentad7e36a6ba35d46128f1bc8d74fa1ad4c979e5ea (diff)
downloadserd-583af959cc8b804914c887b9dc2b1c821bdf41ab.tar.gz
serd-583af959cc8b804914c887b9dc2b1c821bdf41ab.tar.bz2
serd-583af959cc8b804914c887b9dc2b1c821bdf41ab.zip
Move system utilities to separate source files
Diffstat (limited to 'src')
-rw-r--r--src/byte_sink.h4
-rw-r--r--src/byte_source.c6
-rw-r--r--src/n3.c1
-rw-r--r--src/reader.c2
-rw-r--r--src/serd_internal.h43
-rw-r--r--src/serdi.c1
-rw-r--r--src/string.c1
-rw-r--r--src/string_utils.h1
-rw-r--r--src/system.c36
-rw-r--r--src/system.h44
-rw-r--r--src/world.c5
-rw-r--r--src/writer.c1
12 files changed, 98 insertions, 47 deletions
diff --git a/src/byte_sink.h b/src/byte_sink.h
index cd9cbce6..0d825020 100644
--- a/src/byte_sink.h
+++ b/src/byte_sink.h
@@ -22,6 +22,8 @@
#include "serd/serd.h"
+#include "system.h"
+
typedef struct SerdByteSinkImpl {
SerdWriteFunc sink;
void* stream;
@@ -39,7 +41,7 @@ serd_byte_sink_new(SerdWriteFunc sink, void* stream, size_t block_size)
bsink.size = 0;
bsink.block_size = block_size;
bsink.buf = ((block_size > 1)
- ? (char*)serd_bufalloc(block_size)
+ ? (char*)serd_allocate_buffer(block_size)
: NULL);
return bsink;
}
diff --git a/src/byte_source.c b/src/byte_source.c
index 2d79e161..19e942a5 100644
--- a/src/byte_source.c
+++ b/src/byte_source.c
@@ -15,9 +15,13 @@
*/
#include "byte_source.h"
+#include "system.h"
#include "serd_internal.h"
+#include <stdlib.h>
+#include <string.h>
+
SerdStatus
serd_byte_source_page(SerdByteSource* source)
{
@@ -56,7 +60,7 @@ serd_byte_source_open_source(SerdByteSource* source,
source->from_stream = true;
if (page_size > 1) {
- source->file_buf = (uint8_t*)serd_bufalloc(page_size);
+ source->file_buf = (uint8_t*)serd_allocate_buffer(page_size);
source->read_buf = source->file_buf;
memset(source->file_buf, '\0', page_size);
} else {
diff --git a/src/n3.c b/src/n3.c
index e200f323..1d43572b 100644
--- a/src/n3.c
+++ b/src/n3.c
@@ -18,7 +18,6 @@
#include <assert.h>
#include <ctype.h>
-#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/src/reader.c b/src/reader.c
index f8459e45..c50b4865 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -25,6 +25,8 @@
#include <string.h>
#include "reader.h"
+#include "system.h"
+#include "world.h"
static SerdStatus serd_reader_prepare(SerdReader* reader);
diff --git a/src/serd_internal.h b/src/serd_internal.h
index f06c7051..911d296f 100644
--- a/src/serd_internal.h
+++ b/src/serd_internal.h
@@ -17,57 +17,14 @@
#ifndef SERD_INTERNAL_H
#define SERD_INTERNAL_H
-#define _POSIX_C_SOURCE 200809L /* for posix_memalign and posix_fadvise */
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
#include "serd/serd.h"
#include "serd_config.h"
-#include "world.h"
-
-#if defined(HAVE_FILENO)
-# include <fcntl.h>
-#endif
-
#define NS_XSD "http://www.w3.org/2001/XMLSchema#"
#define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
-#define SERD_PAGE_SIZE 4096
-
#ifndef MIN
# define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
-/** fread-like wrapper for getc (which is faster). */
-static inline size_t
-serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream)
-{
- (void)size;
- (void)nmemb;
-
- const int c = getc((FILE*)stream);
- if (c == EOF) {
- *((uint8_t*)buf) = 0;
- return 0;
- }
- *((uint8_t*)buf) = (uint8_t)c;
- return 1;
-}
-
-static inline void*
-serd_bufalloc(size_t size)
-{
-#ifdef HAVE_POSIX_MEMALIGN
- void* ptr;
- const int ret = posix_memalign(&ptr, SERD_PAGE_SIZE, size);
- return ret ? NULL : ptr;
-#else
- return malloc(size);
-#endif
-}
-
#endif // SERD_INTERNAL_H
diff --git a/src/serdi.c b/src/serdi.c
index 0d0bce04..1272a70b 100644
--- a/src/serdi.c
+++ b/src/serdi.c
@@ -27,6 +27,7 @@
#include <string.h>
#include "string_utils.h"
+#include "system.h"
#include "world.h"
#define SERDI_ERROR(msg) fprintf(stderr, "serdi: " msg);
diff --git a/src/string.c b/src/string.c
index 6e0edcd8..83f94fd9 100644
--- a/src/string.c
+++ b/src/string.c
@@ -18,6 +18,7 @@
#include "string_utils.h"
#include <math.h>
+#include <stdlib.h>
void
serd_free(void* ptr)
diff --git a/src/string_utils.h b/src/string_utils.h
index f4620859..0c5ae557 100644
--- a/src/string_utils.h
+++ b/src/string_utils.h
@@ -18,6 +18,7 @@
#define SERD_STRING_UTILS_H
#include <ctype.h>
+#include <string.h>
/** Unicode replacement character in UTF-8 */
static const uint8_t replacement_char[] = { 0xEF, 0xBF, 0xBD };
diff --git a/src/system.c b/src/system.c
new file mode 100644
index 00000000..cf26bb37
--- /dev/null
+++ b/src/system.c
@@ -0,0 +1,36 @@
+/*
+ Copyright 2011-2018 David Robillard <http://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.
+*/
+
+#define _POSIX_C_SOURCE 200809L /* for posix_memalign */
+
+#include "system.h"
+
+#include "serd_config.h"
+#include "serd_internal.h"
+
+#include <stdlib.h>
+
+void*
+serd_allocate_buffer(size_t size)
+{
+#ifdef HAVE_POSIX_MEMALIGN
+ void* ptr;
+ const int ret = posix_memalign(&ptr, SERD_PAGE_SIZE, size);
+ return ret ? NULL : ptr;
+#else
+ return malloc(size);
+#endif
+}
diff --git a/src/system.h b/src/system.h
new file mode 100644
index 00000000..f17eb278
--- /dev/null
+++ b/src/system.h
@@ -0,0 +1,44 @@
+/*
+ Copyright 2011-2018 David Robillard <http://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_SYSTEM_H
+#define SERD_SYSTEM_H
+
+#include <stdint.h>
+#include <stdio.h>
+
+#define SERD_PAGE_SIZE 4096
+
+/** Faster fread-like wrapper for getc. */
+static inline size_t
+serd_file_read_byte(void* buf, size_t size, size_t nmemb, void* stream)
+{
+ (void)size;
+ (void)nmemb;
+
+ const int c = getc((FILE*)stream);
+ if (c == EOF) {
+ *((uint8_t*)buf) = 0;
+ return 0;
+ }
+ *((uint8_t*)buf) = (uint8_t)c;
+ return 1;
+}
+
+/** Allocate an aligned buffer for I/O. */
+void* serd_allocate_buffer(size_t size);
+
+#endif // SERD_SYSTEM_H
diff --git a/src/world.c b/src/world.c
index 7ec483e8..49a84d42 100644
--- a/src/world.c
+++ b/src/world.c
@@ -19,9 +19,12 @@
#include "serd_internal.h"
#include "serd_config.h"
+#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
-#if defined(HAVE_POSIX_FADVISE)
+#include <stdlib.h>
+#include <string.h>
+#if defined(HAVE_POSIX_FADVISE) || defined(HAVE_FILENO)
# include <fcntl.h>
#endif
diff --git a/src/writer.c b/src/writer.c
index ed8ab0c3..796780ee 100644
--- a/src/writer.c
+++ b/src/writer.c
@@ -26,6 +26,7 @@
#include "stack.h"
#include "string_utils.h"
#include "uri_utils.h"
+#include "world.h"
typedef struct {
SerdNode* graph;