From 6eb7d126da2fe431276b75dd0ff1b25c29027db8 Mon Sep 17 00:00:00 2001 From: Quinn Date: Thu, 18 Sep 2025 10:39:57 +0200 Subject: [PATCH] create a custom definition for `size_t` and `ssize_t`, which is `ssize` and `usize`, for conveinience. --- src/dat/mcx.c | 24 ++++++++++++------------ src/dat/mcx.h | 10 +++++----- src/dat/nbt.c | 2 +- src/io/shader.c | 2 +- src/util/conf.c | 16 ++++++++-------- src/util/conf.h | 6 +++--- src/util/intdef.h | 4 ++++ test/main.c | 2 ++ test/test_conf.c | 9 +++++---- test/test_conf.h | 2 +- 10 files changed, 42 insertions(+), 35 deletions(-) diff --git a/src/dat/mcx.c b/src/dat/mcx.c index 5e9173e..a92ad8b 100644 --- a/src/dat/mcx.c +++ b/src/dat/mcx.c @@ -56,7 +56,7 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int return 1; } - ssize_t size = -1; + ssize size = -1; for (;;) { // TODO: handle buffer // size = archive_read_data(archive, , ); @@ -74,11 +74,11 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int /* Moves chunks `src_s` to `src_e` (inclusive) from `src`, back onto `dst`. */ static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e) { assert(src > dst); - size_t len = src - dst; // acquire the amount of bytes that we shall move + usize len = src - dst; // acquire the amount of bytes that we shall move assert(!(len % SECTOR)); // count how many bytes we need to move, whilst updating location data - size_t blen = 0; + usize blen = 0; for (src_s++; src_s <= src_e; src_s++) { blen += (be32toh(table[src_s]) & 0xFF) * SECTOR; table[src_s] -= htobe32((len / SECTOR) << 8); @@ -89,9 +89,9 @@ static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e /* Deletes chunk `sidx` by moving chunks up to `eidx` back over `sidx` in `buf`. * `rmb` is an optional additional offset that can be applied, and signifies bytes already removed. * Returns the bytes removed by this function. */ -static size_t delchunk(u8 *restrict buf, u32 *restrict table, size_t rmb, int sidx, int eidx) { +static usize delchunk(u8 *restrict buf, u32 *restrict table, usize rmb, int sidx, int eidx) { // load the table data - size_t slen, bidx, blen; + usize slen, bidx, blen; slen = be32toh(table[sidx]) & 0xFF; // acquire the sector length of the chunk bidx = (be32toh(table[sidx]) >> 8) * SECTOR; // acquire and compute the byte offset the chunk starts at blen = slen * SECTOR; // compute the byte length of the chunk @@ -110,15 +110,15 @@ static size_t delchunk(u8 *restrict buf, u32 *restrict table, size_t rmb, int si /* Call `delchunk` with the parameters and some defaults. Ensuring the table is copied correctly as well. * This is done instead of `delchunk` being globally linked, because * `delchunk` requests more specific parameters, which is confusing outside this module. */ -size_t mcx_delchunk(u8 *restrict buf, int chunk) { +usize mcx_delchunk(u8 *restrict buf, int chunk) { u32 table[TABLE]; memcpy(table, buf, sizeof(table)); - size_t res = delchunk(buf, table, 0, chunk, CHUNKS); + usize res = delchunk(buf, table, 0, chunk, CHUNKS); memcpy(buf, table, sizeof(table)); return res; } -size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) { +usize mcx_delchunk_range(u8 *restrict buf, int start, int end) { assert(start < end && end < CHUNKS); u32 table[TABLE]; memcpy(table, buf, sizeof(table)); @@ -149,7 +149,7 @@ static int cmp_chunkids(const void *restrict x, const void *restrict y) { /* Sorts the chunks marked for deletion from smallest to greatest index. * Then performs the deletion in this order. Making sure to only update the chunks up to the next. */ -size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) { +usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) { // ensure the chunks ids we're working on are sorted from least to greatest u16 chunkids[chunkc + 1]; memcpy(chunkids, chunks, chunkc); @@ -159,7 +159,7 @@ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunk u32 table[TABLE]; memcpy(table, buf, sizeof(table)); - size_t rmb = 0; + usize rmb = 0; for (int i = 0; i < chunkc; i++) rmb += delchunk(buf, table, rmb, chunkids[i], chunkids[i + 1]); @@ -169,8 +169,8 @@ size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunk /* Sum together the 4th byte in each location integer to compute the sector size of all chunks. * Multiplying by `SECTOR`, and adding the size of the table itself. */ -size_t mcx_calcsize(const u8 *restrict buf) { - size_t size = 0; +usize mcx_calcsize(const u8 *restrict buf) { + usize size = 0; for (uint i = 0; i < CHUNKS; i++) size += *(buf + (i * 4) + 3); return (size * SECTOR) + (TABLE * 4); diff --git a/src/dat/mcx.h b/src/dat/mcx.h index c4d94eb..5e39fc7 100644 --- a/src/dat/mcx.h +++ b/src/dat/mcx.h @@ -9,7 +9,7 @@ /* contains chunk metadata */ struct mcx_chunk { - size_t idx; // byte offset for start of chunk data + usize idx; // byte offset for start of chunk data u32 len; // byte length of chunk (+ padding) u32 time; // modification time in epoch seconds }; @@ -18,20 +18,20 @@ struct mcx_chunk { * The chunk's location data shall become `0`, and timestamp data the current time. * All succeeding chunks shall be moved back, freeing space. * Returns the amount of bytes removed. */ -size_t mcx_delchunk(u8 *restrict buf, int chunk) NONNULL((1)); +usize mcx_delchunk(u8 *restrict buf, int chunk) NONNULL((1)); /* Deletes the range defined by `start`—`end` (inclusive) of chunks out of `buf`. * The chunk's location data shall become `0`, and timestamp data the current time. * All succeeding chunks shall be moved back, freeing space. * Returns the amount of bytes removed */ -size_t mcx_delchunk_range(u8 *restrict buf, int start, int end) NONNULL((1)); +usize mcx_delchunk_range(u8 *restrict buf, int start, int end) NONNULL((1)); /* Deletes a `chunkc` chunks from `chunks` out of `buf`. * If the `chunks` indices are known to be sequential, i.e. have a constant difference of `1`, `mcx_delchunk_range` should be preferred. * The chunk's location data shall become `0`, and timestamp data the current time. * All succeeding chunks shall be moved back, freeing space. * Returns the amount of bytes removed */ -size_t mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2)); +usize mcx_delchunk_bulk(u8 *restrict buf, const u16 *restrict chunks, int chunkc) NONNULL((1, 2)); /* Computes the byte size of the `*.mcX` file in `buf` and returns it. */ -size_t mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE; +usize mcx_calcsize(const u8 *restrict buf) NONNULL((1)) PURE; diff --git a/src/dat/nbt.c b/src/dat/nbt.c index 7177ec9..e36d0fc 100644 --- a/src/dat/nbt.c +++ b/src/dat/nbt.c @@ -37,7 +37,7 @@ static inline u64 buftoh64(const void *restrict buf) { * The data shall be converted to little-endian on little-endian systems * Outputs the allocated data to `out`, returns where the next pointer would be. */ static const u8 *procarr(const u8 *restrict buf, i32 nmemb, uint size, struct nbt_array *restrict out) { - size_t len = nmemb * size; + usize len = nmemb * size; *out = (struct nbt_array){ out->nmemb = nmemb, out->dat = malloc(len), diff --git a/src/io/shader.c b/src/io/shader.c index f33a67b..6e69537 100644 --- a/src/io/shader.c +++ b/src/io/shader.c @@ -19,7 +19,7 @@ extern const uint sh_geom_glsl_len; /* Compiles a shader of `type` from `src` with `len` bytes. * Returns the integer for the shader. */ -static GLuint shader_compile(GLenum type, const char *src, size_t len) { +static GLuint shader_compile(GLenum type, const char *src, usize len) { int ilen = len; GLuint shader = glCreateShader(type); glShaderSource(shader, 1, &src, &ilen); diff --git a/src/util/conf.c b/src/util/conf.c index d0d028a..98b4eef 100644 --- a/src/util/conf.c +++ b/src/util/conf.c @@ -14,14 +14,14 @@ #include "atrb.h" #include "intdef.h" -int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, size_t len) { +int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, usize len) { bool feq = false; // whether we've found the equal sign // data traversal char *pos = kout; // will point to the next point in the buffer, where we'll write data // acquire data - for (size_t i = 0; i < len; i++) { + for (usize i = 0; i < len; i++) { // handling of termination tokens bool brk = false; // whether we broke out of the loop, and are done reading switch (buf[i]) { @@ -53,9 +53,9 @@ int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict v return (pos == kout) ? CONF_ENODAT : (!feq ? CONF_ESYNTAX : 0); } -struct conf_entry const *conf_matchopt(struct conf_entry const *opts, size_t optc, const char *restrict key) { +struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize optc, const char *restrict key) { // find a match for the current key - size_t i = 0; + usize i = 0; for (; i < optc; i++) { if (strcmp(opts[i].key, key) == 0) return opts + i; @@ -128,7 +128,7 @@ int conf_procval(struct conf_entry const *opt, const char *restrict val) { /* utility function for conf_getpat to concatenate 3 strings, where we already know the size */ NONNULL((1, 3)) -static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2, const char *restrict s3, size_t s1len, size_t s2len, size_t s3len) { +static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2, const char *restrict s3, usize s1len, usize s2len, usize s3len) { assert(s2 || (!s2 && !s2len)); // ensuring the programmer passes both s2 and s2len as 0, if they intend to char *buf, *ptr; @@ -140,7 +140,7 @@ static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2 // copy data to the buffer ptr = memcpy(ptr, s1, s1len) + s1len; // copy s1 data to the buffer if (s2len) ptr = memcpy(ptr, s2, s2len) + s2len; // copy s2 data to the buffer (excluding null-terminator) - (void)strcpy(ptr, s3); // copy s3 as a string, thus including null-terminator + (void)strcpy(ptr, s3); // copy s3 as a string, thus including null-terminator // return the buffer return buf; @@ -149,8 +149,8 @@ static char *conf_getpat_concat(const char *restrict s1, const char *restrict s2 /* appends str to the config directory string we acquire from environment variables. */ char *conf_getpat(const char *restrict str) { char *buf = NULL; - size_t len; - size_t str_len = strlen(str); + usize len; + usize str_len = strlen(str); #if defined(__linux__) buf = getenv("XDG_CONFIG_HOME"); if (!buf) { diff --git a/src/util/conf.h b/src/util/conf.h index 79cc7a5..a0323ab 100644 --- a/src/util/conf.h +++ b/src/util/conf.h @@ -35,7 +35,7 @@ enum conf_primitive { /* for outputting a fixed string as this config field */ struct conf_fstr { - size_t len; // length in BYTES of the output data + usize len; // length in BYTES of the output data char *out; // where we will output the data }; @@ -51,10 +51,10 @@ struct conf_entry { * `kout` and `vout` will contain a null-terminated string if the function returned successfully. * returns `0` on success, `<0` when no data was found. `>0` when data was invalid but something went wrong. * see `CONF_E*` or `enum conf_err` */ -int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, size_t len); +int conf_procbuf(const char *restrict buf, char *restrict kout, char *restrict vout, usize len); /* matches the key with one of the options and returns the pointer. Returns NULL if none could be found. */ -struct conf_entry const *conf_matchopt(struct conf_entry const *opts, size_t optc, const char *restrict key); +struct conf_entry const *conf_matchopt(struct conf_entry const *opts, usize optc, const char *restrict key); /* processes the value belonging to the key and outputs the result to opts. * - `val` points to a null-terminated string which contains the key and value. diff --git a/src/util/intdef.h b/src/util/intdef.h index bd65eab..36702e0 100644 --- a/src/util/intdef.h +++ b/src/util/intdef.h @@ -19,6 +19,10 @@ typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; +#include +typedef size_t usize; +typedef ssize_t ssize; + /* floating point types */ typedef float f32; // single-precision floating-point typedef double f64; // double-precision floating-point diff --git a/test/main.c b/test/main.c index 753fb52..7c62a94 100644 --- a/test/main.c +++ b/test/main.c @@ -1,5 +1,6 @@ /* Copyright (c) 2025 Quinn * Licensed under the MIT Licence. See LICENSE for details */ +#include #include #include "test.h" @@ -13,6 +14,7 @@ int main(void) { assert_true(sizeof(u64) == 8); assert_true(sizeof(f32) == 4); assert_true(sizeof(f64) == 8); + assert_true(sizeof(size_t) == sizeof(intptr_t)); test_conf_procbuf("key=val", "key", "val", 0); test_conf_procbuf("sometxt", "sometxt", "", CONF_ESYNTAX); test_conf_procbuf("# comment", "", "", CONF_ENODAT); diff --git a/test/test_conf.c b/test/test_conf.c index de0eee0..02b8a68 100644 --- a/test/test_conf.c +++ b/test/test_conf.c @@ -6,10 +6,11 @@ #include #include "../src/util/conf.h" +#include "../src/util/intdef.h" #include "test.h" void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key, const char *restrict expect_val, int expect_return) { - size_t len = strlen(buf) + 1; + usize len = strlen(buf) + 1; char k[len], v[len]; *k = '\0', *v = '\0'; (void)(assert_true(conf_procbuf(buf, k, v, len) == expect_return) && @@ -17,9 +18,9 @@ void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key assert_true(!strcmp(v, expect_val))); } -void test_conf_matchopt(struct conf_entry *opts, size_t optc, const char *restrict key, int expect_index) { - size_t idx = opts - conf_matchopt(opts, optc, key); - idx = (ssize_t)idx < 0 ? -idx : idx; +void test_conf_matchopt(struct conf_entry *opts, usize optc, const char *restrict key, int expect_index) { + usize idx = opts - conf_matchopt(opts, optc, key); + idx = (ssize)idx < 0 ? -idx : idx; int i = idx < optc ? (int)idx : -1; assert_true(i == expect_index); } diff --git a/test/test_conf.h b/test/test_conf.h index daf77d0..fb0d4e0 100644 --- a/test/test_conf.h +++ b/test/test_conf.h @@ -6,7 +6,7 @@ #include "../src/util/intdef.h" void test_conf_procbuf(const char *restrict buf, const char *restrict expect_key, const char *restrict expect_val, int expect_return); -void test_conf_matchopt(struct conf_entry *restrict opts, size_t optc, const char *restrict key, int expect_index); +void test_conf_matchopt(struct conf_entry *restrict opts, usize optc, const char *restrict key, int expect_index); void test_conf_procval_int(const char *val, u64 expect_value, int type); void test_conf_procval_f32(const char *val, f32 expect_value); void test_conf_procval_fstr(const char *val, u64 expect_value, int type);