diff options
author | David Robillard <d@drobilla.net> | 2018-05-11 11:09:23 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2018-05-27 18:23:15 +0200 |
commit | 227834eebd2aad13b25bddae3e65cf51877333ee (patch) | |
tree | 3add777bddf9f66f2cabf6b5da2994b070bc3e04 | |
parent | a3b6babaf0186b34d801a75b62253573ef26a154 (diff) | |
download | serd-227834eebd2aad13b25bddae3e65cf51877333ee.tar.gz serd-227834eebd2aad13b25bddae3e65cf51877333ee.tar.bz2 serd-227834eebd2aad13b25bddae3e65cf51877333ee.zip |
Move system utilities to separate source files
-rw-r--r-- | src/byte_sink.h | 4 | ||||
-rw-r--r-- | src/byte_source.c | 3 | ||||
-rw-r--r-- | src/reader.c | 1 | ||||
-rw-r--r-- | src/serd_internal.h | 27 | ||||
-rw-r--r-- | src/serdi.c | 1 | ||||
-rw-r--r-- | src/system.c | 37 | ||||
-rw-r--r-- | src/system.h | 42 | ||||
-rw-r--r-- | wscript | 1 |
8 files changed, 87 insertions, 29 deletions
diff --git a/src/byte_sink.h b/src/byte_sink.h index dca08825..64279534 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 { SerdSink sink; void* stream; @@ -39,7 +41,7 @@ serd_byte_sink_new(SerdSink 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 25184f13..6e11f863 100644 --- a/src/byte_source.c +++ b/src/byte_source.c @@ -17,6 +17,7 @@ #include "serd_internal.h" #include "byte_source.h" +#include "system.h" static inline SerdStatus serd_byte_source_page(SerdByteSource* source) @@ -56,7 +57,7 @@ serd_byte_source_open_source(SerdByteSource* source, source->from_stream = true; if (page_size > 1) { - source->file_buf = (char*)serd_bufalloc(page_size); + source->file_buf = (char*)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 28c2b2f0..45ef43bc 100644 --- a/src/reader.c +++ b/src/reader.c @@ -25,6 +25,7 @@ #include <string.h> #include "reader.h" +#include "system.h" static SerdStatus serd_reader_prepare(SerdReader* reader); diff --git a/src/serd_internal.h b/src/serd_internal.h index 9200776d..c7260101 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -36,35 +36,8 @@ #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) -{ - 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 2c87e5e9..9e72737d 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -22,6 +22,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/system.c b/src/system.c new file mode 100644 index 00000000..7229ab6a --- /dev/null +++ b/src/system.c @@ -0,0 +1,37 @@ +/* + 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_fadvise */ + +#include "system.h" + +#include "serd_config.h" +#include "serd_internal.h" + +#include <stddef.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..b52895f6 --- /dev/null +++ b/src/system.h @@ -0,0 +1,42 @@ +/* + 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 <stddef.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) +{ + 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 @@ -81,6 +81,7 @@ lib_source = ['src/byte_source.c', 'src/node.c', 'src/reader.c', 'src/string.c', + 'src/system.c', 'src/uri.c', 'src/world.c', 'src/writer.c'] |