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    xmlFree, xmlInitMemory, xmlMalloc, xmlMallocAtomic, xmlMallocAtomicZero, xmlMallocZero,
28    xmlMemBlocks, xmlMemDisplay, xmlMemGet, xmlMemSetup, xmlMemShow, xmlMemStrdup, xmlMemUsed,
29    xmlRealloc, xmlReallocZero,
30};
31
32/// Initialize the memory subsystem.
33///
34/// Called during `xmlInitParser`. Returns 0 on success.
35pub fn init_memory() -> i32 {
36    xmlInitMemory()
37}
38
39/// Clean up the memory subsystem.
40pub 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    use core::ffi::c_void;
52
53    #[test]
54    fn test_memory_module_delegates_to_allocator() {
55        unsafe {
56            let ptr = xmlMalloc(100);
57            assert!(!ptr.is_null());
58            xmlFree(ptr);
59        }
60    }
61
62    #[test]
63    fn test_memory_zero_alloc() {
64        unsafe {
65            let ptr = xmlMallocZero(64);
66            assert!(!ptr.is_null());
67            // Verify zero-initialized
68            let bytes = core::slice::from_raw_parts(ptr as *const u8, 64);
69            assert!(bytes.iter().all(|&b| b == 0));
70            xmlFree(ptr);
71        }
72    }
73}