create a custom definition for size_t and ssize_t, which is ssize and usize, for conveinience.

This commit is contained in:
2025-09-18 10:39:57 +02:00
parent 1e24a4ee92
commit 6eb7d126da
10 changed files with 42 additions and 35 deletions

View File

@@ -56,7 +56,7 @@ static int mcx_loadchunk(const u8 *restrict buf, const i32 *restrict table, int
return 1; return 1;
} }
ssize_t size = -1; ssize size = -1;
for (;;) { for (;;) {
// TODO: handle buffer // TODO: handle buffer
// size = archive_read_data(archive, , ); // 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`. */ /* 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) { static void mvchunks(u8 *dst, u8 *src, u32 *restrict table, int src_s, int src_e) {
assert(src > dst); 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)); assert(!(len % SECTOR));
// count how many bytes we need to move, whilst updating location data // 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++) { for (src_s++; src_s <= src_e; src_s++) {
blen += (be32toh(table[src_s]) & 0xFF) * SECTOR; blen += (be32toh(table[src_s]) & 0xFF) * SECTOR;
table[src_s] -= htobe32((len / SECTOR) << 8); 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`. /* 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. * `rmb` is an optional additional offset that can be applied, and signifies bytes already removed.
* Returns the bytes removed by this function. */ * 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 // 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 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 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 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. /* 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 * This is done instead of `delchunk` being globally linked, because
* `delchunk` requests more specific parameters, which is confusing outside this module. */ * `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]; u32 table[TABLE];
memcpy(table, buf, sizeof(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)); memcpy(buf, table, sizeof(table));
return res; 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); assert(start < end && end < CHUNKS);
u32 table[TABLE]; u32 table[TABLE];
memcpy(table, buf, sizeof(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. /* 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. */ * 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 // ensure the chunks ids we're working on are sorted from least to greatest
u16 chunkids[chunkc + 1]; u16 chunkids[chunkc + 1];
memcpy(chunkids, chunks, chunkc); 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]; u32 table[TABLE];
memcpy(table, buf, sizeof(table)); memcpy(table, buf, sizeof(table));
size_t rmb = 0; usize rmb = 0;
for (int i = 0; i < chunkc; i++) for (int i = 0; i < chunkc; i++)
rmb += delchunk(buf, table, rmb, chunkids[i], chunkids[i + 1]); 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. /* 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. */ * Multiplying by `SECTOR`, and adding the size of the table itself. */
size_t mcx_calcsize(const u8 *restrict buf) { usize mcx_calcsize(const u8 *restrict buf) {
size_t size = 0; usize size = 0;
for (uint i = 0; i < CHUNKS; i++) for (uint i = 0; i < CHUNKS; i++)
size += *(buf + (i * 4) + 3); size += *(buf + (i * 4) + 3);
return (size * SECTOR) + (TABLE * 4); return (size * SECTOR) + (TABLE * 4);

View File

@@ -9,7 +9,7 @@
/* contains chunk metadata */ /* contains chunk metadata */
struct mcx_chunk { 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 len; // byte length of chunk (+ padding)
u32 time; // modification time in epoch seconds 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. * The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space. * All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed. */ * 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`. /* 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. * The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space. * All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */ * 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`. /* 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. * 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. * The chunk's location data shall become `0`, and timestamp data the current time.
* All succeeding chunks shall be moved back, freeing space. * All succeeding chunks shall be moved back, freeing space.
* Returns the amount of bytes removed */ * 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. */ /* 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;

View File

@@ -37,7 +37,7 @@ static inline u64 buftoh64(const void *restrict buf) {
* The data shall be converted to little-endian on little-endian systems * 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. */ * 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) { 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 = (struct nbt_array){
out->nmemb = nmemb, out->nmemb = nmemb,
out->dat = malloc(len), out->dat = malloc(len),

View File

@@ -19,7 +19,7 @@ extern const uint sh_geom_glsl_len;
/* Compiles a shader of `type` from `src` with `len` bytes. /* Compiles a shader of `type` from `src` with `len` bytes.
* Returns the integer for the shader. */ * 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; int ilen = len;
GLuint shader = glCreateShader(type); GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, &ilen); glShaderSource(shader, 1, &src, &ilen);

View File

@@ -14,14 +14,14 @@
#include "atrb.h" #include "atrb.h"
#include "intdef.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 bool feq = false; // whether we've found the equal sign
// data traversal // data traversal
char *pos = kout; // will point to the next point in the buffer, where we'll write data char *pos = kout; // will point to the next point in the buffer, where we'll write data
// acquire data // acquire data
for (size_t i = 0; i < len; i++) { for (usize i = 0; i < len; i++) {
// handling of termination tokens // handling of termination tokens
bool brk = false; // whether we broke out of the loop, and are done reading bool brk = false; // whether we broke out of the loop, and are done reading
switch (buf[i]) { 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); 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 // find a match for the current key
size_t i = 0; usize i = 0;
for (; i < optc; i++) { for (; i < optc; i++) {
if (strcmp(opts[i].key, key) == 0) if (strcmp(opts[i].key, key) == 0)
return opts + i; 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 */ /* utility function for conf_getpat to concatenate 3 strings, where we already know the size */
NONNULL((1, 3)) 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 assert(s2 || (!s2 && !s2len)); // ensuring the programmer passes both s2 and s2len as 0, if they intend to
char *buf, *ptr; char *buf, *ptr;
@@ -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. */ /* appends str to the config directory string we acquire from environment variables. */
char *conf_getpat(const char *restrict str) { char *conf_getpat(const char *restrict str) {
char *buf = NULL; char *buf = NULL;
size_t len; usize len;
size_t str_len = strlen(str); usize str_len = strlen(str);
#if defined(__linux__) #if defined(__linux__)
buf = getenv("XDG_CONFIG_HOME"); buf = getenv("XDG_CONFIG_HOME");
if (!buf) { if (!buf) {

View File

@@ -35,7 +35,7 @@ enum conf_primitive {
/* for outputting a fixed string as this config field */ /* for outputting a fixed string as this config field */
struct conf_fstr { 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 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. * `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. * 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` */ * 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. */ /* 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. /* 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. * - `val` points to a null-terminated string which contains the key and value.

View File

@@ -19,6 +19,10 @@ typedef uint16_t u16;
typedef uint32_t u32; typedef uint32_t u32;
typedef uint64_t u64; typedef uint64_t u64;
#include <stdlib.h>
typedef size_t usize;
typedef ssize_t ssize;
/* floating point types */ /* floating point types */
typedef float f32; // single-precision floating-point typedef float f32; // single-precision floating-point
typedef double f64; // double-precision floating-point typedef double f64; // double-precision floating-point

View File

@@ -1,5 +1,6 @@
/* Copyright (c) 2025 Quinn /* Copyright (c) 2025 Quinn
* Licensed under the MIT Licence. See LICENSE for details */ * Licensed under the MIT Licence. See LICENSE for details */
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "test.h" #include "test.h"
@@ -13,6 +14,7 @@ int main(void) {
assert_true(sizeof(u64) == 8); assert_true(sizeof(u64) == 8);
assert_true(sizeof(f32) == 4); assert_true(sizeof(f32) == 4);
assert_true(sizeof(f64) == 8); assert_true(sizeof(f64) == 8);
assert_true(sizeof(size_t) == sizeof(intptr_t));
test_conf_procbuf("key=val", "key", "val", 0); test_conf_procbuf("key=val", "key", "val", 0);
test_conf_procbuf("sometxt", "sometxt", "", CONF_ESYNTAX); test_conf_procbuf("sometxt", "sometxt", "", CONF_ESYNTAX);
test_conf_procbuf("# comment", "", "", CONF_ENODAT); test_conf_procbuf("# comment", "", "", CONF_ENODAT);

View File

@@ -6,10 +6,11 @@
#include <string.h> #include <string.h>
#include "../src/util/conf.h" #include "../src/util/conf.h"
#include "../src/util/intdef.h"
#include "test.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) { 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]; char k[len], v[len];
*k = '\0', *v = '\0'; *k = '\0', *v = '\0';
(void)(assert_true(conf_procbuf(buf, k, v, len) == expect_return) && (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))); assert_true(!strcmp(v, expect_val)));
} }
void test_conf_matchopt(struct conf_entry *opts, size_t optc, const char *restrict key, int expect_index) { void test_conf_matchopt(struct conf_entry *opts, usize optc, const char *restrict key, int expect_index) {
size_t idx = opts - conf_matchopt(opts, optc, key); usize idx = opts - conf_matchopt(opts, optc, key);
idx = (ssize_t)idx < 0 ? -idx : idx; idx = (ssize)idx < 0 ? -idx : idx;
int i = idx < optc ? (int)idx : -1; int i = idx < optc ? (int)idx : -1;
assert_true(i == expect_index); assert_true(i == expect_index);
} }

View File

@@ -6,7 +6,7 @@
#include "../src/util/intdef.h" #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_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_int(const char *val, u64 expect_value, int type);
void test_conf_procval_f32(const char *val, f32 expect_value); void test_conf_procval_f32(const char *val, f32 expect_value);
void test_conf_procval_fstr(const char *val, u64 expect_value, int type); void test_conf_procval_fstr(const char *val, u64 expect_value, int type);