wasmtime/runtime/vm/instance/allocator/pooling/
gc_heap_pool.rs1use super::index_allocator::{SimpleIndexAllocator, SlotId};
2use super::{GcHeapAllocationIndex, PoolConcurrencyLimitError};
3use crate::config::PoolingAllocationConfig;
4use crate::runtime::vm::{GcHeap, GcRuntime, Result};
5use crate::vm::MemoryAllocationIndex;
6use crate::{Engine, prelude::*};
7use std::sync::Mutex;
8use wasmtime_environ::Tunables;
9
10enum HeapSlot {
11 Free(Option<Box<dyn GcHeap>>),
14
15 InUse(MemoryAllocationIndex),
18}
19
20impl HeapSlot {
21 fn alloc(&mut self, memory_alloc_index: MemoryAllocationIndex) -> Option<Box<dyn GcHeap>> {
22 match self {
23 HeapSlot::Free(gc_heap) => {
24 let gc_heap = gc_heap.take();
25 *self = HeapSlot::InUse(memory_alloc_index);
26 gc_heap
27 }
28 HeapSlot::InUse(_) => panic!("already in use"),
29 }
30 }
31
32 fn dealloc(&mut self, heap: Option<Box<dyn GcHeap>>) -> MemoryAllocationIndex {
33 match *self {
34 HeapSlot::Free(_) => panic!("already free"),
35 HeapSlot::InUse(memory_alloc_index) => {
36 *self = HeapSlot::Free(heap);
37 memory_alloc_index
38 }
39 }
40 }
41}
42
43pub struct GcHeapPool {
45 max_gc_heaps: usize,
46 index_allocator: SimpleIndexAllocator,
47 heaps: Mutex<Box<[HeapSlot]>>,
48}
49
50impl std::fmt::Debug for GcHeapPool {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 f.debug_struct("GcHeapPool")
53 .field("max_gc_heaps", &self.max_gc_heaps)
54 .field("index_allocator", &self.index_allocator)
55 .field("heaps", &"..")
56 .finish()
57 }
58}
59
60impl GcHeapPool {
61 pub fn new(config: &PoolingAllocationConfig, tunables: &Tunables) -> Result<Self> {
63 debug_assert_eq!(
66 tunables.memory_reservation, tunables.gc_heap_reservation,
67 "pooling allocator requires memory_reservation == gc_heap_reservation"
68 );
69 debug_assert_eq!(
70 tunables.memory_guard_size, tunables.gc_heap_guard_size,
71 "pooling allocator requires memory_guard_size == gc_heap_guard_size"
72 );
73 debug_assert_eq!(
74 tunables.memory_reservation_for_growth, tunables.gc_heap_reservation_for_growth,
75 "pooling allocator requires memory_reservation_for_growth == gc_heap_reservation_for_growth"
76 );
77 debug_assert_eq!(
78 tunables.memory_may_move, tunables.gc_heap_may_move,
79 "pooling allocator requires memory_may_move == gc_heap_may_move"
80 );
81
82 let index_allocator = SimpleIndexAllocator::new(config.limits.total_gc_heaps)?;
83 let max_gc_heaps = usize::try_from(config.limits.total_gc_heaps).unwrap();
84
85 let heaps = Mutex::new((0..max_gc_heaps).map(|_| HeapSlot::Free(None)).collect());
88
89 Ok(Self {
90 max_gc_heaps,
91 index_allocator,
92 heaps,
93 })
94 }
95
96 pub fn is_empty(&self) -> bool {
98 self.index_allocator.is_empty()
99 }
100
101 pub fn allocate(
103 &self,
104 engine: &Engine,
105 gc_runtime: &dyn GcRuntime,
106 memory_alloc_index: MemoryAllocationIndex,
107 ) -> Result<(GcHeapAllocationIndex, Box<dyn GcHeap>)> {
108 let allocation_index = self
109 .index_allocator
110 .alloc()
111 .map(|slot| GcHeapAllocationIndex(slot.0))
112 .ok_or_else(|| PoolConcurrencyLimitError::new(self.max_gc_heaps, "GC heaps"))?;
113 debug_assert_ne!(allocation_index, GcHeapAllocationIndex::default());
114
115 let heap = match {
116 let mut heaps = self.heaps.lock().unwrap();
117 heaps[allocation_index.index()].alloc(memory_alloc_index)
118 } {
119 Some(heap) => heap,
121 None => match gc_runtime.new_gc_heap(engine) {
124 Ok(heap) => heap,
125 Err(e) => {
126 self.deallocate_maybe_with_heap(allocation_index, None);
127 return Err(e);
128 }
129 },
130 };
131
132 debug_assert!(!heap.is_attached());
133
134 Ok((allocation_index, heap))
135 }
136
137 pub fn deallocate(
139 &self,
140 allocation_index: GcHeapAllocationIndex,
141 heap: Box<dyn GcHeap>,
142 ) -> MemoryAllocationIndex {
143 self.deallocate_maybe_with_heap(allocation_index, Some(heap))
144 }
145
146 fn deallocate_maybe_with_heap(
147 &self,
148 allocation_index: GcHeapAllocationIndex,
149 heap: Option<Box<dyn GcHeap>>,
150 ) -> MemoryAllocationIndex {
151 debug_assert_ne!(allocation_index, GcHeapAllocationIndex::default());
152 if let Some(heap) = &heap {
153 debug_assert!(!heap.is_attached());
154 }
155
156 let memory_alloc_index = {
161 let mut heaps = self.heaps.lock().unwrap();
162 heaps[allocation_index.index()].dealloc(heap)
163 };
164
165 self.index_allocator.free(SlotId(allocation_index.0), 0);
166
167 memory_alloc_index
168 }
169}