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
use crate::base::*;
use core::alloc::{self, AllocError, Allocator};
use core::ptr::NonNull;

pub struct Logging<A: Allocator>(pub A);

unsafe impl<A: NonUnwinding> NonUnwinding for Logging<A> { }

unsafe impl<A: Fallbackable> Fallbackable for Logging<A> {
    unsafe fn has_allocated(&self, ptr: NonNull<u8>, layout: alloc::Layout) -> bool {
        self.0.has_allocated(ptr, layout)
    }

    fn allows_fallback(&self, layout: alloc::Layout) -> bool {
        self.0.allows_fallback(layout)
    }
}

unsafe impl<A: Allocator> Allocator for Logging<A> {
    fn allocate(&self, layout: alloc::Layout) -> Result<NonNull<[u8]>, AllocError> {
        eprintln!("allocate: {layout:?}");
        self.0.allocate(layout)
    }

    fn allocate_zeroed(&self, layout: alloc::Layout) -> Result<NonNull<[u8]>, AllocError> {
        eprintln!("allocate_zeroed: {layout:?}");
        self.0.allocate_zeroed(layout)
    }

    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: alloc::Layout) {
        eprintln!("deallocate: {layout:?}");
        self.0.deallocate(ptr, layout)
    }

    unsafe fn grow(
        &self, 
        ptr: NonNull<u8>, 
        old_layout: alloc::Layout, 
        new_layout: alloc::Layout
    ) -> Result<NonNull<[u8]>, AllocError> {
        eprintln!("grow: {old_layout:?} -> {new_layout:?}");
        self.0.grow(ptr, old_layout, new_layout)
    }

    unsafe fn grow_zeroed(
        &self, 
        ptr: NonNull<u8>, 
        old_layout: alloc::Layout, 
        new_layout: alloc::Layout
    ) -> Result<NonNull<[u8]>, AllocError> {
        eprintln!("grow_zeroed: {old_layout:?} -> {new_layout:?}");
        self.0.grow_zeroed(ptr, old_layout, new_layout)
    }

    unsafe fn shrink(
        &self, 
        ptr: NonNull<u8>, 
        old_layout: alloc::Layout, 
        new_layout: alloc::Layout
    ) -> Result<NonNull<[u8]>, AllocError> {
        eprintln!("shrink: {old_layout:?} -> {new_layout:?}");
        self.0.shrink(ptr, old_layout, new_layout)
    }
}