1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2024 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.
//! # Cimpl - Simple C implementations from Rust
//!
//! A Rust library providing utilities and macros for creating safe, ergonomic C FFI bindings.
//!
//! ## Features
//!
//! - **Handle-based API**: Thread-safe handle management system for passing Rust objects to C
//! - **Allocation tracking**: Prevents double-free of raw pointers with automatic leak detection at shutdown
//! - **Buffer safety**: Validates buffer sizes and pointer arithmetic
//! - **FFI macros**: Ergonomic macros for null checks, string conversion, and error handling
//! - **Memory leak detection**: Automatically reports unfreed pointers when the program exits
//! - **Test-mode debugging**: Enhanced error reporting in test builds for memory management issues
//!
//! ## Memory Safety
//!
//! All pointers allocated via `box_tracked!` or `track_box` are tracked in a global registry.
//! When the program shuts down, any pointers that weren't freed are reported:
//!
//! ```text
//! ⚠️ WARNING: n pointer(s) were not freed at shutdown!
//! This indicates C code did not properly free all allocated pointers (n left).
//! Each pointer should be freed exactly once with cimpl_free().
//! ```
//!
//! This helps catch memory leaks during development and testing. See the [`macros`] module
//! documentation for details on test-mode debugging features.
//!
//! ## Example
//!
//! ```rust,ignore
//! use cimpl::{cstr_or_return_null, to_c_string};
//!
//! #[no_mangle]
//! pub extern "C" fn process_string(
//! input: *const std::os::raw::c_char,
//! ) -> *mut std::os::raw::c_char {
//! // Convert C string to Rust String with automatic null check
//! let rust_string = cstr_or_return_null!(input);
//!
//! // Process the string
//! let result = rust_string.to_uppercase();
//!
//! // Convert back to C string (automatically tracked for memory safety)
//! to_c_string(result)
//! }
//! ```
// Declare foundational modules first
// Then macros that depend on them
// Re-export internal utilities (for macro use only - not part of public API)
pub use CimplError;
pub use ;