ffi

Fast array access using Racket FFI

北城以北 提交于 2020-02-03 04:59:48
问题 I am trying to write OpenCV FFI in Racket and arrived at a point where arrays need to be manipulated efficiently. However, all my attempts to access arrays by using Racket FFI resulted in very inefficient code. Is there a way for fast access of C arrays using FFI? In Racket, this type of manipulation is reasonably fast, i.e.: (define a-vector (make-vector (* 640 480 3))) (time (let loop ([i (- (* 640 480 3) 1)]) (when (>= i 0) ;; invert each pixel channel-wise (vector-set! a-vector i (- 255

Fast array access using Racket FFI

无人久伴 提交于 2020-02-03 04:58:12
问题 I am trying to write OpenCV FFI in Racket and arrived at a point where arrays need to be manipulated efficiently. However, all my attempts to access arrays by using Racket FFI resulted in very inefficient code. Is there a way for fast access of C arrays using FFI? In Racket, this type of manipulation is reasonably fast, i.e.: (define a-vector (make-vector (* 640 480 3))) (time (let loop ([i (- (* 640 480 3) 1)]) (when (>= i 0) ;; invert each pixel channel-wise (vector-set! a-vector i (- 255

true - false response of query from prolog in C#

北战南征 提交于 2020-01-25 11:22:06
问题 I am using swi- prolog with c# as front end. I want to to display whether the the query is executed or not in a mmessagebox. So i need the answer in 'true' or 'false' like it comes in prolog console. here is my code: static void Main(string[] args) { Environment.SetEnvironmentVariable("SWI_HOME_DIR", @"C:\Program Files\swipl"); Environment.SetEnvironmentVariable("PATH", @"C:\Program Files\swipl"); Environment.SetEnvironmentVariable("PATH", @"C:\Program Files\swipl\bin"); string p1 = @"C:

true - false response of query from prolog in C#

人盡茶涼 提交于 2020-01-25 11:21:08
问题 I am using swi- prolog with c# as front end. I want to to display whether the the query is executed or not in a mmessagebox. So i need the answer in 'true' or 'false' like it comes in prolog console. here is my code: static void Main(string[] args) { Environment.SetEnvironmentVariable("SWI_HOME_DIR", @"C:\Program Files\swipl"); Environment.SetEnvironmentVariable("PATH", @"C:\Program Files\swipl"); Environment.SetEnvironmentVariable("PATH", @"C:\Program Files\swipl\bin"); string p1 = @"C:

How to call a C++ dynamic library from Rust?

强颜欢笑 提交于 2020-01-24 22:42:09
问题 I want to call a C++ dynamic library (*.so) from Rust, but I don't want to build it from Rust. Like this, cc::Build::new() .file("src/foo.cc") .shared_flag(true) .compile("libfoo.so"); In some cases, I only need to call several functions, not all the functions. How can I use it? 回答1: Before you go further, make sure you have a basic idea of Rust FFI (foreign function interface). In Rust, it's easy to call C, but hard to call C++. To call C functions in Rust, you just have to wrap them with

Is it possible to create PHP extensions in Haskell?

人走茶凉 提交于 2020-01-23 04:42:10
问题 Is it possible to create PHP extensions with Haskell? Usually PHP extensions are written using C. Is using Haskell FFI to provide functionality for a stub C extension possible (or even a good idea)? What are the caveats to such an approach? Does Zend perform some magic in the background that would disrupt such a scheme? 回答1: You can certainly do this, though I'm not sure anyone has tried. Haskell interoperates with C via its FFI mechanism, and you can certainly: Call Haskell from C so if you

How to wrap existing C functions with Rust or how to call C functions from Rust?

送分小仙女□ 提交于 2020-01-16 08:43:13
问题 I have existing C code and its header and I need to call the C code from Rust. I tried it many ways and referred to documents but I didn't understand how to apply that to my code. I'm facing difficulties converting C functions into Rust. Please help me with some examples so that I can understand easily. I tried to use the examples given in the Rust book and other website examples, but no resource has more details on this. C_code.h void ifx_vec_init_r(ifx_Vector_R_t* vector, ifx_Float_t* d,

Is it undefined behavior to do runtime borrow management with the help of raw pointers in Rust?

…衆ロ難τιáo~ 提交于 2020-01-14 19:50:51
问题 As part of binding a C API to Rust, I have a mutable reference ph: &mut Ph , a struct struct EnsureValidContext<'a> { ph: &'a mut Ph } , and some methods: impl Ph { pub fn print(&mut self, s: &str) { /*...*/ } pub fn with_context<F, R>(&mut self, ctx: &Context, f: F) -> Result<R, InvalidContextError> where F: Fn(EnsureValidContext) -> R, { /*...*/ } /* some others */ } impl<'a> EnsureValidContext<'a> { pub fn print(&mut self, s: &str) { self.ph.print(s) } pub fn close(self) {} /* some others

Creating a Vec in Rust from a C array pointer and safely freeing it?

只谈情不闲聊 提交于 2020-01-14 14:13:32
问题 I'm calling a C function from Rust which takes a null pointer as as an argument, then allocates some memory to point it to. What is the correct way to efficiently (i.e. avoiding unnecessary copies) and safely (i.e. avoid memory leaks or segfaults) turn data from the C pointer into a Vec ? I've got something like: extern "C" { // C function that allocates an array of floats fn allocate_data(data_ptr: *mut *const f32, data_len: *mut i32); } fn get_vec() -> Vec<f32> { // C will set this to

Creating a Vec in Rust from a C array pointer and safely freeing it?

谁说我不能喝 提交于 2020-01-14 14:13:26
问题 I'm calling a C function from Rust which takes a null pointer as as an argument, then allocates some memory to point it to. What is the correct way to efficiently (i.e. avoiding unnecessary copies) and safely (i.e. avoid memory leaks or segfaults) turn data from the C pointer into a Vec ? I've got something like: extern "C" { // C function that allocates an array of floats fn allocate_data(data_ptr: *mut *const f32, data_len: *mut i32); } fn get_vec() -> Vec<f32> { // C will set this to