libcore: De-export ptr, send_map, and task::local_data

This commit is contained in:
Patrick Walton 2012-09-26 17:47:29 -07:00
parent c91821d356
commit cd79e1d1b2
3 changed files with 51 additions and 82 deletions

View File

@ -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};
@ -49,11 +28,11 @@ 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))
}
@ -61,7 +40,7 @@ pure fn mut_addr_of<T>(val: T) -> *mut T {
/// 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
}
@ -69,7 +48,7 @@ fn offset<T>(ptr: *T, count: uint) -> *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
}
@ -77,19 +56,19 @@ fn const_offset<T>(ptr: *const T, count: uint) -> *const 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; }
@ -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
@ -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);
}
@ -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);
}
@ -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) }
}
@ -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) }
}
@ -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) }
}
@ -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;
}
@ -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};
@ -285,7 +264,7 @@ fn test() {
}
#[test]
fn test_position() {
pub fn test_position() {
use str::as_c_str;
use libc::c_char;
@ -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";
@ -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();

View File

@ -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;
@ -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,
@ -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(),
@ -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);
@ -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;
@ -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);
@ -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);
@ -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();
@ -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);
@ -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);

View File

@ -16,12 +16,6 @@ magic.
*/
export LocalDataKey;
export local_data_pop;
export local_data_get;
export local_data_set;
export local_data_modify;
use local_data_priv::{
local_pop,
local_get,
@ -43,13 +37,13 @@ use local_data_priv::{
*
* These two cases aside, the interface is safe.
*/
type LocalDataKey<T: Owned> = &fn(+v: @T);
pub type LocalDataKey<T: Owned> = &fn(+v: @T);
/**
* Remove a task-local data value from the table, returning the
* reference that was originally created to insert it.
*/
unsafe fn local_data_pop<T: Owned>(
pub unsafe fn local_data_pop<T: Owned>(
key: LocalDataKey<T>) -> Option<@T> {
local_pop(rt::rust_get_task(), key)
@ -58,7 +52,7 @@ unsafe fn local_data_pop<T: Owned>(
* Retrieve a task-local data value. It will also be kept alive in the
* table until explicitly removed.
*/
unsafe fn local_data_get<T: Owned>(
pub unsafe fn local_data_get<T: Owned>(
key: LocalDataKey<T>) -> Option<@T> {
local_get(rt::rust_get_task(), key)
@ -67,7 +61,7 @@ unsafe fn local_data_get<T: Owned>(
* Store a value in task-local data. If this key already has a value,
* that value is overwritten (and its destructor is run).
*/
unsafe fn local_data_set<T: Owned>(
pub unsafe fn local_data_set<T: Owned>(
key: LocalDataKey<T>, +data: @T) {
local_set(rt::rust_get_task(), key, data)
@ -76,7 +70,7 @@ unsafe fn local_data_set<T: Owned>(
* Modify a task-local data value. If the function returns 'None', the
* data is removed (and its reference dropped).
*/
unsafe fn local_data_modify<T: Owned>(
pub unsafe fn local_data_modify<T: Owned>(
key: LocalDataKey<T>,
modify_fn: fn(Option<@T>) -> Option<@T>) {
@ -84,7 +78,7 @@ unsafe fn local_data_modify<T: Owned>(
}
#[test]
fn test_tls_multitask() unsafe {
pub fn test_tls_multitask() unsafe {
fn my_key(+_x: @~str) { }
local_data_set(my_key, @~"parent data");
do task::spawn unsafe {
@ -100,7 +94,7 @@ fn test_tls_multitask() unsafe {
}
#[test]
fn test_tls_overwrite() unsafe {
pub fn test_tls_overwrite() unsafe {
fn my_key(+_x: @~str) { }
local_data_set(my_key, @~"first data");
local_data_set(my_key, @~"next data"); // Shouldn't leak.
@ -108,7 +102,7 @@ fn test_tls_overwrite() unsafe {
}
#[test]
fn test_tls_pop() unsafe {
pub fn test_tls_pop() unsafe {
fn my_key(+_x: @~str) { }
local_data_set(my_key, @~"weasel");
assert *(local_data_pop(my_key).get()) == ~"weasel";
@ -117,7 +111,7 @@ fn test_tls_pop() unsafe {
}
#[test]
fn test_tls_modify() unsafe {
pub fn test_tls_modify() unsafe {
fn my_key(+_x: @~str) { }
local_data_modify(my_key, |data| {
match data {
@ -136,7 +130,7 @@ fn test_tls_modify() unsafe {
}
#[test]
fn test_tls_crust_automorestack_memorial_bug() unsafe {
pub fn test_tls_crust_automorestack_memorial_bug() unsafe {
// This might result in a stack-canary clobber if the runtime fails to set
// sp_limit to 0 when calling the cleanup extern - it might automatically
// jump over to the rust stack, which causes next_c_sp to get recorded as
@ -149,7 +143,7 @@ fn test_tls_crust_automorestack_memorial_bug() unsafe {
}
#[test]
fn test_tls_multiple_types() unsafe {
pub fn test_tls_multiple_types() unsafe {
fn str_key(+_x: @~str) { }
fn box_key(+_x: @@()) { }
fn int_key(+_x: @int) { }
@ -161,7 +155,7 @@ fn test_tls_multiple_types() unsafe {
}
#[test]
fn test_tls_overwrite_multiple_types() {
pub fn test_tls_overwrite_multiple_types() {
fn str_key(+_x: @~str) { }
fn box_key(+_x: @@()) { }
fn int_key(+_x: @int) { }
@ -177,7 +171,7 @@ fn test_tls_overwrite_multiple_types() {
#[test]
#[should_fail]
#[ignore(cfg(windows))]
fn test_tls_cleanup_on_failure() unsafe {
pub fn test_tls_cleanup_on_failure() unsafe {
fn str_key(+_x: @~str) { }
fn box_key(+_x: @@()) { }
fn int_key(+_x: @int) { }