summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2022-12-24 01:45:27 +0100
committerAlejandro Colomar <alx@kernel.org>2022-12-24 01:47:19 +0100
commitb4b01eb28ba7e19a82f1e94a75b924c0ec0cf987 (patch)
tree266351a2a91807385c14446e571adfa493cd1bc7
parentdee48b9b34f74feb37ae9629b7e83ab94ab38510 (diff)
_compiler.h, stpecpy.c, stpeprintf.c: Add stp_impossible() wrapper around unreachable()
Having conditionals with stp_unreachable() makes it look like there's a branch, but there's not; it's just a hint to the compiler. This wrapper reduces cognitive load, I hope. Signed-off-by: Alejandro Colomar <alx@kernel.org>
-rw-r--r--include/stp/_compiler.h6
-rw-r--r--src/stp/stpe/stpecpy.c3
-rw-r--r--src/stp/stpe/stpeprintf.c6
3 files changed, 9 insertions, 6 deletions
diff --git a/include/stp/_compiler.h b/include/stp/_compiler.h
index 7ac8c70..001ddcf 100644
--- a/include/stp/_compiler.h
+++ b/include/stp/_compiler.h
@@ -11,6 +11,12 @@
#endif
+#define stp_impossible(e) do \
+{ \
+ if (e) \
+ stp_unreachable(); \
+} while (0)
+
#if defined(__clang__)
# define stp_nullable _Nullable
#else
diff --git a/src/stp/stpe/stpecpy.c b/src/stp/stpe/stpecpy.c
index b1ed12e..3e2f060 100644
--- a/src/stp/stpe/stpecpy.c
+++ b/src/stp/stpe/stpecpy.c
@@ -21,8 +21,7 @@ stpecpy(char *stp_nullable dst, char end[0], const char *restrict src)
return end;
if (stp_unlikely(dst == NULL)) // Allow chaining with stpeprintf().
return NULL;
- if (dst > end)
- stp_unreachable();
+ stp_impossible(dst > end);
p = memccpy(dst, src, '\0', end - dst);
if (p != NULL)
diff --git a/src/stp/stpe/stpeprintf.c b/src/stp/stpe/stpeprintf.c
index 28ce04b..c124857 100644
--- a/src/stp/stpe/stpeprintf.c
+++ b/src/stp/stpe/stpeprintf.c
@@ -35,13 +35,11 @@ vstpeprintf(char *stp_nullable dst, char end[0], const char *restrict fmt,
return end;
if (stp_unlikely(dst == NULL))
return NULL;
- if (dst > end)
- stp_unreachable();
+ stp_impossible(dst > end);
len = vsnprintf(dst, end - dst, fmt, ap);
- if (len < -1)
- stp_unreachable();
+ stp_impossible(len < -1);
if (stp_unlikely(len == -1))
return NULL;
if (len >= end - dst)