|
21 | 21 | extern "C" { |
22 | 22 | #endif |
23 | 23 |
|
| 24 | +/** |
| 25 | + * @brief A cross-platform, safe null pointer management library for C and C++. |
| 26 | + * |
| 27 | + * This library provides a set of macros and utilities to ensure consistent |
| 28 | + * null pointer management across different versions of C and C++. |
| 29 | + * It introduces safer, more expressive pointer handling inspired by concepts |
| 30 | + * from modern programming languages like Rust, offering features like `Option` |
| 31 | + * semantics, safe pointer casting, and enhanced error management. |
| 32 | + * |
| 33 | + * Key Features: |
| 34 | + * - **Platform Agnostic Null Pointers:** Provides consistent `cnull` and `cnullptr` |
| 35 | + * definitions across C and C++ using modern `nullptr` or fallback `void*` based |
| 36 | + * null representation. |
| 37 | + * - **Safe Pointer Manipulation:** Macros like `cnullify()`, `cnotnull()`, and |
| 38 | + * `csafe_cast()` ensure safer memory management and prevent invalid pointer |
| 39 | + * dereferences. |
| 40 | + * - **Error Handling:** Offers expressive error management using `cpanic()` and |
| 41 | + * `cunwrap()`, providing detailed error messages and file/line diagnostics. |
| 42 | + * - **Optional Pointers:** Implements `COption`, a struct emulating Rust’s `Option<T>` |
| 43 | + * with macros like `csome()`, `cnone()`, `cunwrap_option()`, and `cunwrap_or_option()`. |
| 44 | + * - **Compile-Time Safety Hints:** Provides annotations like `cnullable` and `cnonnull` |
| 45 | + * for static analysis, improving code safety by detecting null pointer misuse. |
| 46 | + * - **Branch Prediction Optimization:** Includes `clikely()` and `cunlikely()` macros |
| 47 | + * to optimize conditional branches based on runtime behavior. |
| 48 | + * - **String Safety Constants:** Defines safe constants for null terminators, |
| 49 | + * newline characters, and empty strings in both C and wide-character formats. |
| 50 | + * |
| 51 | + * Intended Usage: |
| 52 | + * The library is suitable for scenarios requiring robust pointer management, particularly |
| 53 | + * in low-level systems programming, embedded environments, and performance-critical |
| 54 | + * applications. Developers transitioning from modern languages like Rust or C++ may |
| 55 | + * find the familiar semantics helpful. |
| 56 | + * |
| 57 | + * Compatibility: |
| 58 | + * - Supports **C11** and **C23** standards. |
| 59 | + * - Fully compatible with **C++11** and later. |
| 60 | + * - Provides graceful fallbacks for older compilers using `void*` based null pointers. |
| 61 | + * |
| 62 | + * Example Usage: |
| 63 | + * ```c |
| 64 | + * int* ptr = cnull; |
| 65 | + * cnullify(ptr); // Safely set to null |
| 66 | + * |
| 67 | + * int* data = malloc(sizeof(int)); |
| 68 | + * *data = 42; |
| 69 | + * COption opt = csome(data); |
| 70 | + * |
| 71 | + * int* result = (int*)cunwrap_option(opt); // Unwrap safely |
| 72 | + * printf("Value: %d\n", *result); |
| 73 | + * |
| 74 | + * cdrop(data); // Nullify pointer safely |
| 75 | + * ``` |
| 76 | + */ |
| 77 | + |
24 | 78 | // Ensure null pointer definitions across C and C++ environments |
25 | 79 | #ifndef FOSSIL_CNULL |
26 | 80 |
|
|
0 commit comments