Skip to content

Commit

Permalink
Use new allocator API (rust-lang/rust#32838)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhoo committed Jul 10, 2017
1 parent 6d5871e commit 0be4124
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "arccstr"
version = "1.0.0"
version = "1.0.1"

description = "Thread-safe, reference-counted null-terminated immutable strings."
readme = "README.md"
Expand Down
17 changes: 11 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@
//!
//! [arc]: struct.ArcCStr.html

#![feature(shared, core_intrinsics, alloc, heap_api, unique, try_from)]
#![feature(shared, core_intrinsics, alloc, allocator_api, unique, try_from)]
extern crate alloc;

#[cfg(feature = "serde")]
extern crate serde;
#[cfg(all(test, feature = "serde"))]
extern crate serde_test;

use alloc::allocator::Alloc;
use std::sync::atomic;
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
use std::borrow;
Expand All @@ -78,7 +79,6 @@ use std::ptr::{self, Shared};
use std::hash::{Hash, Hasher};
use std::{isize, usize};
use std::convert::From;
use alloc::heap;

// Note that much of this code is taken directly from

Expand Down Expand Up @@ -212,8 +212,13 @@ impl ArcCStr {
let aus = size_of::<atomic::AtomicUsize>();
let aual = align_of::<atomic::AtomicUsize>();
let sz = aus + buf.len() + 1;
let aul = alloc::allocator::Layout::from_size_align(sz, aual).unwrap();

let mut s = ptr::Unique::new(heap::allocate(sz, aual));
let mut s = ptr::Unique::new(
alloc::heap::Heap
.alloc(aul)
.expect("could not allocate memory"),
);
let cstr = s.as_ptr().offset(aus as isize);
// initialize the AtomicUsize to 1
{
Expand Down Expand Up @@ -273,11 +278,11 @@ impl ArcCStr {
unsafe fn drop_slow(&mut self) {
atomic::fence(Acquire);
let blen = self.to_bytes_with_nul().len();
heap::deallocate(
self.ptr.as_ptr().offset(0) as *mut _,
let aul = alloc::allocator::Layout::from_size_align(
size_of::<atomic::AtomicUsize>() + blen,
align_of::<atomic::AtomicUsize>(),
)
).unwrap();
alloc::heap::Heap.dealloc(self.ptr.as_ptr().offset(0) as *mut _, aul)
}

#[inline]
Expand Down

0 comments on commit 0be4124

Please sign in to comment.