Skip to main content

libxml_rs/xml/memory/
mod.rs

1//! Memory management (§58, §85 Phase 1).
2//!
3//! Wraps the allocator hooks from `crate::abi::allocator` for use by the XML
4//! implementation modules. This module provides the internal Rust interface
5//! to the allocator system.
6//!
7//! # UPSTREAM-PARITY
8//!
9//! libxml2 exposes several memory management APIs:
10//!
11//! - `xmlMemSetup` / `xmlMemGet` — set/get custom allocator hooks
12//! - `xmlGcMemSetup` / `xmlGcMemGet` — GC-aware variants (now identical)
13//! - `xmlMalloc` / `xmlMallocAtomic` / `xmlRealloc` / `xmlFree` / `xmlMemStrdup`
14//! - `xmlMallocZero` / `xmlMallocAtomicZero` / `xmlReallocZero`
15//! - `xmlMemUsed` / `xmlMemBlocks` — debugging statistics
16//! - `xmlMemDisplay` / `xmlMemShow` — debugging output
17//! - `xmlInitMemory` / `xmlCleanupMemory` — lifecycle
18//!
19//! All of these are implemented in `crate::abi::allocator`. This module
20//! re-exports them for internal use.
21//!
22//! # Phase 1 status
23//!
24//! Complete — all memory functions delegate to the ABI allocator layer.
25
26pub use crate::abi::allocator::{
27    xmlFreeImpl, xmlInitMemory, xmlMallocAtomicImpl, xmlMallocAtomicZero, xmlMallocImpl,
28    xmlMallocZero, xmlMemBlocks, xmlMemDisplay, xmlMemGet, xmlMemSetup, xmlMemShow,
29    xmlMemStrdupImpl, xmlMemUsed, xmlReallocImpl, xmlReallocZero,
30};
31
32/// Initialize the memory subsystem.
33///
34/// Called during `xmlInitParser`. Returns 0 on success.
35pub const fn init_memory() -> i32 {
36    xmlInitMemory()
37}
38
39/// Clean up the memory subsystem.
40pub const fn cleanup_memory() {
41    // Phase 1: no cleanup needed for the default allocator.
42}
43
44// ═══════════════════════════════════════════════════════════════════════════════
45// Tests
46// ═══════════════════════════════════════════════════════════════════════════════
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51
52    #[test]
53    fn test_memory_module_delegates_to_allocator() {
54        unsafe {
55            let ptr = xmlMallocImpl(100);
56            assert!(!ptr.is_null());
57            xmlFreeImpl(ptr);
58        }
59    }
60
61    #[test]
62    fn test_memory_zero_alloc() {
63        unsafe {
64            let ptr = xmlMallocZero(64);
65            assert!(!ptr.is_null());
66            // Verify zero-initialized
67            let bytes = core::slice::from_raw_parts(ptr as *const u8, 64);
68            assert!(bytes.iter().all(|&b| b == 0));
69            xmlFreeImpl(ptr);
70        }
71    }
72}