Skip to content

Commit

Permalink
pass pointer to opendal_bytes_free
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun committed Oct 8, 2024
1 parent 32dba0c commit 70cef32
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion bindings/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main()
printf("\n");

/* the opendal_bytes read is heap allocated, please free it */
opendal_bytes_free(read_bytes);
opendal_bytes_free(&read_bytes);

/* the operator_ptr is also heap allocated */
opendal_operator_free(&op);
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/examples/basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int main()
printf("\n");

/* the opendal_bytes read is heap allocated, please free it */
opendal_bytes_free(read_bytes);
opendal_bytes_free(&read_bytes);

/* the operator_ptr is also heap allocated */
opendal_operator_free(op);
Expand Down
4 changes: 2 additions & 2 deletions bindings/c/include/opendal.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ struct opendal_error *opendal_operator_write(const struct opendal_operator *op,
*
* opendal_bytes bytes = r.data;
* assert(bytes.len == 13);
* opendal_bytes_free(bytes);
* opendal_bytes_free(&bytes);
* ```
*
* # Safety
Expand Down Expand Up @@ -1314,7 +1314,7 @@ struct opendal_capability opendal_operator_info_get_native_capability(const stru
/**
* \brief Frees the heap memory used by the opendal_bytes
*/
void opendal_bytes_free(struct opendal_bytes bs);
void opendal_bytes_free(struct opendal_bytes *bs);

/**
* \brief Construct a heap-allocated opendal_operator_options
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub unsafe extern "C" fn opendal_operator_write(
///
/// opendal_bytes bytes = r.data;
/// assert(bytes.len == 13);
/// opendal_bytes_free(bytes);
/// opendal_bytes_free(&bytes);
/// ```
///
/// # Safety
Expand Down
23 changes: 13 additions & 10 deletions bindings/c/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,24 @@ impl opendal_bytes {

/// \brief Frees the heap memory used by the opendal_bytes
#[no_mangle]
pub unsafe extern "C" fn opendal_bytes_free(bs: opendal_bytes) {
drop(bs);
pub unsafe extern "C" fn opendal_bytes_free(bs: *mut opendal_bytes) {
if !bs.is_null() {
let bs = &mut *bs;
if !bs.data.is_null() {
drop(Vec::from_raw_parts(bs.data, bs.len, bs.capacity));
bs.data = std::ptr::null_mut();
bs.len = 0;
bs.capacity = 0;
}
}
}
}

impl Drop for opendal_bytes {
fn drop(&mut self) {
if !self.data.is_null() {
unsafe {
// Safety: the data is not null, and the capacity is correct
drop(Vec::from_raw_parts(self.data, self.len, self.capacity));
}
self.data = std::ptr::null_mut();
self.len = 0;
self.capacity = 0;
unsafe {
// Safety: the pointer is always valid
Self::opendal_bytes_free(self);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/tests/bdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ TEST_F(OpendalBddTest, FeatureTest)
error = opendal_operator_delete(this->p, this->path.c_str());
EXPECT_EQ(error, nullptr);

opendal_bytes_free(r.data);
opendal_bytes_free(&r.data);

// The directory "tmpdir/" should exist and should be a directory
error = opendal_operator_create_dir(this->p, "tmpdir/");
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/tests/error_msg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ TEST_F(OpendalErrorTest, ErrorReadTest)
ASSERT_GT(error_msg->len, 0);

// the opendal_bytes read is heap allocated, please free it
opendal_bytes_free(r.data);
opendal_bytes_free(&r.data);

// free the error
opendal_error_free(r.error);
Expand Down
2 changes: 1 addition & 1 deletion bindings/go/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ type bytesFree func(b *opendalBytes)
var withBytesFree = withFFI(ffiOpts{
sym: symBytesFree,
rType: &ffi.TypeVoid,
aTypes: []*ffi.Type{&typeBytes},
aTypes: []*ffi.Type{&ffi.TypePointer},
}, func(_ context.Context, ffiCall func(rValue unsafe.Pointer, aValues ...unsafe.Pointer)) bytesFree {
return func(b *opendalBytes) {
ffiCall(
Expand Down
6 changes: 3 additions & 3 deletions bindings/swift/OpenDAL/Sources/OpenDAL/Data+OpenDAL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ extension Data {
/// This can be used to read data from Rust with zero-copying.
/// The underlying buffer will be freed when the data gets
/// deallocated.
init(openDALBytes: opendal_bytes) {
let address = UnsafeRawPointer(openDALBytes.data)!
let length = Int(openDALBytes.len)
init(openDALBytes: UnsafeMutablePointer<opendal_bytes>) {
let address = UnsafeRawPointer(openDALBytes.pointee.data)!
let length = Int(openDALBytes.pointee.len)
self.init(
bytesNoCopy: .init(mutating: address),
count: length,
Expand Down
4 changes: 2 additions & 2 deletions bindings/swift/OpenDAL/Sources/OpenDAL/Operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class Operator {
}

public func blockingRead(_ path: String) throws -> Data {
let ret = opendal_operator_read(nativeOp, path)
var ret = opendal_operator_read(nativeOp, path)
if let err = ret.error {
defer {
opendal_error_free(err)
Expand All @@ -95,6 +95,6 @@ public class Operator {
)
}

return Data(openDALBytes: ret.data)
return withUnsafeMutablePointer(to: &ret.data) { Data(openDALBytes: $0) }
}
}
4 changes: 2 additions & 2 deletions bindings/zig/test/bdd.zig
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ test "Opendal BDD test" {
defer opendal.c.opendal_metadata_free(meta);

// The blocking file "test" must have content "Hello, World!"
const r: opendal.c.opendal_result_read = opendal.c.opendal_operator_read(testkit.p, testkit.path);
defer opendal.c.opendal_bytes_free(r.data);
var r: opendal.c.opendal_result_read = opendal.c.opendal_operator_read(testkit.p, testkit.path);
defer opendal.c.opendal_bytes_free(&r.data);
try testing.expect(r.@"error" == null);
try testing.expectEqual(std.mem.len(testkit.content), r.data.len);

Expand Down

0 comments on commit 70cef32

Please sign in to comment.