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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use probe::probe;

pub struct Allocator {
    inner: mimalloc::MiMalloc,
}

impl Allocator {
    pub const fn new() -> Self {
        Self {
            inner: mimalloc::MiMalloc,
        }
    }
}

#[inline(never)]
#[allow(unused_variables)]
fn alloc(size: usize) {
    probe!(netbench, netbench__alloc, size);
}

#[inline(never)]
#[allow(unused_variables)]
fn dealloc(size: usize) {
    probe!(netbench, netbench__dealloc, size);
}

#[inline(never)]
#[allow(unused_variables)]
fn realloc(prev_size: usize, new_size: usize) {
    probe!(netbench, netbench__realloc, prev_size, new_size);
}

unsafe impl std::alloc::GlobalAlloc for Allocator {
    #[inline]
    unsafe fn alloc(&self, layout: std::alloc::Layout) -> *mut u8 {
        alloc(layout.size());
        self.inner.alloc(layout)
    }

    #[inline]
    unsafe fn dealloc(&self, ptr: *mut u8, layout: std::alloc::Layout) {
        dealloc(layout.size());
        self.inner.dealloc(ptr, layout)
    }

    #[inline]
    unsafe fn realloc(&self, ptr: *mut u8, layout: std::alloc::Layout, new_size: usize) -> *mut u8 {
        realloc(layout.size(), new_size);
        self.inner.realloc(ptr, layout, new_size)
    }
}