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;
}
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);