From 4215a8ab7cc004c617b91a1628dbad3f19fbbe30 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Thu, 13 Aug 2020 17:26:00 +0200 Subject: Shrink some code --- test/btree_test.c | 16 +++++----------- zix/ring.c | 4 ++-- zix/thread.h | 14 +++++--------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/test/btree_test.c b/test/btree_test.c index 67419be..7c470c0 100644 --- a/test/btree_test.c +++ b/test/btree_test.c @@ -40,10 +40,10 @@ unique_rand(size_t i) static const uint32_t prime = 4294967291; if (i >= prime) { return i; // Values >= prime are mapped to themselves - } else { - const uint64_t residue = ((uint64_t)i * i) % prime; - return (i <= prime / 2) ? residue : prime - residue; } + + const uint64_t residue = ((uint64_t)i * i) % prime; + return (i <= prime / 2) ? residue : prime - residue; } static int @@ -51,14 +51,8 @@ int_cmp(const void* a, const void* b, const void* ZIX_UNUSED(user_data)) { const uintptr_t ia = (uintptr_t)a; const uintptr_t ib = (uintptr_t)b; - // note the (ia - ib) trick here would overflow - if (ia == ib) { - return 0; - } else if (ia < ib) { - return -1; - } else { - return 1; - } + + return ia < ib ? -1 : ia > ib ? 1 : 0; } static uintptr_t diff --git a/zix/ring.c b/zix/ring.c index a62d833..cdbcea8 100644 --- a/zix/ring.c +++ b/zix/ring.c @@ -173,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/zix/thread.h b/zix/thread.h index 2af0c5e..e2b59c4 100644 --- a/zix/thread.h +++ b/zix/thread.h @@ -101,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 -- cgit v1.2.1