Skip to content

Modify tensorflow-sys/src/lib.rs to be bindgen generated #67

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

Merged
merged 9 commits into from
Mar 11, 2017
Merged
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
9 changes: 5 additions & 4 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::ops::Range;
use std::ops::RangeFrom;
use std::ops::RangeFull;
use std::ops::RangeTo;
use std::os::raw::c_void as std_c_void;
use std::ptr;
use std::slice;
use super::BufferTrait;
Expand Down Expand Up @@ -65,7 +66,7 @@ impl<T: TensorType> Buffer<T> {
let msg = CStr::from_ptr(c_msg);
panic!("Failed to allocate: {}", msg.to_str().unwrap());
}
(*inner).data = ptr;
(*inner).data = ptr as *mut std_c_void;
(*inner).length = len;
(*inner).data_deallocator = Some(deallocator);
Buffer {
Expand All @@ -81,7 +82,7 @@ impl<T: TensorType> Buffer<T> {
/// The underlying data is *not* freed when the buffer is destroyed.
pub unsafe fn from_ptr(ptr: *mut T, len: usize) -> Self {
let inner = tf::TF_NewBuffer();
(*inner).data = ptr as *const c_void;
(*inner).data = ptr as *const std_c_void;
(*inner).length = len;
Buffer {
inner: inner,
Expand Down Expand Up @@ -155,8 +156,8 @@ impl<T: TensorType> BufferTrait for Buffer<T> {
}
}

unsafe extern "C" fn deallocator(data: *mut c_void, _length: size_t) {
libc::free(data);
unsafe extern "C" fn deallocator(data: *mut std_c_void, _length: size_t) {
libc::free(data as *mut c_void);
}

impl<T: TensorType> Drop for Buffer<T> {
Expand Down
33 changes: 17 additions & 16 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std;
use std::ffi::CStr;
use std::ffi::CString;
use std::ffi::NulError;
use std::os::raw::c_void as std_c_void;
use std::ptr;
use std::str::Utf8Error;
use std::sync::Arc;
Expand Down Expand Up @@ -302,7 +303,7 @@ impl Operation {
pub fn output_type(&self, index: usize) -> DataType {
unsafe {
DataType::from_c(tf::TF_OperationOutputType(tf::TF_Output {
operation: self.inner,
oper: self.inner,
index: index as c_int,
}))
}
Expand Down Expand Up @@ -332,7 +333,7 @@ impl Operation {
pub fn input_type(&self, index: usize) -> DataType {
unsafe {
DataType::from_c(tf::TF_OperationInputType(tf::TF_Input {
operation: self.inner,
oper: self.inner,
index: index as c_int,
}))
}
Expand All @@ -359,11 +360,11 @@ impl Operation {
pub fn input(&self, index: usize) -> (Operation, usize) {
unsafe {
let port = tf::TF_OperationInput(tf::TF_Input {
operation: self.inner,
oper: self.inner,
index: index as c_int,
});
(Operation {
inner: port.operation,
inner: port.oper,
gimpl: self.gimpl.clone(),
},
port.index as usize)
Expand All @@ -374,7 +375,7 @@ impl Operation {
pub fn output_num_consumers(&self, index: usize) -> usize {
unsafe {
tf::TF_OperationOutputNumConsumers(tf::TF_Output {
operation: self.inner,
oper: self.inner,
index: index as c_int,
}) as usize
}
Expand All @@ -387,12 +388,12 @@ impl Operation {
pub fn output_consumers(&self, index: usize) -> Vec<(Operation, usize)> {
unsafe {
let num_consumers = tf::TF_OperationOutputNumConsumers(tf::TF_Output {
operation: self.inner,
oper: self.inner,
index: index as c_int,
});
let mut vec = <Vec<tf::TF_Input>>::with_capacity(num_consumers as usize);
let len = tf::TF_OperationOutputConsumers(tf::TF_Output {
operation: self.inner,
oper: self.inner,
index: index as c_int,
},
vec.as_mut_ptr(),
Expand All @@ -401,7 +402,7 @@ impl Operation {
vec.into_iter()
.map(|port| {
(Operation {
inner: port.operation,
inner: port.oper,
gimpl: self.gimpl.clone(),
},
port.index as usize)
Expand Down Expand Up @@ -483,7 +484,7 @@ pub struct Input<'a> {
impl<'a> Input<'a> {
fn to_c(&self) -> tf::TF_Input {
tf::TF_Input {
operation: self.operation.inner,
oper: self.operation.inner,
index: self.index,
}
}
Expand All @@ -505,7 +506,7 @@ pub struct Output<'a> {
impl<'a> Output<'a> {
fn to_c(&self) -> tf::TF_Output {
tf::TF_Output {
operation: self.operation.inner,
oper: self.operation.inner,
index: self.index,
}
}
Expand Down Expand Up @@ -606,7 +607,7 @@ impl<'a> OperationDescription<'a> {
unsafe {
tf::TF_SetAttrString(self.inner,
c_attr_name.as_ptr(),
c_value.as_ptr() as *const c_void,
c_value.as_ptr() as *const std_c_void,
c_value.len() as size_t);
}
Ok(())
Expand All @@ -625,7 +626,7 @@ impl<'a> OperationDescription<'a> {
unsafe {
tf::TF_SetAttrStringList(self.inner,
c_attr_name.as_ptr(),
ptrs.as_ptr(),
ptrs.as_ptr() as *const *const std_c_void,
lens.as_ptr(),
ptrs.len() as c_int);
}
Expand Down Expand Up @@ -821,7 +822,7 @@ impl<'a> OperationDescription<'a> {
unsafe {
tf::TF_SetAttrTensorShapeProto(self.inner,
c_attr_name.as_ptr(),
value.as_ptr() as *const c_void,
value.as_ptr() as *const std_c_void,
value.len() as size_t,
status.inner());
}
Expand All @@ -843,7 +844,7 @@ impl<'a> OperationDescription<'a> {
unsafe {
tf::TF_SetAttrTensorShapeProtoList(self.inner,
c_attr_name.as_ptr(),
ptrs.as_ptr(),
ptrs.as_ptr() as *const *const std_c_void,
lens.as_ptr(),
ptrs.len() as c_int,
status.inner());
Expand Down Expand Up @@ -878,7 +879,7 @@ impl<'a> OperationDescription<'a> {
let ptrs: Vec<*mut tf::TF_Tensor> = value.into_iter().map(|x| x.into_ptr()).collect();
tf::TF_SetAttrTensorList(self.inner,
c_attr_name.as_ptr(),
ptrs.as_ptr(),
ptrs.as_ptr() as *const *const tf::TF_Tensor,
ptrs.len() as c_int,
status.inner());
}
Expand All @@ -893,7 +894,7 @@ impl<'a> OperationDescription<'a> {
unsafe {
tf::TF_SetAttrValueProto(self.inner,
c_attr_name.as_ptr(),
value.as_ptr() as *const c_void,
value.as_ptr() as *const std_c_void,
// Allow trivial_numeric_casts because usize is not
// necessarily size_t.
value.len() as size_t,
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use std::ops::Deref;
use std::ops::DerefMut;
use std::ops::Drop;
use std::ops::Index;
use std::os::raw::c_void as std_c_void;
use std::str::Utf8Error;

////////////////////////
Expand Down Expand Up @@ -844,9 +845,9 @@ pub struct Tensor<T: TensorType> {
owned: bool,
}

unsafe extern "C" fn noop_deallocator(_: *mut c_void, _: size_t, _: *mut c_void) -> () {}
unsafe extern "C" fn noop_deallocator(_: *mut std_c_void, _: size_t, _: *mut std_c_void) -> () {}

unsafe extern "C" fn deallocator(_: *mut c_void, _: size_t, buffer: *mut c_void) -> () {
unsafe extern "C" fn deallocator(_: *mut std_c_void, _: size_t, buffer: *mut std_c_void) -> () {
tf::TF_DeleteBuffer(buffer as *mut tf::TF_Buffer);
}

Expand Down
6 changes: 3 additions & 3 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Session {
tf::TF_SessionRun(self.inner,
ptr::null(),
step.input_ports.as_ptr(),
input_tensors.as_mut_ptr(),
input_tensors.as_ptr() as *const *const tf::TF_Tensor,
input_tensors.len() as c_int,
step.output_ports.as_ptr(),
step.output_tensors.as_mut_ptr(),
Expand Down Expand Up @@ -143,7 +143,7 @@ impl<'l> StepWithGraph<'l> {
index: c_int,
tensor: &'l Tensor<T>) {
self.input_ports.push(tf::TF_Output {
operation: operation.inner(),
oper: operation.inner(),
index: index,
});
self.input_tensors.push(tensor.inner);
Expand All @@ -153,7 +153,7 @@ impl<'l> StepWithGraph<'l> {
/// Returns an index that you can then use to fetch this output from the step after running it.
pub fn request_output(&mut self, operation: &Operation, index: c_int) -> OutputToken {
self.output_ports.push(tf::TF_Output {
operation: operation.inner(),
oper: operation.inner(),
index: index,
});
self.output_tensors.push(ptr::null_mut());
Expand Down
4 changes: 2 additions & 2 deletions tensorflow-sys/examples/multiplication.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extern crate libc;
extern crate tensorflow_sys as ffi;

use libc::{c_int, c_void, int64_t, size_t};
use libc::{c_int, int64_t, size_t};
use std::ffi::{CStr, CString};
use std::path::Path;

Expand Down Expand Up @@ -107,7 +107,7 @@ fn main() {
ffi::TF_DeleteSessionOptions(options);
}

unsafe extern "C" fn noop(_: *mut c_void, _: size_t, _: *mut c_void) {}
unsafe extern "C" fn noop(_: *mut std::os::raw::c_void, _: size_t, _: *mut std::os::raw::c_void) {}
}

fn read<T: AsRef<Path>>(path: T) -> Vec<u8> {
Expand Down
17 changes: 17 additions & 0 deletions tensorflow-sys/generate_bindgen_rs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

if ! which bindgen &> /dev/null; then
echo "ERROR: Please install 'bindgen' using cargo:"
echo " cargo install bindgen"
echo "See https://github.com/servo/rust-bindgen for more information."
exit 1
fi

# See https://github.com/servo/rust-bindgen/issues/550 as to why
# this is blacklisted.
bindgen_options="--blacklist-type max_align_t"
header="/usr/include/tensorflow/c_api.h"

cmd="bindgen ${bindgen_options} ${header} --output src/bindgen.rs"
echo ${cmd}
${cmd}
Loading