This commit is contained in:
Krzosa Karol
2025-03-27 11:44:52 +01:00
parent e8841153c6
commit 8a47f93b2f
21 changed files with 235 additions and 118 deletions

View File

@@ -2,9 +2,12 @@
#include "core_platform_wasm.c"
#elif PLATFORM_WINDOWS
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "DbgHelp.lib")
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <TlHelp32.h>
#include <DbgHelp.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
@@ -21,11 +24,12 @@
#include <time.h>
#include <sys/mman.h>
#include <signal.h>
#include <execinfo.h>
#include <ucontext.h>
#include <unistd.h>
#include <dlfcn.h>
#define B_STACKTRACE_IMPL
#include "stacktrace.h"
//:unix_debug_break
//static void os_unix_sigtrap(int signum) { signal(SIGTRAP, SIG_DFL); }
#include "core_platform_unix.c"
#endif

View File

@@ -140,13 +140,15 @@ union convert_f32_i32_t { f32 f; i32 i; };
#define FILE_AND_LINE __FILE__":"STRINGIFY(__LINE__)
#endif
#if PLATFORM_CL
#if PLATFORM_WINDOWS
#define debug__break() (IsDebuggerPresent() && (__debugbreak(), 1))
#else
#define debug__break() __builtin_trap()
//:unix_debug_break
#elif PLATFORM_UNIX
// https://github.com/r-lyeh/tinybits/blob/master/tinyassert.c
// #define debug__break() (signal(SIGTRAP, break_handler_), raise(SIGTRAP))
#include <signal.h>
static void os_unix_sigtrap(int signum) { signal(SIGTRAP, SIG_DFL); }
#define debug__break() (signal(SIGTRAP, os_unix_sigtrap), raise(SIGTRAP))
#else
#define debug__break() __builtin_trap()
#endif
#define debug_break() (debug__break(), 1)

View File

@@ -28,18 +28,18 @@ fn void memory_zero(void *dst, usize size) {
memory_set(dst, 0, size);
}
fn_inline f32 f32_sqrt(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_sqrtf(x), sqrtf(x)); }
fn_inline f64 f64_sqrt(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_sqrt(x), sqrt(x)); }
fn_inline f32 f32_ceil(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_ceilf(x), ceilf(x)); }
fn_inline f64 f64_ceil(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_ceil(x), ceil(x)); }
fn_inline f32 f32_floor(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_floorf(x), floorf(x)); }
fn_inline f64 f64_floor(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_floor(x), floor(x)); }
fn_inline f32 f32_abs(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_fabsf(x), fabsf(x)); }
fn_inline f64 f64_abs(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_fabs(x), fabs(x)); }
// fn_inline f32 f32_round(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_roundf(x), roundf(x)); }
fn_inline f64 f64_round(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_round(x), round(x)); }
fn_inline f64 f64_mod(f64 a, f64 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmod(a, b), fmod(a, b)); }
fn_inline f32 f32_mod(f32 a, f32 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmodf(a, b), fmodf(a, b)); }
fn f32 f32_sqrt(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_sqrtf(x), sqrtf(x)); }
fn f64 f64_sqrt(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_sqrt(x), sqrt(x)); }
fn f32 f32_ceil(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_ceilf(x), ceilf(x)); }
fn f64 f64_ceil(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_ceil(x), ceil(x)); }
fn f32 f32_floor(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_floorf(x), floorf(x)); }
fn f64 f64_floor(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_floor(x), floor(x)); }
fn f32 f32_abs(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_fabsf(x), fabsf(x)); }
fn f64 f64_abs(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_fabs(x), fabs(x)); }
// fn f32 f32_round(f32 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_roundf(x), roundf(x)); }
fn f64 f64_round(f64 x) { return IF_PLATFORM_CLANG_ELSE(__builtin_round(x), round(x)); }
fn f64 f64_mod(f64 a, f64 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmod(a, b), fmod(a, b)); }
fn f32 f32_mod(f32 a, f32 b) { return IF_PLATFORM_CLANG_ELSE(__builtin_fmodf(a, b), fmodf(a, b)); }
/* https://gitlab.com/nakst/essence/-/blob/master/shared/math.cpp
**

View File

@@ -35,5 +35,4 @@ fn void log_basef(log_level_t level, s8_t file_and_line, const char *str, ...);
#define not_implemented assert(!"not implemented!")
#define invalid_codepath assert(!"invalid code path!")
#define default_is_invalid default: assert(!"invalid default case")
#define else_is_invalid else { assert(!"else was not expected to be executed!"); }
#define else_is_invalid else { assert(!"else was not expected to be executed!"); }

View File

@@ -1,4 +1,4 @@
// auto generated using: C:\dev\wasm\src\core\core_math_gen.py
// auto generated using: core_math_gen.py
fn_inline v2f32_t v2f32(f32 x, f32 y) { return (v2f32_t){ x, y }; }
gb_read_only v2f32_t v2f32_null = {0};
fn_inline b32 v2f32_is_null(v2f32_t a) { return memory_equal(&a, &v2f32_null, sizeof(a)); }

View File

@@ -86,4 +86,8 @@
#define IF_PLATFORM_WINDOWS(x) x
#else
#define IF_PLATFORM_WINDOWS(x)
#endif
#endif
#if PLATFORM_LINUX && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif

View File

@@ -3,17 +3,17 @@ fn void *os_vmem_reserve(usize size) {
return result == MAP_FAILED ? NULL : result;
}
fn b32 os_vmem_commit(void *p, usize size) {
mprotect(p, size, PROT_READ|PROT_WRITE);
fn b32 os_vmem_commit(void *ptr, usize size) {
mprotect(ptr, size, PROT_READ|PROT_WRITE);
return true;
}
fn b32 os_vmem_release(void *p) {
void *result = munmap(ptr, size);
return result == MAP_FAILED ? false : true;
fn b32 os_vmem_release(void *ptr) {
int result = munmap(ptr, 0);
return result == 0 ? true : false;
}
fn b32 os_vmem_decommit(void *p, usize size) {
fn b32 os_vmem_decommit(void *ptr, usize size) {
madvise(ptr, size, MADV_DONTNEED);
mprotect(ptr, size, PROT_NONE);
return true;
@@ -34,7 +34,7 @@ fn f64 os_parse_float(char *str) {
}
fn void os_unix_crash_handler(int signal, siginfo_t* info, void* context) {
context; // unused
(void)context; // unused
printf("signal: %d", signal);
if (signal == SIGSEGV) {
printf(" at addr: %p", info->si_addr);

View File

@@ -1,5 +1,3 @@
#if !defined(B_STACKTRACE_INCLUDED)
#define B_STACKTRACE_INCLUDED (1)
/*
b_stacktrace v0.21 -- a cross-platform stack-trace generator
SPDX-License-Identifier: MIT
@@ -67,14 +65,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#if !defined(B_STACKTRACE_API)
#define B_STACKTRACE_API extern
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
A stacktrace handle
*/
@@ -85,38 +75,22 @@ typedef struct b_stacktrace_tag* b_stacktrace_handle;
can be expanded to a string via b_stacktrace_to_string.
The handle is allocated with `malloc` and needs to be freed with `free`
*/
B_STACKTRACE_API b_stacktrace_handle b_stacktrace_get();
fn b_stacktrace_handle b_stacktrace_get();
/*
Converts a stack-trace handle to a human-readable string.
The string is allocated with `malloc` and needs to be freed with `free`
*/
B_STACKTRACE_API char* b_stacktrace_to_string(b_stacktrace_handle stacktrace);
fn char* b_stacktrace_to_string(b_stacktrace_handle stacktrace);
/*
Returns a human-readable stack-trace string from the point of view of the
caller.
The string is allocated with `malloc` and needs to be freed with `free`
*/
B_STACKTRACE_API char* b_stacktrace_get_string(void);
fn char* b_stacktrace_get_string(void);
/* version */
#define B_STACKTRACE_VER_MAJOR 0
#define B_STACKTRACE_VER_MINOR 20
#ifdef __cplusplus
}
#endif
#if defined(B_STACKTRACE_IMPL)
#if defined(__linux__) && !defined(_GNU_SOURCE)
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
typedef struct print_buf {
char* buf;
@@ -160,15 +134,7 @@ char* b_stacktrace_get_string(void) {
#define B_STACKTRACE_MAX_DEPTH 1024
#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <TlHelp32.h>
#include <DbgHelp.h>
#pragma comment(lib, "DbgHelp.lib")
#if PLATFORM_WINDOWS
#define B_STACKTRACE_ERROR_FLAG ((DWORD64)1 << 63)
typedef struct b_stacktrace_entry {
@@ -296,11 +262,7 @@ char* b_stacktrace_to_string(b_stacktrace_handle h) {
return out.buf;
}
#elif defined(__APPLE__)
#include <execinfo.h>
#include <unistd.h>
#include <dlfcn.h>
#elif PLATFORM_MAC_OS
typedef struct b_stacktrace {
void* trace[B_STACKTRACE_MAX_DEPTH];
@@ -327,13 +289,7 @@ char* b_stacktrace_to_string(b_stacktrace_handle h) {
return out.buf;
}
#elif defined(__linux__)
#include <execinfo.h>
#include <ucontext.h>
#include <unistd.h>
#include <dlfcn.h>
#include <string.h>
#elif PLATFORM_POSIX
typedef struct b_stacktrace {
void* trace[B_STACKTRACE_MAX_DEPTH];
@@ -407,6 +363,3 @@ char* b_stacktrace_get_string(void) {
}
#endif /* platform */
#endif /* B_STACKTRACE_IMPL */
#endif /* B_STACKTRACE_INCLUDED */