From cc8768d33a6ff6cc64306d01679a10c3a1847736 Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 10 Oct 2026 10:31:01 +0200 Subject: chore: small refactor --- user/user.h | 150 ++++++++++++++++++++++++++++++------------------------------ 1 file changed, 75 insertions(+), 75 deletions(-) (limited to 'user/user.h') diff --git a/user/user.h b/user/user.h index 86c2f91..58a1bc5 100644 --- a/user/user.h +++ b/user/user.h @@ -7,55 +7,55 @@ #define O_TRUNC 0x200 #define O_APPEND 0x400 -static long sys_write(long fd, const char* buffer, unsigned long length) +static inline __attribute__((warn_unused_result)) long sys_write(long fd, const char* buffer, unsigned long length) { long result; __asm__ volatile("int $0x80" : "=a"(result) : "a"(1L), "D"(fd), "S"(buffer), "d"(length) : "memory"); return result; } -static long sys_read(long fd, char* buffer, unsigned long length) +static inline __attribute__((warn_unused_result)) long sys_read(long fd, char* buffer, unsigned long length) { long result; __asm__ volatile("int $0x80" : "=a"(result) : "a"(2L), "D"(fd), "S"(buffer), "d"(length) : "memory"); return result; } -static long sys_open(const char* path, long flags) +static inline __attribute__((warn_unused_result)) long sys_open(const char* path, long flags) { long result; __asm__ volatile("int $0x80" : "=a"(result) : "a"(7L), "D"(path), "S"(flags) : "memory"); return result; } -static long sys_close(long fd) +static inline __attribute__((warn_unused_result)) long sys_close(long fd) { long result; __asm__ volatile("int $0x80" : "=a"(result) : "a"(8L), "D"(fd) : "memory"); return result; } -static long sys_lseek(long fd, long offset, long whence) +static inline __attribute__((warn_unused_result)) long sys_lseek(long fd, long offset, long whence) { long result; __asm__ volatile("int $0x80" : "=a"(result) : "a"(9L), "D"(fd), "S"(offset), "d"(whence) : "memory"); return result; } -static void sys_exit(long code) +static inline void sys_exit(long code) { __asm__ volatile("int $0x80" : : "a"(0L), "D"(code) : "memory"); for (;;); } -static long sys_fs_size(const char* name) +static inline __attribute__((warn_unused_result)) long sys_fs_size(const char* name) { long result; __asm__ volatile("int $0x80" : "=a"(result) : "a"(4L), "D"(name) : "memory"); return result; } -static long sys_fs_read(const char* name, unsigned long offset, char* buffer, unsigned long length) +static inline __attribute__((warn_unused_result)) long sys_fs_read(const char* name, unsigned long offset, char* buffer, unsigned long length) { long result; __asm__ volatile("int $0x80" @@ -65,7 +65,7 @@ static long sys_fs_read(const char* name, unsigned long offset, char* buffer, un return result; } -static long sys_fs_write(const char* name, const char* buffer, unsigned long length) +static inline __attribute__((warn_unused_result)) long sys_fs_write(const char* name, const char* buffer, unsigned long length) { long result; __asm__ volatile("int $0x80" @@ -75,14 +75,14 @@ static long sys_fs_write(const char* name, const char* buffer, unsigned long len return result; } -static long sys_brk(void* addr) +static inline __attribute__((warn_unused_result)) long sys_brk(void* address) { long result; - __asm__ volatile("int $0x80" : "=a"(result) : "a"(10L), "D"(addr) : "memory"); + __asm__ volatile("int $0x80" : "=a"(result) : "a"(10L), "D"(address) : "memory"); return result; } -static void* sys_sbrk(long increment) +static inline __attribute__((warn_unused_result)) void* sys_sbrk(long increment) { long result; __asm__ volatile("int $0x80" : "=a"(result) : "a"(11L), "D"(increment) : "memory"); @@ -91,7 +91,7 @@ static void* sys_sbrk(long increment) // shitty heap implementation // temporary af this will be in its own libc -#define HEAP_HDR 16UL +#define HEAP_HEADER 16UL #define HEAP_ALIGN 16UL #define HEAP_MIN 32UL #define HEAP_CHUNK (64UL * 1024UL) @@ -100,28 +100,28 @@ static unsigned long heap_start = 0; static unsigned long heap_end = 0; static unsigned long heap_rover = 0; -static unsigned long heap_blk_size(unsigned long b) +static inline __attribute__((warn_unused_result)) unsigned long heap_block_size(unsigned long block) { - return *(unsigned long*)b & ~15UL; + return *(unsigned long*)block & ~15UL; } -static int heap_blk_used(unsigned long b) +static inline __attribute__((warn_unused_result)) int heap_block_used(unsigned long block) { - return (int)(*(unsigned long*)b & 1UL); + return (int)(*(unsigned long*)block & 1UL); } -static void heap_set(unsigned long b, unsigned long size, int used) +static inline void heap_set(unsigned long block, unsigned long size, int used) { - *(unsigned long*)b = size | (used ? 1UL : 0UL); + *(unsigned long*)block = size | (used ? 1UL : 0UL); } -static unsigned long heap_align(unsigned long x) +static inline __attribute__((warn_unused_result)) unsigned long heap_align(unsigned long value) { - return (x + HEAP_ALIGN - 1UL) & ~(HEAP_ALIGN - 1UL); + return (value + HEAP_ALIGN - 1UL) & ~(HEAP_ALIGN - 1UL); } // pull another hunk off sbrk and drop a free block over it -static int heap_grow(unsigned long need) +static inline __attribute__((warn_unused_result)) int heap_grow(unsigned long need) { unsigned long want = heap_align(need); if (want < HEAP_CHUNK) @@ -141,11 +141,11 @@ static int heap_grow(unsigned long need) } // yummy O(n) malloc implementation -static void* malloc(unsigned long len) +static inline __attribute__((warn_unused_result)) void* malloc(unsigned long length) { - if (len == 0) + if (length == 0) return 0; - unsigned long need = heap_align(HEAP_HDR + len); + unsigned long need = heap_align(HEAP_HEADER + length); if (need < HEAP_MIN) need = HEAP_MIN; @@ -153,39 +153,39 @@ static void* malloc(unsigned long len) { if (heap_rover < heap_start || heap_rover >= heap_end) heap_rover = heap_start; - unsigned long b = heap_rover; + unsigned long block = heap_rover; unsigned long limit = heap_end; for (int pass = 0; pass < 2; pass++) { - while (b < limit) + while (block < limit) { - if (!heap_blk_used(b)) + if (!heap_block_used(block)) { - unsigned long size = heap_blk_size(b); - while (b + size < heap_end && !heap_blk_used(b + size)) - size += heap_blk_size(b + size); - heap_set(b, size, 0); + unsigned long size = heap_block_size(block); + while (block + size < heap_end && !heap_block_used(block + size)) + size += heap_block_size(block + size); + heap_set(block, size, 0); if (size >= need) { if (size >= need + HEAP_MIN) { - heap_set(b, need, 1); - heap_set(b + need, size - need, 0); - heap_rover = b + need; + heap_set(block, need, 1); + heap_set(block + need, size - need, 0); + heap_rover = block + need; } else { - heap_set(b, size, 1); - heap_rover = b + size; + heap_set(block, size, 1); + heap_rover = block + size; } if (heap_rover >= heap_end) heap_rover = heap_start; - return (void*)(b + HEAP_HDR); + return (void*)(block + HEAP_HEADER); } } - b += heap_blk_size(b); + block += heap_block_size(block); } - b = heap_start; + block = heap_start; limit = heap_rover; } if (!heap_grow(need)) @@ -193,64 +193,64 @@ static void* malloc(unsigned long len) } } -static void free(void* ptr) +static inline void free(void* pointer) { - if (ptr == 0) + if (pointer == 0) return; - unsigned long b = (unsigned long)ptr - HEAP_HDR; - heap_set(b, heap_blk_size(b), 0); + unsigned long block = (unsigned long)pointer - HEAP_HEADER; + heap_set(block, heap_block_size(block), 0); } -static void* calloc(unsigned long count, unsigned long size) +static inline __attribute__((warn_unused_result)) void* calloc(unsigned long count, unsigned long size) { unsigned long total = count * size; - unsigned char* p = (unsigned char*)malloc(total); - if (p) + unsigned char* bytes = (unsigned char*)malloc(total); + if (bytes) for (unsigned long i = 0; i < total; i++) - p[i] = 0; - return p; + bytes[i] = 0; + return bytes; } -static void* realloc(void* ptr, unsigned long len) +static inline __attribute__((warn_unused_result)) void* realloc(void* pointer, unsigned long length) { - if (ptr == 0) - return malloc(len); - if (len == 0) + if (pointer == 0) + return malloc(length); + if (length == 0) { - free(ptr); + free(pointer); return 0; } - unsigned long b = (unsigned long)ptr - HEAP_HDR; - unsigned long have = heap_blk_size(b) - HEAP_HDR; - if (len <= have) - return ptr; - unsigned char* np = (unsigned char*)malloc(len); - if (!np) + unsigned long block = (unsigned long)pointer - HEAP_HEADER; + unsigned long have = heap_block_size(block) - HEAP_HEADER; + if (length <= have) + return pointer; + unsigned char* new_block = (unsigned char*)malloc(length); + if (!new_block) return 0; - unsigned char* src = (unsigned char*)ptr; + unsigned char* source = (unsigned char*)pointer; for (unsigned long i = 0; i < have; i++) - np[i] = src[i]; - free(ptr); - return np; + new_block[i] = source[i]; + free(pointer); + return new_block; } -static void put(const char* string) +static inline void put(const char* string) { unsigned long length = 0; while (string[length]) length++; - sys_write(1, string, length); + (void)sys_write(1, string, length); } -static int get_char(void) +static inline __attribute__((warn_unused_result)) int get_char(void) { - char c; - if (sys_read(0, &c, 1) <= 0) + char character; + if (sys_read(0, &character, 1) <= 0) return -1; - return (unsigned char)c; + return (unsigned char)character; } -static int read_line(char* buffer, int max_length) +static inline __attribute__((warn_unused_result)) int read_line(char* buffer, int max_length) { int length = 0; for (;;) @@ -265,21 +265,21 @@ static int read_line(char* buffer, int max_length) if (length > 0) { length--; - sys_write(1, "\b \b", 3); + (void)sys_write(1, "\b \b", 3); } continue; } if (length < max_length - 1) { buffer[length++] = character; - sys_write(1, &character, 1); + (void)sys_write(1, &character, 1); } } buffer[length] = 0; return length; } -static void put_int(long value) +static inline void put_int(long value) { char buffer[21]; int length = 0; @@ -305,5 +305,5 @@ static void put_int(long value) buffer[right] = swap; } - sys_write(1, buffer, length); + (void)sys_write(1, buffer, length); } -- cgit v1.3