]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/global_heap.rs
580390c19530edcdaa0ef9b54ee60c0b240acd53
[rust.git] / src / libstd / rt / global_heap.rs
1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use libc::{c_void, c_char, size_t, uintptr_t, free, malloc, realloc};
12 use managed::raw::{BoxHeaderRepr, BoxRepr};
13 use unstable::intrinsics::TyDesc;
14 use sys::size_of;
15
16 extern {
17     #[rust_stack]
18     fn abort();
19 }
20
21 #[inline]
22 fn get_box_size(body_size: uint, body_align: uint) -> uint {
23     let header_size = size_of::<BoxHeaderRepr>();
24     // FIXME (#2699): This alignment calculation is suspicious. Is it right?
25     let total_size = align_to(header_size, body_align) + body_size;
26     total_size
27 }
28
29 // Rounds |size| to the nearest |alignment|. Invariant: |alignment| is a power
30 // of two.
31 #[inline]
32 fn align_to(size: uint, align: uint) -> uint {
33     assert!(align != 0);
34     (size + align - 1) & !(align - 1)
35 }
36
37 /// A wrapper around libc::malloc, aborting on out-of-memory
38 #[inline]
39 pub unsafe fn malloc_raw(size: uint) -> *c_void {
40     let p = malloc(size as size_t);
41     if p.is_null() {
42         // we need a non-allocating way to print an error here
43         abort();
44     }
45     p
46 }
47
48 /// A wrapper around libc::realloc, aborting on out-of-memory
49 #[inline]
50 pub unsafe fn realloc_raw(ptr: *mut c_void, size: uint) -> *mut c_void {
51     let p = realloc(ptr, size as size_t);
52     if p.is_null() {
53         // we need a non-allocating way to print an error here
54         abort();
55     }
56     p
57 }
58
59 /// The allocator for unique pointers without contained managed pointers.
60 #[cfg(not(test))]
61 #[lang="exchange_malloc"]
62 #[inline]
63 pub unsafe fn exchange_malloc(size: uintptr_t) -> *c_char {
64     malloc_raw(size as uint) as *c_char
65 }
66
67 // FIXME: #7496
68 #[cfg(not(test))]
69 #[lang="closure_exchange_malloc"]
70 #[inline]
71 pub unsafe fn closure_exchange_malloc_(td: *c_char, size: uintptr_t) -> *c_char {
72     closure_exchange_malloc(td, size)
73 }
74
75 #[inline]
76 pub unsafe fn closure_exchange_malloc(td: *c_char, size: uintptr_t) -> *c_char {
77     let td = td as *TyDesc;
78     let size = size as uint;
79
80     assert!(td.is_not_null());
81
82     let total_size = get_box_size(size, (*td).align);
83     let p = malloc_raw(total_size as uint);
84
85     let box: *mut BoxRepr = p as *mut BoxRepr;
86     (*box).header.type_desc = td;
87
88     box as *c_char
89 }
90
91 // NB: Calls to free CANNOT be allowed to fail, as throwing an exception from
92 // inside a landing pad may corrupt the state of the exception handler.
93 #[cfg(not(test))]
94 #[lang="exchange_free"]
95 #[inline]
96 pub unsafe fn exchange_free_(ptr: *c_char) {
97     exchange_free(ptr)
98 }
99
100 #[inline]
101 pub unsafe fn exchange_free(ptr: *c_char) {
102     free(ptr as *c_void);
103 }