Skip to content

Commit

Permalink
feat(bindings/c): add free op
Browse files Browse the repository at this point in the history
* Add opendal_operator_free
* Add opendal_operator_free to test

Signed-off-by: Ji-Xinyou <[email protected]>
  • Loading branch information
xyjixyjixyji committed Apr 9, 2023
1 parent c23e1c5 commit eb17f7d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
7 changes: 6 additions & 1 deletion bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ extern "C" {
*/
opendal_operator_ptr opendal_operator_new(const char *scheme);

/*
Free the allocated operator pointed by [`opendal_operator_ptr`]
*/
void opendal_operator_free(opendal_operator_ptr op_ptr);

/*
Write the data into the path blockingly by operator, returns the error code OPENDAL_OK
if succeeds, others otherwise
Expand Down Expand Up @@ -195,7 +200,7 @@ struct opendal_result_read opendal_operator_blocking_read(opendal_operator_ptr o
const char *path);

/*
Frees the heap memory used by the [`Bytes`]
Frees the heap memory used by the [`opendal_bytes`]
*/
void opendal_bytes_free(const struct opendal_bytes *vec);

Expand Down
10 changes: 10 additions & 0 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ pub unsafe extern "C" fn opendal_operator_new(scheme: *const c_char) -> opendal_
opendal_operator_ptr::from(op)
}

/// Free the allocated operator pointed by [`opendal_operator_ptr`]
#[no_mangle]
pub extern "C" fn opendal_operator_free(op_ptr: opendal_operator_ptr) {
if op_ptr.is_null() {
return;
}
let _ = unsafe { Box::from_raw(op_ptr.get_ref_mut()) };
// dropped
}

/// Write the data into the path blockingly by operator, returns the error code OPENDAL_OK
/// if succeeds, others otherwise
///
Expand Down
15 changes: 14 additions & 1 deletion bindings/c/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,23 @@ impl opendal_operator_ptr {
}
}

/// Returns whether this points to NULL
pub(crate) fn is_null(&self) -> bool {
self.ptr.is_null()
}

/// Returns a reference to the underlying [`BlockingOperator`]
pub(crate) fn get_ref(&self) -> &od::BlockingOperator {
unsafe { &*(self.ptr) }
}

/// Returns a mutable reference to the underlying [`od::BlockingOperator`].
/// Note that this should be only used when the operator is being freed
#[allow(clippy::mut_from_ref)]
pub(crate) fn get_ref_mut(&self) -> &mut od::BlockingOperator {
let ptr_mut = self.ptr as *mut od::BlockingOperator;
unsafe { &mut (*ptr_mut) }
}
}

#[allow(clippy::from_over_into)]
Expand Down Expand Up @@ -91,7 +104,7 @@ impl Into<bytes::Bytes> for opendal_bytes {
}
}

/// Frees the heap memory used by the [`Bytes`]
/// Frees the heap memory used by the [`opendal_bytes`]
#[no_mangle]
pub extern "C" fn opendal_bytes_free(vec: *const opendal_bytes) {
unsafe {
Expand Down
12 changes: 11 additions & 1 deletion bindings/c/tests/basicio.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@
#include "opendal.h"
#include "stdio.h"

// Tests the basic IO operations work as expected
//
// Asserts:
// * A valid ptr is given
// * The blocking write operation is successful
// * The blocking read operation is successful and works as expected
void test_operator_rw(opendal_operator_ptr ptr) {
// have to be valid ptr
assert(ptr);
Expand Down Expand Up @@ -49,11 +55,15 @@ void test_operator_rw(opendal_operator_ptr ptr) {
}

int main(int argc, char *argv[]) {
// test memory operator
// construct the memory operator
char scheme1[] = "memory";
opendal_operator_ptr p1 = opendal_operator_new(scheme1);
assert(p1);

test_operator_rw(p1);

// free the operator
opendal_operator_free(p1);

return 0;
}

0 comments on commit eb17f7d

Please sign in to comment.