| 1 | //! # allocator infrastructure |
| 2 | //! |
| 3 | //! The public interface allows setting a custom allocator, but we need to configure a default |
| 4 | //! allocator if the user did not configure one. We have two choices, configured by feature flags: |
| 5 | //! |
| 6 | //! - `"rust-allocator"` uses the rust global allocator |
| 7 | //! - `"c-allocator"` uses an allocator based on `malloc` and `free` |
| 8 | //! |
| 9 | //! When both configured, `"rust-allocator"` is preferred. |
| 10 | //! |
| 11 | //! The interface for the allocator is not a great fit for rust. In particular, rust always needs |
| 12 | //! the layout of an allocation to deallocate it, and C interfaces don't usually provide this |
| 13 | //! information. Luckily in the library we know in all cases how big the allocation was at the |
| 14 | //! point where we deallocate it. |
| 15 | |
| 16 | #[cfg(feature = "rust-allocator")] |
| 17 | extern crate allocrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/alloc/; |
| 18 | |
| 19 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ffirust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/::{c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#}; |
| 20 | |
| 21 | use craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/::bzlibrust-analyzer cargo libbz2-rs-sys 0.2.5 bzlib/::{BzStreamrust-analyzer cargo libbz2-rs-sys 0.2.5 bzlib/stream/BzStream#, StreamStaterust-analyzer cargo libbz2-rs-sys 0.2.5 bzlib/stream/StreamState#}; |
| 22 | |
| 23 | type AllocFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/AllocFunc# = unsafe extern "C" fn(*mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#, c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) -> *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#; |
| 24 | type FreeFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/FreeFunc# = unsafe extern "C" fn(*mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#, *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#) -> (); |
| 25 | |
| 26 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) enum Allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator# { |
| 27 | #[cfg(feature = "rust-allocator")] |
| 28 | Rustrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Rust#, |
| 29 | #[cfg(feature = "c-allocator")] |
| 30 | Crust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#C#, |
| 31 | Customrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom# { |
| 32 | allocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#allocate.: AllocFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/AllocFunc#, |
| 33 | deallocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#deallocate.: FreeFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/FreeFunc#, |
| 34 | opaquerust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#opaque.: *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#, |
| 35 | }, |
| 36 | } |
| 37 | |
| 38 | impl Allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator# { |
| 39 | #[allow(unreachable_code)] |
| 40 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) const DEFAULTrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]DEFAULT.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]> = 'blk: { |
| 41 | #[cfg(feature = "rust-allocator")] |
| 42 | break 'blk Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]::Rustrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Rust#); |
| 43 | |
| 44 | #[cfg(feature = "c-allocator")] |
| 45 | break 'blk Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]::Crust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#C#); |
| 46 | |
| 47 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 48 | }; |
| 49 | |
| 50 | #[allow(unreachable_code)] |
| 51 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) fn default_function_pointersrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]default_function_pointers().() -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<(AllocFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/AllocFunc#, FreeFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/FreeFunc#)> { |
| 52 | #[cfg(feature = "rust-allocator")] |
| 53 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(rust_allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/::ALLOCATORrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/ALLOCATOR.); |
| 54 | |
| 55 | #[cfg(feature = "c-allocator")] |
| 56 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(c_allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/::ALLOCATORrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/ALLOCATOR.); |
| 57 | |
| 58 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 59 | } |
| 60 | |
| 61 | /// # Safety |
| 62 | /// |
| 63 | /// - `strm.bzalloc` and `strm.opaque` must form a valid allocator, meaning `strm.bzalloc` returns either |
| 64 | /// * a `NULL` pointer |
| 65 | /// * a valid pointer to an allocation of `len * size_of::<T>()` bytes aligned to at least `align_of::<usize>()` |
| 66 | /// - `strm.bzfree` frees memory allocated by `strm.bzalloc` |
| 67 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) unsafe fn from_bz_streamrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]from_bz_stream().<S: StreamStaterust-analyzer cargo libbz2-rs-sys 0.2.5 bzlib/stream/StreamState#>(strm: &BzStreamrust-analyzer cargo libbz2-rs-sys 0.2.5 bzlib/stream/BzStream#<S>) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]> { |
| 68 | let bzalloc = strm.bzallocrust-analyzer cargo libbz2-rs-sys 0.2.5 bzlib/stream/BzStream#bzalloc.?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 69 | let bzfree = strm.bzfreerust-analyzer cargo libbz2-rs-sys 0.2.5 bzlib/stream/BzStream#bzfree.?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 70 | |
| 71 | #[cfg(feature = "rust-allocator")] |
| 72 | if (bzalloc, bzfree) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().rust_allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/::ALLOCATORrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/ALLOCATOR. { |
| 73 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]::Rustrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Rust#); |
| 74 | } |
| 75 | |
| 76 | #[cfg(feature = "c-allocator")] |
| 77 | if (bzalloc, bzfree) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]eq().c_allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/::ALLOCATORrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/ALLOCATOR. { |
| 78 | return Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]::Crust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#C#); |
| 79 | } |
| 80 | |
| 81 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]::customrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]custom().(bzalloc, bzfree, strm.opaquerust-analyzer cargo libbz2-rs-sys 0.2.5 bzlib/stream/BzStream#opaque.)) |
| 82 | } |
| 83 | |
| 84 | /// # Safety |
| 85 | /// |
| 86 | /// - `allocate` and `opaque` must form a valid allocator, meaning `allocate` returns either |
| 87 | /// * a `NULL` pointer |
| 88 | /// * a valid pointer to an allocation of `len * size_of::<T>()` bytes aligned to at least `align_of::<usize>()` |
| 89 | /// - `deallocate` frees memory allocated by `allocate` |
| 90 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) fn customrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]custom().(allocate: AllocFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/AllocFunc#, deallocate: FreeFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/FreeFunc#, opaque: *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#) -> Selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator] { |
| 91 | Selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]::Customrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom# { |
| 92 | allocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#allocate., |
| 93 | deallocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#deallocate., |
| 94 | opaquerust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#opaque., |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #[cfg(feature = "c-allocator")] |
| 100 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) mod c_allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/ { |
| 101 | use superrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/::*; |
| 102 | |
| 103 | // make sure that the only way these function pointers leave this module is via this constant |
| 104 | // that way the function pointer address is a reliable way to know that the default C allocator |
| 105 | // is used. |
| 106 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) static ALLOCATORrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/ALLOCATOR.: (AllocFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/AllocFunc#, FreeFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/FreeFunc#) = (selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/::allocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/allocate()., selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/::deallocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/deallocate().); |
| 107 | |
| 108 | unsafe extern "C" fn allocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/allocate().(_opaque: *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#, count: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, size: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#) -> *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void# { |
| 109 | // NOTE: allocations bigger than isize::MAX are UB in LLVM. |
| 110 | let (Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(count), Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(size)) = (isize::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[isize][`TryFrom<i32>`]try_from().(count), isize::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/ptr_try_from_impls/impl#[isize][`TryFrom<i32>`]try_from().(size)) else { |
| 111 | return corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().(); |
| 112 | }; |
| 113 | |
| 114 | let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(len) = count.checked_mulrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[isize]checked_mul().(size) else { |
| 115 | return corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().(); |
| 116 | }; |
| 117 | |
| 118 | let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(len) = usize::try_fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[usize][`TryFrom<isize>`]try_from().(len) else { |
| 119 | return corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().(); |
| 120 | }; |
| 121 | |
| 122 | unsafe { libcrust-analyzer cargo libc 0.2.161 crate/::mallocrust-analyzer cargo libc 0.2.161 malloc().(len) } |
| 123 | } |
| 124 | |
| 125 | unsafe extern "C" fn deallocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/c_allocator/deallocate().(_opaque: *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#, ptr: *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#) { |
| 126 | if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() { |
| 127 | unsafe { |
| 128 | libcrust-analyzer cargo libc 0.2.161 crate/::freerust-analyzer cargo libc 0.2.161 free().(ptr); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | #[cfg(feature = "rust-allocator")] |
| 135 | mod rust_allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/ { |
| 136 | use superrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/::*; |
| 137 | |
| 138 | // make sure that the only way these function pointers leave this module is via this constant |
| 139 | // that way the function pointer address is a reliable way to know that the default C allocator |
| 140 | // is used. |
| 141 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) static ALLOCATORrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/ALLOCATOR.: (AllocFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/AllocFunc#, FreeFuncrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/FreeFunc#) = (selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/::allocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/allocate()., selfrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/::deallocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/deallocate().); |
| 142 | |
| 143 | unsafe extern "C" fn allocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/allocate().( |
| 144 | _opaque: *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#, |
| 145 | _count: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, |
| 146 | _size: c_intrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/primitives/c_int#, |
| 147 | ) -> *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void# { |
| 148 | unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!("the default rust allocation function should never be called directly"); |
| 149 | } |
| 150 | |
| 151 | unsafe extern "C" fn deallocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/rust_allocator/deallocate().(_opaque: *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#, _ptr: *mut c_voidrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_void#) { |
| 152 | unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!("the default rust deallocation function should never be called directly"); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | impl Allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator# { |
| 157 | /// Allocates `count` contiguous values of type `T`, and zeros out all elements. |
| 158 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) fn allocate_zeroedrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]allocate_zeroed().<T>(&self, count: usize) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<*mut T> { |
| 159 | const { |
| 160 | assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>() != 0); |
| 161 | } |
| 162 | assert_nerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_ne!!(count, 0); |
| 163 | |
| 164 | match self { |
| 165 | #[cfg(feature = "rust-allocator")] |
| 166 | Allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#::Rustrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Rust# => { |
| 167 | let layout = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::allocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/::Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout#::arrayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/impl#[Layout]array().::<T>(count).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 168 | let ptr = unsafe { allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc alloc/::alloc_zeroedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc alloc/alloc_zeroed().(layout) }; |
| 169 | (!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().()).then_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then_some().(ptr.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().()) |
| 170 | } |
| 171 | #[cfg(feature = "c-allocator")] |
| 172 | Allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#::Crust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#C# => { |
| 173 | let ptr = unsafe { libcrust-analyzer cargo libc 0.2.161 crate/::callocrust-analyzer cargo libc 0.2.161 calloc().(count, corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>()) }; |
| 174 | (!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().()).then_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core bool/impl#[bool]then_some().(ptr.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().()) |
| 175 | } |
| 176 | Allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#::Customrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom# { |
| 177 | allocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#allocate., opaquerust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#opaque., .. |
| 178 | } => unsafe { |
| 179 | let ptr = (allocate)(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().opaque, count as i32, corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<T>() as i32); |
| 180 | let ptr = ptr.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().::<T>(); |
| 181 | |
| 182 | if ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() { |
| 183 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 184 | } |
| 185 | |
| 186 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::write_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/write_bytes().(ptr, 0, count); |
| 187 | |
| 188 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ptr) |
| 189 | }, |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | pub(craterust-analyzer cargo libbz2-rs-sys 0.2.5 crate/) unsafe fn deallocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/impl#[Allocator]deallocate().<T>(&self, ptr: *mut T, count: usize) { |
| 194 | if ptr.is_nullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]is_null().() || count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | match self { |
| 199 | #[cfg(feature = "rust-allocator")] |
| 200 | Allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#::Rustrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Rust# => { |
| 201 | let layout = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::allocrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/::Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout#::arrayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/impl#[Layout]array().::<T>(count).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 202 | unsafe { allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc alloc/::deallocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc alloc/dealloc().(ptr.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().(), layout) } |
| 203 | } |
| 204 | #[cfg(feature = "c-allocator")] |
| 205 | Allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#::Crust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#C# => { |
| 206 | unsafe { libcrust-analyzer cargo libc 0.2.161 crate/::freerust-analyzer cargo libc 0.2.161 free().(ptr.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().()) }; |
| 207 | } |
| 208 | Allocatorrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#::Customrust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom# { |
| 209 | deallocaterust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#deallocate., opaquerust-analyzer cargo libbz2-rs-sys 0.2.5 allocator/Allocator#Custom#opaque., .. |
| 210 | } => { |
| 211 | unsafe { deallocate(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().opaque, ptr.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().()) }; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | } |