@@ -30,8 +30,6 @@ use std::result::{Ok, Err};
30
30
use std:: slice:: ImmutableVector ;
31
31
32
32
mod table {
33
- extern crate libc;
34
-
35
33
use std:: clone:: Clone ;
36
34
use std:: cmp;
37
35
use std:: cmp:: Eq ;
@@ -42,10 +40,10 @@ mod table {
42
40
use std:: prelude:: Drop ;
43
41
use std:: ptr;
44
42
use std:: ptr:: RawPtr ;
45
- use std:: rt:: libc_heap;
46
- use std:: intrinsics:: { size_of, min_align_of, transmute} ;
47
- use std:: intrinsics:: { move_val_init, set_memory} ;
43
+ use std:: mem:: { min_align_of, size_of} ;
44
+ use std:: intrinsics:: { move_val_init, set_memory, transmute} ;
48
45
use std:: iter:: { Iterator , range_step_inclusive} ;
46
+ use std:: rt:: heap:: { allocate, deallocate} ;
49
47
50
48
static EMPTY_BUCKET : u64 = 0u64 ;
51
49
@@ -185,10 +183,6 @@ mod table {
185
183
assert_eq ! ( round_up_to_next( 5 , 4 ) , 8 ) ;
186
184
}
187
185
188
- fn has_alignment ( n : uint , alignment : uint ) -> bool {
189
- round_up_to_next ( n, alignment) == n
190
- }
191
-
192
186
// Returns a tuple of (minimum required malloc alignment, hash_offset,
193
187
// key_offset, val_offset, array_size), from the start of a mallocated array.
194
188
fn calculate_offsets (
@@ -243,12 +237,7 @@ mod table {
243
237
keys_size, min_align_of :: < K > ( ) ,
244
238
vals_size, min_align_of :: < V > ( ) ) ;
245
239
246
- let buffer = libc_heap:: malloc_raw ( size) as * mut u8 ;
247
-
248
- // FIXME #13094: If malloc was not at as aligned as we expected,
249
- // our offset calculations are just plain wrong. We could support
250
- // any alignment if we switched from `malloc` to `posix_memalign`.
251
- assert ! ( has_alignment( buffer as uint, malloc_alignment) ) ;
240
+ let buffer = allocate ( size, malloc_alignment) ;
252
241
253
242
let hashes = buffer. offset ( hash_offset as int ) as * mut u64 ;
254
243
let keys = buffer. offset ( keys_offset as int ) as * mut K ;
@@ -418,7 +407,7 @@ mod table {
418
407
// modified to no longer assume this.
419
408
#[ test]
420
409
fn can_alias_safehash_as_u64 ( ) {
421
- unsafe { assert_eq ! ( size_of:: <SafeHash >( ) , size_of:: <u64 >( ) ) } ;
410
+ assert_eq ! ( size_of:: <SafeHash >( ) , size_of:: <u64 >( ) )
422
411
}
423
412
424
413
pub struct Entries < ' a , K , V > {
@@ -560,8 +549,15 @@ mod table {
560
549
561
550
assert_eq ! ( self . size, 0 ) ;
562
551
552
+ let hashes_size = self . capacity * size_of :: < u64 > ( ) ;
553
+ let keys_size = self . capacity * size_of :: < K > ( ) ;
554
+ let vals_size = self . capacity * size_of :: < V > ( ) ;
555
+ let ( align, _, _, _, size) = calculate_offsets ( hashes_size, min_align_of :: < u64 > ( ) ,
556
+ keys_size, min_align_of :: < K > ( ) ,
557
+ vals_size, min_align_of :: < V > ( ) ) ;
558
+
563
559
unsafe {
564
- libc :: free ( self . hashes as * mut libc :: c_void ) ;
560
+ deallocate ( self . hashes as * mut u8 , size , align ) ;
565
561
// Remember how everything was allocated out of one buffer
566
562
// during initialization? We only need one call to free here.
567
563
}
0 commit comments