summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-10-20 21:34:20 -0400
committerDavid Robillard <d@drobilla.net>2022-10-21 12:45:39 -0400
commitd072adfbe40e15715e2065f1900f8d5a76491c5f (patch)
tree350f7c5c70e84d7a1d98a8e9389af4a4d3631cb1 /src
parent60e86c7b2a05dab33fc66f4fa5b2954d79456cb6 (diff)
downloadzix-d072adfbe40e15715e2065f1900f8d5a76491c5f.tar.gz
zix-d072adfbe40e15715e2065f1900f8d5a76491c5f.tar.bz2
zix-d072adfbe40e15715e2065f1900f8d5a76491c5f.zip
Hide errno utility functions
Diffstat (limited to 'src')
-rw-r--r--src/bump_allocator.c1
-rw-r--r--src/errno_status.c48
-rw-r--r--src/errno_status.h20
-rw-r--r--src/ring.c6
-rw-r--r--src/sem.c4
-rw-r--r--src/status.c41
-rw-r--r--src/thread.c3
7 files changed, 80 insertions, 43 deletions
diff --git a/src/bump_allocator.c b/src/bump_allocator.c
index f9031b1..924a1c8 100644
--- a/src/bump_allocator.c
+++ b/src/bump_allocator.c
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: ISC
#include "zix/bump_allocator.h"
+
#include "zix/allocator.h"
#include "zix/attributes.h"
diff --git a/src/errno_status.c b/src/errno_status.c
new file mode 100644
index 0000000..1830234
--- /dev/null
+++ b/src/errno_status.c
@@ -0,0 +1,48 @@
+// Copyright 2014-2022 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "errno_status.h"
+
+#include "zix/common.h"
+
+#include <errno.h>
+#include <stddef.h>
+
+ZixStatus
+zix_errno_status_if(const int r)
+{
+ return r ? zix_errno_status(errno) : ZIX_STATUS_SUCCESS;
+}
+
+ZixStatus
+zix_errno_status(const int e)
+{
+ typedef struct {
+ int code;
+ ZixStatus status;
+ } Mapping;
+
+ static const Mapping map[] = {
+ {0, ZIX_STATUS_SUCCESS},
+ {EACCES, ZIX_STATUS_BAD_PERMS},
+ {EAGAIN, ZIX_STATUS_UNAVAILABLE},
+ {EEXIST, ZIX_STATUS_EXISTS},
+ {EINVAL, ZIX_STATUS_BAD_ARG},
+ {ENOENT, ZIX_STATUS_NOT_FOUND},
+ {ENOMEM, ZIX_STATUS_NO_MEM},
+ {ENOSYS, ZIX_STATUS_NOT_SUPPORTED},
+ {EPERM, ZIX_STATUS_BAD_PERMS},
+ {ETIMEDOUT, ZIX_STATUS_TIMEOUT},
+ {0, ZIX_STATUS_ERROR}, // Fallback mapping
+ };
+
+ static const size_t n_mappings = sizeof(map) / sizeof(Mapping);
+
+ // Find the index of the matching mapping (or leave it at the fallback entry)
+ size_t m = 0;
+ while (m < n_mappings && map[m].code != e) {
+ ++m;
+ }
+
+ return map[m].status;
+}
diff --git a/src/errno_status.h b/src/errno_status.h
new file mode 100644
index 0000000..da26b66
--- /dev/null
+++ b/src/errno_status.h
@@ -0,0 +1,20 @@
+// Copyright 2022 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#ifndef ZIX_ERRNO_STATUS_H
+#define ZIX_ERRNO_STATUS_H
+
+#include "zix/attributes.h"
+#include "zix/common.h"
+
+/// Return an errno value converted to a status code
+ZIX_CONST_FUNC
+ZixStatus
+zix_errno_status(int e);
+
+/// Return success if `r` is non-zero, or `errno` as a status code otherwise
+ZIX_PURE_FUNC
+ZixStatus
+zix_errno_status_if(int r);
+
+#endif // ZIX_ERRNO_STATUS_H
diff --git a/src/ring.c b/src/ring.c
index ca957e2..287639b 100644
--- a/src/ring.c
+++ b/src/ring.c
@@ -2,10 +2,13 @@
// SPDX-License-Identifier: ISC
#include "zix/ring.h"
-#include "zix/common.h"
+#include "errno_status.h"
#include "zix_config.h"
+#include "zix/allocator.h"
+#include "zix/common.h"
+
#if USE_MLOCK
# include <sys/mman.h>
#elif defined(_WIN32)
@@ -21,6 +24,7 @@
# include <intrin.h>
#endif
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
diff --git a/src/sem.c b/src/sem.c
index cb00c3a..8593908 100644
--- a/src/sem.c
+++ b/src/sem.c
@@ -1,10 +1,12 @@
// Copyright 2012-2022 David Robillard <d@drobilla.net>
// SPDX-License-Identifier: ISC
+#include "zix/sem.h"
+
+#include "errno_status.h"
#include "zix_config.h"
#include "zix/common.h"
-#include "zix/sem.h"
#ifdef __APPLE__
# include <mach/mach.h>
diff --git a/src/status.c b/src/status.c
index e6fbec8..9cdab32 100644
--- a/src/status.c
+++ b/src/status.c
@@ -3,8 +3,6 @@
#include "zix/common.h"
-#include <errno.h>
-
const char*
zix_strerror(const ZixStatus status)
{
@@ -36,42 +34,3 @@ zix_strerror(const ZixStatus status)
}
return "Unknown error";
}
-
-ZixStatus
-zix_errno_status_if(const int r)
-{
- return r ? zix_errno_status(errno) : ZIX_STATUS_SUCCESS;
-}
-
-ZixStatus
-zix_errno_status(const int e)
-{
- switch (e) {
- case 0:
- return ZIX_STATUS_SUCCESS;
-#ifdef EAGAIN
- case EAGAIN:
- return ZIX_STATUS_UNAVAILABLE;
-#endif
-#ifdef EEXIST
- case EEXIST:
- return ZIX_STATUS_EXISTS;
-#endif
-#ifdef EINVAL
- case EINVAL:
- return ZIX_STATUS_BAD_ARG;
-#endif
-#ifdef EPERM
- case EPERM:
- return ZIX_STATUS_BAD_PERMS;
-#endif
-#ifdef ETIMEDOUT
- case ETIMEDOUT:
- return ZIX_STATUS_TIMEOUT;
-#endif
- default:
- break;
- }
-
- return ZIX_STATUS_ERROR;
-}
diff --git a/src/thread.c b/src/thread.c
index 72e4d46..c13f2ab 100644
--- a/src/thread.c
+++ b/src/thread.c
@@ -2,6 +2,9 @@
// SPDX-License-Identifier: ISC
#include "zix/thread.h"
+
+#include "errno_status.h"
+
#include "zix/common.h"
#ifdef _WIN32