diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-06-09 11:44:21 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-06-09 11:44:21 +0200 |
commit | a44d67eeed2bae1f268db5e480a37a29625cdf00 (patch) | |
tree | 8ad37f317f30b9ba7c0e449e5dc078a1122c96a8 | |
parent | 6fbe50be1c3297680e2a9802923907638193e234 (diff) |
Slightly simplify code
-rw-r--r-- | src/lexer.c | 18 |
1 files changed, 8 insertions, 10 deletions
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; } |