From 77fd98baaa312a2871d059444fde15017cad6e5e Mon Sep 17 00:00:00 2001 From: jacob Date: Fri, 12 Apr 2024 16:10:25 -0500 Subject: [PATCH] fix read-only macro in debug mode --- src/common.h | 11 ++++++++--- src/entity.c | 1 + src/entity.h | 7 +++++-- src/log.c | 2 +- src/math.h | 20 ++++++++++---------- src/uni.c | 22 +++++++++++----------- 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/common.h b/src/common.h index a8ca07b5..317a5900 100644 --- a/src/common.h +++ b/src/common.h @@ -162,9 +162,14 @@ void __asan_unpoison_memory_region(void const volatile *add, size_t); #define INTERNAL static #define GLOBAL static -/* Storage specifiers */ -#pragma section(".roglob", read) -#define READONLY __declspec(allocate(".roglob")) +/* Read-only */ +#if OS_WINDOWS +# define READONLY __declspec(allocate(".rdata")) +#elif OS_MAC +#define READONLY __attribute((section("__TEXT,__const"))) +#else +#define READONLY __attribute((section(".rodata"))) +#endif /* Markup */ #define UNUSED void diff --git a/src/entity.c b/src/entity.c index df6963c4..1d0d5e70 100644 --- a/src/entity.c +++ b/src/entity.c @@ -1,6 +1,7 @@ #include "entity.h" #include "math.h" +/* Accessed via entity_nil() */ READONLY struct entity _g_entity_nil = { .rel_xform = XFORM_IDENT, .world_xform = XFORM_IDENT, diff --git a/src/entity.h b/src/entity.h index 78e9d2b8..cb9d37f5 100644 --- a/src/entity.h +++ b/src/entity.h @@ -116,8 +116,11 @@ struct entity_prop_array { * Nil * ========================== */ -extern READONLY struct entity _g_entity_nil; -INLINE READONLY struct entity *entity_nil(void) { return &_g_entity_nil; } +INLINE struct entity *entity_nil(void) +{ + extern READONLY struct entity _g_entity_nil; + return &_g_entity_nil; +} /* ========================== * * Property helpers diff --git a/src/log.c b/src/log.c index 915892dd..bcf061a7 100644 --- a/src/log.c +++ b/src/log.c @@ -22,7 +22,7 @@ GLOBAL struct { b32 file_valid; } G = { 0 }, DEBUG_ALIAS(G, G_log); -GLOBAL READONLY const struct log_level_settings g_log_level_settings[LOG_LEVEL_COUNT] = { +GLOBAL READONLY struct log_level_settings g_log_level_settings[LOG_LEVEL_COUNT] = { [LOG_LEVEL_CRITICAL] = { STR_NOCAST("CRITICAL"), 0xFFFF00FF diff --git a/src/math.h b/src/math.h index 7c3669a3..183eece0 100644 --- a/src/math.h +++ b/src/math.h @@ -133,8 +133,8 @@ INLINE u64 math_pow_u64(u64 base, u8 exp) { * https://github.com/freebsd/freebsd-src/blob/main/lib/msun/src/e_logf.c */ INLINE f32 math_ln(f32 x) { - static const f32 ln2_hi = 6.9313812256e-01; - static const f32 ln2_lo = 9.0580006145e-06; + LOCAL_PERSIST const f32 ln2_hi = 6.9313812256e-01; + LOCAL_PERSIST const f32 ln2_lo = 9.0580006145e-06; i32 x_int = *(u32 *)&x; @@ -202,14 +202,14 @@ INLINE f32 math_ln(f32 x) * https://github.com/freebsd/freebsd-src/blob/main/lib/msun/src/e_expf.c */ INLINE f32 math_exp(f32 x) { - static const f32 half[2] = { 0.5, -0.5 }; - static const f32 o_threshold = 8.8721679688e+01; - static const f32 u_threshold = -1.0397208405e+02; - static const f32 ln2_hi[2] = { 6.9314575195e-01, -6.9314575195e-01 }; - static const f32 ln2_lo[2] = { 1.4286067653e-06, -1.4286067653e-06 }; - static const f32 inv_ln2 = 1.4426950216e+00; - static const f32 huge = 1.0e+30; - static const f32 two_m100 = 7.8886090522e-31; + LOCAL_PERSIST const f32 half[2] = { 0.5, -0.5 }; + LOCAL_PERSIST const f32 o_threshold = 8.8721679688e+01; + LOCAL_PERSIST const f32 u_threshold = -1.0397208405e+02; + LOCAL_PERSIST const f32 ln2_hi[2] = { 6.9314575195e-01, -6.9314575195e-01 }; + LOCAL_PERSIST const f32 ln2_lo[2] = { 1.4286067653e-06, -1.4286067653e-06 }; + LOCAL_PERSIST const f32 inv_ln2 = 1.4426950216e+00; + LOCAL_PERSIST const f32 huge = 1.0e+30; + LOCAL_PERSIST const f32 two_m100 = 7.8886090522e-31; u32 x_uint = *(u32 *)&x; i32 x_sign_bit = (x_uint >> 31) & 1; diff --git a/src/uni.c b/src/uni.c index db4e40f7..43070359 100644 --- a/src/uni.c +++ b/src/uni.c @@ -4,17 +4,17 @@ * utf8 * ========================== */ -GLOBAL READONLY u8 g_utf8_lens[32] = { - 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,2,2,2,2,3,3,4,5 -}; - struct uni_decode_utf8_result uni_decode_utf8(struct string str) { + LOCAL_PERSIST const u8 lengths[32] = { + 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,2,2,2,2,3,3,4,5 + }; + u32 codepoint = U32_MAX; u32 advance = 0; if (str.len > 0) { u8 c0 = str.text[0]; - u8 utf8_len = g_utf8_lens[c0 >> 3]; + u8 utf8_len = lengths[c0 >> 3]; advance = 1; switch (utf8_len) { @@ -25,7 +25,7 @@ struct uni_decode_utf8_result uni_decode_utf8(struct string str) case 2: { if (str.len >= 2) { u8 c1 = str.text[1]; - if (g_utf8_lens[c1 >> 3] == 0) { + if (lengths[c1 >> 3] == 0) { codepoint = (c1 & 0x3F) << 0; codepoint |= (c0 & 0x1F) << 6; advance = 2; @@ -37,8 +37,8 @@ struct uni_decode_utf8_result uni_decode_utf8(struct string str) if (str.len >= 3) { u8 c1 = str.text[1]; u8 c2 = str.text[2]; - if (g_utf8_lens[c1 >> 3] == 0 && - g_utf8_lens[c2 >> 3] == 0) { + if (lengths[c1 >> 3] == 0 && + lengths[c2 >> 3] == 0) { codepoint = (c2 & 0x3F) << 0; codepoint |= (c1 & 0x3F) << 6; codepoint |= (c0 & 0x0F) << 12; @@ -52,9 +52,9 @@ struct uni_decode_utf8_result uni_decode_utf8(struct string str) u8 c1 = str.text[1]; u8 c2 = str.text[2]; u8 c3 = str.text[3]; - if (g_utf8_lens[c1 >> 3] == 0 && - g_utf8_lens[c2 >> 3] == 0 && - g_utf8_lens[c3 >> 3] == 0) { + if (lengths[c1 >> 3] == 0 && + lengths[c2 >> 3] == 0 && + lengths[c3 >> 3] == 0) { codepoint = (c3 & 0x3F) << 0; codepoint |= (c2 & 0x3F) << 6; codepoint |= (c1 & 0x3F) << 12;