composable_allocators/
global.rs

1use crate::base::*;
2use ::alloc::alloc::{self, AllocError, Allocator};
3use const_default_derive::ConstDefault;
4use core::ptr::NonNull;
5
6#[derive(Debug, Copy, Clone, ConstDefault)]
7pub struct Global;
8
9unsafe impl NonUnwinding for Global { }
10
11unsafe impl Allocator for Global {
12    fn allocate(&self, layout: alloc::Layout) -> Result<NonNull<[u8]>, AllocError> {
13        alloc::Global.allocate(layout)
14    }
15
16    fn allocate_zeroed(&self, layout: alloc::Layout) -> Result<NonNull<[u8]>, AllocError> {
17        alloc::Global.allocate_zeroed(layout)
18    }
19
20    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: alloc::Layout) {
21        alloc::Global.deallocate(ptr, layout)
22    }
23
24    unsafe fn grow(
25        &self, 
26        ptr: NonNull<u8>, 
27        old_layout: alloc::Layout, 
28        new_layout: alloc::Layout
29    ) -> Result<NonNull<[u8]>, AllocError> {
30        alloc::Global.grow(ptr, old_layout, new_layout)
31    }
32
33    unsafe fn grow_zeroed(
34        &self, 
35        ptr: NonNull<u8>, 
36        old_layout: alloc::Layout, 
37        new_layout: alloc::Layout
38    ) -> Result<NonNull<[u8]>, AllocError> {
39        alloc::Global.grow_zeroed(ptr, old_layout, new_layout)
40    }
41
42    unsafe fn shrink(
43        &self, 
44        ptr: NonNull<u8>, 
45        old_layout: alloc::Layout, 
46        new_layout: alloc::Layout
47    ) -> Result<NonNull<[u8]>, AllocError> {
48        alloc::Global.shrink(ptr, old_layout, new_layout)
49    }
50}