From d0068976f61ca99d77ad4643dd02a2a2cd2e8cc2 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 13 Jul 2018 10:10:00 -0700 Subject: [PATCH] Remove usage of the `try_reserve` nightly feature Now that `GlobalAlloc` is stable no need to use it! --- src/lib.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a43a8d80..ee2d19c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,7 @@ //! this crate and this crate also provides JS bindings through the `JsValue` //! interface. -#![feature(use_extern_macros, wasm_import_module, try_reserve, unsize)] +#![feature(use_extern_macros, wasm_import_module, unsize)] #![cfg_attr(feature = "js_globals", feature(proc_macro, wasm_custom_section))] #![no_std] @@ -661,24 +661,29 @@ pub mod __rt { } if_std! { - use std::prelude::v1::*; + use std::alloc::{System, GlobalAlloc, Layout}; + use std::mem; #[no_mangle] pub extern fn __wbindgen_malloc(size: usize) -> *mut u8 { - use core::mem; - - let mut ret = Vec::new(); - if ret.try_reserve_exact(size).is_err() { - super::throw("invalid malloc request"); + let align = mem::align_of::(); + if let Ok(layout) = Layout::from_size_align(size, align) { + unsafe { + let ptr = System.alloc(layout); + if !ptr.is_null() { + return ptr + } + } } - let ptr = ret.as_mut_ptr(); - mem::forget(ret); - return ptr + + super::throw("invalid malloc request"); } #[no_mangle] pub unsafe extern fn __wbindgen_free(ptr: *mut u8, size: usize) { - drop(Vec::::from_raw_parts(ptr, 0, size)); + let align = mem::align_of::(); + let layout = Layout::from_size_align_unchecked(size, align); + System.dealloc(ptr, layout); } }