Skip to content

Commit a90e0d7

Browse files
committed
rollup merge of #19534: scialex/liballoc-cfg
Adds the ability to use a custom allocator heap by passing either --cfg external_crate and --extern external=<allocator_crate_name> or --cfg external_funcs and defining the allocator functions prefixed by 'rust_' somewhere. This is useful for many applications including OS/embedded development, and allocator development and testing.
2 parents b8eaf7b + 8723568 commit a90e0d7

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

src/liballoc/heap.rs

+55-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,59 @@ const MIN_ALIGN: uint = 8;
123123
target_arch = "x86_64"))]
124124
const MIN_ALIGN: uint = 16;
125125

126-
#[cfg(jemalloc)]
126+
#[cfg(external_funcs)]
127+
mod imp {
128+
extern {
129+
fn rust_allocate(size: uint, align: uint) -> *mut u8;
130+
fn rust_deallocate(ptr: *mut u8, old_size: uint, align: uint);
131+
fn rust_reallocate(ptr: *mut u8, old_size: uint, size: uint, align: uint) -> *mut u8;
132+
fn rust_reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint,
133+
align: uint) -> uint;
134+
fn rust_usable_size(size: uint, align: uint) -> uint;
135+
fn rust_stats_print();
136+
}
137+
138+
#[inline]
139+
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
140+
rust_allocate(size, align)
141+
}
142+
143+
#[inline]
144+
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint,
145+
align: uint) -> uint {
146+
rust_reallocate_inplace(ptr, old_size, size, align)
147+
}
148+
149+
#[inline]
150+
pub unsafe fn deallocate(ptr: *mut u8, old_size: uint, align: uint) {
151+
rust_deallocate(ptr, old_size, align)
152+
}
153+
154+
#[inline]
155+
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: uint, size: uint,
156+
align: uint) -> uint {
157+
rust_reallocate_inplace(ptr, old_size, size, align)
158+
}
159+
160+
#[inline]
161+
pub fn usable_size(size: uint, align: uint) -> uint {
162+
unsafe { rust_usable_size(size, align) }
163+
}
164+
165+
#[inline]
166+
pub fn stats_print() {
167+
unsafe { rust_stats_print() }
168+
}
169+
}
170+
171+
#[cfg(external_crate)]
172+
mod imp {
173+
extern crate external;
174+
pub use self::external::{allocate, deallocate, reallocate_inplace, reallocate};
175+
pub use self::external::{usable_size, stats_print};
176+
}
177+
178+
#[cfg(all(not(external_funcs), not(external_crate), jemalloc))]
127179
mod imp {
128180
use core::option::{None, Option};
129181
use core::ptr::{null_mut, null};
@@ -199,7 +251,7 @@ mod imp {
199251
}
200252
}
201253

202-
#[cfg(all(not(jemalloc), unix))]
254+
#[cfg(all(not(external_funcs), not(external_crate), not(jemalloc), unix))]
203255
mod imp {
204256
use core::cmp;
205257
use core::ptr;
@@ -260,7 +312,7 @@ mod imp {
260312
pub fn stats_print() {}
261313
}
262314

263-
#[cfg(all(not(jemalloc), windows))]
315+
#[cfg(all(not(external_funcs), not(external_crate), not(jemalloc), windows))]
264316
mod imp {
265317
use libc::{c_void, size_t};
266318
use libc;

0 commit comments

Comments
 (0)