summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arena.c38
-rw-r--r--arena.h6
-rwxr-xr-xb52
-rwxr-xr-xbuild.sh31
-rwxr-xr-xexample/build.sh31
-rw-r--r--examples/arena.c (renamed from example/arena.c)0
-rwxr-xr-xexamples/b53
-rw-r--r--examples/macros.c (renamed from example/macros.c)0
-rw-r--r--examples/string.c (renamed from example/string.c)3
-rw-r--r--examples/sys.c (renamed from example/sys.c)0
-rw-r--r--examples/utf8.txt (renamed from example/utf8.txt)0
-rw-r--r--linux.c115
-rw-r--r--macros.h51
-rw-r--r--prb.h8
-rw-r--r--prbm.c392
-rw-r--r--prbm.h181
-rw-r--r--prbs.c219
-rw-r--r--prbs.h32
-rw-r--r--prbwin.c121
-rw-r--r--sys.h1
20 files changed, 755 insertions, 579 deletions
diff --git a/arena.c b/arena.c
index 03a1c1c..4459a5d 100644
--- a/arena.c
+++ b/arena.c
@@ -4,35 +4,35 @@
struct arena alloc_arena(u64 capacity)
{
- /* TODO(pryazha): Find reasonable maximum capacity through testing */
- assert(capacity <= (u64)gigabytes(16));
- if (!capacity)
- capacity = kilobytes(4);
- void *memory = sys_alloc(capacity);
- assert(memory);
- return (struct arena){memory, capacity, 0};
+ /* TODO(pryazha): Find reasonable maximum capacity through testing */
+ assert(capacity <= (u64)gigabytes(16));
+ if (!capacity)
+ capacity = kilobytes(4);
+ void *memory = sys_alloc(capacity);
+ assert(memory);
+ return (struct arena){memory, capacity, 0};
}
void release_arena(struct arena *arena)
{
- assert(arena);
- sys_free(arena->memory, arena->capacity);
- arena->memory = 0;
- arena->capacity = 0;
- arena->used = 0;
+ assert(arena);
+ sys_free(arena->memory, arena->capacity);
+ arena->memory = 0;
+ arena->capacity = 0;
+ arena->used = 0;
}
void *push_arena(struct arena *arena, u64 size)
{
- assert(arena);
- assert(arena->used + size <= arena->capacity);
- void *memory = arena->memory + arena->used;
- arena->used += size;
- return memory;
+ assert(arena);
+ assert(arena->used + size <= arena->capacity);
+ void *memory = arena->memory + arena->used;
+ arena->used += size;
+ return memory;
}
void pop_arena(struct arena *arena, u64 size)
{
- assert(arena);
- arena->used -= min(size, arena->used);
+ assert(arena);
+ arena->used -= min(size, arena->used);
}
diff --git a/arena.h b/arena.h
index 38e3483..b05d990 100644
--- a/arena.h
+++ b/arena.h
@@ -4,9 +4,9 @@
#include "types.h"
struct arena {
- void *memory;
- u64 capacity;
- u64 used;
+ void *memory;
+ u64 capacity;
+ u64 used;
};
struct arena alloc_arena(u64 capacity);
diff --git a/b b/b
new file mode 100755
index 0000000..235cc28
--- /dev/null
+++ b/b
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+echorun()
+{
+ echo $1
+ $1
+}
+
+compile()
+{
+ echorun "$cc $cflags $include $lflags -o $dir/$1.o $dir/$1.c $libs"
+}
+
+cur=`pwd`
+dir=`realpath $0`
+dir=`dirname $dir`
+
+echorun "cd $dir"
+
+if [ "$1" = 'clean' ] ; then
+ echorun "rm -f *.o"
+ echorun "cd $cur"
+ exit
+fi
+if [ "$1" = 'cleanlib' ] ; then
+ echorun "rm -f *.o libprb.a"
+ echorun "cd $cur"
+ exit
+fi
+
+cc='gcc'
+debug='-g'
+release='-O2'
+cflags="-c"
+if [ "`echo \"$*\" | grep debug -`" ] ; then
+ cflags="$debug $cflags"
+else
+ cflags="$release $cflags"
+fi
+
+compile prbm
+compile arena
+compile prbs
+compile linux
+
+echorun "ar rcs $dir/libprb.a \
+ $dir/prbm.o \
+ $dir/arena.o \
+ $dir/prbs.o \
+ $dir/linux.o"
+
+echorun "cd $cur"
diff --git a/build.sh b/build.sh
deleted file mode 100755
index 53ad6cb..0000000
--- a/build.sh
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/bin/sh
-start=`pwd`
-dir=`dirname "$0"`
-if [ "$1" = 'clean' ] ; then
- set -x
- cd $dir
- rm -f *.o
- cd "$start"
- exit
-fi
-cc='tcc'
-debug='-g'
-release='-O2'
-cflags="-c -std=c99"
-if [ "$1" = "debug" ] ; then
- cflags="$debug $cflags"
-else
- cflags="$release $cflags"
-fi
-set -x
-cd $dir
-$cc $cflags prbm.c
-$cc $cflags arena.c
-$cc $cflags prbs.c
-$cc $cflags linux.c
-ar rcs libprb.a\
- prbm.o \
- arena.o \
- prbs.o \
- linux.o
-cd "$start"
diff --git a/example/build.sh b/example/build.sh
deleted file mode 100755
index 285b072..0000000
--- a/example/build.sh
+++ /dev/null
@@ -1,31 +0,0 @@
-#!/bin/sh
-start=`pwd`
-dir=`dirname "$0"`
-if [ "$1" = 'clean' ] ; then
- set -x
- cd "$dir"
- rm -f macros macros.exe arena arena.exe sys sys.exe string string.exe
- cd "$start"
- exit
-fi
-cc='tcc'
-if [ "$1" = 'windows' ] ; then
- cc='x86_64-w64-mingw32-gcc'
-fi
-libsdir='../..'
-if [ ! -d "$libsdir/prb" ] ; then
- echo "error: failed to get prb library path"
- exit
-fi
-"$libsdir/prb/build.sh"
-cflags='-g -Wall'
-include="-I$libsdir/prb"
-lflags="-L$libsdir/prb"
-libs='-lprb'
-set -x
-cd "$dir"
-$cc $cflags $include $lflags -o macros macros.c $libs
-$cc $cflags $include $lflags -o arena arena.c $libs
-$cc $cflags $include $lflags -o sys sys.c $libs
-$cc $cflags $include $lflags -o string string.c $libs
-cd "$start"
diff --git a/example/arena.c b/examples/arena.c
index 6579849..6579849 100644
--- a/example/arena.c
+++ b/examples/arena.c
diff --git a/examples/b b/examples/b
new file mode 100755
index 0000000..9c29028
--- /dev/null
+++ b/examples/b
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+echorun()
+{
+ echo $1
+ $1
+}
+
+compile()
+{
+ echorun "$cc $cflags $include $lflags -o $dir/$1 $dir/$1.c $libs"
+}
+
+cur=`pwd`
+dir=`realpath $0`
+dir=`dirname $dir`
+
+echorun "cd $dir"
+
+if [ "$1" = 'clean' ] ; then
+ echorun "rm -f $dir/macros \
+ $dir/macros.exe \
+ $dir/arena \
+ $dir/arena.exe \
+ $dir/sys \
+ $dir/sys.exe \
+ $dir/string \
+ $dir/string.exe"
+ echorun "cd $cur"
+ exit
+fi
+
+cc='tcc'
+if [ "$1" = 'windows' ] ; then
+ cc='x86_64-w64-mingw32-gcc'
+fi
+
+prbdir="$dir/.."
+if [ ! -e "$prbdir/libprb.a" ] ; then
+ $prbdir/build.sh
+fi
+
+cflags='-g -Wall -std=c99'
+include="-I$prbdir"
+lflags="-L$prbdir"
+libs='-lprb -lm'
+
+compile macros
+compile arena
+compile sys
+compile string
+
+echorun "cd $cur"
diff --git a/example/macros.c b/examples/macros.c
index 0e836b0..0e836b0 100644
--- a/example/macros.c
+++ b/examples/macros.c
diff --git a/example/string.c b/examples/string.c
index 57c460d..bbda94d 100644
--- a/example/string.c
+++ b/examples/string.c
@@ -1,4 +1,5 @@
#include "prb.h"
+#include <stdio.h>
i32 main(void)
{
@@ -15,5 +16,5 @@ i32 main(void)
struct string catstr = join_string_list(&arena, &list);
info("Concatenated string list:");
print_string(catstr);
- return(0);
+ return 0;
}
diff --git a/example/sys.c b/examples/sys.c
index 881d2be..881d2be 100644
--- a/example/sys.c
+++ b/examples/sys.c
diff --git a/example/utf8.txt b/examples/utf8.txt
index 3af7875..3af7875 100644
--- a/example/utf8.txt
+++ b/examples/utf8.txt
diff --git a/linux.c b/linux.c
index ff20e92..cfdcf6a 100644
--- a/linux.c
+++ b/linux.c
@@ -1,70 +1,91 @@
#include "sys.h"
#include "macros.h"
+#include "prbs.h"
+
#include <sys/mman.h>
#include <stdarg.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
-void *sys_alloc(u64 length)
+void
+*sys_alloc(u64 length)
{
- assert(length);
- i32 prot = PROT_READ | PROT_WRITE;
- i32 flags = MAP_PRIVATE|MAP_ANONYMOUS;
- void *result = mmap(0, length, prot, flags, -1, 0);
- return result;
+ assert(length);
+ i32 prot = PROT_READ | PROT_WRITE;
+ i32 flags = MAP_PRIVATE|MAP_ANONYMOUS;
+ void *result = mmap(0, length, prot, flags, -1, 0);
+ return result;
}
-void sys_free(void *memory, u64 length)
+void
+sys_free(void *memory, u64 length)
{
- assert(memory);
- assert(length);
- munmap(memory, length);
+ assert(memory);
+ assert(length);
+ munmap(memory, length);
}
-void die(const char *format, ...)
+void
+die(const char *format, ...)
{
- va_list args;
- va_start(args, format);
- fprintf(stderr, "error: ");
- vfprintf(stderr, format, args);
- fprintf(stderr, "\n");
- va_end(args);
- _exit(1);
+ va_list args;
+ va_start(args, format);
+ fprintf(stderr, "error: ");
+ vfprintf(stderr, format, args);
+ fprintf(stderr, "\n");
+ va_end(args);
+ _exit(1);
}
-void info(const char *format, ...)
+void
+info(const char *format, ...)
{
- va_list args;
- va_start(args, format);
- vprintf(format, args);
- putchar('\n');
- va_end(args);
+ va_list args;
+ va_start(args, format);
+ vprintf(format, args);
+ putchar('\n');
+ va_end(args);
}
-char *read_entire_file(struct arena *arena, u64 *len, const char *filename)
+char *
+read_entire_file(struct arena *arena, u64 *len, const char *filename)
{
- FILE *file = fopen(filename, "rb");
- if (!file)
- return 0;
- if (fseek(file, 0, SEEK_END) == -1)
- goto error;
- i64 n = ftell(file);
- if (n <= 0)
- goto error;
- if (len)
- *len = n;
- if (fseek(file, 0, SEEK_SET) == -1)
- goto error;
- char *buffer = push_arena(arena, n + 1);
- if (!fread(buffer, 1, n, file)) {
- pop_arena(arena, n + 1);
- goto error;
- }
- fclose(file);
- buffer[n] = 0;
- return buffer;
+ FILE *file = fopen(filename, "rb");
+ if (!file)
+ return 0;
+ if (fseek(file, 0, SEEK_END) == -1)
+ goto error;
+ i64 n = ftell(file);
+ if (n <= 0)
+ goto error;
+ if (len)
+ *len = n;
+ if (fseek(file, 0, SEEK_SET) == -1)
+ goto error;
+ char *buffer = push_arena(arena, n + 1);
+ if (!fread(buffer, 1, n, file)) {
+ pop_arena(arena, n + 1);
+ goto error;
+ }
+ fclose(file);
+ buffer[n] = 0;
+ return buffer;
+
error:
- fclose(file);
- return 0;
+ fclose(file);
+ return 0;
+}
+
+struct string
+get_basedir(struct arena *arena)
+{
+ char buf[1024] = {0};
+ if (readlink("/proc/self/exe", buf, 1024 - 1) == -1)
+ die("failed to read link /proc/self/exe");
+ struct arena temp = alloc_arena(0);
+ struct string full = string_from_cstring(&temp, buf);
+ struct string base = push_string(arena, get_dirname(full));
+ release_arena(&temp);
+ return base;
}
diff --git a/macros.h b/macros.h
index eb514b5..2fa04a3 100644
--- a/macros.h
+++ b/macros.h
@@ -1,22 +1,25 @@
#ifndef macros_h
#define macros_h
+/*
#undef assert
-#define assert(expr) if (!(expr)) { *(int *)0 = 0; }
+#define assert(expr) if (!(expr)) { *(int *)0 = 0; }
+*/
+#include <assert.h>
-#define array_count(array) (sizeof(array)/sizeof(*(array)))
+#define array_count(array) (sizeof(array)/sizeof(*(array)))
#undef offsetof
-#define offsetof(type, element) ((u64)(&(((type *)0)->element)))
+#define offsetof(type, element) ((u64)(&(((type *)0)->element)))
-#define min(a, b) (((a) < (b)) ? (a) : (b))
-#define max(a, b) (((a) > (b)) ? (a) : (b))
+#define min(a, b) (((a) < (b)) ? (a) : (b))
+#define max(a, b) (((a) > (b)) ? (a) : (b))
#define clamp(a, x, b) \
- (((x) < (a)) ? (a) : \
- (((x) > (b)) ? (b) : (x)))
+ (((x) < (a)) ? (a) : \
+ (((x) > (b)) ? (b) : (x)))
-#define swap(type, a, b) { type tmp = (a); (a) = (b); (b) = tmp; }
+#define swap(type, a, b) { type tmp = (a); (a) = (b); (b) = tmp; }
#define kilobytes(n) n*1024
#define megabytes(n) kilobytes(n)*1024
@@ -24,27 +27,27 @@
/* NOTE(pryazha): Singly linked list */
#define sllpush(first, last, node) \
- ((first) == 0 ? \
- ((first) = (last) = (node), (node)->next = 0) : \
- ((last)->next = (node), (last) = (node), (node)->next = 0))
+ ((first) == 0 ? \
+ ((first) = (last) = (node), (node)->next = 0) : \
+ ((last)->next = (node), (last) = (node), (node)->next = 0))
#define sllpop(first, last) \
- ((first) == (last) ? \
- ((first) = (last) = 0) : \
- ((first) = (first)->next))
+ ((first) == (last) ? \
+ ((first) = (last) = 0) : \
+ ((first) = (first)->next))
/* NOTE(pryazha): Doubly linked list */
#define dllpush(first, last, node, next, prev) \
- ((first) == 0 ? \
- ((first) = (last) = (node), (node)->next = (node)->prev = 0) : \
- ((node)->prev = (last), (last)->next = (node), (last) = (node), (node)->next = 0))
-#define dllpushback(first, last, node) dllpush(first, last, node, next, prev)
-#define dllpushfront(first, last, node) dllpush(last, first, node, prev, next)
+ ((first) == 0 ? \
+ ((first) = (last) = (node), (node)->next = (node)->prev = 0) : \
+ ((node)->prev = (last), (last)->next = (node), (last) = (node), (node)->next = 0))
+#define dllpushback(first, last, node) dllpush(first, last, node, next, prev)
+#define dllpushfront(first, last, node) dllpush(last, first, node, prev, next)
#define dllremove(first, last, node) \
- ((first) == (node) ? \
- ((first) == (last) ? (first) = (last) = 0 : \
- ((first) = (first)->next, (first)->prev = 0)) : \
- ((last) == (node) ? ((last) = (last)->prev, (last)->next = 0) : \
- ((node)->next->prev = (node)->prev, (node)->prev->next = (node)->next)))
+ ((first) == (node) ? \
+ ((first) == (last) ? (first) = (last) = 0 : \
+ ((first) = (first)->next, (first)->prev = 0)) : \
+ ((last) == (node) ? ((last) = (last)->prev, (last)->next = 0) : \
+ ((node)->next->prev = (node)->prev, (node)->prev->next = (node)->next)))
#endif
diff --git a/prb.h b/prb.h
index 829f294..cbf4c52 100644
--- a/prb.h
+++ b/prb.h
@@ -8,15 +8,15 @@
#include "prbm.h"
#include "prbs.h"
-#define os_none 0
+#define os_none 0
#define os_windows 1
#define os_linux 2
#if defined(__linux)
-# define os os_linux
+# define os os_linux
#elif defined(__WIN64)
-# define os os_windows
-# include <windows.h>
+# define os os_windows
+# include <windows.h>
#endif
#endif
diff --git a/prbm.c b/prbm.c
index ae1ac97..3606569 100644
--- a/prbm.c
+++ b/prbm.c
@@ -1,297 +1,345 @@
#include "prbm.h"
+#include "macros.h"
#include <math.h>
-struct v2 fillv2(f32 a)
+v2
+fillv2(f32 a)
{
- return (struct v2){a, a};
+ return (v2){a, a};
}
-struct v2 invv2(struct v2 a)
+v2
+invv2(v2 a)
{
- return (struct v2){-a.x, -a.y};
+ return (v2){-a.x, -a.y};
}
-struct v2 addv2(struct v2 a, struct v2 b)
+v2
+addv2(v2 a, v2 b)
{
- return (struct v2){a.x + b.x, a.y + b.y};
+ return (v2){a.x + b.x, a.y + b.y};
}
-struct v2 subv2(struct v2 a, struct v2 b)
+v2
+subv2(v2 a, v2 b)
{
- return (struct v2){a.x - b.x, a.y - b.y};
+ return (v2){a.x - b.x, a.y - b.y};
}
-struct v2 scalev2(struct v2 a, f32 s)
+v2
+scalev2(v2 a, f32 s)
{
- return (struct v2){a.x * s, a.y * s};
+ return (v2){a.x * s, a.y * s};
}
-struct v2 scalevv2(struct v2 a, struct v2 s)
+v2
+scalevv2(v2 a, v2 s)
{
- return (struct v2){a.x * s.x, a.y * s.y};
+ return (v2){a.x * s.x, a.y * s.y};
}
-f32 dotv2(struct v2 a, struct v2 b)
+f32
+dotv2(v2 a, v2 b)
{
- return (a.x * b.x + a.y * b.y);
+ return (a.x * b.x + a.y * b.y);
}
-f32 len2v2(struct v2 a)
+f32
+len2v2(v2 a)
{
- return dotv2(a, a);
+ return dotv2(a, a);
}
-f32 lenv2(struct v2 a)
+f32
+lenv2(v2 a)
{
- return sqrtf(len2v2(a));
+ return sqrtf(len2v2(a));
}
-struct v2 normv2(struct v2 a)
+v2
+normv2(v2 a)
{
- f32 len = lenv2(a);
- if (len) {
- return v2_zero;
- }
- f32 ilen = 1.0f / len;
- return (struct v2){a.x * len, a.y * len};
+ f32 len = lenv2(a);
+ if (len) {
+ return v2_zero;
+ }
+ f32 ilen = 1.0f / len;
+ return (v2){a.x * len, a.y * len};
}
-struct v3 fillv3(f32 a)
+v3
+fillv3(f32 a)
{
- return (struct v3){a, a, a};
+ return (v3){a, a, a};
}
-struct v3 invv3(struct v3 a)
+v3
+invv3(v3 a)
{
- return (struct v3){-a.x, -a.y, -a.z};
+ return (v3){-a.x, -a.y, -a.z};
}
-struct v3 addv3(struct v3 a, struct v3 b)
+v3
+addv3(v3 a, v3 b)
{
- return (struct v3){a.x + b.x, a.y + b.y, a.z + b.z};
+ return (v3){a.x + b.x, a.y + b.y, a.z + b.z};
}
-struct v3 subv3(struct v3 a, struct v3 b)
+v3
+subv3(v3 a, v3 b)
{
- return (struct v3){a.x - b.x, a.y - b.y, a.z - b.z};
+ return (v3){a.x - b.x, a.y - b.y, a.z - b.z};
}
-struct v3 scalev3(struct v3 a, f32 s)
+v3
+scalev3(v3 a, f32 s)
{
- return (struct v3){a.x * s, a.y * s, a.z * s};
+ return (v3){a.x * s, a.y * s, a.z * s};
}
-struct v3 scalevv3(struct v3 a, struct v3 s)
+v3
+scalevv3(v3 a, v3 s)
{
- return (struct v3){a.x * s.x, a.y * s.y, a.z * s.z};
+ return (v3){a.x * s.x, a.y * s.y, a.z * s.z};
}
-f32 dotv3(struct v3 a, struct v3 b)
+f32
+dotv3(v3 a, v3 b)
{
- return (a.x * b.x + a.y * b.y + a.z * b.z);
+ return (a.x * b.x + a.y * b.y + a.z * b.z);
}
-struct v3 crossv3(struct v3 l, struct v3 r)
+v3
+crossv3(v3 l, v3 r)
{
- return (struct v3){
- (l.y * r.z - r.y * l.z),
- (r.x * l.z - l.x * r.z),
- (l.x * r.y - r.x * l.y)
- };
+ return (v3){
+ (l.y * r.z - r.y * l.z),
+ (r.x * l.z - l.x * r.z),
+ (l.x * r.y - r.x * l.y)
+ };
}
-f32 len2v3(struct v3 a)
+f32
+len2v3(v3 a)
{
- return dotv3(a, a);
+ return dotv3(a, a);
}
-f32 lenv3(struct v3 a)
+f32
+lenv3(v3 a)
{
- return sqrtf(len2v3(a));
+ return sqrtf(len2v3(a));
}
-struct v3 normv3(struct v3 a)
+v3
+normv3(v3 a)
{
- f32 len = lenv3(a);
- if (!len)
- return v3_zero;
- f32 ilen = 1.0f / len;
- return (struct v3){a.x * len, a.y * len, a.z * len};
+ f32 len = lenv3(a);
+ if (!len)
+ return v3_zero;
+ f32 ilen = 1.0f / len;
+ return (v3){a.x * len, a.y * len, a.z * len};
}
-struct v4 fillv4(f32 a)
+v3
+maxv3(v3 a, v3 b)
{
- return (struct v4){a, a, a, a};
+ return (v3){max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)};
}
-struct v4 invv4(struct v4 a)
+v4
+fillv4(f32 a)
{
- return (struct v4){-a.x, -a.y, -a.z, -a.w};
+ return (v4){a, a, a, a};
}
-struct v4 addv4(struct v4 a, struct v4 b)
+v4
+invv4(v4 a)
{
- return (struct v4){a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
+ return (v4){-a.x, -a.y, -a.z, -a.w};
}
-struct v4 subv4(struct v4 a, struct v4 b)
+v4
+addv4(v4 a, v4 b)
{
- return (struct v4){a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
+ return (v4){a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w};
}
-struct v4 scalev4(struct v4 a, f32 s)
+v4
+subv4(v4 a, v4 b)
{
- return (struct v4){a.x * s, a.y * s, a.z * s, a.w * s};
+ return (v4){a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w};
}
-struct v4 scalevv4(struct v4 a, struct v4 s)
+v4
+scalev4(v4 a, f32 s)
{
- return (struct v4){a.x * s.x, a.y * s.y, a.z * s.z, a.w * s.w};
+ return (v4){a.x * s, a.y * s, a.z * s, a.w * s};
}
-f32 dotv4(struct v4 a, struct v4 b)
+v4
+scalevv4(v4 a, v4 s)
{
- return (a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w);
+ return (v4){a.x * s.x, a.y * s.y, a.z * s.z, a.w * s.w};
}
-f32 len2v4(struct v4 a)
+f32
+dotv4(v4 a, v4 b)
{
- return dotv4(a, a);
+ return (a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w);
}
-f32 lenv4(struct v4 a)
+f32
+len2v4(v4 a)
{
- return sqrtf(len2v4(a));
+ return dotv4(a, a);
}
-struct v4 normv4(struct v4 a)
+f32
+lenv4(v4 a)
{
- f32 len = lenv4(a);
- if (!len)
- return v4_zero;
- f32 ilen = 1.0f / len;
- return (struct v4){a.x * ilen, a.y * ilen, a.z * ilen, a.w * ilen};
+ return sqrtf(len2v4(a));
}
-struct mat transpmat(struct mat m)
+v4
+normv4(v4 a)
{
- struct mat r = m;
- r.c0.y = m.c1.x;
- r.c0.z = m.c2.x;
- r.c0.w = m.c3.x;
- r.c1.z = m.c2.y;
- r.c1.w = m.c3.y;
- r.c2.w = m.c3.z;
- return r;
+ f32 len = lenv4(a);
+ if (!len)
+ return v4_zero;
+ f32 ilen = 1.0f / len;
+ return (v4){a.x * ilen, a.y * ilen, a.z * ilen, a.w * ilen};
}
-struct mat mulmat(struct mat left, struct mat right)
+mat
+transpose_mat(mat m)
{
- f32 l00 = left.c0.x, l01 = left.c0.y, l02 = left.c0.z, l03 = left.c0.w;
- f32 l10 = left.c1.x, l11 = left.c1.y, l12 = left.c1.z, l13 = left.c1.w;
- f32 l20 = left.c2.x, l21 = left.c2.y, l22 = left.c2.z, l23 = left.c2.w;
- f32 l30 = left.c3.x, l31 = left.c3.y, l32 = left.c3.z, l33 = left.c3.w;
- f32 r00 = right.c0.x, r01 = right.c0.y, r02 = right.c0.z, r03 = right.c0.w;
- f32 r10 = right.c1.x, r11 = right.c1.y, r12 = right.c1.z, r13 = right.c1.w;
- f32 r20 = right.c2.x, r21 = right.c2.y, r22 = right.c2.z, r23 = right.c2.w;
- f32 r30 = right.c3.x, r31 = right.c3.y, r32 = right.c3.z, r33 = right.c3.w;
- return (struct mat){
- {
- l00*r00 + l10*r01 + l20*r02 + l30*r03,
- l01*r00 + l11*r01 + l21*r02 + l31*r03,
- l02*r00 + l12*r01 + l22*r02 + l32*r03,
- l03*r00 + l13*r01 + l23*r02 + l33*r03
- },
- {
- l00*r10 + l10*r11 + l20*r12 + l30*r13,
- l01*r10 + l11*r11 + l21*r12 + l31*r13,
- l02*r10 + l12*r11 + l22*r12 + l32*r13,
- l03*r10 + l13*r11 + l23*r12 + l33*r13
- },
- {
- l00*r20 + l10*r21 + l20*r22 + l30*r23,
- l01*r20 + l11*r21 + l21*r22 + l31*r23,
- l02*r20 + l12*r21 + l22*r22 + l32*r23,
- l03*r20 + l13*r21 + l23*r22 + l33*r23
- },
- {
- l00*r30 + l10*r31 + l20*r32 + l30*r33,
- l01*r30 + l11*r31 + l21*r32 + l31*r33,
- l02*r30 + l12*r31 + l22*r32 + l32*r33,
- l03*r30 + l13*r31 + l23*r32 + l33*r33
- }
- };
+ mat r = m;
+ r.c0.y = m.c1.x;
+ r.c0.z = m.c2.x;
+ r.c0.w = m.c3.x;
+ r.c1.z = m.c2.y;
+ r.c1.w = m.c3.y;
+ r.c2.w = m.c3.z;
+ return r;
}
-struct mat make_translate(struct v3 v)
+mat
+mulmat(mat left, mat right)
{
- return (struct mat){
- {1.0f, 0.0f, 0.0f, 0.0f},
- {0.0f, 1.0f, 0.0f, 0.0f},
- {0.0f, 0.0f, 1.0f, 0.0f},
- {v.x, v.y, v.z, 1.0f}
- };
+ f32 l00 = left.c0.x, l01 = left.c0.y, l02 = left.c0.z, l03 = left.c0.w;
+ f32 l10 = left.c1.x, l11 = left.c1.y, l12 = left.c1.z, l13 = left.c1.w;
+ f32 l20 = left.c2.x, l21 = left.c2.y, l22 = left.c2.z, l23 = left.c2.w;
+ f32 l30 = left.c3.x, l31 = left.c3.y, l32 = left.c3.z, l33 = left.c3.w;
+ f32 r00 = right.c0.x, r01 = right.c0.y, r02 = right.c0.z, r03 = right.c0.w;
+ f32 r10 = right.c1.x, r11 = right.c1.y, r12 = right.c1.z, r13 = right.c1.w;
+ f32 r20 = right.c2.x, r21 = right.c2.y, r22 = right.c2.z, r23 = right.c2.w;
+ f32 r30 = right.c3.x, r31 = right.c3.y, r32 = right.c3.z, r33 = right.c3.w;
+ return (mat){
+ {
+ l00*r00 + l10*r01 + l20*r02 + l30*r03,
+ l01*r00 + l11*r01 + l21*r02 + l31*r03,
+ l02*r00 + l12*r01 + l22*r02 + l32*r03,
+ l03*r00 + l13*r01 + l23*r02 + l33*r03
+ },
+ {
+ l00*r10 + l10*r11 + l20*r12 + l30*r13,
+ l01*r10 + l11*r11 + l21*r12 + l31*r13,
+ l02*r10 + l12*r11 + l22*r12 + l32*r13,
+ l03*r10 + l13*r11 + l23*r12 + l33*r13
+ },
+ {
+ l00*r20 + l10*r21 + l20*r22 + l30*r23,
+ l01*r20 + l11*r21 + l21*r22 + l31*r23,
+ l02*r20 + l12*r21 + l22*r22 + l32*r23,
+ l03*r20 + l13*r21 + l23*r22 + l33*r23
+ },
+ {
+ l00*r30 + l10*r31 + l20*r32 + l30*r33,
+ l01*r30 + l11*r31 + l21*r32 + l31*r33,
+ l02*r30 + l12*r31 + l22*r32 + l32*r33,
+ l03*r30 + l13*r31 + l23*r32 + l33*r33
+ }
+ };
}
-struct mat make_scale(struct v3 v)
+mat
+make_translate(v3 v)
{
- return (struct mat){
- {v.x, 0.0f, 0.0f, 0.0f},
- {0.0f, v.y, 0.0f, 0.0f},
- {0.0f, 0.0f, v.z, 0.0f},
- {0.0f, 0.0f, 0.0f, 1.0f}
- };
+ return (mat){
+ {1.0f, 0.0f, 0.0f, 0.0f},
+ {0.0f, 1.0f, 0.0f, 0.0f},
+ {0.0f, 0.0f, 1.0f, 0.0f},
+ {v.x, v.y, v.z, 1.0f}
+ };
}
-struct mat make_rotate(struct v3 x, struct v3 y, struct v3 z)
+mat
+make_scale(v3 v)
{
- return (struct mat){
- {x.x, x.y, x.z, 0.0f},
- {y.x, y.y, y.z, 0.0f},
- {z.x, z.y, z.z, 0.0f},
- {0.0f, 0.0f, 0.0f, 1.0f}
- };
+ return (mat){
+ {v.x, 0.0f, 0.0f, 0.0f},
+ {0.0f, v.y, 0.0f, 0.0f},
+ {0.0f, 0.0f, v.z, 0.0f},
+ {0.0f, 0.0f, 0.0f, 1.0f}
+ };
}
-struct mat translate_mat(struct mat m, struct v3 v)
+mat
+make_rotate(v3 x, v3 y, v3 z)
{
- return mulmat(make_translate(v), m);
+ return (mat){
+ {x.x, x.y, x.z, 0.0f},
+ {y.x, y.y, y.z, 0.0f},
+ {z.x, z.y, z.z, 0.0f},
+ {0.0f, 0.0f, 0.0f, 1.0f}
+ };
}
-struct mat scale_mat(struct mat m, struct v3 v)
+mat
+translate_mat(mat m, v3 v)
{
- return mulmat(make_scale(v), m);
+ return mulmat(make_translate(v), m);
}
-struct mat rotate_mat(struct mat m, struct v3 angles)
+mat
+scale_mat(mat m, v3 v)
{
- f32 angle = deg2rad(angles.x);
- f32 cx = cosf(angle);
- f32 sx = sinf(angle);
- angle = deg2rad(angles.y);
- f32 cy = cosf(angle);
- f32 sy = sinf(angle);
- angle = deg2rad(angles.z);
- f32 cz = cosf(angle);
- f32 sz = sinf(angle);
- struct v3 x = { cy*cz, sx*sy*cz + cx*sz, -cx*sy*cz + sx*sz};
- struct v3 y = {-cy*sz, -sx*sy*sz + cx*cz, cx*sy*sz + sx*cz};
- struct v3 z = { sy, -sx*cy, cx*cy};
- return mulmat(make_rotate(x, y, z), m);
+ return mulmat(make_scale(v), m);
}
-struct v4 mulmatv4(struct mat m, struct v4 v)
+mat
+rotate_mat(mat m, v3 angles)
{
- return (struct v4){
- m.c0.x*v.x + m.c1.x*v.y + m.c2.x*v.z + m.c3.x*v.w,
- m.c0.y*v.x + m.c1.y*v.y + m.c2.y*v.z + m.c3.y*v.w,
- m.c0.z*v.x + m.c1.z*v.y + m.c2.z*v.z + m.c3.z*v.w,
- m.c0.w*v.x + m.c1.w*v.y + m.c2.w*v.z + m.c3.w*v.w
- };
+ f32 angle = deg2rad(angles.x);
+ f32 cx = cosf(angle);
+ f32 sx = sinf(angle);
+ angle = deg2rad(angles.y);
+ f32 cy = cosf(angle);
+ f32 sy = sinf(angle);
+ angle = deg2rad(angles.z);
+ f32 cz = cosf(angle);
+ f32 sz = sinf(angle);
+ v3 x = { cy*cz, sx*sy*cz + cx*sz, -cx*sy*cz + sx*sz};
+ v3 y = {-cy*sz, -sx*sy*sz + cx*cz, cx*sy*sz + sx*cz};
+ v3 z = { sy, -sx*cy, cx*cy};
+ return mulmat(make_rotate(x, y, z), m);
}
-i32 in_rect(struct v2 pos, struct rect rect)
+v4
+mulmatv4(mat m, v4 v)
{
- return (pos.x > rect.start.x) && (pos.x < rect.end.x) &&
- (pos.y > rect.start.y) && (pos.y < rect.end.y);
+ return (v4){
+ m.c0.x*v.x + m.c1.x*v.y + m.c2.x*v.z + m.c3.x*v.w,
+ m.c0.y*v.x + m.c1.y*v.y + m.c2.y*v.z + m.c3.y*v.w,
+ m.c0.z*v.x + m.c1.z*v.y + m.c2.z*v.z + m.c3.z*v.w,
+ m.c0.w*v.x + m.c1.w*v.y + m.c2.w*v.z + m.c3.w*v.w
+ };
+}
+
+i32
+in_rect(v2 pos, rect r)
+{
+ return (pos.x > r.start.x) && (pos.x < r.end.x) &&
+ (pos.y > r.start.y) && (pos.y < r.end.y);
}
diff --git a/prbm.h b/prbm.h
index f3932dd..a5432de 100644
--- a/prbm.h
+++ b/prbm.h
@@ -3,110 +3,111 @@
#include "types.h"
-struct v2 {
- f32 x;
- f32 y;
-};
+#define v2_zero (v2){ 0.0f, 0.0f}
+#define v2_one (v2){ 1.0f, 1.0f}
+#define v2_right (v2){ 1.0f, 0.0f}
+#define v2_up (v2){ 0.0f, 1.0f}
+#define v2_left (v2){-1.0f, 0.0f}
+#define v2_down (v2){ 0.0f, -1.0f}
-struct v3 {
- f32 x;
- f32 y;
- f32 z;
-};
+#define v3_zero (v3){ 0.0f, 0.0f, 0.0f}
+#define v3_one (v3){ 1.0f, 1.0f, 1.0f}
+#define v3_right (v3){ 1.0f, 0.0f, 0.0f}
+#define v3_up (v3){ 0.0f, 1.0f, 0.0f}
+#define v3_left (v3){-1.0f, 0.0f, 0.0f}
+#define v3_down (v3){ 0.0f, -1.0f, 0.0f}
+#define v3_forward (v3){ 0.0f, 0.0f, 1.0f}
+#define v3_backward (v3){ 0.0f, 0.0f, -1.0f}
-struct v4 {
- f32 x;
- f32 y;
- f32 z;
- f32 w;
-};
+#define v4_zero (v4){0.0f, 0.0f, 0.0f, 0.0f}
+#define v4_one (v4){1.0f, 1.0f, 1.0f, 1.0f}
-// column-major
-struct mat {
- struct v4 c0;
- struct v4 c1;
- struct v4 c2;
- struct v4 c3;
-};
+#define mat_identity (mat){ \
+ {1.0f, 0.0f, 0.0f, 0.0f}, \
+ {0.0f, 1.0f, 0.0f, 0.0f}, \
+ {0.0f, 0.0f, 1.0f, 0.0f}, \
+ {0.0f, 0.0f, 0.0f, 1.0f}}
-struct rect {
- struct v2 start;
- struct v2 end;
-};
+#define f32pi 3.14159265359f
-#define v2_zero (struct v2){ 0.0f, 0.0f}
-#define v2_one (struct v2){ 1.0f, 1.0f}
-#define v2_right (struct v2){ 1.0f, 0.0f}
-#define v2_up (struct v2){ 0.0f, 1.0f}
-#define v2_left (struct v2){-1.0f, 0.0f}
-#define v2_down (struct v2){ 0.0f, -1.0f}
+#define deg2rad(angle) (f32pi/180.0f*(angle))
-#define v3_zero (struct v3){ 0.0f, 0.0f, 0.0f}
-#define v3_one (struct v3){ 1.0f, 1.0f, 1.0f}
-#define v3_right (struct v3){ 1.0f, 0.0f, 0.0f}
-#define v3_up (struct v3){ 0.0f, 1.0f, 0.0f}
-#define v3_left (struct v3){-1.0f, 0.0f, 0.0f}
-#define v3_down (struct v3){ 0.0f, -1.0f, 0.0f}
-#define v3_forward (struct v3){ 0.0f, 0.0f, 1.0f}
-#define v3_backward (struct v3){ 0.0f, 0.0f, -1.0f}
+typedef struct {
+ f32 x;
+ f32 y;
+} v2;
-#define v4_zero (struct v4){0.0f, 0.0f, 0.0f, 0.0f}
-#define v4_one (struct v4){1.0f, 1.0f, 1.0f, 1.0f}
+typedef struct {
+ f32 x;
+ f32 y;
+ f32 z;
+} v3;
-#define mat_identity (struct mat){ \
- {1.0f, 0.0f, 0.0f, 0.0f}, \
- {0.0f, 1.0f, 0.0f, 0.0f}, \
- {0.0f, 0.0f, 1.0f, 0.0f}, \
- {0.0f, 0.0f, 0.0f, 1.0f}}
+typedef struct {
+ f32 x;
+ f32 y;
+ f32 z;
+ f32 w;
+} v4;
-#define f32pi 3.14159265359f
+// column-major
+typedef struct {
+ v4 c0;
+ v4 c1;
+ v4 c2;
+ v4 c3;
+} mat;
-#define deg2rad(angle) (f32pi/180.0f*(angle))
+typedef struct {
+ v2 start;
+ v2 end;
+} rect;
-struct v2 fillv2(f32 a);
-struct v2 invv2(struct v2 a);
-struct v2 addv2(struct v2 a, struct v2 b);
-struct v2 subv2(struct v2 a, struct v2 b);
-struct v2 scalev2(struct v2 a, f32 s);
-struct v2 scalevv2(struct v2 a, struct v2 s);
-f32 dotv2(struct v2 a, struct v2 b);
-f32 len2v2(struct v2 a);
-f32 lenv2(struct v2 a);
-struct v2 normv2(struct v2 a);
+v2 fillv2(f32 a);
+v2 invv2(v2 a);
+v2 addv2(v2 a, v2 b);
+v2 subv2(v2 a, v2 b);
+v2 scalev2(v2 a, f32 s);
+v2 scalevv2(v2 a, v2 s);
+f32 dotv2(v2 a, v2 b);
+f32 len2v2(v2 a);
+f32 lenv2(v2 a);
+v2 normv2(v2 a);
-struct v3 fillv3(f32 a);
-struct v3 invv3(struct v3 a);
-struct v3 addv3(struct v3 a, struct v3 b);
-struct v3 subv3(struct v3 a, struct v3 b);
-struct v3 scalev3(struct v3 a, f32 s);
-struct v3 scalevv3(struct v3 a, struct v3 s);
-f32 dotv3(struct v3 a, struct v3 b);
-struct v3 crossv3(struct v3 l, struct v3 r);
-f32 len2v3(struct v3 a);
-f32 lenv3(struct v3 a);
-struct v3 normv3(struct v3 a);
+v3 fillv3(f32 a);
+v3 invv3(v3 a);
+v3 addv3(v3 a, v3 b);
+v3 subv3(v3 a, v3 b);
+v3 scalev3(v3 a, f32 s);
+v3 scalevv3(v3 a, v3 s);
+f32 dotv3(v3 a, v3 b);
+v3 crossv3(v3 l, v3 r);
+f32 len2v3(v3 a);
+f32 lenv3(v3 a);
+v3 normv3(v3 a);
+v3 maxv3(v3 a, v3 b);
-struct v4 fillv4(f32 a);
-struct v4 invv4(struct v4 a);
-struct v4 addv4(struct v4 a, struct v4 b);
-struct v4 subv4(struct v4 a, struct v4 b);
-struct v4 scalev4(struct v4 a, f32 s);
-struct v4 scalevv4(struct v4 a, struct v4 s);
-f32 dotv4(struct v4 a, struct v4 b);
-f32 len2v4(struct v4 a);
-f32 lenv4(struct v4 a);
-struct v4 normv4(struct v4 a);
+v4 fillv4(f32 a);
+v4 invv4(v4 a);
+v4 addv4(v4 a, v4 b);
+v4 subv4(v4 a, v4 b);
+v4 scalev4(v4 a, f32 s);
+v4 scalevv4(v4 a, v4 s);
+f32 dotv4(v4 a, v4 b);
+f32 len2v4(v4 a);
+f32 lenv4(v4 a);
+v4 normv4(v4 a);
-struct mat transpmat(struct mat m);
-struct mat mulmat(struct mat left, struct mat right);
-struct mat make_translate(struct v3 v);
-struct mat make_scale(struct v3 v);
-struct mat make_rotate(struct v3 x, struct v3 y, struct v3 z);
-struct mat translate_mat(struct mat m, struct v3 v);
-struct mat scale_mat(struct mat m, struct v3 v);
-struct mat rotate_mat(struct mat m, struct v3 angles);
-struct v4 mulmatv4(struct mat m, struct v4 v);
+mat transpose_mat(mat m);
+mat mulmat(mat left, mat right);
+mat make_translate(v3 v);
+mat make_scale(v3 v);
+mat make_rotate(v3 x, v3 y, v3 z);
+mat translate_mat(mat m, v3 v);
+mat scale_mat(mat m, v3 v);
+mat rotate_mat(mat m, v3 angles);
+v4 mulmatv4(mat m, v4 v);
-i32 in_rect(struct v2 pos, struct rect rect);
+i32 in_rect(v2 pos, rect r);
#endif
diff --git a/prbs.c b/prbs.c
index 818a110..0c6bd8a 100644
--- a/prbs.c
+++ b/prbs.c
@@ -1,132 +1,179 @@
#include "prbs.h"
#include "macros.h"
+#include "sys.h"
#include <string.h>
#include <stdio.h>
-i32 cstrings_equal(const char *s1, const char *s2)
+i32
+cstrings_equal(const char *s1, const char *s2)
{
- assert(s1 && s2);
- while (*s1++ == *s2++)
- if (!*s1)
- return 1;
- return 0;
+ assert(s1 && s2);
+ while (*s1++ == *s2++)
+ if (!*s1)
+ return 1;
+ return 0;
}
-char *string_to_cstring(struct arena *arena, struct string str)
+char *
+string_to_cstring(struct arena *arena, struct string str)
{
- u64 len = str.len + 1;
- char *cstr = push_arena(arena, len);
- memmove(cstr, str.ptr, len);
- cstr[str.len] = 0;
- return cstr;
+ u64 len = str.len + 1;
+ char *cstr = push_arena(arena, len);
+ memmove(cstr, str.ptr, len);
+ cstr[str.len] = 0;
+ return cstr;
}
-i32 strings_equal(struct string str1, struct string str2)
+struct string
+string_from_cstring(struct arena *arena, const char *cstr)
{
- if (str1.len != str2.len)
- return 0;
- for (u64 i = 0; i < str1.len; ++i)
- if (str1.ptr[i] != str2.ptr[i])
- return 0;
- return 1;
+ u64 len = 0;
+ while (cstr[len++])
+ ;
+ len--;
+ u8 *ptr = push_arena(arena, len);
+ for (i32 i = 0; i < len; i++)
+ ptr[i] = cstr[i];
+ return (struct string){ptr, len};
}
-u8 *find_char(struct string str, i32 c)
+i32
+strings_equal(struct string str1, struct string str2)
{
- for (u64 i = 0; i < str.len; ++i)
- if (str.ptr[i] == c)
- return str.ptr + i;
- return 0;
+ if (str1.len != str2.len)
+ return 0;
+ for (u64 i = 0; i < str1.len; ++i)
+ if (str1.ptr[i] != str2.ptr[i])
+ return 0;
+ return 1;
}
-u8 *find_last_char(struct string str, i32 c)
+u8 *
+find_char(struct string str, i32 c)
{
- for (u64 i = str.len - 1; i > 0; --i)
- if (str.ptr[i] == c)
- return str.ptr + i;
- return 0;
+ for (u64 i = 0; i < str.len; ++i)
+ if (str.ptr[i] == c)
+ return str.ptr + i;
+ return 0;
}
-struct string string_from_range(u8 *start, u8 *end)
+u8 *
+find_last_char(struct string str, i32 c)
{
- assert(start);
- assert(end);
- assert(start < end);
- return (struct string){start, end - start};
+ for (u64 i = str.len - 1; i > 0; --i)
+ if (str.ptr[i] == c)
+ return str.ptr + i;
+ return 0;
}
-struct string get_dirname(struct string str)
+struct string
+string_from_range(u8 *start, u8 *end)
{
- assert(str.len);
- u8 *slash = find_last_char(str, '/');
- if (!slash)
- return str;
- return string_from_range(str.ptr, slash);
+ assert(start);
+ assert(end);
+ assert(start < end);
+ return (struct string){start, end - start};
}
-struct string push_fstringv(struct arena *arena, const char *format, va_list args)
+struct string
+get_dirname(struct string str)
{
- char tmp[kilobytes(4) + 1];
- i32 len = vsnprintf(tmp, kilobytes(4), format, args);
- struct string str = {0};
- if (len < 0)
- return str;
- u8 *ptr = push_arena(arena, len);
- memmove(ptr, tmp, len);
- return (struct string){ptr, len};
+ assert(str.len);
+ u8 *dot = find_char(str, '.');
+ u8 *slash = find_last_char(str, '/');
+ if (!slash)
+ return str;
+ return string_from_range(str.ptr, slash);
}
-struct string push_fstring(struct arena *arena, const char *format, ...)
+struct string
+push_string(struct arena *arena, struct string str)
{
- va_list args;
- va_start(args, format);
- struct string str = push_fstringv(arena, format, args);
- va_end(args);
- return str;
+ assert(str.ptr && str.len);
+ u8 *ptr = push_arena(arena, str.len);
+ memmove(ptr, str.ptr, str.len);
+ return (struct string){ptr, str.len};
}
-void print_string(struct string str)
+static struct string
+push_fstringv(struct arena *arena, const char *format, va_list args)
{
- assert(str.ptr && str.len);
- printf("%.*s", str.len, str.ptr);
+ char tmp[kilobytes(4) + 1];
+ i32 len = vsnprintf(tmp, kilobytes(4), format, args);
+ struct string str = {0};
+ if (len < 0)
+ return str;
+ u8 *ptr = push_arena(arena, len);
+ memmove(ptr, tmp, len);
+ return (struct string){ptr, len};
}
-void push_string_to_list(struct arena *arena, struct string_list *list, i32 front, struct string str)
+struct string
+push_fstring(struct arena *arena, const char *format, ...)
{
- struct string_node *node = push_arena(arena, sizeof(struct string_node));
- node->str = str;
- if (front)
- dllpushfront(list->first, list->last, node);
- else
- dllpushback(list->first, list->last, node);
- list->len += str.len;
- list->cnt++;
+ va_list args;
+ va_start(args, format);
+ struct string str = push_fstringv(arena, format, args);
+ va_end(args);
+ return str;
}
-void push_fstring_to_list(struct arena *arena, struct string_list *list, i32 front, const char *format, ...)
+void
+print_string(struct string str)
{
- va_list args;
- va_start(args, format);
- struct string str = push_fstringv(arena, format, args);
- va_end(args);
- push_string_to_list(arena, list, front, str);
+ assert(str.ptr && str.len);
+ printf("%.*s", str.len, str.ptr);
}
-struct string join_string_list(struct arena *arena, struct string_list *list)
+void
+print_stringnl(struct string str)
{
- assert(arena);
- assert(list);
- struct string str = {arena->memory + arena->used, 0};
- for (struct string_node *node = list->first; node; node = node->next) {
- u8 *ptr = push_arena(arena, node->str.len);
- memmove(ptr, node->str.ptr, node->str.len);
- str.len += node->str.len;
- }
- return str;
+ print_string(str);
+ printf("\n");
}
-void print_string_list(struct string_list *list)
+void
+push_string_to_list(struct arena *arena, struct string_list *list,
+ i32 front, struct string str)
{
- for (struct string_node *node = list->first; node; node = node->next)
- print_string(node->str);
+ struct string_node *node = push_arena(arena, sizeof(struct string_node));
+ node->str = str;
+ if (front)
+ dllpushfront(list->first, list->last, node);
+ else
+ dllpushback(list->first, list->last, node);
+ list->len += str.len;
+ list->cnt++;
+}
+
+void
+push_fstring_to_list(struct arena *arena, struct string_list *list,
+ i32 front, const char *format, ...)
+{
+ va_list args;
+ va_start(args, format);
+ struct string str = push_fstringv(arena, format, args);
+ va_end(args);
+ push_string_to_list(arena, list, front, str);
+}
+
+struct string
+join_string_list(struct arena *arena, struct string_list *list)
+{
+ assert(arena);
+ assert(list);
+ struct string str = {arena->memory + arena->used, 0};
+ for (struct string_node *node = list->first; node; node = node->next) {
+ u8 *ptr = push_arena(arena, node->str.len);
+ memmove(ptr, node->str.ptr, node->str.len);
+ str.len += node->str.len;
+ }
+ return str;
+}
+
+void
+print_string_list(struct string_list *list)
+{
+ for (struct string_node *node = list->first; node; node = node->next)
+ print_string(node->str);
}
diff --git a/prbs.h b/prbs.h
index 4547d7c..2384c49 100644
--- a/prbs.h
+++ b/prbs.h
@@ -8,37 +8,41 @@
i32 cstrings_equal(const char *s1, const char *s2);
struct string {
- u8 *ptr;
- u64 len;
+ u8 *ptr;
+ u64 len;
};
struct string_node {
- struct string str;
- struct string_node *next;
- struct string_node *prev;
+ struct string str;
+ struct string_node *next;
+ struct string_node *prev;
};
struct string_list {
- i32 cnt;
- u64 len;
- struct string_node *first;
- struct string_node *last;
+ i32 cnt;
+ u64 len;
+ struct string_node *first;
+ struct string_node *last;
};
-#define string_from_cliteral(str) (sturct string){(u8 *)(str), sizeof(str) - 1}
-#define expand_string(str) (i32)((str).len), ((str).ptr)
+#define string_from_cliteral(str) (struct string){(u8 *)(str), sizeof(str) - 1}
+#define expand_string(str) (i32)((str).len), ((str).ptr)
char *string_to_cstring(struct arena *arena, struct string str);
+struct string string_from_cstring(struct arena *arena, const char *cstr);
i32 strings_equal(struct string str1, struct string str2);
u8 *find_char(struct string str, i32 c);
u8 *find_last_char(struct string str, i32 c);
struct string string_from_range(u8 *start, u8 *end);
struct string get_dirname(struct string str);
-struct string push_fstringv(struct arena *arena, const char *format, va_list args);
+struct string push_string(struct arena *arena, struct string str);
struct string push_fstring(struct arena *arena, const char *format, ...);
void print_string(struct string str);
-void push_string_to_list(struct arena *arena, struct string_list *list, i32 front, struct string str);
-void push_fstring_to_list(struct arena *arena, struct string_list *list, i32 front, const char *format, ...);
+void print_stringnl(struct string str);
+void push_string_to_list(struct arena *arena, struct string_list *list,
+ i32 front, struct string str);
+void push_fstring_to_list(struct arena *arena, struct string_list *list,
+ i32 front, const char *format, ...);
struct string join_string_list(struct arena *arena, struct string_list *list);
void print_string_list(struct string_list *list);
diff --git a/prbwin.c b/prbwin.c
index 81ad626..ff81c7f 100644
--- a/prbwin.c
+++ b/prbwin.c
@@ -1,77 +1,84 @@
#include "sys.h"
-void *sys_alloc(u64 length)
+void
+*sys_alloc(u64 length)
{
- assert(length);
- void *result = VirtualAlloc(0, length, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
- return result;
+ assert(length);
+ void *result = VirtualAlloc(0, length, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
+ return result;
}
-void sys_free(void *memory, u64 length)
+void
+sys_free(void *memory, u64 length)
{
- assert(memory);
- assert(length);
- assert(VirtualFree(memory, 0, MEM_RELEASE));
+ assert(memory);
+ assert(length);
+ assert(VirtualFree(memory, 0, MEM_RELEASE));
}
-void die(const char *error_string, ...)
+void
+die(const char *error_string, ...)
{
- va_list args;
- va_start(args, error_string);
- printf("error: ");
- vprintf(error_string, args);
- printf("\n");
- va_end(args);
- exit(1);
+ va_list args;
+ va_start(args, error_string);
+ printf("error: ");
+ vprintf(error_string, args);
+ printf("\n");
+ va_end(args);
+ exit(1);
}
-void info(const char *format, ...)
+void
+info(const char *format, ...)
{
- va_list args;
- va_start(args, format);
- vprintf(format, args);
- putchar('\n');
- va_end(args);
+ va_list args;
+ va_start(args, format);
+ vprintf(format, args);
+ putchar('\n');
+ va_end(args);
}
-char *read_entire_file(struct arena *arena, u64 *len, const char *filename)
+char *
+read_entire_file(struct arena *arena, u64 *len, const char *filename)
{
- HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ,
- 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- if (file == INVALID_HANDLE_VALUE)
- return 0;
- LARGE_INTEGER lisize;
- if (!GetFileSizeEx(file, &lisize))
- goto error;
- *len = lisize.QuadPart;
- if (!*len)
- goto error;
- char *buffer = push_arena(arena, *len + 1);
- DWORD bytes_read;
- if (!ReadFile(file, *buffer, size, &bytes_read, 0)) {
- pop_arena(arena, *len + 1);
- goto error;
- }
- CloseHandle(file);
- buffer[*len] = 0;
- return buffer;
+ HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ,
+ 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
+ if (file == INVALID_HANDLE_VALUE)
+ return 0;
+ LARGE_INTEGER lisize;
+ if (!GetFileSizeEx(file, &lisize))
+ goto error;
+ *len = lisize.QuadPart;
+ if (!*len)
+ goto error;
+ char *buffer = push_arena(arena, *len + 1);
+ DWORD bytes_read;
+ if (!ReadFile(file, *buffer, size, &bytes_read, 0)) {
+ pop_arena(arena, *len + 1);
+ goto error;
+ }
+ CloseHandle(file);
+ buffer[*len] = 0;
+ return buffer;
+
error:
- CloseHandle(file);
- return 0;
+ CloseHandle(file);
+ return 0;
}
-char *get_exe_path(arena_t *arena)
+char *
+get_exe_path(arena_t *arena)
{
- char path[max_path_len] = {0};
- u32 len = GetModuleFileName(0, path, max_path_len);
- if (!len)
- return 0;
- char *dir = strrchr(path, '\\');
- if (!dir)
- return path;
- len = dir - path;
- dir = push_arena(arena, len + 1);
- memmove(dir, path, len);
- dir[len] = 0;
- return dir;
+ char path[max_path_len] = {0};
+ u32 len = GetModuleFileName(0, path, max_path_len);
+ if (!len)
+ return 0;
+ char *dir = strrchr(path, '\\');
+ if (!dir)
+ return path;
+ len = dir - path;
+ dir = push_arena(arena, len + 1);
+ memmove(dir, path, len);
+ dir[len] = 0;
+ return dir;
}
diff --git a/sys.h b/sys.h
index 81d9f0c..f16c719 100644
--- a/sys.h
+++ b/sys.h
@@ -10,5 +10,6 @@ void sys_free(void *memory, u64 length);
void die(const char *format, ...);
void info(const char *format, ...);
char *read_entire_file(struct arena *arena, u64 *len, const char *filename);
+struct string get_basedir(struct arena *arena);
#endif