aboutsummaryrefslogtreecommitdiffstats
path: root/src/serd_internal.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2011-12-11 22:11:22 +0000
committerDavid Robillard <d@drobilla.net>2011-12-11 22:11:22 +0000
commitea1936a594d7e4e656731223b76d355fc5431ef9 (patch)
tree87393e02793fba4cd44fb8da2db83f24380b509a /src/serd_internal.h
parente08decb5992d6511e6896939a4cd9ad157831b78 (diff)
downloadserd-ea1936a594d7e4e656731223b76d355fc5431ef9.tar.gz
serd-ea1936a594d7e4e656731223b76d355fc5431ef9.tar.bz2
serd-ea1936a594d7e4e656731223b76d355fc5431ef9.zip
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
Diffstat (limited to 'src/serd_internal.h')
-rw-r--r--src/serd_internal.h36
1 files changed, 36 insertions, 0 deletions
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 <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