composable_allocators/
logging.rs1use crate::base::*;
2use core::alloc::{self, AllocError, Allocator};
3use core::ptr::NonNull;
4use print_no_std::Stderr;
5
6pub struct Logging<A: Allocator>(pub A);
7
8unsafe impl<A: NonUnwinding> NonUnwinding for Logging<A> { }
9
10unsafe impl<A: Fallbackable> Fallbackable for Logging<A> {
11 unsafe fn has_allocated(&self, ptr: NonNull<u8>, layout: alloc::Layout) -> bool {
12 self.0.has_allocated(ptr, layout)
13 }
14
15 fn allows_fallback(&self, layout: alloc::Layout) -> bool {
16 self.0.allows_fallback(layout)
17 }
18}
19
20unsafe impl<A: Allocator> Allocator for Logging<A> {
21 fn allocate(&self, layout: alloc::Layout) -> Result<NonNull<[u8]>, AllocError> {
22 let _ = writeln!(Stderr { panic: false }, "allocate: {layout:?}");
23 self.0.allocate(layout)
24 }
25
26 fn allocate_zeroed(&self, layout: alloc::Layout) -> Result<NonNull<[u8]>, AllocError> {
27 let _ = writeln!(Stderr { panic: false }, "allocate_zeroed: {layout:?}");
28 self.0.allocate_zeroed(layout)
29 }
30
31 unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: alloc::Layout) {
32 let _ = writeln!(Stderr { panic: false }, "deallocate: {layout:?}");
33 self.0.deallocate(ptr, layout)
34 }
35
36 unsafe fn grow(
37 &self,
38 ptr: NonNull<u8>,
39 old_layout: alloc::Layout,
40 new_layout: alloc::Layout
41 ) -> Result<NonNull<[u8]>, AllocError> {
42 let _ = writeln!(Stderr { panic: false }, "grow: {old_layout:?} -> {new_layout:?}");
43 self.0.grow(ptr, old_layout, new_layout)
44 }
45
46 unsafe fn grow_zeroed(
47 &self,
48 ptr: NonNull<u8>,
49 old_layout: alloc::Layout,
50 new_layout: alloc::Layout
51 ) -> Result<NonNull<[u8]>, AllocError> {
52 let _ = writeln!(Stderr { panic: false }, "grow_zeroed: {old_layout:?} -> {new_layout:?}");
53 self.0.grow_zeroed(ptr, old_layout, new_layout)
54 }
55
56 unsafe fn shrink(
57 &self,
58 ptr: NonNull<u8>,
59 old_layout: alloc::Layout,
60 new_layout: alloc::Layout
61 ) -> Result<NonNull<[u8]>, AllocError> {
62 let _ = writeln!(Stderr { panic: false }, "shrink: {old_layout:?} -> {new_layout:?}");
63 self.0.shrink(ptr, old_layout, new_layout)
64 }
65}