Skip to content

Commit

Permalink
Fix allocation size not being properly registered
Browse files Browse the repository at this point in the history
  • Loading branch information
DelSkayn committed Jan 29, 2024
1 parent 71335cb commit 59216f2
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions core/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl AllocatorHolder {
A: Allocator,
{
qjs::JSMallocFunctions {
js_malloc: Some(Self::malloc),
js_malloc: Some(Self::malloc::<A>),
js_free: Some(Self::free::<A>),
js_realloc: Some(Self::realloc::<A>),
js_malloc_usable_size: Some(Self::malloc_usable_size::<A>),
Expand All @@ -63,10 +63,13 @@ impl AllocatorHolder {
self.0
}

unsafe extern "C" fn malloc(
unsafe extern "C" fn malloc<A>(
state: *mut qjs::JSMallocState,
size: qjs::size_t,
) -> *mut qjs::c_void {
) -> *mut qjs::c_void
where
A: Allocator,
{
if size == 0 {
return null_mut();
}
Expand All @@ -88,8 +91,12 @@ impl AllocatorHolder {
return null_mut();
}

let size = A::usable_size(res);

println!("ALLOC: {}", size);

state.malloc_count += 1;
state.malloc_size += size;
state.malloc_size += qjs::size_t::try_from(size).expect(qjs::SIZE_T_ERROR);

res as *mut qjs::c_void
}
Expand All @@ -113,6 +120,7 @@ impl AllocatorHolder {
allocator.dealloc(ptr as _);

state.malloc_size -= qjs::size_t::try_from(size).expect(qjs::SIZE_T_ERROR);
println!("FREE: {}", size);
}

unsafe extern "C" fn realloc<A>(
Expand Down Expand Up @@ -144,6 +152,8 @@ impl AllocatorHolder {
state.malloc_count += 1;
state.malloc_size += size;

println!("ALLOC: {}", size);

return res as *mut qjs::c_void;
} else if size == 0 {
let old_size = A::usable_size(ptr as RawMemPtr);
Expand All @@ -153,6 +163,8 @@ impl AllocatorHolder {
state.malloc_count -= 1;
state.malloc_size -= qjs::size_t::try_from(old_size).expect(qjs::SIZE_T_ERROR);

println!("FREE: {}", size);

return null_mut();
}

Expand All @@ -171,6 +183,9 @@ impl AllocatorHolder {

state.malloc_size = new_malloc_size;

println!("FREE: {}", old_size);
println!("ALLOC: {}", size);

ptr
}

Expand Down

0 comments on commit 59216f2

Please sign in to comment.