From f91127d72a1227a5adae57d641100c8f29741937 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 11 May 2018 11:09:23 +0200 Subject: Move system utilities to separate source files --- src/byte_sink.h | 4 +++- src/byte_source.c | 9 ++------- src/reader.c | 1 + src/serd_internal.h | 46 +--------------------------------------------- src/serdi.c | 2 +- src/string_utils.h | 4 +--- src/system.c | 36 ++++++++++++++++++++++++++++++++++++ src/system.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/world.c | 8 ++++---- src/writer.c | 1 + 10 files changed, 94 insertions(+), 61 deletions(-) create mode 100644 src/system.c create mode 100644 src/system.h (limited to 'src') diff --git a/src/byte_sink.h b/src/byte_sink.h index 7f891e7d..69a0c8c9 100644 --- a/src/byte_sink.h +++ b/src/byte_sink.h @@ -18,10 +18,12 @@ #define SERD_BYTE_SINK_H #include "serd_internal.h" +#include "system.h" #include "serd/serd.h" #include +#include #include typedef struct SerdByteSinkImpl { @@ -41,7 +43,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 59ab673f..0153c08b 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -15,13 +15,8 @@ */ #include "byte_source.h" +#include "system.h" -#include "serd_internal.h" - -#include "serd/serd.h" - -#include -#include #include #include @@ -65,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/reader.c b/src/reader.c index 7e34bee7..44a90912 100644 --- a/src/reader.c +++ b/src/reader.c @@ -17,6 +17,7 @@ #include "reader.h" #include "serd_internal.h" +#include "system.h" #include "world.h" #include diff --git a/src/serd_internal.h b/src/serd_internal.h index 64094a88..d2ae23a6 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -17,57 +17,13 @@ #ifndef SERD_INTERNAL_H #define SERD_INTERNAL_H -#define _POSIX_C_SOURCE 200809L /* for posix_memalign and posix_fadvise */ - -#include "serd_config.h" -#include "world.h" - -#if defined(HAVE_POSIX_MEMALIGN) -# include -#endif - -#include -#include -#include -#include -#include -#include +#include "serd/serd.h" #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 = NULL; - 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 b9a7011b..9621cc3a 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -15,8 +15,8 @@ */ #include "serd_config.h" -#include "serd_internal.h" #include "string_utils.h" +#include "system.h" #include "serd/serd.h" diff --git a/src/string_utils.h b/src/string_utils.h index 23ad4914..a6c731bb 100644 --- a/src/string_utils.h +++ b/src/string_utils.h @@ -21,9 +21,7 @@ #include #include -#include -#include -#include +#include /** 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..8ee41640 --- /dev/null +++ b/src/system.c @@ -0,0 +1,36 @@ +/* + Copyright 2011-2018 David Robillard + + 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 + +void* +serd_allocate_buffer(size_t size) +{ +#ifdef HAVE_POSIX_MEMALIGN + void* ptr = NULL; + const int ret = posix_memalign(&ptr, SERD_PAGE_SIZE, size); + return ret ? NULL : ptr; +#else + (void)alignment; + 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 + + 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 +#include + +#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 2a29820a..50f5bc9e 100644 --- a/src/world.c +++ b/src/world.c @@ -20,16 +20,16 @@ #include "serd_config.h" -#if defined(HAVE_POSIX_FADVISE) -# include -#endif - #include #include #include #include #include +#if defined(HAVE_POSIX_FADVISE) || defined(HAVE_FILENO) +# include +#endif + FILE* serd_world_fopen(SerdWorld* world, const char* path, const char* mode) { diff --git a/src/writer.c b/src/writer.c index a81aba1a..28de2fc0 100644 --- a/src/writer.c +++ b/src/writer.c @@ -19,6 +19,7 @@ #include "serd_internal.h" #include "stack.h" #include "string_utils.h" +#include "system.h" #include "uri_utils.h" #include "world.h" -- cgit v1.2.1