From 13d9e4df0f4555079d16313a54c6035e22d575a6 Mon Sep 17 00:00:00 2001 From: nasr Date: Thu, 16 Apr 2026 22:00:14 +0200 Subject: feature(base): attempt at generating a random number --- source/base/base_arena.c | 2 +- source/base/base_include.h | 19 ++++++------------- source/base/base_os.h | 37 ++++++++++++++++++++++--------------- source/base/base_platform.h | 12 ------------ source/base/base_rand.h | 5 ++--- source/tb/tb.h | 0 6 files changed, 31 insertions(+), 44 deletions(-) create mode 100644 source/tb/tb.h diff --git a/source/base/base_arena.c b/source/base/base_arena.c index 60133e0..8fc8c13 100755 --- a/source/base/base_arena.c +++ b/source/base/base_arena.c @@ -110,7 +110,7 @@ arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_siz } else { - warn(0); + // warn(0); } return NULL; diff --git a/source/base/base_include.h b/source/base/base_include.h index 55ef0db..e70ccf2 100755 --- a/source/base/base_include.h +++ b/source/base/base_include.h @@ -19,26 +19,19 @@ #include "base_arena.h" #include "base_stack.h" -#include "base_string.h" -#include "base_string.c" - -#ifdef BASE_PLATFORM -#include -#include -#include +#ifdef BASE_IMPLEMENTATION +#include "base_arena.c" +#endif -#include "base_platform.h" -#endif +#include "base_string.h" +#include "base_string.c" -#include "base_error.h" #include "base_os.h" +#include "base_error.h" #include "base_rand.h" -#ifdef BASE_IMPLEMENTATION -#include "base_arena.c" -#endif #endif diff --git a/source/base/base_os.h b/source/base/base_os.h index b87c946..536119e 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h @@ -1,9 +1,6 @@ #ifndef BASE_OS_H #define BASE_OS_H -#ifdef OS_LINUX - - #define STDIN_FD 0 #define STDOUT_FD 1 #define STDERR_FD 2 @@ -22,12 +19,11 @@ load_file(mem_arena *arena, const char *path) s32 file = open(path, O_RDONLY); if(file == -1) { - warn("fialed to open file. path could be invalid"); return (string8){0}; + return (string8){0}; } if(fstat(file, &sbuf) == -1) { - warn("error: fstat failed"); close(file); return (string8){0}; } @@ -52,7 +48,6 @@ write_file(const char *path, string8 data) s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); if(file == -1) { - warn("failed to open file for writing. path could be invalid"); return (string8){0}; } @@ -62,7 +57,6 @@ write_file(const char *path, string8 data) s64 err = write(file, data.data + written, data.size - written); if(err == -1) { - warn("write syscall failed"); close(file); return (string8){0}; } @@ -74,8 +68,8 @@ write_file(const char *path, string8 data) return result; } -#define os_write(void const *buf, u64 count) _os_write(STDIN, buf, count) -#define os_read(void const *buf, u64 count) _os_read(STDIN, buf, count) +#define os_write(buf, count) _os_write(STDIN_FD, buf, count) +#define os_read(buf, count) _os_read(STDIN_FD, buf, count) internal s64 _os_write(s32 fd, void const *buf, u64 count) @@ -90,26 +84,26 @@ _os_read(s32 fd, void *buf, u64 count) } internal void -print_s8(string8 s) +log_s8(string8 s) { os_write(s.data, s.size); } internal void -print(const char *str) +_log(const char *str) { s32 len = 0; while (str[len]) len++; - os_write(STDOUT_FILENO, str, len); + os_write(str, len); } internal void -write_string(s32 fd, const char *str) +write_string(const char *str) { s32 len = 0; while (str[len]) len++; - os_write(fd, str, len); + os_write(str, len); } internal void @@ -128,5 +122,18 @@ write_int(s32 num) write(STDERR_FILENO, &digit, 1); } -#endif +internal inline void +sleep_ms(long ms) +{ + struct timespec ts; + ts.tv_sec = ms / 1000; + ts.tv_nsec = (ms % 1000) * 1000000L; + + while (nanosleep(&ts, &ts)) + { + NULL; + } +} + + #endif diff --git a/source/base/base_platform.h b/source/base/base_platform.h index 91cd55d..b34a7d0 100644 --- a/source/base/base_platform.h +++ b/source/base/base_platform.h @@ -2,17 +2,5 @@ #define BASE_PLATFORM_H #ifdef BASE_PLATFORM_IMPLEMENTATION -internal inline void -sleep_ms(long ms) -{ - struct timespec ts; - ts.tv_sec = ms / 1000; - ts.tv_nsec = (ms % 1000) * 1000000L; - - while (nanosleep(&ts, &ts)) - { - NULL; - } -} #endif /* BASE_PLATFORM_IMPLEMENTATION */ #endif /* BASE_PLATFORM_H */ diff --git a/source/base/base_rand.h b/source/base/base_rand.h index 86acafe..bfdab0f 100644 --- a/source/base/base_rand.h +++ b/source/base/base_rand.h @@ -12,9 +12,8 @@ internal u64 generate_random_u64(u64 constant) { - - time_t current_time = time(NULL); - return current_time ^ constant; + time_t current_time = time(0); + constant = current_time ^ constant; constant += 0x9e3779b97f4a7c15; diff --git a/source/tb/tb.h b/source/tb/tb.h new file mode 100644 index 0000000..e69de29 -- cgit v1.3