From ea1936a594d7e4e656731223b76d355fc5431ef9 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 11 Dec 2011 22:11:22 +0000 Subject: Move all #ifdef gunk to serd_internal.h. Centralise file open and buffer allocation to localize platform tweaks. git-svn-id: http://svn.drobilla.net/serd/trunk@244 490d8e77-9747-427b-9fa3-0b8f29cee8a0 --- src/env.c | 4 ++-- src/node.c | 4 ++-- src/reader.c | 21 +++------------------ src/serd_internal.h | 36 ++++++++++++++++++++++++++++++++++++ src/serdi.c | 19 ++----------------- src/sink.c | 11 ++--------- src/uri.c | 4 +--- src/writer.c | 4 ++-- 8 files changed, 50 insertions(+), 53 deletions(-) diff --git a/src/env.c b/src/env.c index aaa910e1..6afcc77e 100644 --- a/src/env.c +++ b/src/env.c @@ -14,13 +14,13 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "serd_internal.h" + #include #include #include #include -#include "serd_internal.h" - typedef struct { SerdNode name; SerdNode uri; diff --git a/src/node.c b/src/node.c index 224bf407..02d6f493 100644 --- a/src/node.c +++ b/src/node.c @@ -14,11 +14,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "serd_internal.h" + #include #include -#include "serd_internal.h" - SERD_API SerdNode serd_node_from_string(SerdType type, const uint8_t* buf) diff --git a/src/reader.c b/src/reader.c index 68b5446d..a1ff0d1c 100644 --- a/src/reader.c +++ b/src/reader.c @@ -14,7 +14,7 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define _POSIX_C_SOURCE 201112L /* for posix_memalign */ +#include "serd_internal.h" #include #include @@ -25,13 +25,6 @@ #include #include -#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO) -# include -#endif - -#include "serd_internal.h" -#include "serd-config.h" - #define NS_XSD "http://www.w3.org/2001/XMLSchema#" #define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#" @@ -1516,14 +1509,10 @@ serd_reader_read_file(SerdReader* reader, return SERD_ERR_BAD_ARG; } - FILE* fd = fopen((const char*)path, "r"); + FILE* fd = serd_fopen((const char*)path, "r"); if (!fd) { - fprintf(stderr, "Error opening file %s (%s)\n", path, strerror(errno)); return SERD_ERR_UNKNOWN; } -#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO) - posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL); -#endif SerdStatus ret = serd_reader_read_file_handle(reader, fd, path); fclose(fd); @@ -1540,11 +1529,7 @@ serd_reader_read_file_handle(SerdReader* me, FILE* file, const uint8_t* name) me->cur = cur; me->from_file = true; me->eof = false; -#ifdef HAVE_POSIX_MEMALIGN - posix_memalign((void**)&me->read_buf, 4096, SERD_PAGE_SIZE * 2); -#else - me->read_buf = (uint8_t*)malloc(SERD_PAGE_SIZE * 2); -#endif + me->read_buf = serd_bufalloc(SERD_PAGE_SIZE * 2); /* Read into the second page of the buffer. Occasionally peek_string will move the read_head to before this point when readahead causes diff --git a/src/serd_internal.h b/src/serd_internal.h index f5f0f3b4..bf1f630f 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -17,13 +17,49 @@ #ifndef SERD_INTERNAL_H #define SERD_INTERNAL_H +#define _POSIX_C_SOURCE 201112L /* for posix_memalign and posix_fadvise */ + #include +#include +#include #include +#include #include "serd/serd.h" +#include "serd-config.h" + +#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO) +# include +#endif #define SERD_PAGE_SIZE 4096 +static inline FILE* +serd_fopen(const char* path, const char* mode) +{ + FILE* fd = fopen((const char*)path, mode); + if (!fd) { + fprintf(stderr, "Error opening file %s (%s)\n", path, strerror(errno)); + return NULL; + } +#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO) + posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL); +#endif + return fd; +} + +static inline void* +serd_bufalloc(size_t size) +{ +#ifdef HAVE_POSIX_MEMALIGN + void* ptr; + posix_memalign(&ptr, SERD_PAGE_SIZE, size); + return ptr; +#else + return malloc(size); +#endif +} + /** A dynamic stack in memory. */ typedef struct { uint8_t* buf; ///< Stack memory diff --git a/src/serdi.c b/src/serdi.c index b3261eac..dca821bb 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -14,22 +14,13 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define _POSIX_C_SOURCE 201112L /* for posix_memalign */ +#include "serd_internal.h" #include #include #include #include -#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO) -# include -#endif - -#include "serd/serd.h" - -#include "serd-config.h" -#include "serd_internal.h" - typedef struct { SerdEnv* env; SerdWriter* writer; @@ -176,15 +167,9 @@ main(int argc, char** argv) input += 5; } } - in_fd = fopen((const char*)input, "r"); - if (!in_fd) { - fprintf(stderr, "Error opening file %s (%s)\n", - input, strerror(errno)); + if (!(in_fd = serd_fopen((const char*)input, "r"))) { return 1; } -#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO) - posix_fadvise(fileno(in_fd), 0, 0, POSIX_FADV_SEQUENTIAL); -#endif } } diff --git a/src/sink.c b/src/sink.c index 3eb44b9a..3fa90e8c 100644 --- a/src/sink.c +++ b/src/sink.c @@ -14,14 +14,11 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#define _POSIX_C_SOURCE 201112L /* for posix_memalign */ +#include "serd_internal.h" #include #include -#include "serd_internal.h" -#include "serd-config.h" - #ifndef MIN # define MIN(a, b) (((a) < (b)) ? (a) : (b)) #endif @@ -43,11 +40,7 @@ serd_bulk_sink_new(SerdSink sink, void* stream, size_t block_size) bsink->stream = stream; bsink->size = 0; bsink->block_size = block_size; -#ifdef HAVE_POSIX_MEMALIGN - posix_memalign((void**)&bsink->buf, block_size, block_size); -#else - bsink->buf = (uint8_t*)malloc(block_size); -#endif + bsink->buf = serd_bufalloc(block_size); return bsink; } diff --git a/src/uri.c b/src/uri.c index 61fb35ce..24dd91d1 100644 --- a/src/uri.c +++ b/src/uri.c @@ -14,14 +14,12 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -/** @file uri.c */ +#include "serd_internal.h" #include #include #include -#include "serd_internal.h" - // #define URI_DEBUG 1 SERD_API diff --git a/src/writer.c b/src/writer.c index 8ab533d4..58ac35cd 100644 --- a/src/writer.c +++ b/src/writer.c @@ -14,13 +14,13 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include "serd_internal.h" + #include #include #include #include -#include "serd_internal.h" - #define NS_RDF "http://www.w3.org/1999/02/22-rdf-syntax-ns#" #define NS_XSD "http://www.w3.org/2001/XMLSchema#" -- cgit v1.2.1