about summary refs log tree commit diff
path: root/src/libstd/rt
diff options
context:
space:
mode:
authorDaniel Micay <danielmicay@gmail.com>2014-04-02 17:23:52 -0400
committerAlex Crichton <alex@alexcrichton.com>2014-04-03 13:43:35 -0700
commit898669c4e203ae91e2048fb6c0f8591c867bccc6 (patch)
tree00dd1385c83fb35dff75055f477bcc795732d6f8 /src/libstd/rt
parent1ac8b34ccd02965886d3ca0bd83115c7f7b73729 (diff)
downloadrust-898669c4e203ae91e2048fb6c0f8591c867bccc6.tar.gz
rust-898669c4e203ae91e2048fb6c0f8591c867bccc6.zip
fix Option<~ZeroSizeType>
1778b6361627c5894bf75ffecf427573af02d390 provided the guarantee of no
`exchange_free` calls for ~ZeroSizeType, so a sentinel can now be used
without overhead.

Closes #11998
Diffstat (limited to 'src/libstd/rt')
-rw-r--r--src/libstd/rt/global_heap.rs13
1 files changed, 11 insertions, 2 deletions
diff --git a/src/libstd/rt/global_heap.rs b/src/libstd/rt/global_heap.rs
index 23b23cf8af0..3917857e1af 100644
--- a/src/libstd/rt/global_heap.rs
+++ b/src/libstd/rt/global_heap.rs
@@ -64,12 +64,21 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
     }
 }
 
+// The compiler never calls `exchange_free` on ~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.
+static EMPTY: () = ();
+
 /// The allocator for unique pointers without contained managed pointers.
 #[cfg(not(test))]
 #[lang="exchange_malloc"]
 #[inline]
-pub unsafe fn exchange_malloc(size: uint) -> *u8 {
-    malloc_raw(size) as *u8
+pub unsafe fn exchange_malloc(size: uint) -> *mut u8 {
+    if size == 0 {
+        &EMPTY as *() as *mut u8
+    } else {
+        malloc_raw(size)
+    }
 }
 
 // FIXME: #7496