From ac43894184ddca0a65b14fd6c07b9e9930da9942 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 28 Apr 2017 16:11:25 +0200 Subject: [PATCH 1/3] Update alloc API to latest nightly --- src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 4791b2c..640c04e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -97,6 +97,15 @@ pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 { }) } +#[doc(hidden)] +#[no_mangle] +pub extern fn __rust_allocate_zeroed(size: usize, align: usize) -> *mut u8 { + let ptr = __rust_allocate(size, align); + if !ptr.is_null() { + ptr::write_bytes(ptr, 0, size); + } +} + /// Rust de-allocation function (c.f. free) #[doc(hidden)] #[no_mangle] From 892e48f5e55390f6d39f3339c433576420b7c8ce Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 28 Apr 2017 16:12:52 +0200 Subject: [PATCH 2/3] Fix return value --- src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lib.rs b/src/lib.rs index 640c04e..9f7cbae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,6 +104,7 @@ pub extern fn __rust_allocate_zeroed(size: usize, align: usize) -> *mut u8 { if !ptr.is_null() { ptr::write_bytes(ptr, 0, size); } + ptr } /// Rust de-allocation function (c.f. free) From d953f725f300519f5c5dec8e8e0013cc72f5bccc Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 28 Apr 2017 16:13:24 +0200 Subject: [PATCH 3/3] write_bytes is unsafe --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9f7cbae..7147522 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -102,7 +102,9 @@ pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 { pub extern fn __rust_allocate_zeroed(size: usize, align: usize) -> *mut u8 { let ptr = __rust_allocate(size, align); if !ptr.is_null() { - ptr::write_bytes(ptr, 0, size); + unsafe { + ptr::write_bytes(ptr, 0, size); + } } ptr }