From a44d67eeed2bae1f268db5e480a37a29625cdf00 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 9 Jun 2024 11:44:21 +0200 Subject: Slightly simplify code --- src/lexer.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/lexer.c b/src/lexer.c index 6aa59c5..8ca35e7 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -146,23 +146,21 @@ out: struct lexemes_soa mk_lexemes_soa(void) { + struct lexemes_soa soa; + static_assert(offsetof(struct lexemes_soa, kinds) < offsetof(struct lexemes_soa, strs), "KINDS is not the first field before STRS"); + static_assert((LEXEMES_DFLT_CAP * sizeof(*soa.kinds) % alignof(*soa.strs)) + == 0, + "Additional padding is required to properly align STRS"); - struct lexemes_soa soa; soa.len = 0; - soa.cap = 2048; - - /* Ensure that soa.strs is properly aligned */ - size_t pad = alignof(*soa.strs) - - soa.cap * sizeof(*soa.kinds) % alignof(*soa.strs); - if (pad == 8) - pad = 0; + soa.cap = LEXEMES_DFLT_CAP; - if ((soa.kinds = malloc(soa.cap * LEXEMES_SOA_BLKSZ + pad)) == NULL) + if ((soa.kinds = malloc(soa.cap * LEXEMES_SOA_BLKSZ)) == NULL) err("malloc:"); - soa.strs = (void *)((char *)soa.kinds + soa.cap * sizeof(*soa.kinds) + pad); + soa.strs = (void *)((char *)soa.kinds + soa.cap * sizeof(*soa.kinds)); return soa; } -- cgit v1.2.3