@@ -40,13 +40,28 @@ impl Alloc
4040 self . mem_size
4141 }
4242
43+ pub fn bytes_used ( & self ) -> usize
44+ {
45+ self . next_idx
46+ }
47+
4348 pub fn bytes_free ( & self ) -> usize
4449 {
4550 assert ! ( self . next_idx <= self . mem_size) ;
4651 self . mem_size - self . next_idx
4752 }
4853
49- // Allocate a block of a given size
54+ /// Shrink the available memory to a smaller size
55+ /// This is primarily used to test the GC
56+ pub fn shrink_to ( & mut self , new_size : usize )
57+ {
58+ assert ! ( self . next_idx <= new_size) ;
59+ self . mem_size = new_size;
60+
61+ // TODO: try to realloc to a smaller size?
62+ }
63+
64+ /// Allocate a block of a given size
5065 fn alloc_bytes ( & mut self , size_bytes : usize ) -> Result < * mut u8 , ( ) >
5166 {
5267 let align_bytes = 8 ;
@@ -64,7 +79,7 @@ impl Alloc
6479 Ok ( unsafe { self . mem_block . add ( obj_pos) } )
6580 }
6681
67- // Allocate a variable-sized table of elements of a given type
82+ /// Allocate a variable-sized table of elements of a given type
6883 pub fn alloc_table < T > ( & mut self , num_elems : usize ) -> Result < * mut [ T ] , ( ) >
6984 {
7085 let num_bytes = num_elems * std:: mem:: size_of :: < T > ( ) ;
@@ -74,7 +89,7 @@ impl Alloc
7489 Ok ( std:: ptr:: slice_from_raw_parts_mut ( p, num_elems) )
7590 }
7691
77- // Allocate a new object of a given type
92+ /// Allocate a new object of a given type
7893 pub fn alloc < T > ( & mut self , obj : T ) -> Result < * mut T , ( ) >
7994 {
8095 let num_bytes = std:: mem:: size_of :: < T > ( ) ;
@@ -88,7 +103,7 @@ impl Alloc
88103 Ok ( p)
89104 }
90105
91- // Allocate a new object with a given number of slots
106+ /// Allocate a new object with a given number of slots
92107 pub fn new_object ( & mut self , class_id : ClassId , num_slots : usize ) -> Result < Value , ( ) >
93108 {
94109 // Allocate the slots for the object
0 commit comments