tvm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-tvm] binarybana commented on a change in pull request #5328: [Rust][Runtime] Add basic object system support.
Date Mon, 20 Apr 2020 04:32:08 GMT

binarybana commented on a change in pull request #5328:
URL: https://github.com/apache/incubator-tvm/pull/5328#discussion_r411052316



##########
File path: rust/tvm-rt/src/object.rs
##########
@@ -0,0 +1,226 @@
+use std::ffi::{CString};
+use crate::Function;
+use tvm_sys::{TVMRetValue, TVMArgValue};
+use tvm_sys::ffi::{TVMObjectRetain, TVMObjectFree, TVMObjectTypeKey2Index};
+use std::ptr::NonNull;
+use std::convert::TryFrom;
+use std::convert::TryInto;
+
+type Deleter<T> = unsafe extern fn(object: *mut T) -> ();
+
+#[derive(Debug)]
+#[repr(C)]
+pub struct Object {
+    pub type_index: u32,
+    pub ref_count: i32,
+    pub fdeleter: Deleter<Object>,
+}
+
+unsafe extern fn delete<T: IsObject>(object: *mut Object) {
+    let typed_object: *mut T =
+        std::mem::transmute(object);
+    T::typed_delete(typed_object);
+}
+
+impl Object {
+    fn new(type_index: u32, deleter: Deleter<Object>) -> Object {
+        Object {
+            type_index,
+            // Note: do not touch this field directly again, this is
+            // a critical section, we write a 1 to the atomic which will now
+            // be managed by the C++ atomics.
+            // In the future we should probably use C-atomcis.
+            ref_count: 1,
+            fdeleter: deleter,
+        }
+    }
+
+    fn get_type_index(type_key: &'static str) -> u32 {
+        let cstring = CString::new(type_key).expect("type key must not contain null characters");
+        if type_key == "Object" {
+            return 0;
+        } else {
+            let mut index = 0;
+            unsafe {
+                let index_ptr = std::mem::transmute(&mut index);

Review comment:
       It looks like you should be able to replace this with a safe `self.ptr.as_ptr() as
*mut std::ffi::c_void;` conversion? Unless I mis-read something.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message