@@ -1111,10 +1111,24 @@ mod tests {
11111111 Ok ( ( ) )
11121112 }
11131113
1114+ const PAGE_SIZE : usize = wasmtime_environ:: Memory :: DEFAULT_PAGE_SIZE as usize ;
1115+
1116+ fn check_mem_grow_behavior ( mut store : & mut Store < ( ) > , mem : & Memory ) -> Result < ( ) > {
1117+ assert_eq ! ( mem. data( & store) [ 0 ] , 0 ) ;
1118+ mem. data_mut ( & mut store) [ 0 ] = 42 ;
1119+ assert_eq ! ( mem. data( & store) [ 0 ] , 42 ) ;
1120+
1121+ assert_eq ! ( mem. grow( & mut store, 1 ) ?, 1 , "memory grows from 1 page to 2" ) ;
1122+ assert_eq ! ( mem. size( & store) , 2 , "memory now 2 pages" ) ;
1123+ assert_eq ! ( mem. data( & store) [ PAGE_SIZE + 1 ] , 0 , "grown memory zeroed" ) ;
1124+ mem. data_mut ( & mut store) [ PAGE_SIZE + 1 ] = 24 ;
1125+ assert_eq ! ( mem. data( & store) [ PAGE_SIZE + 1 ] , 24 , "grown memory mutated" ) ;
1126+
1127+ Ok ( ( ) )
1128+ }
1129+
11141130 #[ test]
11151131 fn memory_grow ( ) -> Result < ( ) > {
1116- const PAGE_SIZE : usize = wasmtime_environ:: Memory :: DEFAULT_PAGE_SIZE as usize ;
1117-
11181132 let mut store = Store :: < ( ) > :: default ( ) ;
11191133 let module = Module :: new (
11201134 store. engine ( ) ,
@@ -1127,14 +1141,7 @@ mod tests {
11271141 let instance = Instance :: new ( & mut store, & module, & [ ] ) ?;
11281142 let m = instance. get_memory ( & mut store, "m" ) . unwrap ( ) ;
11291143
1130- assert_eq ! ( m. data( & store) [ 0 ] , 0 ) ;
1131- m. data_mut ( & mut store) [ 0 ] = 42 ;
1132- assert_eq ! ( m. data( & mut store) [ 0 ] , 42 ) ;
1133-
1134- assert_eq ! ( m. grow( & mut store, 1 ) ?, 1 , "memory grows from 1 page to 2" ) ;
1135- assert_eq ! ( m. size( & mut store) , 2 , "memory now 2 pages" ) ;
1136- m. data_mut ( & mut store) [ PAGE_SIZE ] = 24 ;
1137- assert_eq ! ( m. data( & mut store) [ PAGE_SIZE ] , 24 ) ;
1144+ check_mem_grow_behavior ( & mut store, & m) ?;
11381145
11391146 Ok ( ( ) )
11401147 }
@@ -1143,17 +1150,16 @@ mod tests {
11431150 // the pagemap_scan instance reuse on Linux.
11441151 #[ test]
11451152 fn memory_grow_pagemap ( ) -> Result < ( ) > {
1146- const PAGE_SIZE : usize = wasmtime_environ:: Memory :: DEFAULT_PAGE_SIZE as usize ;
1147-
11481153 let mut config = Config :: new ( ) ;
11491154 let mut pool = PoolingAllocationConfig :: new ( ) ;
1155+ pool. max_unused_warm_slots ( 100 ) ;
11501156 pool. max_memory_size ( PAGE_SIZE * 2 ) ;
11511157 pool. linear_memory_keep_resident ( PAGE_SIZE * 2 ) ;
1158+ pool. table_keep_resident ( 20000 * size_of :: < * const ( ) > ( ) ) ;
11521159 config. allocation_strategy ( pool) ;
1153- config. memory_reservation ( ( PAGE_SIZE * 3 ) as u64 ) ;
1160+ config. memory_reservation ( ( PAGE_SIZE * 2 ) as u64 ) ;
11541161
11551162 let engine = Engine :: new ( & config) ?;
1156- let mut store = Store :: < ( ) > :: new ( & engine, ( ) ) ;
11571163 let module = Module :: new (
11581164 & engine,
11591165 r#"
@@ -1162,18 +1168,28 @@ mod tests {
11621168 )
11631169 "# ,
11641170 ) ?;
1165- let instance = Instance :: new ( & mut store, & module, & [ ] ) ?;
1166- let m = instance. get_memory ( & mut store, "m" ) . unwrap ( ) ;
11671171
1168- assert_eq ! ( m. data( & store) [ 0 ] , 0 ) ;
1169- m. data_mut ( & mut store) [ 0 ] = 42 ;
1170- assert_eq ! ( m. data( & mut store) [ 0 ] , 42 ) ;
1172+ {
1173+ let mut store = Store :: < ( ) > :: new ( & engine, ( ) ) ;
1174+ let instance = Instance :: new ( & mut store, & module, & [ ] ) ?;
1175+ let m = instance. get_memory ( & mut store, "m" ) . unwrap ( ) ;
11711176
1172- assert_eq ! ( m. grow( & mut store, 1 ) ?, 1 , "memory grows from 1 page to 2" ) ;
1173- assert_eq ! ( m. size( & mut store) , 2 , "memory now 2 pages" ) ;
1174- assert_eq ! ( m. data( & mut store) [ PAGE_SIZE ] , 0 , "grown memory is zeroed" ) ;
1175- m. data_mut ( & mut store) [ PAGE_SIZE ] = 24 ;
1176- assert_eq ! ( m. data( & mut store) [ PAGE_SIZE ] , 24 , "grown memory is mutated" ) ;
1177+ println ! ( "first store mem: {:x?}" , m. data( & store) as * const _) ;
1178+ check_mem_grow_behavior ( & mut store, & m) ?;
1179+ println ! ( "first store after grow: {:x?}" , m. data( & store) as * const _) ;
1180+ }
1181+
1182+ // Drop first store and create a new one, which should reuse the
1183+ // previous store's backing memory in the pool.
1184+ {
1185+ let mut store = Store :: < ( ) > :: new ( & engine, ( ) ) ;
1186+ let instance = Instance :: new ( & mut store, & module, & [ ] ) ?;
1187+ let m = instance. get_memory ( & mut store, "m" ) . unwrap ( ) ;
1188+
1189+ println ! ( "second store mem: {:x?}" , m. data( & store) as * const _) ;
1190+ check_mem_grow_behavior ( & mut store, & m) ?;
1191+ println ! ( "second store after grow: {:x?}" , m. data( & store) as * const _) ;
1192+ }
11771193
11781194 Ok ( ( ) )
11791195 }
0 commit comments