Skip to content

Commit

Permalink
libcore: De-export ptr, send_map, and task::local_data
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed Sep 27, 2012
1 parent c91821d commit cd79e1d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 82 deletions.
69 changes: 24 additions & 45 deletions src/libcore/ptr.rs
@@ -1,26 +1,5 @@
//! Unsafe pointer utility functions

export addr_of;
export to_unsafe_ptr;
export to_const_unsafe_ptr;
export to_mut_unsafe_ptr;
export mut_addr_of;
export offset;
export const_offset;
export mut_offset;
export null;
export mut_null;
export is_null;
export is_not_null;
export memcpy;
export memmove;
export memset;
export to_uint;
export ref_eq;
export buf_len;
export position;
export Ptr;

use cmp::{Eq, Ord};
use libc::{c_void, size_t};

Expand Down Expand Up @@ -49,47 +28,47 @@ extern mod rusti {

/// Get an unsafe pointer to a value
#[inline(always)]
pure fn addr_of<T>(val: T) -> *T { unsafe { rusti::addr_of(val) } }
pub pure fn addr_of<T>(val: T) -> *T { unsafe { rusti::addr_of(val) } }

/// Get an unsafe mut pointer to a value
#[inline(always)]
pure fn mut_addr_of<T>(val: T) -> *mut T {
pub pure fn mut_addr_of<T>(val: T) -> *mut T {
unsafe {
cast::reinterpret_cast(&rusti::addr_of(val))
}
}

/// Calculate the offset from a pointer
#[inline(always)]
fn offset<T>(ptr: *T, count: uint) -> *T {
pub fn offset<T>(ptr: *T, count: uint) -> *T {
unsafe {
(ptr as uint + count * sys::size_of::<T>()) as *T
}
}

/// Calculate the offset from a const pointer
#[inline(always)]
fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
pub fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
unsafe {
(ptr as uint + count * sys::size_of::<T>()) as *T
}
}

/// Calculate the offset from a mut pointer
#[inline(always)]
fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
pub fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
(ptr as uint + count * sys::size_of::<T>()) as *mut T
}

/// Return the offset of the first null pointer in `buf`.
#[inline(always)]
unsafe fn buf_len<T>(buf: **T) -> uint {
pub unsafe fn buf_len<T>(buf: **T) -> uint {
position(buf, |i| i == null())
}

/// Return the first offset `i` such that `f(buf[i]) == true`.
#[inline(always)]
unsafe fn position<T>(buf: *T, f: fn(T) -> bool) -> uint {
pub unsafe fn position<T>(buf: *T, f: fn(T) -> bool) -> uint {
let mut i = 0u;
loop {
if f(*offset(buf, i)) { return i; }
Expand All @@ -99,17 +78,17 @@ unsafe fn position<T>(buf: *T, f: fn(T) -> bool) -> uint {

/// Create an unsafe null pointer
#[inline(always)]
pure fn null<T>() -> *T { unsafe { cast::reinterpret_cast(&0u) } }
pub pure fn null<T>() -> *T { unsafe { cast::reinterpret_cast(&0u) } }

/// Create an unsafe mutable null pointer
#[inline(always)]
pure fn mut_null<T>() -> *mut T { unsafe { cast::reinterpret_cast(&0u) } }
pub pure fn mut_null<T>() -> *mut T { unsafe { cast::reinterpret_cast(&0u) } }

/// Returns true if the pointer is equal to the null pointer.
pure fn is_null<T>(ptr: *const T) -> bool { ptr == null() }
pub pure fn is_null<T>(ptr: *const T) -> bool { ptr == null() }

/// Returns true if the pointer is not equal to the null pointer.
pure fn is_not_null<T>(ptr: *const T) -> bool { !is_null(ptr) }
pub pure fn is_not_null<T>(ptr: *const T) -> bool { !is_null(ptr) }

/**
* Copies data from one location to another
Expand All @@ -118,7 +97,7 @@ pure fn is_not_null<T>(ptr: *const T) -> bool { !is_null(ptr) }
* and destination may not overlap.
*/
#[inline(always)]
unsafe fn memcpy<T>(dst: *mut T, src: *const T, count: uint) {
pub unsafe fn memcpy<T>(dst: *mut T, src: *const T, count: uint) {
let n = count * sys::size_of::<T>();
libc_::memcpy(dst as *mut c_void, src as *c_void, n as size_t);
}
Expand All @@ -130,13 +109,13 @@ unsafe fn memcpy<T>(dst: *mut T, src: *const T, count: uint) {
* and destination may overlap.
*/
#[inline(always)]
unsafe fn memmove<T>(dst: *mut T, src: *const T, count: uint) {
pub unsafe fn memmove<T>(dst: *mut T, src: *const T, count: uint) {
let n = count * sys::size_of::<T>();
libc_::memmove(dst as *mut c_void, src as *c_void, n as size_t);
}

#[inline(always)]
unsafe fn memset<T>(dst: *mut T, c: int, count: uint) {
pub unsafe fn memset<T>(dst: *mut T, c: int, count: uint) {
let n = count * sys::size_of::<T>();
libc_::memset(dst as *mut c_void, c as libc::c_int, n as size_t);
}
Expand All @@ -148,7 +127,7 @@ unsafe fn memset<T>(dst: *mut T, c: int, count: uint) {
reinterpret_cast.
*/
#[inline(always)]
fn to_unsafe_ptr<T>(thing: &T) -> *T {
pub fn to_unsafe_ptr<T>(thing: &T) -> *T {
unsafe { cast::reinterpret_cast(&thing) }
}

Expand All @@ -158,7 +137,7 @@ fn to_unsafe_ptr<T>(thing: &T) -> *T {
reinterpret_cast.
*/
#[inline(always)]
fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
pub fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
unsafe { cast::reinterpret_cast(&thing) }
}

Expand All @@ -168,7 +147,7 @@ fn to_const_unsafe_ptr<T>(thing: &const T) -> *const T {
reinterpret_cast.
*/
#[inline(always)]
fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
pub fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
unsafe { cast::reinterpret_cast(&thing) }
}

Expand All @@ -180,17 +159,17 @@ fn to_mut_unsafe_ptr<T>(thing: &mut T) -> *mut T {
(I couldn't think of a cutesy name for this one.)
*/
#[inline(always)]
fn to_uint<T>(thing: &T) -> uint unsafe {
pub fn to_uint<T>(thing: &T) -> uint unsafe {
cast::reinterpret_cast(&thing)
}

/// Determine if two borrowed pointers point to the same thing.
#[inline(always)]
fn ref_eq<T>(thing: &a/T, other: &b/T) -> bool {
pub fn ref_eq<T>(thing: &a/T, other: &b/T) -> bool {
to_uint(thing) == to_uint(other)
}

trait Ptr {
pub trait Ptr {
pure fn is_null() -> bool;
pure fn is_not_null() -> bool;
}
Expand Down Expand Up @@ -253,7 +232,7 @@ impl<T:Ord> &const T : Ord {
}

#[test]
fn test() {
pub fn test() {
unsafe {
type Pair = {mut fst: int, mut snd: int};
let p = {mut fst: 10, mut snd: 20};
Expand Down Expand Up @@ -285,7 +264,7 @@ fn test() {
}

#[test]
fn test_position() {
pub fn test_position() {
use str::as_c_str;
use libc::c_char;

Expand All @@ -298,7 +277,7 @@ fn test_position() {
}

#[test]
fn test_buf_len() {
pub fn test_buf_len() {
let s0 = ~"hello";
let s1 = ~"there";
let s2 = ~"thing";
Expand All @@ -316,7 +295,7 @@ fn test_buf_len() {
}

#[test]
fn test_is_null() {
pub fn test_is_null() {
let p: *int = ptr::null();
assert p.is_null();
assert !p.is_not_null();
Expand Down
32 changes: 14 additions & 18 deletions src/libcore/send_map.rs
Expand Up @@ -12,7 +12,7 @@ use cmp::Eq;
use hash::Hash;
use to_bytes::IterBytes;

trait SendMap<K:Eq Hash, V: Copy> {
pub trait SendMap<K:Eq Hash, V: Copy> {
// FIXME(#3148) ^^^^ once find_ref() works, we can drop V:copy

fn insert(&mut self, +k: K, +v: V) -> bool;
Expand All @@ -31,17 +31,15 @@ trait SendMap<K:Eq Hash, V: Copy> {
}

/// Open addressing with linear probing.
mod linear {
#[legacy_exports];
export LinearMap, linear_map, linear_map_with_capacity, public_methods;

pub mod linear {
const initial_capacity: uint = 32u; // 2^5

struct Bucket<K:Eq Hash,V> {
hash: uint,
key: K,
value: V,
}
struct LinearMap<K:Eq Hash,V> {
pub struct LinearMap<K:Eq Hash,V> {
k0: u64,
k1: u64,
resize_at: uint,
Expand All @@ -60,11 +58,11 @@ mod linear {
((capacity as float) * 3. / 4.) as uint
}

fn LinearMap<K:Eq Hash,V>() -> LinearMap<K,V> {
pub fn LinearMap<K:Eq Hash,V>() -> LinearMap<K,V> {
linear_map_with_capacity(32)
}

fn linear_map_with_capacity<K:Eq Hash,V>(
pub fn linear_map_with_capacity<K:Eq Hash,V>(
initial_capacity: uint) -> LinearMap<K,V> {
let r = rand::Rng();
linear_map_with_capacity_and_keys(r.gen_u64(), r.gen_u64(),
Expand Down Expand Up @@ -366,13 +364,11 @@ mod linear {
}

#[test]
mod test {
#[legacy_exports];

pub mod test {
use linear::LinearMap;

#[test]
fn inserts() {
pub fn inserts() {
let mut m = ~LinearMap();
assert m.insert(1, 2);
assert m.insert(2, 4);
Expand All @@ -381,7 +377,7 @@ mod test {
}

#[test]
fn overwrite() {
pub fn overwrite() {
let mut m = ~LinearMap();
assert m.insert(1, 2);
assert m.get(&1) == 2;
Expand All @@ -390,7 +386,7 @@ mod test {
}

#[test]
fn conflicts() {
pub fn conflicts() {
let mut m = linear::linear_map_with_capacity(4);
assert m.insert(1, 2);
assert m.insert(5, 3);
Expand All @@ -401,7 +397,7 @@ mod test {
}

#[test]
fn conflict_remove() {
pub fn conflict_remove() {
let mut m = linear::linear_map_with_capacity(4);
assert m.insert(1, 2);
assert m.insert(5, 3);
Expand All @@ -412,7 +408,7 @@ mod test {
}

#[test]
fn empty() {
pub fn empty() {
let mut m = linear::linear_map_with_capacity(4);
assert m.insert(1, 2);
assert !m.is_empty();
Expand All @@ -421,7 +417,7 @@ mod test {
}

#[test]
fn iterate() {
pub fn iterate() {
let mut m = linear::linear_map_with_capacity(4);
for uint::range(0, 32) |i| {
assert m.insert(i, i*2);
Expand All @@ -435,7 +431,7 @@ mod test {
}

#[test]
fn find_ref() {
pub fn find_ref() {
let mut m = ~LinearMap();
assert m.find_ref(&1).is_none();
m.insert(1, 2);
Expand Down

0 comments on commit cd79e1d

Please sign in to comment.