Skip to content

Commit 7da24ea

Browse files
committed
hashmap: port to the new allocator API
1 parent 032510b commit 7da24ea

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/libcollections/hashmap.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ use std::result::{Ok, Err};
3030
use std::slice::ImmutableVector;
3131

3232
mod table {
33-
extern crate libc;
34-
3533
use std::clone::Clone;
3634
use std::cmp;
3735
use std::cmp::Eq;
@@ -42,10 +40,10 @@ mod table {
4240
use std::prelude::Drop;
4341
use std::ptr;
4442
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};
4845
use std::iter::{Iterator, range_step_inclusive};
46+
use std::rt::heap::{allocate, deallocate};
4947

5048
static EMPTY_BUCKET: u64 = 0u64;
5149

@@ -185,10 +183,6 @@ mod table {
185183
assert_eq!(round_up_to_next(5, 4), 8);
186184
}
187185

188-
fn has_alignment(n: uint, alignment: uint) -> bool {
189-
round_up_to_next(n, alignment) == n
190-
}
191-
192186
// Returns a tuple of (minimum required malloc alignment, hash_offset,
193187
// key_offset, val_offset, array_size), from the start of a mallocated array.
194188
fn calculate_offsets(
@@ -243,12 +237,7 @@ mod table {
243237
keys_size, min_align_of::< K >(),
244238
vals_size, min_align_of::< V >());
245239

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);
252241

253242
let hashes = buffer.offset(hash_offset as int) as *mut u64;
254243
let keys = buffer.offset(keys_offset as int) as *mut K;
@@ -418,7 +407,7 @@ mod table {
418407
// modified to no longer assume this.
419408
#[test]
420409
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>())
422411
}
423412

424413
pub struct Entries<'a, K, V> {
@@ -560,8 +549,15 @@ mod table {
560549

561550
assert_eq!(self.size, 0);
562551

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+
563559
unsafe {
564-
libc::free(self.hashes as *mut libc::c_void);
560+
deallocate(self.hashes as *mut u8, size, align);
565561
// Remember how everything was allocated out of one buffer
566562
// during initialization? We only need one call to free here.
567563
}

0 commit comments

Comments
 (0)