@@ -29,11 +29,8 @@ pub struct AddressAllocator {
29
29
// tree will represent a memory location and can have two states either
30
30
// `NodeState::Free` or `NodeState::Allocated`.
31
31
interval_tree : IntervalTree ,
32
- // Available free memory space in the address space.
33
- // NOTE that due to fragmentations, |available| may not give the actual
34
- // available (contiguous) memory block that can be allocated in next
35
- // allocate() call.
36
- available : usize ,
32
+ // Used memory space in the address space.
33
+ used : usize ,
37
34
}
38
35
39
36
impl AddressAllocator {
@@ -48,7 +45,7 @@ impl AddressAllocator {
48
45
Ok ( AddressAllocator {
49
46
address_space : aux_range,
50
47
interval_tree : IntervalTree :: new ( aux_range) ,
51
- available : aux_range . len ( ) as usize ,
48
+ used : 0 ,
52
49
} )
53
50
}
54
51
@@ -70,24 +67,24 @@ impl AddressAllocator {
70
67
) -> Result < RangeInclusive > {
71
68
let constraint = Constraint :: new ( size, alignment, policy) ?;
72
69
let allocated = self . interval_tree . allocate ( constraint) ?;
73
- self . available - = allocated. len ( ) as usize ;
70
+ self . used + = allocated. len ( ) as usize ;
74
71
Ok ( allocated)
75
72
}
76
73
77
74
/// Deletes the specified memory slot or returns `ResourceNotAvailable` if
78
75
/// the node was not allocated before.
79
76
pub fn free ( & mut self , key : & RangeInclusive ) -> Result < ( ) > {
80
77
self . interval_tree . free ( key) ?;
81
- self . available + = key. len ( ) as usize ;
78
+ self . used - = key. len ( ) as usize ;
82
79
Ok ( ( ) )
83
80
}
84
81
85
- /// Returns the available memory size in this allocator.
82
+ /// Returns the used memory size in this allocator.
86
83
/// NOTE that due to fragmentations, it's not guaranteed that the next
87
- /// allocate() call after querying the available memory can succeed with
88
- /// allocating those available memories and it may still return OOM.
89
- pub fn available ( & self ) -> usize {
90
- self . available
84
+ /// allocate() call after querying the used memory can succeed with
85
+ /// allocating all unused memories and it may still return OOM.
86
+ pub fn used ( & self ) -> usize {
87
+ self . used
91
88
}
92
89
}
93
90
@@ -176,27 +173,27 @@ mod tests {
176
173
#[ test]
177
174
fn test_allocate_with_alignment_first_ok ( ) {
178
175
let mut pool = AddressAllocator :: new ( 0x1000 , 0x1000 ) . unwrap ( ) ;
179
- assert_eq ! ( pool. available ( ) , 0x1000 ) ;
176
+ assert_eq ! ( pool. used ( ) , 0 ) ;
180
177
// Allocate 0x110
181
178
assert_eq ! (
182
179
pool. allocate( 0x110 , 0x100 , AllocPolicy :: FirstMatch )
183
180
. unwrap( ) ,
184
181
RangeInclusive :: new( 0x1000 , 0x110F ) . unwrap( )
185
182
) ;
186
- assert_eq ! ( pool. available ( ) , 0x1000 - 0x110 ) ;
183
+ assert_eq ! ( pool. used ( ) , 0x110 ) ;
187
184
// Allocate 0x100
188
185
assert_eq ! (
189
186
pool. allocate( 0x100 , 0x100 , AllocPolicy :: FirstMatch )
190
187
. unwrap( ) ,
191
188
RangeInclusive :: new( 0x1200 , 0x12FF ) . unwrap( )
192
189
) ;
193
- assert_eq ! ( pool. available ( ) , 0x1000 - 0x110 - 0x100 ) ;
190
+ assert_eq ! ( pool. used ( ) , 0x110 + 0x100 ) ;
194
191
// Allocate 0x10
195
192
assert_eq ! (
196
193
pool. allocate( 0x10 , 0x100 , AllocPolicy :: FirstMatch ) . unwrap( ) ,
197
194
RangeInclusive :: new( 0x1300 , 0x130F ) . unwrap( )
198
195
) ;
199
- assert_eq ! ( pool. available ( ) , 0x1000 - 0x110 - 0x100 - 0x10 ) ;
196
+ assert_eq ! ( pool. used ( ) , 0x110 + 0x100 + 0x10 ) ;
200
197
}
201
198
202
199
#[ test]
@@ -255,24 +252,24 @@ mod tests {
255
252
#[ test]
256
253
fn test_tree_allocate_address_free_and_realloc ( ) {
257
254
let mut pool = AddressAllocator :: new ( 0x1000 , 0x1000 ) . unwrap ( ) ;
258
- assert_eq ! ( pool. available ( ) , 0x1000 ) ;
255
+ assert_eq ! ( pool. used ( ) , 0 ) ;
259
256
// Allocate 0x800
260
257
assert_eq ! (
261
258
pool. allocate( 0x800 , 0x100 , AllocPolicy :: FirstMatch )
262
259
. unwrap( ) ,
263
260
RangeInclusive :: new( 0x1000 , 0x17FF ) . unwrap( )
264
261
) ;
265
- assert_eq ! ( pool. available ( ) , 0x1000 - 0x800 ) ;
262
+ assert_eq ! ( pool. used ( ) , 0x800 ) ;
266
263
// Free 0x800
267
264
let _ = pool. free ( & RangeInclusive :: new ( 0x1000 , 0x17FF ) . unwrap ( ) ) ;
268
- assert_eq ! ( pool. available ( ) , 0x1000 ) ;
265
+ assert_eq ! ( pool. used ( ) , 0 ) ;
269
266
// Allocate 0x800 again
270
267
assert_eq ! (
271
268
pool. allocate( 0x800 , 0x100 , AllocPolicy :: FirstMatch )
272
269
. unwrap( ) ,
273
270
RangeInclusive :: new( 0x1000 , 0x17FF ) . unwrap( )
274
271
) ;
275
- assert_eq ! ( pool. available ( ) , 0x1000 - 0x800 ) ;
272
+ assert_eq ! ( pool. used ( ) , 0x800 ) ;
276
273
}
277
274
278
275
#[ test]
0 commit comments