Skip to main content

ialloc/allocator/cpp/
new_delete_array.rs

1use crate::*;
2use crate::meta::*;
3use super::ffi;
4
5use core::ptr::NonNull;
6
7
8
9/// [`::operator new[](size_t, nothrow_t)`](https://en.cppreference.com/w/cpp/memory/new/operator_new) <br>
10/// [`::operator delete[](void*)`](https://en.cppreference.com/w/cpp/memory/new/operator_delete)
11#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] pub struct NewDeleteArray;
12
13
14
15// meta::*
16
17impl Meta for NewDeleteArray {
18    type Error = ();
19
20    const MAX_ALIGN : Alignment = if !cfg!(target_env = "msvc") {
21        Alignment::of::<f64>() // conservative
22        // __STDCPP_DEFAULT_NEW_ALIGNMENT__; // 8/16 - C++17
23    } else if core::mem::size_of::<usize>() >= 8 {
24        ALIGN_16
25    } else {
26        ALIGN_8
27    };
28
29    const MAX_SIZE : usize = usize::MAX; // *slightly* less in practice
30    const ZST_SUPPORTED : bool = false; // works on both MSVC and Linux/Clang, no idea how standard this is however
31}
32
33// SAFETY: ✔️ global state only
34unsafe impl Stateless for NewDeleteArray {}
35
36
37
38// thin::*
39
40/// | Safety Item   | Description   |
41/// | --------------| --------------|
42/// | `align`       | ✔️ Validated via [`thin::test::alignment`]
43/// | `size`        | ✔️ Validated via [`thin::test::edge_case_sizes`]
44/// | `pin`         | ✔️ [`NewDeleteArray`] is `'static` - allocations by [`::operator new[](size_t, nothrow_t)`](https://en.cppreference.com/w/cpp/memory/new/operator_new) live until [`::operator delete[](void*)`](https://en.cppreference.com/w/cpp/memory/new/operator_delete)ed.
45/// | `compatible`  | ✔️ [`NewDeleteArray`] uses exclusively intercompatible arrayed, implicit-alignment operators from the same stdlib.
46/// | `compatible`  | ✔️ [`::operator delete[](void*)`](https://en.cppreference.com/w/cpp/memory/new/operator_delete) is compatible with [`::operator new[](size_t, nothrow_t)`](https://en.cppreference.com/w/cpp/memory/new/operator_new) (C++03 § 18.4.1.2 ¶ 11)
47/// | `exclusive`   | ✔️ [`::operator new[](size_t, nothrow_t)`](https://en.cppreference.com/w/cpp/memory/new/operator_new) allocations are exclusive/unique
48/// | `exceptions`  | ✔️ [`::operator new[](size_t, nothrow_t)`](https://en.cppreference.com/w/cpp/memory/new/operator_new) is `throw()` and will return null on error (C++03 § 18.4.1.2 ¶ 5, or `noexcept` in C++11+)
49/// | `threads`     | ⚠️ thread-unsafe stdlibs existed once upon a time.  I consider linking them in a multithreaded program defacto undefined behavior beyond the scope of this to guard against.
50/// | `zeroed`      | ✔️ Validated via [`thin::test::zeroed_alloc`]
51///
52// SAFETY: per above
53unsafe impl thin::Alloc for NewDeleteArray {
54    fn alloc_uninit(&self, size: usize) -> Result<AllocNN, Self::Error> {
55        // SAFETY: per above
56        NonNull::new(unsafe { ffi::operator_new_array_nothrow(size) }.cast()).ok_or(())
57    }
58}
59
60/// | Safety Item   | Description   |
61/// | --------------| --------------|
62/// | `compatible`  | ✔️ [`NewDeleteArray`] uses exclusively intercompatible arrayed, implicit-alignment operators from the same stdlib.
63/// | `compatible`  | ✔️ [`::operator delete[](void*)`](https://en.cppreference.com/w/cpp/memory/new/operator_delete) is compatible with [`::operator new[](size_t, nothrow_t)`](https://en.cppreference.com/w/cpp/memory/new/operator_new) (C++03 § 18.4.1.2 ¶ 11)
64/// | `exceptions`  | ✔️ [`::operator delete[](void*)`](https://en.cppreference.com/w/cpp/memory/new/operator_delete) is `throw()` and returns no errors (C++03 § 18.4.1.2 ¶ 9) / `noexcept` (C++11+)
65/// | `threads`     | ⚠️ thread-unsafe stdlibs existed once upon a time.  I consider linking them in a multithreaded program defacto undefined behavior beyond the scope of this to guard against.
66///
67// SAFETY: per above
68unsafe impl thin::Free for NewDeleteArray {
69    unsafe fn free_nullable(&self, ptr: *mut core::mem::MaybeUninit<u8>) {
70        // SAFETY: ✔️ `ptr` can be nullptr (C++03 § 18.4.1.2 ¶ 12, validated via [`thin::test::nullable`])
71        // SAFETY: ✔️ `ptr` otherwise belongs to `self` per [`thin::Free::free_nullable`]'s documented safety preconditions
72        unsafe { ffi::operator_delete_array(ptr.cast()) };
73    }
74}
75
76
77
78// fat::*
79
80// SAFETY: ✔️ default Realloc impl is soundly implemented in terms of Alloc+Free
81unsafe impl fat::Realloc for NewDeleteArray {}
82
83#[no_implicit_prelude] mod cleanroom {
84    use super::{impls, NewDeleteArray};
85
86    impls! {
87        unsafe impl ialloc::fat::Alloc      for NewDeleteArray => ialloc::thin::Alloc;
88        unsafe impl ialloc::fat::Free       for NewDeleteArray => ialloc::thin::Free;
89    }
90}
91
92
93
94#[test] fn thin_alignment()         { thin::test::alignment(NewDeleteArray) }
95#[test] fn thin_edge_case_sizes()   { thin::test::edge_case_sizes(NewDeleteArray) }
96#[test] fn thin_nullable()          { thin::test::nullable(NewDeleteArray) }
97#[test] fn thin_uninit()            { if !allocator::cpp::OPERATOR_NEW_ZERO_INITS { unsafe { thin::test::uninit_alloc_unsound(NewDeleteArray) } } }
98//test] fn thin_uninit_realloc()    { thin::test::uninit_realloc(NewDeleteArray) } // does not implement thin::Realloc
99#[test] fn thin_zeroed()            { thin::test::zeroed_alloc(NewDeleteArray) }
100//test] fn thin_zeroed_realloc()    { thin::test::zeroed_realloc(NewDeleteArray) } // does not implement thin::Realloc
101#[test] fn thin_zst_support()       { thin::test::zst_supported_conservative(NewDeleteArray) }
102
103#[test] fn fat_alignment()          { fat::test::alignment(NewDeleteArray) }
104#[test] fn fat_edge_case_sizes()    { fat::test::edge_case_sizes(NewDeleteArray) }
105#[test] fn fat_uninit()             { if !allocator::cpp::OPERATOR_NEW_ZERO_INITS { unsafe { fat::test::uninit_alloc_unsound(NewDeleteArray) } } }
106#[test] fn fat_uninit_realloc()     { fat::test::uninit_realloc(NewDeleteArray) }
107#[test] fn fat_zeroed()             { fat::test::zeroed_alloc(NewDeleteArray) }
108#[test] fn fat_zeroed_realloc()     { fat::test::zeroed_realloc(NewDeleteArray) }
109#[test] fn fat_zst_support()        { fat::test::zst_supported_conservative(NewDeleteArray) }