Skip to content

Commit f168c12

Browse files
committed
auto merge of #18293 : thestinger/rust/heap, r=cmr
2 parents 80e5fe1 + a9e8510 commit f168c12

File tree

7 files changed

+61
-103
lines changed

7 files changed

+61
-103
lines changed

src/liballoc/heap.rs

+49-40
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

src/liballoc/lib.rs

+2-6
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

src/liballoc/libc_heap.rs

-48
This file was deleted.

src/librustrt/c_str.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ fn main() {
7171
7272
*/
7373

74-
use alloc::libc_heap::malloc_raw;
7574
use collections::string::String;
7675
use collections::hash;
7776
use core::fmt;
@@ -101,7 +100,8 @@ impl Clone for CString {
101100
/// with C's allocator API, rather than the usual shallow clone.
102101
fn clone(&self) -> CString {
103102
let len = self.len() + 1;
104-
let buf = unsafe { malloc_raw(len) } as *mut libc::c_char;
103+
let buf = unsafe { libc::malloc(len as libc::size_t) } as *mut libc::c_char;
104+
if buf.is_null() { fail!("out of memory") }
105105
unsafe { ptr::copy_nonoverlapping_memory(buf, self.buf, len); }
106106
CString { buf: buf as *const libc::c_char, owns_buffer_: true }
107107
}
@@ -393,7 +393,8 @@ impl<'a> ToCStr for &'a [u8] {
393393

394394
unsafe fn to_c_str_unchecked(&self) -> CString {
395395
let self_len = self.len();
396-
let buf = malloc_raw(self_len + 1);
396+
let buf = libc::malloc(self_len as libc::size_t + 1) as *mut u8;
397+
if buf.is_null() { fail!("out of memory") }
397398

398399
ptr::copy_memory(buf, self.as_ptr(), self_len);
399400
*buf.offset(self_len as int) = 0;

src/librustrt/mutex.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ mod imp {
516516

517517
#[cfg(windows)]
518518
mod imp {
519-
use alloc::libc_heap::malloc_raw;
519+
use alloc::heap;
520520
use core::atomic;
521521
use core::ptr;
522522
use libc::{HANDLE, BOOL, LPSECURITY_ATTRIBUTES, c_void, DWORD, LPCSTR};
@@ -607,7 +607,7 @@ mod imp {
607607
}
608608

609609
pub unsafe fn init_lock() -> uint {
610-
let block = malloc_raw(CRIT_SECTION_SIZE as uint) as *mut c_void;
610+
let block = heap::allocate(CRIT_SECTION_SIZE, 8) as *mut c_void;
611611
InitializeCriticalSectionAndSpinCount(block, SPIN_COUNT);
612612
return block as uint;
613613
}
@@ -619,7 +619,7 @@ mod imp {
619619

620620
pub unsafe fn free_lock(h: uint) {
621621
DeleteCriticalSection(h as LPCRITICAL_SECTION);
622-
libc::free(h as *mut c_void);
622+
heap::deallocate(h as *mut u8, CRIT_SECTION_SIZE, 8);
623623
}
624624

625625
pub unsafe fn free_cond(h: uint) {

src/libstd/c_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ mod tests {
165165
use super::CVec;
166166
use libc;
167167
use ptr;
168-
use rt::libc_heap::malloc_raw;
169168

170169
fn malloc(n: uint) -> CVec<u8> {
171170
unsafe {
172-
let mem = malloc_raw(n);
171+
let mem = libc::malloc(n as libc::size_t);
172+
if mem.is_null() { fail!("out of memory") }
173173

174174
CVec::new_with_dtor(mem as *mut u8, n,
175175
proc() { libc::free(mem as *mut libc::c_void); })

src/libstd/rt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub use self::util::{default_sched_threads, min_stack, running_on_valgrind};
6262

6363
// Reexport functionality from librustrt and other crates underneath the
6464
// standard library which work together to create the entire runtime.
65-
pub use alloc::{heap, libc_heap};
65+
pub use alloc::heap;
6666
pub use rustrt::{task, local, mutex, exclusive, stack, args, rtio, thread};
6767
pub use rustrt::{Stdio, Stdout, Stderr, begin_unwind, begin_unwind_fmt};
6868
pub use rustrt::{bookkeeping, at_exit, unwind, DEFAULT_ERROR_CODE, Runtime};

0 commit comments

Comments
 (0)