ffi

How long should I expect a garbage collection to take before removing an opaque FFI object? Is it possible to speed it up some way?

时光毁灭记忆、已成空白 提交于 2019-12-07 03:38:56
问题 I consider writing Haskell bindings to a quantum mechanics library written in C++ (I'd write a plain C wrapper) and CUDA. A major bottleneck is always the GPU memory used by the CUDA parts. In C++, this is handled quite efficiently because all objects have automatic memory management, i.e. are erased as soon as they leave scope. Also I use C++11 move semantics to avoid copies, those obviously wouldn't be necessary in Haskell anyway. Yet I'm concerned it might not work as smoothly anymore when

How to check if function pointer passed from C is non-NULL

余生长醉 提交于 2019-12-07 02:10:17
问题 Example code below The Rust part: #[no_mangle] pub extern fn call_c_function(value: i32, fun: fn(i32) -> i32) -> i32 { fun(value) } And the C part: int32_t call_c_function(int32_t value, int32_t (*fun)(int32_t)); int32_t triple(int32_t x) { return x*3; } int main(int argc, char *argv[]) { int32_t value = 3; int32_t result = call_c_function(value, triple); printf("%d tripled is %d\n", value, result); call_c_function(0, NULL); // Crash here return EXIT_SUCCESS; } Of course second call of call_c

Calling Haskell FFI Function Ptrs from C

时间秒杀一切 提交于 2019-12-07 01:19:50
问题 I am trying to get the following code to work: sample_hs :: CInt -> (CInt -> CInt) sample_hs x = (x+) foreign export ccall sample_hs :: CInt -> (CInt -> CInt) I would like to be able to do something like this in c: pf = sample_hs(2); result = pf(3); //Should be 5; When I try to do this, however, I get an error message: error: too few arguments to function ‘sample_hs’ I am guessing that the interface between the language isn't working how I thought it would. Is there a way to do what I'm

What is the correct type for returning a C99 `bool` to Rust via the FFI?

醉酒当歌 提交于 2019-12-06 19:31:33
问题 A colleague and I have been scratching our heads over how to return a bool from <stdbool.h> (a.k.a. _Bool ) back to Rust via the FFI. We have our C99 code we want to use from Rust: bool myfunc(void) { ... } We let Rust know about myfunc using an extern C block: extern "C" { fn myfunc() -> T; } What concrete type should T be? Rust doesn't have a c_bool in the libc crate, and if you search the internet, you will find various GitHub issues and RFCs where people discuss this, but don't really

unable to install compass

こ雲淡風輕ζ 提交于 2019-12-06 19:21:17
问题 Last week I made an update in cygwin because of the bash "vulnerable problem". After that I couldn't compile sass anymore. I tried to reinstall ruby, after some install fails and reinstalling cygwin I installed it and updated with "gem update --system" it run, but when I tried to install compass I got following message: > /usr/bin/ruby.exe -r ./siteconf20141006-7856-1td7wzb.rb extconf.rb > checking for ffi.h... *** extconf.rb failed *** Could not create > Makefile due to some reason, probably

How do I call a C function which returns many types of different function pointers?

ε祈祈猫儿з 提交于 2019-12-06 15:20:42
I'm trying to access the OpenGL function glCreateVertexArrays . Consider the following small C++ program: #include <GL/glx.h> #include <GLFW/glfw3.h> int main() { // Init GLFW glfwInit(); // Set OpenGL Version glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Select core profile glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true); // Create Window auto window = glfwCreateWindow(400, 400, "Test", nullptr, nullptr); glfwMakeContextCurrent(window); void (*glCreateVertexArrays)(unsigned int,

C library freeing a pointer coming from Rust

余生长醉 提交于 2019-12-06 09:01:05
问题 I want to do Rust bindings to a C library which requires a callback, and this callback must return a C-style char* pointer to the C library which will then free it. The callback must be in some sense exposed to the user of my library (probably using closures), and I want to provide a Rust interface as convenient as possible (meaning accepting a String output if possible). However, the C library complains when trying to free() a pointer coming from memory allocated by Rust, probably because

How to wrap function in Ruby FFI method that takes struct as argument?

不羁岁月 提交于 2019-12-06 06:45:36
I am trying to call a function from a shared object using ruby-ffi. I compiled the following into a shared object: #include <stdio.h> typedef struct _WHAT { int d; void * something; } WHAT; int doit(WHAT w) { printf("%d\n", w.d); return w.d; } The problem is, how do I declare the function with attach_function in Ruby? How is the struct argument (WHAT w) defined in the list of arguments in Ruby? It is not a :pointer, and does not seem to fit any of the other available types described in the ruby-ffi documentation, so what would it be? Check how to use Structs in https://github.com/ffi/ffi/wiki

How do I return an vector of dynamic length in a pub extern “C” fn?

旧街凉风 提交于 2019-12-06 02:53:35
问题 I want to return a vector in a pub extern "C" fn . Since a vector has an arbitrary length, I guess I need to return a struct with the pointer to the vector, and the number of elements in the vector My current code is: extern crate libc; use self::libc::{size_t, int32_t, int64_t}; // struct to represent an array and its size #[repr(C)] pub struct array_and_size { values: int64_t, // this is probably not how you denote a pointer, right? size: int32_t, } // The vector I want to return the

Create shared C object linked to Rust dylib for use in R

主宰稳场 提交于 2019-12-06 01:51:02
I am trying to create a shared object I can load into R that calls Rust functions by way of R's C API. To call Rust from C, I am following this blog post . My problem arises when I try to create the shared library and link to the Rust library. The linker complains that it can't find my Rust function. I am quite new to compiled languages and have given this a couple days' worth of effort before turning to SO. In that time I have learned a lot about compiler flags and yet come no closer to a solution. I think it may be something obvious. My C++ code: #include "Rinternals.h" #include "R.h"