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

fix: Update allocation and deallocation of memory #22

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
checks:
if: 'false'
name: Tests + Lint
runs-on: ubuntu-latest
runs-on: ubuntu-latest-4-cores
steps:
- uses: actions/checkout@v3

Expand Down
11 changes: 3 additions & 8 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use quickjs_sys::{JSContext, JSValue};
use std::mem;
use std::os::raw::c_int;
use std::slice;
use std::alloc::{System, Layout};

use convert_case::{Case, Casing};

Expand Down Expand Up @@ -287,17 +288,11 @@ pub unsafe extern "C" fn run_e(pointer: *mut u8, size: i32, ident: i32) {

#[no_mangle]
pub unsafe extern "C" fn allocate(size: i32) -> *const u8 {
let mut buffer = Vec::with_capacity(size as usize);

let pointer = buffer.as_mut_ptr();

mem::forget(buffer);

pointer as *const u8
System.alloc(Layout.from_size_align(size, 1))
}

/// # Safety
#[no_mangle]
pub unsafe extern "C" fn deallocate(pointer: *mut u8, size: i32) {
drop(Vec::from_raw_parts(pointer, size as usize, size as usize))
System.dealloc(pointer, Layout.from_size_align(size, 1))
}