Skip to content

Commit e45298d

Browse files
committed
---
yaml --- r: 157051 b: refs/heads/try c: f037452 h: refs/heads/master i: 157049: ca45f84 157047: 919a92e v: v3
1 parent 0c6dfca commit e45298d

File tree

102 files changed

+1687
-1108
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+1687
-1108
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 2d27bfaeb6522d386d0a2735cb3f75cc5707314a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: d44ea720fa9dfe062ef06d0eb49a58d4e7e92344
5-
refs/heads/try: b3ed61703cf02d0dd023a23a275f2c5abc83d9b0
5+
refs/heads/try: f037452447f5f46deb26e1c483fe88fb51a19198
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 6601b0501e31d08d3892a2d5a7d8a57ab120bf75

branches/try/src/doc/guide.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,13 +3466,14 @@ for destroying that resource as well. Given that we're discussing pointers
34663466
right now, let's discuss this in the context of memory allocation, though
34673467
it applies to other resources as well.
34683468

3469-
When you allocate heap memory, you need a mechanism to free that memory. Many
3470-
languages let the programmer control the allocation, and then use a garbage
3471-
collector to handle the deallocation. This is a valid, time-tested strategy,
3472-
but it's not without its drawbacks. Because the programmer does not have to
3473-
think as much about deallocation, allocation becomes something commonplace,
3474-
because it's easy. And if you need precise control over when something is
3475-
deallocated, leaving it up to your runtime can make this difficult.
3469+
When you allocate heap memory, you need a mechanism to free that memory. Many
3470+
languages use a garbage collector to handle deallocation. This is a valid,
3471+
time-tested strategy, but it's not without its drawbacks: it adds overhead, and
3472+
can lead to unpredictable pauses in execution. Because the programmer does not
3473+
have to think as much about deallocation, allocation becomes something
3474+
commonplace, leading to more memory usage. And if you need precise control
3475+
over when something is deallocated, leaving it up to your runtime can make this
3476+
difficult.
34763477

34773478
Rust chooses a different path, and that path is called **ownership**. Any
34783479
binding that creates a resource is the **owner** of that resource.
@@ -3498,17 +3499,19 @@ memory. The length of time that the borrower is borrowing the pointer
34983499
from you is called a **lifetime**.
34993500

35003501
If two distinct bindings share a pointer, and the memory that pointer points to
3501-
is immutable, then there are no problems. But if it's mutable, both pointers
3502-
can attempt to write to the memory at the same time, causing a **race
3503-
condition**. Therefore, if someone wants to mutate something that they've
3504-
borrowed from you, you must not have lent out that pointer to anyone else.
3502+
is immutable, then there are no problems. But if it's mutable, the result of
3503+
changing it can vary unpredictably depending on who happens to access it first,
3504+
which is called a **race condition**. To avoid this, if someone wants to mutate
3505+
something that they've borrowed from you, you must not have lent out that
3506+
pointer to anyone else.
35053507

35063508
Rust has a sophisticated system called the **borrow checker** to make sure that
35073509
everyone plays by these rules. At compile time, it verifies that none of these
3508-
rules are broken. If there's no problem, our program compiles successfully, and
3509-
there is no runtime overhead for any of this. The borrow checker works only at
3510-
compile time. If the borrow checker did find a problem, it will report a
3511-
**lifetime error**, and your program will refuse to compile.
3510+
rules are broken. If our program compiles successfully, Rust can guarantee it
3511+
is free of data races and other memory errors, and there is no runtime overhead
3512+
for any of this. The borrow checker works only at compile time. If the borrow
3513+
checker did find a problem, it will report a **lifetime error**, and your
3514+
program will refuse to compile.
35123515

35133516
That's a lot to take in. It's also one of the _most_ important concepts in
35143517
all of Rust. Let's see this syntax in action:
@@ -3877,6 +3880,7 @@ match x {
38773880
If you have a struct, you can destructure it inside of a pattern:
38783881

38793882
```{rust}
3883+
# #![allow(non_shorthand_field_patterns)]
38803884
struct Point {
38813885
x: int,
38823886
y: int,
@@ -3892,6 +3896,7 @@ match origin {
38923896
If we only care about some of the values, we don't have to give them all names:
38933897

38943898
```{rust}
3899+
# #![allow(non_shorthand_field_patterns)]
38953900
struct Point {
38963901
x: int,
38973902
y: int,
@@ -3977,6 +3982,7 @@ You can also define methods that do not take a `self` parameter. Here's a
39773982
pattern that's very common in Rust code:
39783983

39793984
```{rust}
3985+
# #![allow(non_shorthand_field_patterns)]
39803986
struct Circle {
39813987
x: f64,
39823988
y: f64,

branches/try/src/liballoc/heap.rs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
2828
/// size on the platform.
2929
///
3030
/// The `old_size` and `align` parameters are the parameters that were used to
31-
/// create the allocation referenced by `ptr`. The `old_size` parameter may also
32-
/// be the value returned by `usable_size` for the requested size.
31+
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
32+
/// any value in range_inclusive(requested_size, usable_size).
3333
#[inline]
3434
pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
3535
imp::reallocate(ptr, old_size, size, align)
@@ -38,8 +38,8 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint)
3838
/// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of
3939
/// memory in-place.
4040
///
41-
/// Returns true if successful, otherwise false if the allocation was not
42-
/// altered.
41+
/// If the operation succeeds, it returns `usable_size(size, align)` and if it
42+
/// fails (or is a no-op) it returns `usable_size(old_size, align)`.
4343
///
4444
/// Behavior is undefined if the requested size is 0 or the alignment is not a
4545
/// power of 2. The alignment must be no larger than the largest supported page
@@ -49,20 +49,20 @@ pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint)
4949
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
5050
/// any value in range_inclusive(requested_size, usable_size).
5151
#[inline]
52-
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> bool {
52+
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> uint {
5353
imp::reallocate_inplace(ptr, old_size, size, align)
5454
}
5555

5656
/// Deallocates the memory referenced by `ptr`.
5757
///
5858
/// The `ptr` parameter must not be null.
5959
///
60-
/// The `size` and `align` parameters are the parameters that were used to
61-
/// create the allocation referenced by `ptr`. The `size` parameter may also be
62-
/// the value returned by `usable_size` for the requested size.
60+
/// The `old_size` and `align` parameters are the parameters that were used to
61+
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
62+
/// any value in range_inclusive(requested_size, usable_size).
6363
#[inline]
64-
pub unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
65-
imp::deallocate(ptr, size, align)
64+
pub unsafe fn deallocate(ptr: *mut u8, old_size: uint, align: uint) {
65+
imp::deallocate(ptr, old_size, align)
6666
}
6767

6868
/// Returns the usable size of an allocation created with the specified the
@@ -102,8 +102,8 @@ unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
102102
#[cfg(not(test))]
103103
#[lang="exchange_free"]
104104
#[inline]
105-
unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
106-
deallocate(ptr, size, align);
105+
unsafe fn exchange_free(ptr: *mut u8, old_size: uint, align: uint) {
106+
deallocate(ptr, old_size, align);
107107
}
108108

109109
// The minimum alignment guaranteed by the architecture. This value is used to
@@ -112,10 +112,10 @@ unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
112112
#[cfg(any(target_arch = "arm",
113113
target_arch = "mips",
114114
target_arch = "mipsel"))]
115-
static MIN_ALIGN: uint = 8;
115+
const MIN_ALIGN: uint = 8;
116116
#[cfg(any(target_arch = "x86",
117117
target_arch = "x86_64"))]
118-
static MIN_ALIGN: uint = 16;
118+
const MIN_ALIGN: uint = 16;
119119

120120
#[cfg(jemalloc)]
121121
mod imp {
@@ -178,22 +178,16 @@ mod imp {
178178
}
179179

180180
#[inline]
181-
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint,
182-
align: uint) -> bool {
181+
pub unsafe fn reallocate_inplace(ptr: *mut u8, _old_size: uint, size: uint,
182+
align: uint) -> uint {
183183
let flags = align_to_flags(align);
184-
let new_size = je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as uint;
185-
// checking for failure to shrink is tricky
186-
if size < old_size {
187-
usable_size(size, align) == new_size as uint
188-
} else {
189-
new_size >= size
190-
}
184+
je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as uint
191185
}
192186

193187
#[inline]
194-
pub unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
188+
pub unsafe fn deallocate(ptr: *mut u8, old_size: uint, align: uint) {
195189
let flags = align_to_flags(align);
196-
je_sdallocx(ptr as *mut c_void, size as size_t, flags)
190+
je_sdallocx(ptr as *mut c_void, old_size as size_t, flags)
197191
}
198192

199193
#[inline]
@@ -213,8 +207,8 @@ mod imp {
213207
mod imp {
214208
use core::cmp;
215209
use core::ptr;
210+
use core::ptr::RawPtr;
216211
use libc;
217-
use libc_heap;
218212
use super::MIN_ALIGN;
219213

220214
extern {
@@ -226,7 +220,11 @@ mod imp {
226220
#[inline]
227221
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
228222
if align <= MIN_ALIGN {
229-
libc_heap::malloc_raw(size)
223+
let ptr = libc::malloc(size as libc::size_t);
224+
if ptr.is_null() {
225+
::oom();
226+
}
227+
ptr as *mut u8
230228
} else {
231229
let mut out = 0 as *mut libc::c_void;
232230
let ret = posix_memalign(&mut out,
@@ -242,7 +240,11 @@ mod imp {
242240
#[inline]
243241
pub unsafe fn reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8 {
244242
if align <= MIN_ALIGN {
245-
libc_heap::realloc_raw(ptr, size)
243+
let ptr = libc::realloc(ptr as *mut libc::c_void, size as libc::size_t);
244+
if ptr.is_null() {
245+
::oom();
246+
}
247+
ptr as *mut u8
246248
} else {
247249
let new_ptr = allocate(size, align);
248250
ptr::copy_memory(new_ptr, ptr as *const u8, cmp::min(size, old_size));
@@ -252,13 +254,13 @@ mod imp {
252254
}
253255

254256
#[inline]
255-
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, size: uint,
256-
_align: uint) -> bool {
257-
size == old_size
257+
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, _size: uint,
258+
_align: uint) -> uint {
259+
old_size
258260
}
259261

260262
#[inline]
261-
pub unsafe fn deallocate(ptr: *mut u8, _size: uint, _align: uint) {
263+
pub unsafe fn deallocate(ptr: *mut u8, _old_size: uint, _align: uint) {
262264
libc::free(ptr as *mut libc::c_void)
263265
}
264266

@@ -274,7 +276,6 @@ mod imp {
274276
mod imp {
275277
use libc::{c_void, size_t};
276278
use libc;
277-
use libc_heap;
278279
use core::ptr::RawPtr;
279280
use super::MIN_ALIGN;
280281

@@ -288,7 +289,11 @@ mod imp {
288289
#[inline]
289290
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
290291
if align <= MIN_ALIGN {
291-
libc_heap::malloc_raw(size)
292+
let ptr = libc::malloc(size as size_t);
293+
if ptr.is_null() {
294+
::oom();
295+
}
296+
ptr as *mut u8
292297
} else {
293298
let ptr = _aligned_malloc(size as size_t, align as size_t);
294299
if ptr.is_null() {
@@ -301,7 +306,11 @@ mod imp {
301306
#[inline]
302307
pub unsafe fn reallocate(ptr: *mut u8, _old_size: uint, size: uint, align: uint) -> *mut u8 {
303308
if align <= MIN_ALIGN {
304-
libc_heap::realloc_raw(ptr, size)
309+
let ptr = libc::realloc(ptr as *mut c_void, size as size_t);
310+
if ptr.is_null() {
311+
::oom();
312+
}
313+
ptr as *mut u8
305314
} else {
306315
let ptr = _aligned_realloc(ptr as *mut c_void, size as size_t,
307316
align as size_t);
@@ -313,13 +322,13 @@ mod imp {
313322
}
314323

315324
#[inline]
316-
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, size: uint,
317-
_align: uint) -> bool {
318-
size == old_size
325+
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: uint, _size: uint,
326+
_align: uint) -> uint {
327+
old_size
319328
}
320329

321330
#[inline]
322-
pub unsafe fn deallocate(ptr: *mut u8, _size: uint, align: uint) {
331+
pub unsafe fn deallocate(ptr: *mut u8, _old_size: uint, align: uint) {
323332
if align <= MIN_ALIGN {
324333
libc::free(ptr as *mut libc::c_void)
325334
} else {
@@ -348,7 +357,7 @@ mod test {
348357
let ptr = heap::allocate(size, 8);
349358
let ret = heap::reallocate_inplace(ptr, size, size, 8);
350359
heap::deallocate(ptr, size, 8);
351-
assert!(ret);
360+
assert_eq!(ret, heap::usable_size(size, 8));
352361
}
353362
}
354363

branches/try/src/liballoc/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@
5454
//!
5555
//! ## Heap interfaces
5656
//!
57-
//! The [`heap`](heap/index.html) and [`libc_heap`](libc_heap/index.html)
58-
//! modules are the unsafe interfaces to the underlying allocation systems. The
59-
//! `heap` module is considered the default heap, and is not necessarily backed
60-
//! by libc malloc/free. The `libc_heap` module is defined to be wired up to
61-
//! the system malloc/free.
57+
//! The [`heap`](heap/index.html) module defines the low-level interface to the
58+
//! default global allocator. It is not compatible with the libc allocator API.
6259
6360
#![crate_name = "alloc"]
6461
#![experimental]
@@ -90,7 +87,6 @@ pub use boxed as owned;
9087
// Heaps provided for low-level allocation strategies
9188

9289
pub mod heap;
93-
pub mod libc_heap;
9490

9591
// Primitive types using the heaps above
9692

branches/try/src/liballoc/libc_heap.rs

Lines changed: 0 additions & 48 deletions
This file was deleted.

branches/try/src/libcollections/treemap.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ impl<K: Ord, V> TreeMap<K, V> {
434434
/// assert_eq!(vec, vec![("a", 1), ("b", 2), ("c", 3)]);
435435
/// ```
436436
pub fn into_iter(self) -> MoveEntries<K, V> {
437-
let TreeMap { root: root, length: length } = self;
437+
let TreeMap { root, length } = self;
438438
let stk = match root {
439439
None => vec!(),
440440
Some(box tn) => vec!(tn)
@@ -898,11 +898,11 @@ impl<K, V> Iterator<(K, V)> for MoveEntries<K,V> {
898898
fn next(&mut self) -> Option<(K, V)> {
899899
while !self.stack.is_empty() {
900900
let TreeNode {
901-
key: key,
902-
value: value,
903-
left: left,
904-
right: right,
905-
level: level
901+
key,
902+
value,
903+
left,
904+
right,
905+
level,
906906
} = self.stack.pop().unwrap();
907907

908908
match left {

0 commit comments

Comments
 (0)