Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump version due to changes to allocator API. #265

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rquickjs"
version = "0.4.3"
version = "0.5.0"
authors = ["Mees Delzenne <[email protected]>", "K. <[email protected]>"]
edition = "2021"
rust-version = "1.65"
Expand All @@ -12,11 +12,11 @@ categories = ["api-bindings"]
repository = "https://github.com/DelSkayn/rquickjs.git"

[dependencies.rquickjs-core]
version = "0.4.3"
version = "0.5.0"
path = "core"

[dependencies.rquickjs-macro]
version = "0.4.3"
version = "0.5.0"
path = "macro"
optional = true

Expand Down
4 changes: 2 additions & 2 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rquickjs-core"
version = "0.4.3"
version = "0.5.0"
authors = ["Mees Delzenne <[email protected]>", "K. <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down Expand Up @@ -31,7 +31,7 @@ version = "0.4"
optional = true

[dependencies.rquickjs-sys]
version = "0.4.3"
version = "0.5.0"
path = "../sys"

[dependencies.dlopen]
Expand Down
29 changes: 26 additions & 3 deletions core/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,42 @@ pub use rust::RustAllocator;
pub type RawMemPtr = *mut u8;

/// The allocator interface
///
/// # Safety
/// Failure to implement this trait correctly will result in undefined behavior.
/// - `alloc` must return a either a null pointer or a pointer to an available region of memory
/// atleast `size` bytes and aligned to the size of `usize`.
/// - `realloc` must either return a null pointer or return a pointer to an available region of
/// memory atleast `new_size` bytes and aligned to the size of `usize`.
/// - `usable_size` must return the amount of available memory for any allocation allocated with
/// this allocator.
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "allocator")))]
pub trait Allocator {
pub unsafe trait Allocator {
/// Allocate new memory
unsafe fn alloc(&mut self, size: usize) -> RawMemPtr;
///
///
fn alloc(&mut self, size: usize) -> RawMemPtr;

/// De-allocate previously allocated memory
///
/// # Safety
/// Caller must ensure that the pointer that is being deallocated was allocated by the same
/// Allocator instance.
unsafe fn dealloc(&mut self, ptr: RawMemPtr);

/// Re-allocate previously allocated memory
///
/// # Safety
/// Caller must ensure that the pointer points to an allocation that was allocated by the same
/// Allocator instance.
unsafe fn realloc(&mut self, ptr: RawMemPtr, new_size: usize) -> RawMemPtr;

/// Get usable size of allocated memory region
fn usable_size(ptr: RawMemPtr) -> usize
///
/// # Safety
/// Caller must ensure that the pointer handed to this function points to an allocation
/// allocated by the same allocator instance.
unsafe fn usable_size(ptr: RawMemPtr) -> usize
where
Self: Sized;
}
Expand Down
20 changes: 11 additions & 9 deletions core/src/allocator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ fn round_size(size: usize) -> usize {
/// The allocator which uses Rust global allocator
pub struct RustAllocator;

impl Allocator for RustAllocator {
unsafe fn alloc(&mut self, size: usize) -> RawMemPtr {
unsafe impl Allocator for RustAllocator {
fn alloc(&mut self, size: usize) -> RawMemPtr {
let size = round_size(size);
let alloc_size = size + HEADER_SIZE;
let layout = if let Ok(layout) = Layout::from_size_align(alloc_size, ALLOC_ALIGN) {
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Allocator for RustAllocator {
}

#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn usable_size(ptr: RawMemPtr) -> usize {
unsafe fn usable_size(ptr: RawMemPtr) -> usize {
let ptr = unsafe { ptr.sub(HEADER_SIZE) };
let header = unsafe { &*(ptr as *const Header) };
header.size
Expand All @@ -115,11 +115,13 @@ mod test {

struct TestAllocator;

impl Allocator for TestAllocator {
unsafe fn alloc(&mut self, size: usize) -> crate::allocator::RawMemPtr {
let res = RustAllocator.alloc(size);
ALLOC_SIZE.fetch_add(RustAllocator::usable_size(res), Ordering::AcqRel);
res
unsafe impl Allocator for TestAllocator {
fn alloc(&mut self, size: usize) -> crate::allocator::RawMemPtr {
unsafe {
let res = RustAllocator.alloc(size);
ALLOC_SIZE.fetch_add(RustAllocator::usable_size(res), Ordering::AcqRel);
res
}
}

unsafe fn dealloc(&mut self, ptr: crate::allocator::RawMemPtr) {
Expand All @@ -143,7 +145,7 @@ mod test {
res
}

fn usable_size(ptr: crate::allocator::RawMemPtr) -> usize
unsafe fn usable_size(ptr: crate::allocator::RawMemPtr) -> usize
where
Self: Sized,
{
Expand Down
5 changes: 3 additions & 2 deletions core/src/runtime/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,12 @@ impl AsyncRuntime {
lock.drop_pending();

loop {
match lock.runtime.execute_pending_job().map_err(|e| {
let pending = lock.runtime.execute_pending_job().map_err(|e| {
let ptr = NonNull::new(e)
.expect("executing pending job returned a null context on error");
AsyncJobException(unsafe { AsyncContext::from_raw(ptr, self.clone()) })
}) {
});
match pending {
Err(e) => {
// SAFETY: Runtime is already locked so creating a context is safe.
let ctx = unsafe { Ctx::from_ptr(e.0 .0.ctx.as_ptr()) };
Expand Down
5 changes: 3 additions & 2 deletions core/src/runtime/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,11 @@ impl RawRuntime {
_rt: *mut qjs::JSRuntime,
opaque: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
let should_interrupt = match panic::catch_unwind(move || {
let catch_unwind = panic::catch_unwind(move || {
let opaque = &mut *(opaque as *mut Opaque);
opaque.interrupt_handler.as_mut().expect("handler is set")()
}) {
});
let should_interrupt = match catch_unwind {
Ok(should_interrupt) => should_interrupt,
Err(panic) => {
let opaque = &mut *(opaque as *mut Opaque);
Expand Down
6 changes: 3 additions & 3 deletions macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rquickjs-macro"
version = "0.4.3"
version = "0.5.0"
authors = ["K. <[email protected]>", "Mees Delzenne <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down Expand Up @@ -37,15 +37,15 @@ optional = true

[dependencies.rquickjs-core]
path = "../core"
version = "0.4.3"
version = "0.5.0"
features = ["loader"]

[dev-dependencies.difference]
version = "2"

[dev-dependencies.rquickjs]
path = ".."
version = "0.4.3"
version = "0.5.0"
features = ["macro", "classes", "properties", "futures","phf"]

[dev-dependencies.async-std]
Expand Down
2 changes: 1 addition & 1 deletion sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rquickjs-sys"
version = "0.4.3"
version = "0.5.0"
authors = ["Mees Delzenne <[email protected]>"]
edition = "2021"
license = "MIT"
Expand Down
Loading