aboutsummaryrefslogtreecommitdiffstats
path: root/src/zix
diff options
context:
space:
mode:
Diffstat (limited to 'src/zix')
-rw-r--r--src/zix/common.h57
-rw-r--r--src/zix/ring.c49
-rw-r--r--src/zix/ring.h13
-rw-r--r--src/zix/sem.h8
-rw-r--r--src/zix/thread.h20
5 files changed, 85 insertions, 62 deletions
diff --git a/src/zix/common.h b/src/zix/common.h
index 9318d0e..f7ec4c3 100644
--- a/src/zix/common.h
+++ b/src/zix/common.h
@@ -17,6 +17,8 @@
#ifndef ZIX_COMMON_H
#define ZIX_COMMON_H
+#include <stdbool.h>
+
/**
@addtogroup zix
@{
@@ -44,18 +46,40 @@
# define ZIX_API
# define ZIX_PRIVATE static
#endif
+
+#ifdef __GNUC__
+# define ZIX_PURE_FUNC __attribute__((pure))
+# define ZIX_CONST_FUNC __attribute__((const))
+# define ZIX_MALLOC_FUNC __attribute__((malloc))
+#else
+# define ZIX_PURE_FUNC
+# define ZIX_CONST_FUNC
+# define ZIX_MALLOC_FUNC
+#endif
+
+#define ZIX_PURE_API ZIX_API ZIX_PURE_FUNC
+#define ZIX_CONST_API ZIX_API ZIX_CONST_FUNC
+#define ZIX_MALLOC_API ZIX_API ZIX_MALLOC_FUNC
+
/** @endcond */
#ifdef __cplusplus
extern "C" {
-#else
-# include <stdbool.h>
#endif
#ifdef __GNUC__
-#define ZIX_UNUSED __attribute__((__unused__))
+#define ZIX_LOG_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
+#else
+#define ZIX_LOG_FUNC(fmt, arg1)
+#endif
+
+// Unused parameter macro to suppresses warnings and make it impossible to use
+#if defined(__cplusplus)
+# define ZIX_UNUSED(name)
+#elif defined(__GNUC__)
+# define ZIX_UNUSED(name) name##_unused __attribute__((__unused__))
#else
-#define ZIX_UNUSED
+# define ZIX_UNUSED(name) name
#endif
typedef enum {
@@ -72,20 +96,13 @@ static inline const char*
zix_strerror(const ZixStatus status)
{
switch (status) {
- case ZIX_STATUS_SUCCESS:
- return "Success";
- case ZIX_STATUS_ERROR:
- return "Unknown error";
- case ZIX_STATUS_NO_MEM:
- return "Out of memory";
- case ZIX_STATUS_NOT_FOUND:
- return "Not found";
- case ZIX_STATUS_EXISTS:
- return "Exists";
- case ZIX_STATUS_BAD_ARG:
- return "Bad argument";
- case ZIX_STATUS_BAD_PERMS:
- return "Bad permissions";
+ case ZIX_STATUS_SUCCESS: return "Success";
+ case ZIX_STATUS_ERROR: return "Unknown error";
+ case ZIX_STATUS_NO_MEM: return "Out of memory";
+ case ZIX_STATUS_NOT_FOUND: return "Not found";
+ case ZIX_STATUS_EXISTS: return "Exists";
+ case ZIX_STATUS_BAD_ARG: return "Bad argument";
+ case ZIX_STATUS_BAD_PERMS: return "Bad permissions";
}
return "Unknown error";
}
@@ -93,7 +110,9 @@ zix_strerror(const ZixStatus status)
/**
Function for comparing two elements.
*/
-typedef int (*ZixComparator)(const void* a, const void* b, void* user_data);
+typedef int (*ZixComparator)(const void* a,
+ const void* b,
+ const void* user_data);
/**
Function for testing equality of two elements.
diff --git a/src/zix/ring.c b/src/zix/ring.c
index 6af9e07..cdbcea8 100644
--- a/src/zix/ring.c
+++ b/src/zix/ring.c
@@ -1,5 +1,5 @@
/*
- Copyright 2011-2017 David Robillard <http://drobilla.net>
+ Copyright 2011 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
@@ -16,6 +16,9 @@
#include "zix/ring.h"
+#include <stdlib.h>
+#include <string.h>
+
#ifdef HAVE_MLOCK
# include <sys/mman.h>
# define ZIX_MLOCK(ptr, size) mlock((ptr), (size))
@@ -27,31 +30,19 @@
# define ZIX_MLOCK(ptr, size)
#endif
-#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_ATOMICS__)
-# include <stdatomic.h>
-# define ZIX_WRITE_BARRIER() atomic_thread_fence(memory_order_release)
-# define ZIX_READ_BARRIER() atomic_thread_fence(memory_order_acquire)
-#elif defined(__APPLE__) /* Pre 10.12 */
-# include <libkern/OSAtomic.h>
-# define ZIX_WRITE_BARRIER() OSMemoryBarrier()
-# define ZIX_READ_BARRIER() OSMemoryBarrier()
-#elif defined(_WIN32)
+#if defined(_MSC_VER)
# include <windows.h>
-# define ZIX_WRITE_BARRIER() MemoryBarrier()
# define ZIX_READ_BARRIER() MemoryBarrier()
-#elif (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
-# define ZIX_WRITE_BARRIER() __sync_synchronize()
-# define ZIX_READ_BARRIER() __sync_synchronize()
+# define ZIX_WRITE_BARRIER() MemoryBarrier()
+#elif defined(__GNUC__)
+# define ZIX_READ_BARRIER() __atomic_thread_fence(__ATOMIC_ACQUIRE)
+# define ZIX_WRITE_BARRIER() __atomic_thread_fence(__ATOMIC_RELEASE)
#else
# pragma message("warning: No memory barriers, possible SMP bugs")
-# define ZIX_WRITE_BARRIER()
# define ZIX_READ_BARRIER()
+# define ZIX_WRITE_BARRIER()
#endif
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
-
struct ZixRingImpl {
uint32_t write_head; ///< Read index into buf
uint32_t read_head; ///< Write index into buf
@@ -65,11 +56,11 @@ next_power_of_two(uint32_t size)
{
// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
size--;
- size |= size >> 1;
- size |= size >> 2;
- size |= size >> 4;
- size |= size >> 8;
- size |= size >> 16;
+ size |= size >> 1u;
+ size |= size >> 2u;
+ size |= size >> 4u;
+ size |= size >> 8u;
+ size |= size >> 16u;
size++;
return size;
}
@@ -89,10 +80,8 @@ zix_ring_new(uint32_t size)
void
zix_ring_free(ZixRing* ring)
{
- if (ring) {
- free(ring->buf);
- free(ring);
- }
+ free(ring->buf);
+ free(ring);
}
void
@@ -184,9 +173,9 @@ zix_ring_read(ZixRing* ring, void* dst, uint32_t size)
ZIX_READ_BARRIER();
ring->read_head = (r + size) & ring->size_mask;
return size;
- } else {
- return 0;
}
+
+ return 0;
}
uint32_t
diff --git a/src/zix/ring.h b/src/zix/ring.h
index 168f009..de18561 100644
--- a/src/zix/ring.h
+++ b/src/zix/ring.h
@@ -17,6 +17,8 @@
#ifndef ZIX_RING_H
#define ZIX_RING_H
+#include "zix/common.h"
+
#include <stdint.h>
#ifdef __cplusplus
@@ -44,12 +46,14 @@ typedef struct ZixRingImpl ZixRing;
At most `size` - 1 bytes may be stored in the ring at once.
*/
+ZIX_MALLOC_API
ZixRing*
zix_ring_new(uint32_t size);
/**
Destroy a ring.
*/
+ZIX_API
void
zix_ring_free(ZixRing* ring);
@@ -62,6 +66,7 @@ zix_ring_free(ZixRing* ring);
ring to be truly real-time safe).
*/
+ZIX_API
void
zix_ring_mlock(ZixRing* ring);
@@ -71,48 +76,56 @@ zix_ring_mlock(ZixRing* ring);
This function is NOT thread-safe, it may only be called when there are no
readers or writers.
*/
+ZIX_API
void
zix_ring_reset(ZixRing* ring);
/**
Return the number of bytes of space available for reading.
*/
+ZIX_CONST_API
uint32_t
zix_ring_read_space(const ZixRing* ring);
/**
Return the number of bytes of space available for writing.
*/
+ZIX_CONST_API
uint32_t
zix_ring_write_space(const ZixRing* ring);
/**
Return the capacity (i.e. total write space when empty).
*/
+ZIX_CONST_API
uint32_t
zix_ring_capacity(const ZixRing* ring);
/**
Read from the ring without advancing the read head.
*/
+ZIX_API
uint32_t
zix_ring_peek(ZixRing* ring, void* dst, uint32_t size);
/**
Read from the ring and advance the read head.
*/
+ZIX_API
uint32_t
zix_ring_read(ZixRing* ring, void* dst, uint32_t size);
/**
Skip data in the ring (advance read head without reading).
*/
+ZIX_API
uint32_t
zix_ring_skip(ZixRing* ring, uint32_t size);
/**
Write data to the ring.
*/
+ZIX_API
uint32_t
zix_ring_write(ZixRing* ring, const void* src, uint32_t size);
diff --git a/src/zix/sem.h b/src/zix/sem.h
index e306052..faeb148 100644
--- a/src/zix/sem.h
+++ b/src/zix/sem.h
@@ -25,14 +25,16 @@
# include <limits.h>
# include <windows.h>
#else
-# include <semaphore.h>
# include <errno.h>
+# include <semaphore.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
+#include <stdbool.h>
+
/**
@addtogroup zix
@{
@@ -40,6 +42,8 @@ extern "C" {
@{
*/
+struct ZixSemImpl;
+
/**
A counting semaphore.
@@ -148,7 +152,7 @@ static inline ZixStatus
zix_sem_init(ZixSem* sem, unsigned initial)
{
sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL);
- return (sem->sem) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
+ return (sem->sem) ? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
}
static inline void
diff --git a/src/zix/thread.h b/src/zix/thread.h
index 5802c99..3989f13 100644
--- a/src/zix/thread.h
+++ b/src/zix/thread.h
@@ -26,10 +26,10 @@
# include <pthread.h>
#endif
+#include <stddef.h>
+
#ifdef __cplusplus
extern "C" {
-#else
-# include <stdbool.h>
#endif
/**
@@ -80,6 +80,8 @@ zix_thread_create(ZixThread* thread,
static inline ZixStatus
zix_thread_join(ZixThread thread, void** retval)
{
+ (void)retval;
+
return WaitForSingleObject(thread, INFINITE)
? ZIX_STATUS_SUCCESS : ZIX_STATUS_ERROR;
}
@@ -99,17 +101,13 @@ zix_thread_create(ZixThread* thread,
const int ret = pthread_create(thread, NULL, function, arg);
pthread_attr_destroy(&attr);
- if (ret == EAGAIN) {
- return ZIX_STATUS_NO_MEM;
- } else if (ret == EINVAL) {
- return ZIX_STATUS_BAD_ARG;
- } else if (ret == EPERM) {
- return ZIX_STATUS_BAD_PERMS;
- } else if (ret) {
- return ZIX_STATUS_ERROR;
+ switch (ret) {
+ case EAGAIN: return ZIX_STATUS_NO_MEM;
+ case EINVAL: return ZIX_STATUS_BAD_ARG;
+ case EPERM: return ZIX_STATUS_BAD_PERMS;
}
- return ZIX_STATUS_SUCCESS;
+ return ret ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
}
static inline ZixStatus