From e153be0be2fb8df6656292daab3fa59963c76737 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Tue, 13 Feb 2024 17:51:51 -0800 Subject: Let memory allocators trap by default when attempting to allocate beyond capacity. --- cassert/CMakeLists.txt | 8 ++++++++ cassert/include/cassert.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 cassert/CMakeLists.txt create mode 100644 cassert/include/cassert.h (limited to 'cassert') 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 @@ +cmake_minimum_required(VERSION 3.0) + +project(cassert) + +add_library(cassert INTERFACE) + +target_include_directories(cassert INTERFACE + 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 @@ +#pragma once + +#include // For convenience, bring in soft assertions with assert(). +#include + +// Allow the client to define their own LOGE() macro. +#ifndef LOGE +#include +#define LOGE(...) \ + { \ + fprintf(stderr, "[ASSERT] %s:%d: ", __FILE__, __LINE__); \ + fprintf(stderr, __VA_ARGS__); \ + fprintf(stderr, "\n"); \ + } +#endif // LOGE + +#define TRAP() raise(SIGTRAP) + +/// Unconditional hard assert. +#define FAIL(message) \ + LOGE(message); \ + TRAP(); + +/// Conditional hard assert. +#define ASSERT(condition) \ + if (!condition) { \ + LOGE("Assertion failed: " #condition) \ + TRAP(); \ + } -- cgit v1.2.3