Path: blob/main/crates/polars-python/src/c_api/allocator.rs
7889 views
#[cfg(all(1not(feature = "default_alloc"),2target_family = "unix",3not(target_os = "emscripten"),4))]5#[global_allocator]6static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;78#[cfg(all(9not(feature = "default_alloc"),10any(not(target_family = "unix"), target_os = "emscripten"),11))]12#[global_allocator]13static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;1415use std::alloc::Layout;16use std::ffi::{c_char, c_void};1718use pyo3::ffi::PyCapsule_New;19use pyo3::{Bound, PyAny, PyResult, Python};2021unsafe extern "C" fn alloc(size: usize, align: usize) -> *mut u8 {22unsafe { std::alloc::alloc(Layout::from_size_align_unchecked(size, align)) }23}2425unsafe extern "C" fn dealloc(ptr: *mut u8, size: usize, align: usize) {26unsafe { std::alloc::dealloc(ptr, Layout::from_size_align_unchecked(size, align)) }27}2829unsafe extern "C" fn alloc_zeroed(size: usize, align: usize) -> *mut u8 {30unsafe { std::alloc::alloc_zeroed(Layout::from_size_align_unchecked(size, align)) }31}3233unsafe extern "C" fn realloc(ptr: *mut u8, size: usize, align: usize, new_size: usize) -> *mut u8 {34unsafe {35std::alloc::realloc(36ptr,37Layout::from_size_align_unchecked(size, align),38new_size,39)40}41}4243#[repr(C)]44struct AllocatorCapsule {45alloc: unsafe extern "C" fn(usize, usize) -> *mut u8,46dealloc: unsafe extern "C" fn(*mut u8, usize, usize),47alloc_zeroed: unsafe extern "C" fn(usize, usize) -> *mut u8,48realloc: unsafe extern "C" fn(*mut u8, usize, usize, usize) -> *mut u8,49}5051static ALLOCATOR_CAPSULE: AllocatorCapsule = AllocatorCapsule {52alloc,53alloc_zeroed,54dealloc,55realloc,56};5758static ALLOCATOR_CAPSULE_NAME: &[u8] = b"polars.polars._allocator\0";5960pub fn create_allocator_capsule(py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {61unsafe {62Bound::from_owned_ptr_or_err(63py,64PyCapsule_New(65&ALLOCATOR_CAPSULE as *const AllocatorCapsule66// Users of this capsule is not allowed to modify it.67as *mut c_void,68ALLOCATOR_CAPSULE_NAME.as_ptr() as *const c_char,69None,70),71)72}73}747576