summaryrefslogtreecommitdiff
path: root/libmisc
diff options
context:
space:
mode:
Diffstat (limited to 'libmisc')
-rw-r--r--libmisc/CMakeLists.txt2
-rw-r--r--libmisc/assert.c3
-rw-r--r--libmisc/include/libmisc/_intercept.h10
-rw-r--r--libmisc/include/libmisc/private.h4
-rw-r--r--libmisc/include/libmisc/rand.h4
-rw-r--r--libmisc/intercept.c11
-rw-r--r--libmisc/tests/test_assert.c4
-rw-r--r--libmisc/tests/test_macro.c1
-rw-r--r--libmisc/tests/test_private.c8
9 files changed, 31 insertions, 16 deletions
diff --git a/libmisc/CMakeLists.txt b/libmisc/CMakeLists.txt
index 70ec691..4599ead 100644
--- a/libmisc/CMakeLists.txt
+++ b/libmisc/CMakeLists.txt
@@ -4,7 +4,7 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
add_library(libmisc INTERFACE)
-target_include_directories(libmisc SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
+target_include_directories(libmisc PUBLIC INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_sources(libmisc INTERFACE
assert.c
intercept.c
diff --git a/libmisc/assert.c b/libmisc/assert.c
index 69c0530..fdd8154 100644
--- a/libmisc/assert.c
+++ b/libmisc/assert.c
@@ -1,6 +1,6 @@
/* libmisc/assert.c - More assertions
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -12,6 +12,7 @@
#include <libmisc/assert.h>
#ifndef NDEBUG
+#define __lm_printf __lm_light_printf
void __assert_msg_fail(const char *expr,
const char *file, unsigned int line, const char *func,
const char *msg) {
diff --git a/libmisc/include/libmisc/_intercept.h b/libmisc/include/libmisc/_intercept.h
index 47e4620..a264144 100644
--- a/libmisc/include/libmisc/_intercept.h
+++ b/libmisc/include/libmisc/_intercept.h
@@ -1,6 +1,6 @@
/* libmisc/_intercept.h - Interceptable ("weak") functions
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -13,9 +13,15 @@
* own `__lm_` wrappers that GCC/glibc won't interfere with.
*/
-[[format(printf, 1, 2)]]
+[[gnu::format(printf, 1, 2)]]
int __lm_printf(const char *format, ...);
[[noreturn]] void __lm_abort(void);
+/* __lm_light_printf is expected to have less stack use than regular
+ * __lm_printf, if possible. */
+
+[[gnu::format(printf, 1, 2)]]
+int __lm_light_printf(const char *format, ...);
+
#endif /* _LIBMISC__INTERCEPT_H_ */
diff --git a/libmisc/include/libmisc/private.h b/libmisc/include/libmisc/private.h
index c5382a7..5518d1f 100644
--- a/libmisc/include/libmisc/private.h
+++ b/libmisc/include/libmisc/private.h
@@ -11,7 +11,7 @@
#define YES LM_SENTINEL()
#define IS_IMPLEMENTATION_FOR(name) LM_IS_SENTINEL(IMPLEMENTATION_FOR_##name)
-#define BEGIN_PRIVATE(name) LM_IF(IS_IMPLEMENTATION_FOR(name))()(struct {)
-#define END_PRIVATE(name) LM_IF(IS_IMPLEMENTATION_FOR(name))()(} LM_CAT2_(_HIDDEN_, __COUNTER__);)
+#define BEGIN_PRIVATE(name) LM_IF(IS_IMPLEMENTATION_FOR(name))()(struct {) struct {} LM_CAT2_(_PRIVATE_FORCE_SEMICOLON_, __COUNTER__)
+#define END_PRIVATE(name) LM_IF(IS_IMPLEMENTATION_FOR(name))(struct {} LM_CAT2_(_PRIVATE_FORCE_SEMICOLON_, __COUNTER__))(} LM_CAT2_(_PRIVATE_, __COUNTER__))
#endif /* _LIBMISC_PRIVATE_H_ */
diff --git a/libmisc/include/libmisc/rand.h b/libmisc/include/libmisc/rand.h
index 8072841..bb1ec0b 100644
--- a/libmisc/include/libmisc/rand.h
+++ b/libmisc/include/libmisc/rand.h
@@ -29,14 +29,14 @@ static inline uint64_t rand_uint63n(uint64_t cnt) {
uint64_t fair_cnt = ((UINT64_C(1)<<62) / cnt) * cnt;
uint64_t rnd;
do {
- rnd = (random() << 31) | random();
+ rnd = (((uint64_t)random()) << 31) | random();
} while (rnd >= fair_cnt);
return rnd % cnt;
} else if (cnt <= UINT64_C(1)<<63) {
uint64_t fair_cnt = ((UINT64_C(1)<<63) / cnt) * cnt;
uint64_t rnd;
do {
- rnd = (random() << 62) | (random() << 31) | random();
+ rnd = (((uint64_t)random()) << 62) | (((uint64_t)random()) << 31) | random();
} while (rnd >= fair_cnt);
return rnd % cnt;
}
diff --git a/libmisc/intercept.c b/libmisc/intercept.c
index dda8c09..85a3801 100644
--- a/libmisc/intercept.c
+++ b/libmisc/intercept.c
@@ -1,6 +1,6 @@
/* libmisc/intercept.c - Interceptable ("weak") functions
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -20,6 +20,15 @@ int __lm_printf(const char *format, ...) {
}
[[gnu::weak]]
+int __lm_light_printf(const char *format, ...) {
+ va_list va;
+ va_start(va, format);
+ int ret = vprintf(format, va);
+ va_end(va);
+ return ret;
+}
+
+[[gnu::weak]]
void __lm_abort(void) {
abort();
}
diff --git a/libmisc/tests/test_assert.c b/libmisc/tests/test_assert.c
index 3c2d6b6..15f9446 100644
--- a/libmisc/tests/test_assert.c
+++ b/libmisc/tests/test_assert.c
@@ -1,6 +1,6 @@
/* libmisc/tests/test_assert.c - Tests for <libmisc/assert.h>
*
- * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
@@ -35,7 +35,7 @@ void __lm_abort(void) {
longjmp(global_env, 1);
}
-int __lm_printf(const char *format, ...) {
+int __lm_light_printf(const char *format, ...) {
va_list va;
va_start(va, format);
int ret = vasprintf(&global_log, format, va);
diff --git a/libmisc/tests/test_macro.c b/libmisc/tests/test_macro.c
index 69655d1..1320eb3 100644
--- a/libmisc/tests/test_macro.c
+++ b/libmisc/tests/test_macro.c
@@ -27,7 +27,6 @@ int main() {
/* ... */
test_assert(LM_NEXT_POWER_OF_2(0x8000000000000000-1) == 0x8000000000000000);
/* Valid up to 0x8000000000000000-1 = (1<<63)-1 */
- test_assert(LM_NEXT_POWER_OF_2(0x8000000000000000) == 0); /* :( */
printf("== LM_FLOORLOG2 ===========================================\n");
/* valid down to 1. */
diff --git a/libmisc/tests/test_private.c b/libmisc/tests/test_private.c
index 7aaf1ee..9b39932 100644
--- a/libmisc/tests/test_private.c
+++ b/libmisc/tests/test_private.c
@@ -8,18 +8,18 @@
struct a {
int foo;
- BEGIN_PRIVATE(A)
+ BEGIN_PRIVATE(A);
int bar;
- END_PRIVATE(A)
+ END_PRIVATE(A);
};
#define IMPLEMENTATION_FOR_B YES
struct b {
int foo;
- BEGIN_PRIVATE(B)
+ BEGIN_PRIVATE(B);
int bar;
- END_PRIVATE(B)
+ END_PRIVATE(B);
};
int main() {