@@ -4,6 +4,7 @@ use super::node::Removed;
44use super :: { Comparator , Forest , MAX_PATH , Node , NodeData , NodePool , slice_insert, slice_shift} ;
55use core:: borrow:: Borrow ;
66use core:: marker:: PhantomData ;
7+ use wasmtime_core:: error:: OutOfMemory ;
78
89#[ cfg( test) ]
910use core:: fmt;
@@ -260,11 +261,16 @@ impl<F: Forest> Path<F> {
260261 /// The current position must be the correct insertion location for the key.
261262 /// This function does not check for duplicate keys. Use `find` or similar for that.
262263 /// Returns the new root node.
263- pub fn insert ( & mut self , key : F :: Key , value : F :: Value , pool : & mut NodePool < F > ) -> Node {
264+ pub fn insert (
265+ & mut self ,
266+ key : F :: Key ,
267+ value : F :: Value ,
268+ pool : & mut NodePool < F > ,
269+ ) -> Result < Node , OutOfMemory > {
264270 if !self . try_leaf_insert ( key, value, pool) {
265- self . split_and_insert ( key, value, pool) ;
271+ self . split_and_insert ( key, value, pool) ? ;
266272 }
267- self . node [ 0 ]
273+ Ok ( self . node [ 0 ] )
268274 }
269275
270276 /// Try to insert `key, value` at the current position, but fail and return false if the leaf
@@ -282,7 +288,12 @@ impl<F: Forest> Path<F> {
282288
283289 /// Split the current leaf node and then insert `key, value`.
284290 /// This should only be used if `try_leaf_insert()` fails.
285- fn split_and_insert ( & mut self , mut key : F :: Key , value : F :: Value , pool : & mut NodePool < F > ) {
291+ fn split_and_insert (
292+ & mut self ,
293+ mut key : F :: Key ,
294+ value : F :: Value ,
295+ pool : & mut NodePool < F > ,
296+ ) -> Result < ( ) , OutOfMemory > {
286297 let orig_root = self . node [ 0 ] ;
287298
288299 // Loop invariant: We need to split the node at `level` and then retry a failed insertion.
@@ -294,7 +305,7 @@ impl<F: Forest> Path<F> {
294305 let mut node = self . node [ level] ;
295306 let mut entry = self . entry [ level] . into ( ) ;
296307 split = pool[ node] . split ( entry) ;
297- let rhs_node = pool. alloc_node ( split. rhs_data ) ;
308+ let rhs_node = pool. alloc_node ( split. rhs_data ) ? ;
298309
299310 // Should the path be moved to the new RHS node?
300311 // Prefer the smaller node if we're right in the middle.
@@ -347,18 +358,19 @@ impl<F: Forest> Path<F> {
347358 if node == rhs_node {
348359 self . entry [ level - 1 ] += 1 ;
349360 }
350- return ;
361+ return Ok ( ( ) ) ;
351362 }
352363 }
353364 }
354365
355366 // If we get here we have split the original root node and need to add an extra level.
356367 let rhs_node = ins_node. expect ( "empty path" ) ;
357- let root = pool. alloc_node ( NodeData :: inner ( orig_root, key, rhs_node) ) ;
368+ let root = pool. alloc_node ( NodeData :: inner ( orig_root, key, rhs_node) ) ? ;
358369 let entry = if self . node [ 0 ] == rhs_node { 1 } else { 0 } ;
359370 self . size += 1 ;
360371 slice_insert ( & mut self . node [ 0 ..self . size ] , 0 , root) ;
361372 slice_insert ( & mut self . entry [ 0 ..self . size ] , 0 , entry) ;
373+ Ok ( ( ) )
362374 }
363375
364376 /// Remove the key-value pair at the current position and advance the path to the next
@@ -699,6 +711,8 @@ impl<F: Forest> fmt::Display for Path<F> {
699711
700712#[ cfg( test) ]
701713mod tests {
714+ use wasmtime_core:: alloc:: PanicOnOom ;
715+
702716 use super :: * ;
703717 use core:: cmp:: Ordering ;
704718
@@ -731,7 +745,7 @@ mod tests {
731745 fn search_single_leaf ( ) {
732746 // Testing Path::new() for trees with a single leaf node.
733747 let mut pool = NodePool :: < TF > :: new ( ) ;
734- let root = pool. alloc_node ( NodeData :: leaf ( 10 , 'a' ) ) ;
748+ let root = pool. alloc_node ( NodeData :: leaf ( 10 , 'a' ) ) . panic_on_oom ( ) ;
735749 let mut p = Path :: default ( ) ;
736750 let comp = TC ( ) ;
737751
@@ -784,9 +798,11 @@ mod tests {
784798 fn search_single_inner ( ) {
785799 // Testing Path::new() for trees with a single inner node and two leaves.
786800 let mut pool = NodePool :: < TF > :: new ( ) ;
787- let leaf1 = pool. alloc_node ( NodeData :: leaf ( 10 , 'a' ) ) ;
788- let leaf2 = pool. alloc_node ( NodeData :: leaf ( 20 , 'b' ) ) ;
789- let root = pool. alloc_node ( NodeData :: inner ( leaf1, 20 , leaf2) ) ;
801+ let leaf1 = pool. alloc_node ( NodeData :: leaf ( 10 , 'a' ) ) . panic_on_oom ( ) ;
802+ let leaf2 = pool. alloc_node ( NodeData :: leaf ( 20 , 'b' ) ) . panic_on_oom ( ) ;
803+ let root = pool
804+ . alloc_node ( NodeData :: inner ( leaf1, 20 , leaf2) )
805+ . panic_on_oom ( ) ;
790806 let mut p = Path :: default ( ) ;
791807 let comp = TC ( ) ;
792808
0 commit comments