1#![feature(linkage)]
2#![warn(clippy::all)]
3
4use std::alloc::{GlobalAlloc, Layout};
5
6mod sys;
7mod sys_common;
8
9#[derive(Clone, Copy)]
10pub struct Alloc {
11 alloc: haz_alloc_core::Alloc<sys::Backend>,
12}
13
14impl Alloc {
15 pub const fn new() -> Self {
16 Alloc {
17 alloc: unsafe { haz_alloc_core::Alloc::new() },
18 }
19 }
20}
21
22impl Alloc {
23 #[inline]
27 pub unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
28 self.alloc.alloc(layout)
29 }
30
31 #[inline]
35 pub unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
36 self.alloc.alloc_zeroed(layout)
37 }
38
39 #[inline]
45 pub unsafe fn realloc(&self, ptr: *mut u8, layout: Layout) -> *mut u8 {
46 self.alloc.realloc(ptr, layout)
47 }
48
49 #[inline]
53 pub unsafe fn dealloc(&self, ptr: *mut u8) {
54 self.alloc.dealloc(ptr)
55 }
56
57 #[inline]
61 pub unsafe fn size(&self, ptr: *mut u8) -> usize {
62 self.alloc.size(ptr)
63 }
64}
65
66unsafe impl GlobalAlloc for Alloc {
67 #[inline]
68 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
69 self.alloc(layout)
70 }
71
72 #[inline]
73 unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {
74 self.dealloc(ptr)
75 }
76
77 #[inline]
78 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
79 self.alloc_zeroed(layout)
80 }
81
82 #[inline]
83 unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
84 self.realloc(
85 ptr,
86 Layout::from_size_align_unchecked(new_size, layout.align()),
87 )
88 }
89}
90
91impl Default for Alloc {
92 fn default() -> Self {
93 Alloc::new()
94 }
95}