Browse Source

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
zrythm_meson
David Robillard 11 years ago
parent
commit
ea1936a594
  1. 4
      src/env.c
  2. 4
      src/node.c
  3. 21
      src/reader.c
  4. 36
      src/serd_internal.h
  5. 19
      src/serdi.c
  6. 11
      src/sink.c
  7. 4
      src/uri.c
  8. 4
      src/writer.c

4
src/env.c

@ -14,13 +14,13 @@ @@ -14,13 +14,13 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "serd_internal.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "serd_internal.h"
typedef struct {
SerdNode name;
SerdNode uri;

4
src/node.c

@ -14,11 +14,11 @@ @@ -14,11 +14,11 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "serd_internal.h"
#include <stdlib.h>
#include <string.h>
#include "serd_internal.h"
SERD_API
SerdNode
serd_node_from_string(SerdType type, const uint8_t* buf)

21
src/reader.c

@ -14,7 +14,7 @@ @@ -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 <assert.h>
#include <errno.h>
@ -25,13 +25,6 @@ @@ -25,13 +25,6 @@
#include <stdlib.h>
#include <string.h>
#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO)
# include <fcntl.h>
#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, @@ -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) @@ -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

36
src/serd_internal.h

@ -17,13 +17,49 @@ @@ -17,13 +17,49 @@
#ifndef SERD_INTERNAL_H
#define SERD_INTERNAL_H
#define _POSIX_C_SOURCE 201112L /* for posix_memalign and posix_fadvise */
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "serd/serd.h"
#include "serd-config.h"
#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO)
# include <fcntl.h>
#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

19
src/serdi.c

@ -14,22 +14,13 @@ @@ -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 <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#if defined(HAVE_POSIX_FADVISE) && defined(HAVE_FILENO)
# include <fcntl.h>
#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) @@ -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
}
}

11
src/sink.c

@ -14,14 +14,11 @@ @@ -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 <stdlib.h>
#include <string.h>
#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) @@ -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;
}

4
src/uri.c

@ -14,14 +14,12 @@ @@ -14,14 +14,12 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/** @file uri.c */
#include "serd_internal.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "serd_internal.h"
// #define URI_DEBUG 1
SERD_API

4
src/writer.c

@ -14,13 +14,13 @@ @@ -14,13 +14,13 @@
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "serd_internal.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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#"

Loading…
Cancel
Save