diff options
author | David Robillard <d@drobilla.net> | 2022-10-20 21:34:20 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2022-10-21 12:45:39 -0400 |
commit | d072adfbe40e15715e2065f1900f8d5a76491c5f (patch) | |
tree | 350f7c5c70e84d7a1d98a8e9389af4a4d3631cb1 | |
parent | 60e86c7b2a05dab33fc66f4fa5b2954d79456cb6 (diff) | |
download | zix-d072adfbe40e15715e2065f1900f8d5a76491c5f.tar.gz zix-d072adfbe40e15715e2065f1900f8d5a76491c5f.tar.bz2 zix-d072adfbe40e15715e2065f1900f8d5a76491c5f.zip |
Hide errno utility functions
-rw-r--r-- | include/zix/common.h | 10 | ||||
-rw-r--r-- | include/zix/sem.h | 2 | ||||
-rw-r--r-- | meson.build | 1 | ||||
-rw-r--r-- | src/bump_allocator.c | 1 | ||||
-rw-r--r-- | src/errno_status.c | 48 | ||||
-rw-r--r-- | src/errno_status.h | 20 | ||||
-rw-r--r-- | src/ring.c | 6 | ||||
-rw-r--r-- | src/sem.c | 4 | ||||
-rw-r--r-- | src/status.c | 41 | ||||
-rw-r--r-- | src/thread.c | 3 | ||||
-rw-r--r-- | test/test_status.c | 23 |
11 files changed, 81 insertions, 78 deletions
diff --git a/include/zix/common.h b/include/zix/common.h index df49afe..788fdaa 100644 --- a/include/zix/common.h +++ b/include/zix/common.h @@ -38,16 +38,6 @@ ZIX_CONST_API const char* zix_strerror(ZixStatus status); -/// Return an errno value converted to a status code -ZIX_CONST_API -ZixStatus -zix_errno_status(int e); - -/// Return success if `r` is non-zero, or `errno` as a status code otherwise -ZIX_PURE_API -ZixStatus -zix_errno_status_if(int r); - /// Function for comparing two elements typedef int (*ZixComparator)(const void* a, const void* b, diff --git a/include/zix/sem.h b/include/zix/sem.h index e7db792..25088bf 100644 --- a/include/zix/sem.h +++ b/include/zix/sem.h @@ -19,9 +19,7 @@ extern "C" { #endif -#include <stdbool.h> #include <stdint.h> -#include <time.h> /** @defgroup zix_sem Semaphore diff --git a/meson.build b/meson.build index d5e228a..31f9fc0 100644 --- a/meson.build +++ b/meson.build @@ -112,6 +112,7 @@ sources = files( 'src/btree.c', 'src/bump_allocator.c', 'src/digest.c', + 'src/errno_status.c', 'src/hash.c', 'src/ring.c', 'src/status.c', 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 @@ -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> @@ -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 diff --git a/test/test_status.c b/test/test_status.c index 1a76ac2..a47181c 100644 --- a/test/test_status.c +++ b/test/test_status.c @@ -6,32 +6,10 @@ #include "zix/common.h" #include <assert.h> -#include <errno.h> -#include <limits.h> #include <stdio.h> #include <string.h> static void -test_errno_status(void) -{ - assert(zix_errno_status(0) == ZIX_STATUS_SUCCESS); - assert(zix_errno_status(INT_MAX) == ZIX_STATUS_ERROR); - -#ifdef EEXIST - assert(zix_errno_status(EEXIST) == ZIX_STATUS_EXISTS); -#endif -#ifdef EINVAL - assert(zix_errno_status(EINVAL) == ZIX_STATUS_BAD_ARG); -#endif -#ifdef EPERM - assert(zix_errno_status(EPERM) == ZIX_STATUS_BAD_PERMS); -#endif -#ifdef ETIMEDOUT - assert(zix_errno_status(ETIMEDOUT) == ZIX_STATUS_TIMEOUT); -#endif -} - -static void test_strerror(void) { const char* msg = zix_strerror(ZIX_STATUS_SUCCESS); @@ -54,7 +32,6 @@ test_strerror(void) int main(void) { - test_errno_status(); test_strerror(); return 0; } |