diff options
| author | 3gg <3gg@shellblade.net> | 2024-02-13 17:51:51 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2024-02-13 17:51:51 -0800 |
| commit | e153be0be2fb8df6656292daab3fa59963c76737 (patch) | |
| tree | 7cca3fc134553017cb3c259db1dca33c98556109 /cassert | |
| parent | 84bdfa4a23f5b8daa7921541b007518bc634be0f (diff) | |
Let memory allocators trap by default when attempting to allocate beyond capacity.
Diffstat (limited to 'cassert')
| -rw-r--r-- | cassert/CMakeLists.txt | 8 | ||||
| -rw-r--r-- | cassert/include/cassert.h | 29 |
2 files changed, 37 insertions, 0 deletions
diff --git a/cassert/CMakeLists.txt b/cassert/CMakeLists.txt new file mode 100644 index 0000000..855f261 --- /dev/null +++ b/cassert/CMakeLists.txt | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | cmake_minimum_required(VERSION 3.0) | ||
| 2 | |||
| 3 | project(cassert) | ||
| 4 | |||
| 5 | add_library(cassert INTERFACE) | ||
| 6 | |||
| 7 | target_include_directories(cassert INTERFACE | ||
| 8 | include) | ||
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 | } | ||
