forked from apache/opendal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bindings/c): framework of add basic io and init logics
* Add the init logics for operator * Add the basic io operations (r/w) * Add the test for basicio Fixes: apache#1201 Signed-off-by: Ji-Xinyou <jerryji0414@outlook.com>
- Loading branch information
1 parent
889c84e
commit 2fe646f
Showing
8 changed files
with
319 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use std::os::raw::c_void; | ||
|
||
use bytes::Bytes; | ||
|
||
use ::opendal as od; | ||
|
||
/// The [`OperatorPtr`] owns a pointer to a [`BlockingOperator`]. | ||
/// It is also the key struct that OpenDAL's APIs access the real | ||
/// operator's memory. The use of OperatorPtr is zero cost, it | ||
/// only returns a reference of the underlying Operator. | ||
#[repr(C)] | ||
pub struct OperatorPtr { | ||
// this is typed with [`c_void`] because cbindgen does not | ||
// support our own custom type. | ||
ptr: *const c_void, | ||
} | ||
|
||
impl OperatorPtr { | ||
/// Creates an OperatorPtr will nullptr, indicating this [`OperatorPtr`] | ||
/// is invalid | ||
pub(crate) fn null() -> Self { | ||
Self { | ||
ptr: std::ptr::null(), | ||
} | ||
} | ||
|
||
/// Returns a reference to the underlying [`BlockingOperator`] | ||
pub(crate) fn get_ref(&self) -> &od::BlockingOperator { | ||
unsafe { &*(self.ptr as *const od::BlockingOperator) } | ||
} | ||
|
||
/// Returns whether the [`OperatorPtr`] is valid, i.e. whether | ||
/// there exists a underlying [`BlockingOperator`] | ||
#[no_mangle] | ||
pub extern "C" fn od_is_ptr_valid(&self) -> bool { | ||
!self.ptr.is_null() | ||
} | ||
} | ||
|
||
impl From<&od::BlockingOperator> for OperatorPtr { | ||
fn from(value: &od::BlockingOperator) -> Self { | ||
Self { | ||
ptr: value as *const _ as *const c_void, | ||
} | ||
} | ||
} | ||
|
||
impl From<&mut od::BlockingOperator> for OperatorPtr { | ||
fn from(value: &mut od::BlockingOperator) -> Self { | ||
Self { | ||
ptr: value as *const _ as *const c_void, | ||
} | ||
} | ||
} | ||
|
||
/// The [`Vector`] type is a C-compatable substitute for [`Vec`] | ||
/// in Rust, it will not be deallocated automatically like what | ||
/// has been done in Rust. Instead, you have to call [`free_vec`] | ||
/// to free the heap memory to avoid memory leak. | ||
/// The field `data` should not be modified since it might causes | ||
/// the reallocation of the Vector. | ||
#[repr(C)] | ||
pub struct Vector { | ||
pub data: *const u8, | ||
pub len: usize, | ||
} | ||
|
||
impl Vector { | ||
/// Construct a [`Vector`] from the Rust [`Vec`] of bytes | ||
pub(crate) fn from_vec(vec: Vec<u8>) -> Vector { | ||
let data = vec.as_ptr() as *const u8; | ||
let len = vec.len(); | ||
std::mem::forget(vec); // To avoid deallocation of the vec. | ||
Self { data, len } | ||
} | ||
} | ||
|
||
impl Into<Bytes> for Vector { | ||
fn into(self) -> Bytes { | ||
let slice = unsafe { std::slice::from_raw_parts(self.data, self.len) }; | ||
Bytes::from_static(slice) | ||
} | ||
} | ||
|
||
/// Frees the heap memory used by the [`Vector`] | ||
#[no_mangle] | ||
pub extern "C" fn od_free_vec(vec: *const Vector) { | ||
unsafe { | ||
// this deallocates the vector by reconstructing the vector and letting | ||
// it be dropped when its out of scope | ||
Vec::from_raw_parts((*vec).data as *mut u8, (*vec).len, (*vec).len); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
#include "stdio.h" | ||
#include "opendal.h" | ||
|
||
int main(int argc, char *argv[]) { | ||
// creates a memory operator | ||
char scheme[] = "memory"; | ||
opendal_operator_ptr ptr = od_new_operator(scheme); | ||
if (!od_is_ptr_valid(&ptr)) { | ||
return -1; | ||
} | ||
|
||
// write some contents by the operator | ||
char path[] = "test"; | ||
char content[] = "Hello World"; | ||
const opendal_vector data = { | ||
.len = sizeof(content) - 1, | ||
.data = (uint8_t*)content, | ||
}; | ||
if (!od_operator_blocking_write(ptr, path, data)) { | ||
return -2; | ||
} | ||
|
||
// reads the data out from the vector | ||
opendal_vector* v = od_operator_blocking_read(ptr, path); | ||
for (int i = 0; i < v->len; i++) { | ||
printf("%c", (char)(v->data[i])); | ||
} | ||
|
||
// free the vector's heap memory | ||
od_free_vec(v); | ||
|
||
return 0; | ||
} |