Skip to content

Commit 215b61b

Browse files
committedJan 20, 2025
more linux stuff
1 parent dfe4ca2 commit 215b61b

File tree

5 files changed

+135
-69
lines changed

5 files changed

+135
-69
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ single-file public domain libraries for C/C++
44

55
library | latest version | category | description
66
--------------------------------|----------------|-------------|-------------
7-
[**na.h**](./na.h) | 0.08 | base | Cross-platform helpers
7+
[**na.h**](./na.h) | 0.09 | base | Cross-platform helpers
88
[**na_math.h**](./na_math.h) | 0.04 | math | Math functions
99
[**na_net.h**](./na_net.h) | 0.02 | networking | Networking library for TCP/UDP and HTTP
1010

‎na.h

+131-66
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
na.h - v0.08
3-
Nick Aversano's C++ helper library
3+
Nick Aversano's C/C++ helper library
44
55
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.
77
===========================================================================
88
99
USAGE
@@ -22,6 +22,7 @@ CREDITS
2222
Credits are much appreciated but not required.
2323
2424
VERSION HISTORY
25+
0.09 - linux support
2526
0.08 - bug fixes, fix arena alignment on MacOS ARM
2627
0.07 - bug fixes
2728
0.06 - added comparision helpers, improved stretchy arrays API, added Timing_f64,
@@ -5745,10 +5746,11 @@ function void os_mutex_release_lock(Mutex *mutex) {
57455746
}
57465747

57475748
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+
}
57525754
}
57535755
#elif OS_LINUX
57545756
#include <time.h>
@@ -5772,74 +5774,73 @@ function void os_exit(i32 code)
57725774

57735775
function String os_get_system_path(Arena *arena, SystemPath path)
57745776
{
5775-
String result = {0};
5777+
String result = {0};
57765778

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+
}
58195820

5820-
return result;
5821+
return result;
58215822
}
58225823

58235824
function f64 os_time()
58245825
{
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;
58365837
}
58375838

58385839
function f64 os_clock()
58395840
{
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);
58435844
}
58445845

58455846
function void os_sleep(f64 seconds)
@@ -5891,6 +5892,70 @@ function bool os_shell_open(String path)
58915892
// Threading Primitives
58925893
//
58935894

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+
}
58945959
#endif
58955960

58965961
#if OS_LINUX || OS_MACOS
@@ -7204,11 +7269,11 @@ TestFunction(array__test)
72047269

72057270
i32 key = 42;
72067271
i64 index = array_find(array, &key, compare_i32);
7207-
Dump(index);
7272+
// Dump(index);
72087273

72097274
For(array)
72107275
{
7211-
Dump(it);
7276+
// Dump(it);
72127277
}
72137278

72147279
return true;

‎test/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#define impl
22
#include "../na.h"
33
#include "../na_math.h"
4-
#include "../na_net.h"
4+
// #include "../na_net.h"
55

66
#include <stdlib.h>
77

‎test/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#define impl
22
#include "../na.h"
33
#include "../na_math.h"
4-
#include "../na_net.h"
4+
// #include "../na_net.h"
55

66
#include <stdlib.h>
77

‎test/na_inliner.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// inliner <input> (output)
88
//
99

10+
#define impl
1011
#include "../na.h"
1112

1213
#include <stdio.h>

0 commit comments

Comments
 (0)
Please sign in to comment.