about summary refs log tree commit diff
path: root/src/liballoc
diff options
context:
space:
mode:
authorDaniel Micay <danielmicay@gmail.com>2014-09-15 15:37:01 -0400
committerDaniel Micay <danielmicay@gmail.com>2014-09-15 16:48:20 -0400
commit84b37374bf31b463c36f70123e870f52db19c740 (patch)
tree73d698cc2a76498a748c5e186f3005ce19517009 /src/liballoc
parent396f9106173b941e880a0f311bc2d1429a68c983 (diff)
downloadrust-84b37374bf31b463c36f70123e870f52db19c740.tar.gz
rust-84b37374bf31b463c36f70123e870f52db19c740.zip
heap: optimize EMPTY to avoid relocations
Sized deallocation makes it pointless to provide an address that never
overlaps with pointers returned by an allocator. Code can branch on the
capacity of the allocation instead of a comparison with this sentinel.

This improves the situation in #8859, and the remaining issues are only
from the logging API, which should be disabled by default in optimized
release builds anyway along with debug assertions. The remaining issues
are part of #17081.

Closes #8859
Diffstat (limited to 'src/liballoc')
-rw-r--r--src/liballoc/heap.rs11
1 files changed, 6 insertions, 5 deletions
diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs
index a2c406be5a9..b6c255e7930 100644
--- a/src/liballoc/heap.rs
+++ b/src/liballoc/heap.rs
@@ -86,10 +86,11 @@ pub fn stats_print() {
     imp::stats_print();
 }
 
-// The compiler never calls `exchange_free` on Box<ZeroSizeType>, so zero-size
-// allocations can point to this `static`. It would be incorrect to use a null
-// pointer, due to enums assuming types like unique pointers are never null.
-pub static mut EMPTY: uint = 12345;
+/// An arbitrary non-null address to represent zero-size allocations.
+///
+/// This preserves the non-null invariant for types like `Box<T>`. The address may overlap with
+/// non-zero-size memory allocations.
+pub static EMPTY: *mut () = 0x1 as *mut ();
 
 /// The allocator for unique pointers.
 #[cfg(not(test))]
@@ -97,7 +98,7 @@ pub static mut EMPTY: uint = 12345;
 #[inline]
 unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
     if size == 0 {
-        &EMPTY as *const uint as *mut u8
+        EMPTY as *mut u8
     } else {
         allocate(size, align)
     }