wasmtime/runtime/vm/instance/allocator/pooling/
gc_heap_pool.rs1use super::GcHeapAllocationIndex;
2use super::index_allocator::{SimpleIndexAllocator, SlotId};
3use crate::runtime::vm::{GcHeap, GcRuntime, PoolingInstanceAllocatorConfig, Result};
4use crate::vm::{Memory, MemoryAllocationIndex};
5use crate::{Engine, prelude::*};
6use std::sync::Mutex;
7
8enum HeapSlot {
9 Free(Option<Box<dyn GcHeap>>),
12
13 InUse(MemoryAllocationIndex),
16}
17
18impl HeapSlot {
19 fn alloc(&mut self, memory_alloc_index: MemoryAllocationIndex) -> Option<Box<dyn GcHeap>> {
20 match self {
21 HeapSlot::Free(gc_heap) => {
22 let gc_heap = gc_heap.take();
23 *self = HeapSlot::InUse(memory_alloc_index);
24 gc_heap
25 }
26 HeapSlot::InUse(_) => panic!("already in use"),
27 }
28 }
29
30 fn dealloc(&mut self, heap: Box<dyn GcHeap>) -> MemoryAllocationIndex {
31 match *self {
32 HeapSlot::Free(_) => panic!("already free"),
33 HeapSlot::InUse(memory_alloc_index) => {
34 *self = HeapSlot::Free(Some(heap));
35 memory_alloc_index
36 }
37 }
38 }
39}
40
41pub struct GcHeapPool {
43 max_gc_heaps: usize,
44 index_allocator: SimpleIndexAllocator,
45 heaps: Mutex<Box<[HeapSlot]>>,
46}
47
48impl std::fmt::Debug for GcHeapPool {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 f.debug_struct("GcHeapPool")
51 .field("max_gc_heaps", &self.max_gc_heaps)
52 .field("index_allocator", &self.index_allocator)
53 .field("heaps", &"..")
54 .finish()
55 }
56}
57
58impl GcHeapPool {
59 pub fn new(config: &PoolingInstanceAllocatorConfig) -> Result<Self> {
61 let index_allocator = SimpleIndexAllocator::new(config.limits.total_gc_heaps);
62 let max_gc_heaps = usize::try_from(config.limits.total_gc_heaps).unwrap();
63
64 let heaps = Mutex::new((0..max_gc_heaps).map(|_| HeapSlot::Free(None)).collect());
67
68 Ok(Self {
69 max_gc_heaps,
70 index_allocator,
71 heaps,
72 })
73 }
74
75 pub fn is_empty(&self) -> bool {
77 self.index_allocator.is_empty()
78 }
79
80 pub fn allocate(
82 &self,
83 engine: &Engine,
84 gc_runtime: &dyn GcRuntime,
85 memory_alloc_index: MemoryAllocationIndex,
86 memory: Memory,
87 ) -> Result<(GcHeapAllocationIndex, Box<dyn GcHeap>)> {
88 let allocation_index = self
89 .index_allocator
90 .alloc()
91 .map(|slot| GcHeapAllocationIndex(slot.0))
92 .ok_or_else(|| {
93 anyhow!(
94 "maximum concurrent GC heap limit of {} reached",
95 self.max_gc_heaps
96 )
97 })?;
98 debug_assert_ne!(allocation_index, GcHeapAllocationIndex::default());
99
100 let mut heap = match {
101 let mut heaps = self.heaps.lock().unwrap();
102 heaps[allocation_index.index()].alloc(memory_alloc_index)
103 } {
104 Some(heap) => heap,
106 None => gc_runtime.new_gc_heap(engine)?,
109 };
110
111 debug_assert!(!heap.is_attached());
112 heap.attach(memory);
113
114 Ok((allocation_index, heap))
115 }
116
117 pub fn deallocate(
119 &self,
120 allocation_index: GcHeapAllocationIndex,
121 mut heap: Box<dyn GcHeap>,
122 ) -> (MemoryAllocationIndex, Memory) {
123 debug_assert_ne!(allocation_index, GcHeapAllocationIndex::default());
124
125 let memory = heap.detach();
126
127 let memory_alloc_index = {
132 let mut heaps = self.heaps.lock().unwrap();
133 heaps[allocation_index.index()].dealloc(heap)
134 };
135
136 self.index_allocator.free(SlotId(allocation_index.0));
137
138 (memory_alloc_index, memory)
139 }
140}