1
1
/*
2
2
na.h - v0.08
3
- Nick Aversano's C++ helper library
3
+ Nick Aversano's C/C ++ helper library
4
4
5
5
This is a single header file with a bunch of useful stuff
6
- to replace the C++ standard library.
6
+ to replace the C/C ++ standard library.
7
7
===========================================================================
8
8
9
9
USAGE
@@ -22,6 +22,7 @@ CREDITS
22
22
Credits are much appreciated but not required.
23
23
24
24
VERSION HISTORY
25
+ 0.09 - linux support
25
26
0.08 - bug fixes, fix arena alignment on MacOS ARM
26
27
0.07 - bug fixes
27
28
0.06 - added comparision helpers, improved stretchy arrays API, added Timing_f64,
@@ -5745,10 +5746,11 @@ function void os_mutex_release_lock(Mutex *mutex) {
5745
5746
}
5746
5747
5747
5748
function void os_mutex_destroy (Mutex *mutex) {
5748
- // @Robustness: track if it's been released before deleting?
5749
- pthread_mutex_destroy (cast (pthread_mutex_t *)mutex->handle );
5750
- os_free (mutex->handle );
5751
- mutex->handle = 0 ;
5749
+ if (mutex->handle ) {
5750
+ pthread_mutex_destroy (cast (pthread_mutex_t *)mutex->handle );
5751
+ os_free (mutex->handle );
5752
+ mutex->handle = 0 ;
5753
+ }
5752
5754
}
5753
5755
#elif OS_LINUX
5754
5756
#include < time.h>
@@ -5772,74 +5774,73 @@ function void os_exit(i32 code)
5772
5774
5773
5775
function String os_get_system_path (Arena *arena, SystemPath path)
5774
5776
{
5775
- String result = {0 };
5777
+ String result = {0 };
5776
5778
5777
- switch (path)
5778
- {
5779
- case SystemPath_Current:
5780
- {
5781
- char *buffer = (char *)arena_push (arena, PATH_MAX);
5782
- getcwd (buffer, PATH_MAX);
5783
-
5784
- result = string_from_cstr (buffer);
5785
- i64 unused_size = PATH_MAX - result.count ;
5786
- arena_pop (arena, unused_size);
5787
- } break ;
5788
-
5789
- case SystemPath_Binary:
5790
- {
5791
- char *buffer = (char *)arena_push (arena, PATH_MAX);
5792
- size_t length = readlink (" /proc/self/exe" , buffer, PATH_MAX);
5793
-
5794
- if (length > 0 )
5795
- {
5796
- char *normalized = (char *)arena_push (arena, PATH_MAX);
5797
- if (realpath (buffer, normalized) != NULL )
5798
- {
5799
- result = Str8 (normalized, length);
5800
- i64 unused_size = PATH_MAX - result.count ;
5801
- arena_pop (arena, unused_size);
5802
- }
5803
- else
5804
- {
5805
- result = Str8 (buffer, length);
5806
- }
5807
-
5808
- result = string_chop_last_slash (result);
5809
- }
5810
- } break ;
5811
-
5812
- case SystemPath_AppData:
5813
- {
5814
- char *home_str = getenv (" HOME" );
5815
- String home = string_from_cstr (home_str);
5816
- result = string_concat2 (arena, home, S (" /.config/" ));
5817
- } break ;
5818
- }
5779
+ switch (path)
5780
+ {
5781
+ case SystemPath_Current:
5782
+ {
5783
+ char *buffer = (char *)arena_push (arena, PATH_MAX);
5784
+ getcwd (buffer, PATH_MAX);
5785
+
5786
+ result = string_from_cstr (buffer);
5787
+ i64 unused_size = PATH_MAX - result.count ;
5788
+ arena_pop (arena, unused_size);
5789
+ } break ;
5790
+
5791
+ case SystemPath_Binary:
5792
+ {
5793
+ char *buffer = (char *)arena_push (arena, PATH_MAX);
5794
+ size_t length = readlink (" /proc/self/exe" , buffer, PATH_MAX);
5795
+ if (length > 0 )
5796
+ {
5797
+ char *normalized = (char *)arena_push (arena, PATH_MAX);
5798
+ if (realpath (buffer, normalized) != NULL )
5799
+ {
5800
+ result = Str8 (normalized, length);
5801
+ i64 unused_size = PATH_MAX - result.count ;
5802
+ arena_pop (arena, unused_size);
5803
+ }
5804
+ else
5805
+ {
5806
+ result = Str8 (buffer, length);
5807
+ }
5808
+
5809
+ result = string_chop_last_slash (result);
5810
+ }
5811
+ } break ;
5812
+
5813
+ case SystemPath_AppData:
5814
+ {
5815
+ char *home_str = getenv (" HOME" );
5816
+ String home = string_from_cstr (home_str);
5817
+ result = string_concat2 (arena, home, S (" /.config/" ));
5818
+ } break ;
5819
+ }
5819
5820
5820
- return result;
5821
+ return result;
5821
5822
}
5822
5823
5823
5824
function f64 os_time ()
5824
5825
{
5825
- static struct timespec initial = {0 };
5826
- if (initial.tv_sec == 0 )
5827
- {
5828
- clock_gettime (CLOCK_MONOTONIC_RAW, &initial);
5829
- }
5830
-
5831
- struct timespec now;
5832
- clock_gettime (CLOCK_MONOTONIC_RAW, &now);
5833
-
5834
- return ( f64) (now.tv_sec - initial.tv_sec ) +
5835
- ((f64)(now. tv_nsec - initial. tv_nsec ) / 1e9 ) ;
5826
+ static struct timespec initial = {0 };
5827
+ if (initial.tv_sec == 0 )
5828
+ {
5829
+ clock_gettime (CLOCK_MONOTONIC_RAW, &initial);
5830
+ }
5831
+
5832
+ struct timespec now;
5833
+ clock_gettime (CLOCK_MONOTONIC_RAW, &now);
5834
+
5835
+ f64 result = (now.tv_sec - initial.tv_sec ) + ((f64)(now. tv_nsec - initial. tv_nsec ) / 1e9 );
5836
+ return result ;
5836
5837
}
5837
5838
5838
5839
function f64 os_clock ()
5839
5840
{
5840
- struct timespec t;
5841
- clock_gettime (CLOCK_MONOTONIC_RAW, &t);
5842
- return (f64)t.tv_sec + ((f64)t.tv_nsec / 1e9 );
5841
+ struct timespec t;
5842
+ clock_gettime (CLOCK_MONOTONIC_RAW, &t);
5843
+ return (f64)t.tv_sec + ((f64)t.tv_nsec / 1e9 );
5843
5844
}
5844
5845
5845
5846
function void os_sleep (f64 seconds)
@@ -5891,6 +5892,70 @@ function bool os_shell_open(String path)
5891
5892
// Threading Primitives
5892
5893
//
5893
5894
5895
+ #include < pthread.h>
5896
+ #include < semaphore.h>
5897
+ #include < stdlib.h>
5898
+ #include < assert.h>
5899
+
5900
+ StaticAssert (sizeof (sem_t ) <= sizeof(void *), "check_semaphore_size");
5901
+
5902
+ function Semaphore os_semaphore_create (u32 max_count)
5903
+ {
5904
+ Semaphore result = {0 };
5905
+ sem_t *handle = malloc (sizeof (sem_t ));
5906
+ result.handle = handle;
5907
+ sem_init (handle, 0 , max_count);
5908
+ return result;
5909
+ }
5910
+
5911
+ function void os_semaphore_signal (Semaphore *sem)
5912
+ {
5913
+ sem_post (sem->handle );
5914
+ }
5915
+
5916
+ function void os_semaphore_wait_for (Semaphore *sem, bool infinite)
5917
+ {
5918
+ if (infinite) {
5919
+ sem_wait (sem->handle );
5920
+ } else {
5921
+ assert (!" Invalid code path" );
5922
+ }
5923
+ }
5924
+
5925
+ function void os_semaphore_destroy (Semaphore *sem)
5926
+ {
5927
+ sem_destroy (sem->handle );
5928
+ free (sem->handle );
5929
+ sem->handle = 0 ;
5930
+ }
5931
+
5932
+ function Mutex os_mutex_create (u32 spin_count)
5933
+ {
5934
+ Mutex result = {0 };
5935
+ result.handle = malloc (sizeof (pthread_mutex_t ));
5936
+ pthread_mutex_init (result.handle , NULL );
5937
+ return result;
5938
+ }
5939
+
5940
+ function void os_mutex_aquire_lock (Mutex *mutex) {
5941
+ pthread_mutex_lock (mutex->handle );
5942
+ }
5943
+
5944
+ function bool os_mutex_try_aquire_lock (Mutex *mutex) {
5945
+ return pthread_mutex_trylock (mutex->handle ) != 0 ;
5946
+ }
5947
+
5948
+ function void os_mutex_release_lock (Mutex *mutex) {
5949
+ pthread_mutex_unlock (mutex->handle );
5950
+ }
5951
+
5952
+ function void os_mutex_destroy (Mutex *mutex) {
5953
+ if (mutex->handle ) {
5954
+ pthread_mutex_destroy (mutex->handle );
5955
+ free (mutex->handle );
5956
+ mutex->handle = 0 ;
5957
+ }
5958
+ }
5894
5959
#endif
5895
5960
5896
5961
#if OS_LINUX || OS_MACOS
@@ -7204,11 +7269,11 @@ TestFunction(array__test)
7204
7269
7205
7270
i32 key = 42 ;
7206
7271
i64 index = array_find (array, &key, compare_i32);
7207
- Dump (index );
7272
+ // Dump(index);
7208
7273
7209
7274
For (array)
7210
7275
{
7211
- Dump (it);
7276
+ // Dump(it);
7212
7277
}
7213
7278
7214
7279
return true ;
0 commit comments