Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
Make stats::hooks compatible with wasm-bindgen
Browse files Browse the repository at this point in the history
wasm-bindgen does not allow `&'static` or any other lifetimes for
exported bindings
(rustwasm/wasm-bindgen#1187), so needed to
switch tags to owned Strings.
  • Loading branch information
KronicDeth committed Aug 6, 2019
1 parent a132bd1 commit e56cc2a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions liblumen_alloc/src/stats/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,18 +18,18 @@ 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);
}
}

#[cfg(not(target_arch = "wasm32"))]
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,
Expand All @@ -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::*;
20 changes: 10 additions & 10 deletions liblumen_alloc/src/stats_alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ unsafe impl<T: Alloc, H: Histogram + Clone + Default> Alloc for StatsAlloc<T, H>
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) => {
Expand All @@ -130,7 +130,7 @@ unsafe impl<T: Alloc, H: Histogram + Clone + Default> Alloc for StatsAlloc<T, H>
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)
}
}
Expand All @@ -149,7 +149,7 @@ unsafe impl<T: Alloc, H: Histogram + Clone + Default> Alloc for StatsAlloc<T, H>
match self.allocator.realloc(ptr, layout, new_size) {
err @ Err(_) => {
hooks::on_realloc(
self.tag,
self.tag.to_owned(),
old_size,
new_size,
align,
Expand All @@ -171,7 +171,7 @@ unsafe impl<T: Alloc, H: Histogram + Clone + Default> Alloc for StatsAlloc<T, H>
h.add(new_size as u64).ok();
drop(h);
hooks::on_realloc(
self.tag,
self.tag.to_owned(),
old_size,
new_size,
align,
Expand All @@ -191,7 +191,7 @@ unsafe impl<T: Alloc, H: Histogram + Clone + Default> Alloc for StatsAlloc<T, H>
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);
}
}

Expand All @@ -202,7 +202,7 @@ unsafe impl<T: GlobalAlloc, H: Histogram + Clone + Default> 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;
}

Expand All @@ -211,7 +211,7 @@ unsafe impl<T: GlobalAlloc, H: Histogram + Clone + Default> 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
}
Expand All @@ -223,7 +223,7 @@ unsafe impl<T: GlobalAlloc, H: Histogram + Clone + Default> 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;
}

Expand All @@ -238,7 +238,7 @@ unsafe impl<T: GlobalAlloc, H: Histogram + Clone + Default> 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
}
Expand All @@ -250,6 +250,6 @@ unsafe impl<T: GlobalAlloc, H: Histogram + Clone + Default> 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);
}
}

0 comments on commit e56cc2a

Please sign in to comment.