use std::ffi::c_void;
use libc::{free, malloc, realloc};
use crate::{
error::XmlError,
libxml::xmlmemory::{XmlFreeFunc, XmlMallocFunc, XmlReallocFunc, XmlStrdupFunc},
};
use super::{
threads::xml_get_global_state,
xmlstring::{XmlChar, xml_char_strdup},
};
pub type XmlGlobalStatePtr = *mut XmlGlobalState;
pub struct XmlGlobalState {
pub(crate) xml_free: Option<XmlFreeFunc>,
pub(crate) xml_malloc: Option<XmlMallocFunc>,
pub(crate) xml_mem_strdup: Option<XmlStrdupFunc>,
pub(crate) xml_realloc: Option<XmlReallocFunc>,
pub(crate) xml_last_error: XmlError,
}
#[doc(alias = "xmlMalloc")]
pub(in crate::libxml) static mut _XML_MALLOC: Option<XmlMallocFunc> = Some(malloc);
pub unsafe extern "C" fn xml_malloc(size: usize) -> *mut c_void {
unsafe { _XML_MALLOC.expect("Failed to allocate memory : _XML_MALLOC is None")(size) }
}
pub fn set_xml_malloc(malloc: Option<XmlMallocFunc>) {
unsafe {
_XML_MALLOC = malloc;
(*xml_get_global_state()).xml_malloc = malloc;
}
}
#[doc(alias = "xmlMallocAtomic")]
pub(in crate::libxml) static mut _XML_MALLOC_ATOMIC: XmlMallocFunc = malloc;
pub unsafe extern "C" fn xml_malloc_atomic(size: usize) -> *mut c_void {
unsafe { _XML_MALLOC_ATOMIC(size) }
}
#[doc(alias = "xmlRealloc")]
pub(in crate::libxml) static mut _XML_REALLOC: Option<XmlReallocFunc> = Some(realloc);
pub unsafe extern "C" fn xml_realloc(mem: *mut c_void, size: usize) -> *mut c_void {
unsafe { _XML_REALLOC.expect("Failed to reallocate memory : _XML_REALLOC is None")(mem, size) }
}
pub fn set_xml_realloc(realloc: Option<XmlReallocFunc>) {
unsafe {
_XML_REALLOC = realloc;
(*xml_get_global_state()).xml_realloc = realloc;
}
}
#[doc(alias = "xmlFree")]
pub(in crate::libxml) static mut _XML_FREE: Option<XmlFreeFunc> = Some(free);
pub unsafe extern "C" fn xml_free(mem: *mut c_void) {
unsafe {
_XML_FREE.expect("Failed to deallocate memory : _XML_FREE is None")(mem);
}
}
pub fn set_xml_free(free: Option<XmlFreeFunc>) {
unsafe {
_XML_FREE = free;
(*xml_get_global_state()).xml_free = free;
}
}
#[doc(alias = "xmlPosixStrdup")]
unsafe extern "C" fn xml_posix_strdup(cur: *const XmlChar) -> *mut XmlChar {
unsafe { xml_char_strdup(cur as _) as _ }
}
#[doc(alias = "xmlMemStrdup")]
pub(in crate::libxml) static mut _XML_MEM_STRDUP: Option<XmlStrdupFunc> = Some(xml_posix_strdup);
pub unsafe extern "C" fn xml_mem_strdup(str: *const XmlChar) -> *mut XmlChar {
unsafe {
_XML_MEM_STRDUP.expect("Failed to duplicate xml string : _XML_MEM_STRDUP is None")(str)
}
}
pub fn set_xml_mem_strdup(mem_strdup: Option<XmlStrdupFunc>) {
unsafe {
_XML_MEM_STRDUP = mem_strdup;
(*xml_get_global_state()).xml_mem_strdup = mem_strdup;
}
}