mmtk/util/alloc/
malloc_allocator.rs

1use std::sync::Arc;
2
3use crate::policy::marksweepspace::malloc_ms::MallocSpace;
4use crate::policy::space::Space;
5use crate::util::alloc::Allocator;
6use crate::util::opaque_pointer::*;
7use crate::util::Address;
8use crate::vm::VMBinding;
9
10use super::allocator::AllocatorContext;
11
12/// The allocator that internally uses malloc for all the allocation requests.
13/// This allocator is only intended for experimental uses.
14#[repr(C)]
15pub struct MallocAllocator<VM: VMBinding> {
16    /// [`VMThread`] associated with this allocator instance
17    pub tls: VMThread,
18    /// [`Space`](src/policy/space/Space) instance associated with this allocator instance.
19    space: &'static MallocSpace<VM>,
20    context: Arc<AllocatorContext<VM>>,
21}
22
23impl<VM: VMBinding> Allocator<VM> for MallocAllocator<VM> {
24    fn get_space(&self) -> &'static dyn Space<VM> {
25        self.space as &'static dyn Space<VM>
26    }
27
28    fn get_context(&self) -> &AllocatorContext<VM> {
29        &self.context
30    }
31
32    fn alloc(&mut self, size: usize, align: usize, offset: usize) -> Address {
33        self.alloc_slow(size, align, offset)
34    }
35
36    fn get_tls(&self) -> VMThread {
37        self.tls
38    }
39
40    fn does_thread_local_allocation(&self) -> bool {
41        false
42    }
43
44    fn alloc_slow_once(&mut self, size: usize, align: usize, offset: usize) -> Address {
45        self.space.alloc(self.tls, size, align, offset)
46    }
47}
48
49impl<VM: VMBinding> MallocAllocator<VM> {
50    pub(crate) fn new(
51        tls: VMThread,
52        space: &'static MallocSpace<VM>,
53        context: Arc<AllocatorContext<VM>>,
54    ) -> Self {
55        MallocAllocator {
56            tls,
57            space,
58            context,
59        }
60    }
61}