aboutsummaryrefslogtreecommitdiff
path: root/cassert/include
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2024-02-13 17:51:51 -0800
committer3gg <3gg@shellblade.net>2024-02-13 17:51:51 -0800
commite153be0be2fb8df6656292daab3fa59963c76737 (patch)
tree7cca3fc134553017cb3c259db1dca33c98556109 /cassert/include
parent84bdfa4a23f5b8daa7921541b007518bc634be0f (diff)
Let memory allocators trap by default when attempting to allocate beyond capacity.
Diffstat (limited to 'cassert/include')
-rw-r--r--cassert/include/cassert.h29
1 files changed, 29 insertions, 0 deletions
diff --git a/cassert/include/cassert.h b/cassert/include/cassert.h
new file mode 100644
index 0000000..3349b55
--- /dev/null
+++ b/cassert/include/cassert.h
@@ -0,0 +1,29 @@
1#pragma once
2
3#include <assert.h> // For convenience, bring in soft assertions with assert().
4#include <signal.h>
5
6// Allow the client to define their own LOGE() macro.
7#ifndef LOGE
8#include <stdio.h>
9#define LOGE(...) \
10 { \
11 fprintf(stderr, "[ASSERT] %s:%d: ", __FILE__, __LINE__); \
12 fprintf(stderr, __VA_ARGS__); \
13 fprintf(stderr, "\n"); \
14 }
15#endif // LOGE
16
17#define TRAP() raise(SIGTRAP)
18
19/// Unconditional hard assert.
20#define FAIL(message) \
21 LOGE(message); \
22 TRAP();
23
24/// Conditional hard assert.
25#define ASSERT(condition) \
26 if (!condition) { \
27 LOGE("Assertion failed: " #condition) \
28 TRAP(); \
29 }