11mod tracked_memory_creator;
22pub ( crate ) use self :: tracked_memory_creator:: TrackedMemoryCreator ;
3- use crate :: { define_rb_intern, helpers:: SymbolEnum } ;
3+ use crate :: { define_rb_intern, helpers:: SymbolEnum , PoolingAllocationConfig } ;
44use lazy_static:: lazy_static;
55use magnus:: {
66 exception:: { arg_error, type_error} ,
77 prelude:: * ,
88 r_hash:: ForEach ,
9+ try_convert,
10+ typed_data:: Obj ,
911 Error , RHash , Symbol , TryConvert , Value ,
1012} ;
1113use std:: {
1214 convert:: { TryFrom , TryInto } ,
15+ ops:: Deref ,
1316 sync:: Arc ,
1417} ;
15- use wasmtime:: { Config , OptLevel , ProfilingStrategy , Strategy , WasmBacktraceDetails } ;
18+ use wasmtime:: {
19+ Config , InstanceAllocationStrategy , OptLevel , ProfilingStrategy , Strategy , WasmBacktraceDetails ,
20+ } ;
1621
1722define_rb_intern ! (
1823 DEBUG_INFO => "debug_info" ,
@@ -39,6 +44,9 @@ define_rb_intern!(
3944 AUTO => "auto" ,
4045 CRANELIFT => "cranelift" ,
4146 WINCH => "winch" ,
47+ ALLOCATION_STRATEGY => "allocation_strategy" ,
48+ POOLING => "pooling" ,
49+ ON_DEMAND => "on_demand" ,
4250) ;
4351
4452lazy_static ! {
@@ -123,6 +131,8 @@ pub fn hash_to_config(hash: RHash) -> Result<Config, Error> {
123131 }
124132 } else if * GENERATE_ADDRESS_MAP == id {
125133 config. generate_address_map ( entry. try_into ( ) ?) ;
134+ } else if * ALLOCATION_STRATEGY == id {
135+ config. allocation_strategy ( entry. try_into ( ) ?) ;
126136 } else {
127137 return Err ( Error :: new (
128138 arg_error ( ) ,
@@ -205,3 +215,29 @@ impl TryFrom<ConfigEntry> for wasmtime::Strategy {
205215 STRATEGY_MAPPING . get ( value. 1 )
206216 }
207217}
218+
219+ impl TryFrom < ConfigEntry > for InstanceAllocationStrategy {
220+ type Error = magnus:: Error ;
221+
222+ fn try_from ( value : ConfigEntry ) -> Result < Self , Error > {
223+ if let Ok ( strategy) = Obj :: < PoolingAllocationConfig > :: try_convert ( value. 1 ) {
224+ return Ok ( InstanceAllocationStrategy :: Pooling ( strategy. to_inner ( ) ?) ) ;
225+ }
226+
227+ if let Ok ( symbol) = Symbol :: try_convert ( value. 1 ) {
228+ if symbol. equal ( Symbol :: new ( "pooling" ) ) ? {
229+ return Ok ( InstanceAllocationStrategy :: Pooling ( Default :: default ( ) ) ) ;
230+ } else if symbol. equal ( Symbol :: new ( "on_demand" ) ) ? {
231+ return Ok ( InstanceAllocationStrategy :: OnDemand ) ;
232+ }
233+ }
234+
235+ Err ( Error :: new (
236+ arg_error ( ) ,
237+ format ! (
238+ "invalid instance allocation strategy: {}" ,
239+ value. 1 . inspect( )
240+ ) ,
241+ ) )
242+ }
243+ }
0 commit comments