Mass reformat

This commit is contained in:
Imbus 2025-08-18 14:59:16 +02:00
parent 4d27def35e
commit df11e34235
27 changed files with 121 additions and 136 deletions

View file

@ -30,12 +30,8 @@ typedef struct __attribute__((__packed__)) {
void print_command(CommandMessage *m) {
switch (m->type) {
case CMD_SET_LEVEL:
printf("Type: SetLevel\nPayload: %d\n", m->data.set_level.level);
break;
case CMD_SET_LIGHTS:
printf("Type: SetLights\nPayload: %d\n", m->data.set_lights.on);
break;
case CMD_SET_LEVEL: printf("Type: SetLevel\nPayload: %d\n", m->data.set_level.level); break;
case CMD_SET_LIGHTS: printf("Type: SetLights\nPayload: %d\n", m->data.set_lights.on); break;
case CMD_SET_DISTANCE_FACTOR:
printf("Type: SetDistanceFactor\nPayload: %.2f\n", m->data.set_distance.factor);
break;

11
break.c
View file

@ -1,8 +1,8 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <sys/mman.h> // mmap and friends
#include <unistd.h>
#include <stdbool.h>
int main(void) {
/*
@ -12,8 +12,7 @@ int main(void) {
* See: "man 3 getauxval" or
* https://www.man7.org/linux/man-pages/man3/getauxval.3.html
*/
long page_size =
sysconf(_SC_PAGESIZE); // or _SC_PAGE_SIZE (POSIX allows both)
long page_size = sysconf(_SC_PAGESIZE); // or _SC_PAGE_SIZE (POSIX allows both)
if (page_size == -1) {
perror("sysconf");
return 1;
@ -45,11 +44,9 @@ int main(void) {
* (which must be greater than 0).
*/
uint8_t *first_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
uint8_t *first_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
uint8_t *second_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
uint8_t *second_mmap = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
printf("First mmap: %p\n", first_mmap);
printf("Second mmap: %p\n", second_mmap);

View file

@ -25,7 +25,7 @@ typedef struct block {
// int is_free;
} block_t;
static char *heap;
static char *heap;
static block_t *free_lists[MAX_ORDER - MIN_ORDER + 1];
void buddy_init() {
@ -59,7 +59,7 @@ void *buddy_alloc(size_t size) {
size_t split_size = ORDER_TO_SZ(i);
// FIXME: Char???
char *middle = ((char *)bigger + split_size);
char *middle = ((char *)bigger + split_size);
block_t *buddy = (block_t *)middle;
// block_t *buddy = (block_t *)((char *)bigger + split_size);
buddy->next = NULL;

View file

@ -13,16 +13,16 @@ void buf_append_u32(uint8_t *buf, uint32_t value, size_t *index);
void buf_append_i64(uint8_t *buf, int64_t value, size_t *index);
void buf_append_u64(uint8_t *buf, uint64_t value, size_t *index);
int8_t buf_read_i8(const uint8_t *buf, size_t *index);
int8_t buf_read_i8(const uint8_t *buf, size_t *index);
uint8_t buf_read_u8(const uint8_t *buf, size_t *index);
int16_t buf_read_i16(const uint8_t *buf, size_t *index);
int16_t buf_read_i16(const uint8_t *buf, size_t *index);
uint16_t buf_read_u16(const uint8_t *buf, size_t *index);
int32_t buf_read_i32(const uint8_t *buf, size_t *index);
int32_t buf_read_i32(const uint8_t *buf, size_t *index);
uint32_t buf_read_u32(const uint8_t *buf, size_t *index);
int64_t buf_read_i64(const uint8_t *buf, size_t *index);
int64_t buf_read_i64(const uint8_t *buf, size_t *index);
uint64_t buf_read_u64(const uint8_t *buf, size_t *index);
/*

35
calc.c
View file

@ -2,24 +2,16 @@
#include <stdio.h>
#include <stdlib.h>
typedef enum {
TOKEN_INT,
TOKEN_PLUS,
TOKEN_MUL,
TOKEN_DIV,
TOKEN_LPAREN,
TOKEN_RPAREN,
TOKEN_EOF
} TokenType;
typedef enum { TOKEN_INT, TOKEN_PLUS, TOKEN_MUL, TOKEN_DIV, TOKEN_LPAREN, TOKEN_RPAREN, TOKEN_EOF } TokenType;
typedef struct {
TokenType type;
int value;
int value;
} Token;
const char *input;
size_t pos;
Token current_token;
size_t pos;
Token current_token;
void skip_whitespace() {
while (isspace(input[pos])) pos++;
@ -43,19 +35,12 @@ Token get_next_token() {
pos++;
switch (ch) {
case '+':
return (Token){TOKEN_PLUS, 0};
case '*':
return (Token){TOKEN_MUL, 0};
case '/':
return (Token){TOKEN_DIV, 0};
case '(':
return (Token){TOKEN_LPAREN, 0};
case ')':
return (Token){TOKEN_RPAREN, 0};
default:
fprintf(stderr, "Unexpected character: %c\n", ch);
exit(1);
case '+': return (Token){TOKEN_PLUS, 0};
case '*': return (Token){TOKEN_MUL, 0};
case '/': return (Token){TOKEN_DIV, 0};
case '(': return (Token){TOKEN_LPAREN, 0};
case ')': return (Token){TOKEN_RPAREN, 0};
default: fprintf(stderr, "Unexpected character: %c\n", ch); exit(1);
}
}

View file

@ -14,8 +14,12 @@ typedef struct {
int (*get)();
} Test;
void my_put(int a) { global = a; }
int my_get() { return global; }
void my_put(int a) {
global = a;
}
int my_get() {
return global;
}
void executor(Test *t) {
t->put(10);

2
djb2.c
View file

@ -6,7 +6,7 @@
unsigned long djb2(const char *str) {
unsigned long h = 5381;
int c;
int c;
while ((c = *str++)) h = ((h << 5) + h) + c; // h = h * 33 + c;

View file

@ -74,6 +74,10 @@ void *dynbuf_get(Dynbuf *v, size_t index) {
return (char *)v->data + index * v->elem_size;
}
inline size_t dynbuf_size(const Dynbuf *v) { return v->length; }
inline size_t dynbuf_size(const Dynbuf *v) {
return v->length;
}
inline size_t dynbuf_capacity(const Dynbuf *v) { return v->capacity; }
inline size_t dynbuf_capacity(const Dynbuf *v) {
return v->capacity;
}

View file

@ -30,7 +30,7 @@
* Holds homogenous data. The underlying array will expand automatically.
*/
typedef struct {
void *data;
void *data;
size_t elem_size;
size_t length;
size_t capacity;

View file

@ -1,5 +1,5 @@
#include <stdio.h>
#include <stdint.h>
#include <stdio.h>
/**
* @brief Computes the n-th Fibonacci number using matrix exponentiation.
@ -48,7 +48,8 @@ static FibMatrix matrix_power(FibMatrix base, uint32_t n) {
}
uint64_t fibonacci_matrix(uint32_t n) {
if (n == 0) return 0;
if (n == 0)
return 0;
FibMatrix base = {1, 1, 1, 0};
FibMatrix result = matrix_power(base, n - 1);
return result.a;

View file

@ -5,18 +5,18 @@
// Structure to hold GBA ROM header data
typedef struct {
uint32_t entry_point; // 0x08000000 - Entry Point
uint8_t nintendo_logo[156]; // 0x08000004 - Nintendo Logo
char game_title[12]; // 0x080000A0 - Game Title
char game_code[4]; // 0x080000AC - Game Code (e.g., "AGB-XXXX")
char maker_code[2]; // 0x080000B0 - Maker Code
uint8_t fixed_value; // 0x080000B2 - Always 0x96
uint8_t main_unit_code; // 0x080000B3 - Usually 0x00
uint8_t device_type; // 0x080000B4 - Usually 0x00
uint8_t reserved1[7]; // 0x080000B5 - Reserved
uint8_t software_version; // 0x080000BC - Software Version
uint8_t complement_check; // 0x080000BD - Header Checksum
uint8_t reserved2[2]; // 0x080000BE - Reserved
uint32_t entry_point; // 0x08000000 - Entry Point
uint8_t nintendo_logo[156]; // 0x08000004 - Nintendo Logo
char game_title[12]; // 0x080000A0 - Game Title
char game_code[4]; // 0x080000AC - Game Code (e.g., "AGB-XXXX")
char maker_code[2]; // 0x080000B0 - Maker Code
uint8_t fixed_value; // 0x080000B2 - Always 0x96
uint8_t main_unit_code; // 0x080000B3 - Usually 0x00
uint8_t device_type; // 0x080000B4 - Usually 0x00
uint8_t reserved1[7]; // 0x080000B5 - Reserved
uint8_t software_version; // 0x080000BC - Software Version
uint8_t complement_check; // 0x080000BD - Header Checksum
uint8_t reserved2[2]; // 0x080000BE - Reserved
} GBAHeader;
// Function to read and display the ROM header

View file

@ -3,19 +3,19 @@
#include <string.h>
typedef struct hashmap_entry {
char *key;
void *value;
char *key;
void *value;
struct hashmap_entry *next;
} hashmap_entry_t;
struct hashmap {
hashmap_entry_t **buckets;
size_t bucket_count;
size_t bucket_count;
};
static unsigned long hash(const char *str) {
unsigned long hash = 5381;
int c;
int c;
while ((c = *str++)) hash = ((hash << 5) + hash) + c;
return hash;
}
@ -43,7 +43,7 @@ void hashmap_destroy(hashmap_t *map) {
}
void hashmap_put(hashmap_t *map, const char *key, void *value) {
size_t index = (hash(key) % map->bucket_count);
size_t index = (hash(key) % map->bucket_count);
hashmap_entry_t *entry = map->buckets[index];
while (entry) {
@ -63,7 +63,7 @@ void hashmap_put(hashmap_t *map, const char *key, void *value) {
}
void *hashmap_get(hashmap_t *map, const char *key) {
size_t index = (hash(key) % map->bucket_count);
size_t index = (hash(key) % map->bucket_count);
hashmap_entry_t *entry = map->buckets[index];
while (entry) {
@ -81,7 +81,7 @@ void hashmap_remove(hashmap_t *map, const char *key) {
size_t index = (hash(key) % map->bucket_count);
hashmap_entry_t **prev = &map->buckets[index];
hashmap_entry_t *entry = *prev;
hashmap_entry_t *entry = *prev;
while (entry) {
if (strcmp(entry->key, key) == 0) {

View file

@ -246,8 +246,7 @@ float mat3_det(const Mat3 *m) {
float m21 = MAT3_AT(m, 2, 1);
float m22 = MAT3_AT(m, 2, 2);
return m00 * (m11 * m22 - m12 * m21) - m01 * (m10 * m22 - m12 * m20) +
m02 * (m10 * m21 - m11 * m20);
return m00 * (m11 * m22 - m12 * m21) - m01 * (m10 * m22 - m12 * m20) + m02 * (m10 * m21 - m11 * m20);
}
inline float vec3_dot(const Vec3 *a, const Vec3 *b) {
@ -255,9 +254,7 @@ inline float vec3_dot(const Vec3 *a, const Vec3 *b) {
}
Vec3 vec3_cross(const Vec3 *a, const Vec3 *b) {
Vec3 res = {.x = a->y * b->z - a->z * b->y,
.y = a->x * b->z - a->z * b->x,
.z = a->x * b->y - a->y * b->x};
Vec3 res = {.x = a->y * b->z - a->z * b->y, .y = a->x * b->z - a->z * b->x, .z = a->x * b->y - a->y * b->x};
return res;
}
@ -325,14 +322,10 @@ Vec2 vec2_scale(const Vec2 *a, const float scalar) {
Mat2 mat2_mul(const Mat2 *m1, const Mat2 *m2) {
Mat2 m3 = {.arr = {
MAT2_AT(m1, 0, 0) * MAT2_AT(m2, 0, 0) +
MAT2_AT(m1, 0, 1) * MAT2_AT(m2, 1, 0),
MAT2_AT(m1, 1, 0) * MAT2_AT(m2, 0, 0) +
MAT2_AT(m1, 1, 1) * MAT2_AT(m2, 1, 0),
MAT2_AT(m1, 0, 0) * MAT2_AT(m2, 0, 1) +
MAT2_AT(m1, 0, 1) * MAT2_AT(m2, 1, 1),
MAT2_AT(m1, 1, 0) * MAT2_AT(m2, 0, 1) +
MAT2_AT(m1, 1, 1) * MAT2_AT(m2, 1, 1),
MAT2_AT(m1, 0, 0) * MAT2_AT(m2, 0, 0) + MAT2_AT(m1, 0, 1) * MAT2_AT(m2, 1, 0),
MAT2_AT(m1, 1, 0) * MAT2_AT(m2, 0, 0) + MAT2_AT(m1, 1, 1) * MAT2_AT(m2, 1, 0),
MAT2_AT(m1, 0, 0) * MAT2_AT(m2, 0, 1) + MAT2_AT(m1, 0, 1) * MAT2_AT(m2, 1, 1),
MAT2_AT(m1, 1, 0) * MAT2_AT(m2, 0, 1) + MAT2_AT(m1, 1, 1) * MAT2_AT(m2, 1, 1),
}};
return m3;
@ -385,8 +378,7 @@ bool vec2_approx_eq(const Vec2 *a, const Vec2 *b, float epsilon) {
}
bool vec3_approx_eq(const Vec3 *a, const Vec3 *b, float epsilon) {
return (fabsf(a->x - b->x) <= epsilon) && (fabsf(a->y - b->y) <= epsilon) &&
(fabsf(a->z - b->z) <= epsilon);
return (fabsf(a->x - b->x) <= epsilon) && (fabsf(a->y - b->y) <= epsilon) && (fabsf(a->z - b->z) <= epsilon);
}
Mat2 mat2_adj(const Mat2 *m) {
@ -438,7 +430,7 @@ int main(void) {
assert(mat3_approx_eq(&m3, &m, 0.01));
}
{
Mat3 m = {{1, 0, 0, 0, 1, 0, 0, 0, 1}};
Mat3 m = {{1, 0, 0, 0, 1, 0, 0, 0, 1}};
float d = mat3_det(&m);
assert(fabs(d - 1.0) <= 0.01);

View file

@ -1,19 +1,19 @@
#include <lua.h>
#include <lauxlib.h>
#include <lua.h>
#include <stdint.h>
uint32_t lookup3(const void *key, size_t length, uint32_t initval);
// Lua wrapper
static int l_lookup3(lua_State *L) {
size_t len;
const char *str = luaL_checklstring(L, 1, &len); // get string + length
uint32_t initval = (uint32_t)luaL_checkinteger(L, 2); // get initval
size_t len;
const char *str = luaL_checklstring(L, 1, &len); // get string + length
uint32_t initval = (uint32_t)luaL_checkinteger(L, 2); // get initval
uint32_t hash = lookup3(str, len, initval);
lua_pushinteger(L, hash); // return the hash
return 1; // number of return values
lua_pushinteger(L, hash); // return the hash
return 1; // number of return values
}
// Module open function
@ -23,5 +23,5 @@ int luaopen_lookup3(lua_State *L) {
lua_pushcfunction(L, l_lookup3);
lua_setfield(L, -2, "lookup3");
return 1; // return the table
return 1; // return the table
}

View file

@ -3,13 +3,13 @@
// Macro for register access (volatile pointer dereferencing, not used here)
#define REG32(addr) (*(volatile uint32_t *)(addr))
#define REG16(addr) (*(volatile uint16_t *)(addr))
#define REG8(addr) (*(volatile uint8_t *)(addr))
#define REG8(addr) (*(volatile uint8_t *)(addr))
typedef struct MMIO_device {
volatile uint16_t hello;
volatile uint8_t __RESERVED1[2];
volatile uint8_t __RESERVED1[2];
volatile uint16_t status;
volatile uint8_t __RESERVED2[8];
volatile uint8_t __RESERVED2[8];
volatile uint32_t control;
// ...and so on
} __attribute__((aligned(4))) MMIO_device;
@ -20,8 +20,8 @@ typedef struct MMIO_device {
/* Specific device, often defined as an offset from a base */
#define MY_MMIO (MEM_PERIPH_BASE + 0x10)
#define DVC_CTLR_ENA (0x3 << 1) /* Device Control Register Enable */
#define DVC_CTLR_WRITE (0x1) /* Device Control Register Write */
#define DVC_CTLR_ENA (0x3 << 1) /* Device Control Register Enable */
#define DVC_CTLR_WRITE (0x1) /* Device Control Register Write */
volatile MMIO_device *mmio1 = (volatile MMIO_device *)MY_MMIO;

View file

@ -1,15 +1,15 @@
#include <stdint.h>
#include <stdio.h>
uint32_t murmur3_32(const uint8_t* key, size_t len, uint32_t seed) {
uint32_t murmur3_32(const uint8_t *key, size_t len, uint32_t seed) {
uint32_t h = seed;
uint32_t c1 = 0xcc9e2d51;
uint32_t c2 = 0x1b873593;
uint32_t k;
size_t i;
size_t i;
for (i = 0; i + 4 <= len; i += 4) {
k = *(uint32_t*)(key + i);
k = *(uint32_t *)(key + i);
k *= c1;
k = (k << 15) | (k >> (32 - 15));
k *= c2;
@ -22,12 +22,16 @@ uint32_t murmur3_32(const uint8_t* key, size_t len, uint32_t seed) {
// Tail handling
k = 0;
switch (len & 3) {
case 3: k ^= key[i + 2] << 16;
/* fall through */
case 2: k ^= key[i + 1] << 8;
/* fall through */
case 1: k ^= key[i + 0];
k *= c1; k = (k << 15) | (k >> (32 - 15)); k *= c2; h ^= k;
case 3: k ^= key[i + 2] << 16;
/* fall through */
case 2: k ^= key[i + 1] << 8;
/* fall through */
case 1:
k ^= key[i + 0];
k *= c1;
k = (k << 15) | (k >> (32 - 15));
k *= c2;
h ^= k;
}
h ^= len;
@ -42,7 +46,7 @@ uint32_t murmur3_32(const uint8_t* key, size_t len, uint32_t seed) {
int main() {
const char *key = "hello world";
uint32_t hash = murmur3_32((const uint8_t *)key, 11, 42); // seed = 42
uint32_t hash = murmur3_32((const uint8_t *)key, 11, 42); // seed = 42
printf("MurmurHash3 of \"%s\" is 0x%X\n", key, hash);
return 0;
}

View file

@ -3,9 +3,8 @@
#include <stdio.h>
#include <time.h>
#define BUILD_SEED \
((uint64_t)(__TIME__[0]) * (uint64_t)(__TIME__[1]) * \
(uint64_t)(__TIME__[3]) * (uint64_t)(__TIME__[4]) * \
#define BUILD_SEED \
((uint64_t)(__TIME__[0]) * (uint64_t)(__TIME__[1]) * (uint64_t)(__TIME__[3]) * (uint64_t)(__TIME__[4]) * \
(uint64_t)(__TIME__[6]) * (uint64_t)(__TIME__[7]))
#define PRNG_SAVE_INTERVAL 50 // Save every 1000 calls to prand()
@ -29,7 +28,9 @@ void sprand(uint64_t s) {
}
}
void rand_reseed() { seed = BUILD_SEED; }
void rand_reseed() {
seed = BUILD_SEED;
}
#define PRAND_MAIN
#ifdef PRAND_MAIN

View file

@ -3,7 +3,7 @@
#include <time.h>
struct timespec ts = {.tv_nsec = 30, .tv_sec = 0};
volatile int dummy = 0;
volatile int dummy = 0;
int main(void) {
struct timespec start, end;

4
sin.c
View file

@ -11,7 +11,9 @@ double factorial(int n) {
return result;
}
double abs_double(double x) { return x < 0 ? -x : x; }
double abs_double(double x) {
return x < 0 ? -x : x;
}
// SICP-style iterative approximation for sin(x)
double sin_iter(double x) {

8
sock.c
View file

@ -29,8 +29,7 @@ int main(void) {
};
// Bind socket
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) <
0) {
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind failed");
close(server_fd);
return EXIT_FAILURE;
@ -47,9 +46,8 @@ int main(void) {
// Accept one client connection
struct sockaddr_in client_addr;
socklen_t client_len = sizeof(client_addr);
int client_fd =
accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
socklen_t client_len = sizeof(client_addr);
int client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
if (client_fd < 0) {
perror("accept failed");
close(server_fd);

View file

@ -8,9 +8,9 @@
#define SOCKET_PATH "/tmp/demosocket"
int main() {
int sock_fd;
int sock_fd;
struct sockaddr_un addr;
char buffer[100];
char buffer[100];
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_fd < 0) {
@ -22,8 +22,7 @@ int main() {
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, SOCKET_PATH, sizeof(addr.sun_path) - 1);
if (connect(sock_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) <
0) {
if (connect(sock_fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0) {
perror("connect");
exit(EXIT_FAILURE);
}

View file

@ -7,13 +7,13 @@
/* Unix sockets need a path */
#define SOCK_PATH "/tmp/demosocket"
#define BUF_SIZE 128
#define BUF_SIZE 128
char buf[BUF_SIZE];
int main(void) {
struct sockaddr_un addr;
int server_fd, client_fd;
int server_fd, client_fd;
// Unlink this in case its still around
unlink(SOCK_PATH);

View file

@ -1,17 +1,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define SOCKET_PATH "/tmp/demosocket"
#define BUF_SIZE 128
#define BUF_SIZE 128
int main(void) {
int server_fd, client_fd;
int server_fd, client_fd;
struct sockaddr_un addr;
char buf[BUF_SIZE];
char buf[BUF_SIZE];
// Clean up any leftover socket file
unlink(SOCKET_PATH);

View file

@ -10,7 +10,7 @@
int main(void) {
printf("%s\n", sqlite3_libversion());
sqlite3 *db;
sqlite3 *db;
sqlite3_stmt *res;
int rc = sqlite3_open(":memory:", &db);

2
time.c
View file

@ -3,7 +3,7 @@
#include <time.h>
int main() {
char *out = malloc(200);
char *out = malloc(200);
struct tm *tmp;
time_t t = time(NULL);

View file

@ -3,14 +3,14 @@
#include <string.h>
struct treeset_node {
void *data;
void *data;
struct treeset_node *left;
struct treeset_node *right;
};
struct treeset {
treeset_node_t *root;
size_t node_count;
size_t node_count;
};
void ts_node_destroy(treeset_node_t *n) {
@ -22,4 +22,6 @@ void ts_node_destroy(treeset_node_t *n) {
free(n);
}
void treeset_destroy(treeset_t *set) { ts_node_destroy(set->root); }
void treeset_destroy(treeset_t *set) {
ts_node_destroy(set->root);
}

View file

@ -2,7 +2,7 @@
#include <stddef.h>
typedef struct treeset treeset_t;
typedef struct treeset treeset_t;
typedef struct treeset_node treeset_node_t;
/*