1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#[cfg(any(doc, feature = "alloc"))]
use alloc::{boxed::Box, rc::Rc, sync::Arc};
use core::{
    alloc::{AllocErr, AllocInit, Layout, MemoryBlock, ReallocPlacement},
    ptr::NonNull,
};

/// Backend for the [`Proxy`] allocator.
///
/// As `Callback` is used in `Proxy` and `AllocRef` requires, that a cloned allocator must
/// behave like the same allocator, `Clone` must not be implemented on types, which don't
/// have a shared state. It's possible to use a reference by calling [`by_ref`] or to
/// wrapping them into `Rc` or `Arc` in order to make them cloneable instead. Note, that
/// `Box`, `Rc`, and `Arc` requires the `"alloc"`-feature to be enabled.
///
/// [`by_ref`]: CallbackRef::by_ref
/// [`Proxy`]: crate::Proxy
///
/// # Safety
///
///   * `Clone` must not be implemented on types, which don't have a shared state.
pub unsafe trait CallbackRef {
    /// Called when [`alloc`] was invoked.
    ///
    /// [`alloc`]: core::alloc::AllocRef::alloc
    fn alloc(&self, layout: Layout, init: AllocInit, result: Result<MemoryBlock, AllocErr>);

    /// Called when [`dealloc`] was invoked.
    ///
    /// [`dealloc`]: core::alloc::AllocRef::dealloc
    fn dealloc(&self, ptr: NonNull<u8>, layout: Layout);

    /// Called when [`grow`] was invoked.
    ///
    /// [`grow`]: core::alloc::AllocRef::grow
    fn grow(
        &self,
        ptr: NonNull<u8>,
        layout: Layout,
        new_size: usize,
        placement: ReallocPlacement,
        init: AllocInit,
        result: Result<MemoryBlock, AllocErr>,
    );

    /// Called when [`shrink`] was invoked.
    ///
    /// [`shrink`]: core::alloc::AllocRef::shrink
    fn shrink(
        &self,
        ptr: NonNull<u8>,
        layout: Layout,
        new_size: usize,
        placement: ReallocPlacement,
        result: Result<MemoryBlock, AllocErr>,
    );

    /// Called when [`owns`] was invoked.
    ///
    /// [`owns`]: crate::Owns::owns
    fn owns(&self, success: bool);

    /// Creates a "by reference" adaptor for this instance of `CallbackRef`.
    ///
    /// The returned adaptor also implements `CallbackRef` and will simply borrow this.
    #[inline]
    fn by_ref(&self) -> &Self {
        self
    }
}

unsafe impl<C: CallbackRef> CallbackRef for &C {
    #[inline]
    fn alloc(&self, layout: Layout, init: AllocInit, result: Result<MemoryBlock, AllocErr>) {
        (**self).alloc(layout, init, result)
    }

    #[inline]
    fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
        (**self).dealloc(ptr, layout)
    }

    #[inline]
    fn grow(
        &self,
        ptr: NonNull<u8>,
        layout: Layout,
        new_size: usize,
        placement: ReallocPlacement,
        init: AllocInit,
        result: Result<MemoryBlock, AllocErr>,
    ) {
        (**self).grow(ptr, layout, new_size, placement, init, result)
    }

    #[inline]
    fn shrink(
        &self,
        ptr: NonNull<u8>,
        layout: Layout,
        new_size: usize,
        placement: ReallocPlacement,
        result: Result<MemoryBlock, AllocErr>,
    ) {
        (**self).shrink(ptr, layout, new_size, placement, result)
    }
    #[inline]
    fn owns(&self, success: bool) {
        (**self).owns(success)
    }
}

macro_rules! impl_alloc_stats {
    ($tt:tt) => {
        #[cfg(any(doc, feature = "alloc"))]
        #[cfg_attr(doc, doc(cfg(feature = "alloc")))]
        /// This is only available with the **"alloc"-feature** enabled.
        unsafe impl<C: CallbackRef> CallbackRef for $tt<C> {
            #[inline]
            fn alloc(
                &self,
                layout: Layout,
                init: AllocInit,
                result: Result<MemoryBlock, AllocErr>,
            ) {
                (**self).alloc(layout, init, result)
            }

            #[inline]
            fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
                (**self).dealloc(ptr, layout)
            }

            #[inline]
            fn grow(
                &self,
                ptr: NonNull<u8>,
                layout: Layout,
                new_size: usize,
                placement: ReallocPlacement,
                init: AllocInit,
                result: Result<MemoryBlock, AllocErr>,
            ) {
                (**self).grow(ptr, layout, new_size, placement, init, result)
            }

            #[inline]
            fn shrink(
                &self,
                ptr: NonNull<u8>,
                layout: Layout,
                new_size: usize,
                placement: ReallocPlacement,
                result: Result<MemoryBlock, AllocErr>,
            ) {
                (**self).shrink(ptr, layout, new_size, placement, result)
            }

            #[inline]
            fn owns(&self, success: bool) {
                (**self).owns(success)
            }
        }
    };
}

impl_alloc_stats!(Box);
impl_alloc_stats!(Rc);
impl_alloc_stats!(Arc);