From a71066154b76eedaabb79f9dc480edd4996c02fb Mon Sep 17 00:00:00 2001 From: Luke Imhoff Date: Mon, 29 Jul 2019 08:32:58 -0500 Subject: [PATCH] Make stats::hooks compatible with wasm-bindgen wasm-bindgen does not allow `&'static` or any other lifetimes for exported bindings (https://github.com/rustwasm/wasm-bindgen/issues/1187), so needed to switch tags to owned Strings. --- liblumen_alloc/src/stats/hooks.rs | 12 ++++++------ liblumen_alloc/src/stats_alloc.rs | 20 ++++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/liblumen_alloc/src/stats/hooks.rs b/liblumen_alloc/src/stats/hooks.rs index 8e6f4fa3c..527f2c2a4 100644 --- a/liblumen_alloc/src/stats/hooks.rs +++ b/liblumen_alloc/src/stats/hooks.rs @@ -5,11 +5,11 @@ mod internal { #[wasm_bindgen] extern "C" { #[wasm_bindgen(js_namespace = LumenStatsAlloc)] - pub fn on_alloc(tag: &'static str, size: usize, align: usize, ptr: *mut u8); + pub fn on_alloc(tag: String, size: usize, align: usize, ptr: *mut u8); #[wasm_bindgen(js_namespace = LumenStatsAlloc)] pub fn on_realloc( - tag: &'static str, + tag: String, old_size: usize, new_size: usize, align: usize, @@ -18,7 +18,7 @@ mod internal { ); #[wasm_bindgen(js_namespace = LumenStatsAlloc)] - pub fn on_dealloc(tag: &'static str, size: usize, align: usize, ptr: *mut u8); + pub fn on_dealloc(tag: String, size: usize, align: usize, ptr: *mut u8); } } @@ -26,10 +26,10 @@ mod internal { mod internal { #![allow(unused)] - pub fn on_alloc(_tag: &'static str, _size: usize, _align: usize, _ptr: *mut u8) {} + pub fn on_alloc(_tag: String, _size: usize, _align: usize, _ptr: *mut u8) {} pub fn on_realloc( - _tag: &'static str, + _tag: String, _old_size: usize, _new_size: usize, _align: usize, @@ -38,7 +38,7 @@ mod internal { ) { } - pub fn on_dealloc(_tag: &'static str, _size: usize, _align: usize, _ptr: *mut u8) {} + pub fn on_dealloc(_tag: String, _size: usize, _align: usize, _ptr: *mut u8) {} } pub use internal::*; diff --git a/liblumen_alloc/src/stats_alloc.rs b/liblumen_alloc/src/stats_alloc.rs index 43bb71c35..36e8e3add 100644 --- a/liblumen_alloc/src/stats_alloc.rs +++ b/liblumen_alloc/src/stats_alloc.rs @@ -121,7 +121,7 @@ unsafe impl Alloc for StatsAlloc let align = layout.align(); match self.allocator.alloc(layout) { err @ Err(_) => { - hooks::on_alloc(self.tag, size, align, ptr::null_mut()); + hooks::on_alloc(self.tag.to_owned(), size, align, ptr::null_mut()); err } Ok(result) => { @@ -130,7 +130,7 @@ unsafe impl Alloc for StatsAlloc let mut h = self.histogram.write(); h.add(size as u64).ok(); drop(h); - hooks::on_alloc(self.tag, size, align, result.as_ptr()); + hooks::on_alloc(self.tag.to_owned(), size, align, result.as_ptr()); Ok(result) } } @@ -149,7 +149,7 @@ unsafe impl Alloc for StatsAlloc match self.allocator.realloc(ptr, layout, new_size) { err @ Err(_) => { hooks::on_realloc( - self.tag, + self.tag.to_owned(), old_size, new_size, align, @@ -171,7 +171,7 @@ unsafe impl Alloc for StatsAlloc h.add(new_size as u64).ok(); drop(h); hooks::on_realloc( - self.tag, + self.tag.to_owned(), old_size, new_size, align, @@ -191,7 +191,7 @@ unsafe impl Alloc for StatsAlloc self.allocator.dealloc(ptr, layout); self.dealloc_calls.fetch_add(1, Ordering::SeqCst); self.total_bytes_freed.fetch_add(size, Ordering::SeqCst); - hooks::on_dealloc(self.tag, size, align, freed); + hooks::on_dealloc(self.tag.to_owned(), size, align, freed); } } @@ -202,7 +202,7 @@ unsafe impl GlobalAlloc for Stat let align = layout.align(); let result = self.allocator.alloc(layout); if result.is_null() { - hooks::on_alloc(self.tag, size, align, result); + hooks::on_alloc(self.tag.to_owned(), size, align, result); return result; } @@ -211,7 +211,7 @@ unsafe impl GlobalAlloc for Stat let mut h = self.histogram.write(); h.add(size as u64).ok(); drop(h); - hooks::on_alloc(self.tag, size, align, result); + hooks::on_alloc(self.tag.to_owned(), size, align, result); result } @@ -223,7 +223,7 @@ unsafe impl GlobalAlloc for Stat let result = self.allocator.realloc(ptr, layout, new_size); if result.is_null() { - hooks::on_realloc(self.tag, old_size, new_size, align, ptr, result); + hooks::on_realloc(self.tag.to_owned(), old_size, new_size, align, ptr, result); return result; } @@ -238,7 +238,7 @@ unsafe impl GlobalAlloc for Stat let mut h = self.histogram.write(); h.add(new_size as u64).ok(); drop(h); - hooks::on_realloc(self.tag, old_size, new_size, align, ptr, result); + hooks::on_realloc(self.tag.to_owned(), old_size, new_size, align, ptr, result); result } @@ -250,6 +250,6 @@ unsafe impl GlobalAlloc for Stat self.allocator.dealloc(ptr, layout); self.dealloc_calls.fetch_add(1, Ordering::SeqCst); self.total_bytes_freed.fetch_add(size, Ordering::SeqCst); - hooks::on_dealloc(self.tag, size, align, ptr); + hooks::on_dealloc(self.tag.to_owned(), size, align, ptr); } }