ffi

Can a struct containing a raw pointer implement Send and be FFI safe?

落花浮王杯 提交于 2019-12-06 00:48:00
问题 I have a scenario where Rust will call C to malloc a buffer and stash the resulting pointer into a struct. Later on, the struct will be moved to a thread and passed to a C function which mutates it. The naive approach to my problem looks like this (playground): extern crate libc; use libc::{c_void, malloc, size_t}; use std::thread; const INITIAL_CAPACITY: size_t = 8; extern "C" { fn mutate(s: *mut Storage); } #[repr(C)] struct Storage { #[allow(dead_code)] buf: *mut c_void, capacity: usize, }

How to make library installed from OPAM available to OCaml?

 ̄綄美尐妖づ 提交于 2019-12-05 22:41:30
I followed this tutorial on OCaml FFI and installed Ctypes through OPAM: opam install ctypes However, OCaml does not find the module: open Ctypes (* ... *) I receive the error: Unbound module Ctypes It looks like I need to let OCaml know where my Ctypes installation is? Do I need to update some path variable to let OCaml look for my libraries installed through OPAM? This is Ubuntu 15.04, OCaml 4.01.0, OPAM 1.2.0. Installing something on your system doesn't make it automatically visible for the compiler, this is true not only for OCaml, but for most conventional systems, like C or C++ to name a

How to install ffi on Heroku

∥☆過路亽.° 提交于 2019-12-05 22:08:15
This started today with our Heroku deployments. Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension. /tmp/build_6e4275c6-8442-4a39-9175-f20505baf383/vendor/ruby-2.0.0/bin/ruby extconf.rb checking for ffi.h... no checking for ffi.h in /usr/local/include,/usr/include/ffi... no checking for rb_thread_blocking_region()... yes checking for rb_thread_call_with_gvl()... yes checking for rb_thread_call_without_gvl()... yes checking for ffi_prep_cif_var()... no creating extconf.h creating Makefile make "DESTDIR=" Configuring libffi make -C "/tmp/build_6e4275c6-8442-4a39

Updating a field value of a C struct from Julia

ε祈祈猫儿з 提交于 2019-12-05 20:30:33
My question is simple but I don't know the best way to do that (or Julia doesn't offer such a way at the moment): how can I set a field value of a C struct from Julia? Imagine you have a struct type to represent a node of a tree in a C library: typedef struct node_s { int type; node_t* next; node_t* children; node_t* parent; } node_t; and copy it in Julia: immutable node_t typ::Cint next::Ptr{node_t} children::Ptr{node_t} parent::Ptr{node_t} end Now assume that you have a pointer to node_t allocated in C and want to update the parent field in Julia. I know we have unsafe_store! to update a

How to store a void* reference to a struct in Rust?

橙三吉。 提交于 2019-12-05 18:55:22
I'm interacting with some C callbacks that use the standard void* userdata method to allow you to store a reference to some context (e.g. a struct). How can I store a reference to a Rust struct in a void* and still allow it to be moved around? It seems that Rust moves really are moves, i.e. this code fails (as expected): struct Thing { pointer_to_self: *mut Thing, } fn create_thing() -> Thing { let mut a = Thing { pointer_to_self: std::ptr::null_mut(), }; a.pointer_to_self = &mut a as *mut _; a } fn main() { let mut b = create_thing(); assert_eq!(&mut b as *mut _, b.pointer_to_self); } Is

How can I specify linker flags/arguments in a build script?

送分小仙女□ 提交于 2019-12-05 12:59:45
I'm using Rust, bindgen , and a build script to work on some FFI bindings to a library. This library is built using OpenMP , so when linking against it, I'd normally pass the -fopenmp flag to the compiler. How can I get this flag to be set by build.rs when the library is built by Cargo? Currently, building using Cargo fails, with the failing command being something like: cc -Wl,--as-needed -Wl,-z,noexecstack -m64 -l gomp -l stdc++ ...skipping dozens of paths/files... -Wl,-Bdynamic -l dl -l rt -l pthread -l gcc_s -l c -l m -l rt -l pthread -l util which fails with hundreds of undefined

Library not loaded: /opt/local/lib/libffi.5.dylib but I'm using homebrew

喜你入骨 提交于 2019-12-05 10:33:25
I'm trying to run guard on Mac OS X Lion(XCode 4.3+OSX GCC Installer+Homebrew) Error message: Library not loaded: /opt/local/lib/libffi.5.dylib I have no /opt/local directory since I'm not using MacPorts but Homebrew I tried brew install libffi , which was successful, but the gem still doesn't work. I also have tried uninstalling and reinstalling the gem without success. Problem is also occurring on OSX 10.9 (Mavericks) with apple-gcc42 installed. I have also tried telling gem about the presence of libffi as follows: gem install ffi:1.0.7 -- --with-ldflags='-L/usr/local/opt/libffi/lib' I have

How to transfer ownership of a value to C code from Rust?

ε祈祈猫儿з 提交于 2019-12-05 09:01:13
I'm trying to write some Rust code with FFI that involves C taking ownership of a struct: fn some_function() { let c = SomeStruct::new(); unsafe { c_function(&mut c); } } I want c_function to take ownership of c . In C++, this could be achieved by the release method of unqiue_ptr . Is there something similar in Rust? kennytm The std::unique_ptr type in C++ corresponds to Box in Rust, and .release() corresponds to Box::into_raw . let c = Box::new(SomeStruct::new()); unsafe { c_function(Box::into_raw(c)); } Note that the C function should return the ownership of the pointer to Rust to destroy

GHCi runtime linker issue when using FFI declarations

让人想犯罪 __ 提交于 2019-12-05 08:25:02
问题 I have a problem regarding FFI in Haskell and the interactive mode of GHC again. Consider FFISo.hs : {-# LANGUAGE OverloadedStrings #-} module Main where import qualified Data.ByteString.Char8 as B import FFIFun.Foo main :: IO () main = do B.putStrLn "main" callMeFromC callMeFromHaskell return () c.c : #include <stdio.h> void callMeFromC(void); void callMeFromHaskell(void) { printf("callMeFromHaskell\n"); callMeFromC(); } FFIFun/Foo.hs : {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE

How to force GHC to inline FFI calls?

和自甴很熟 提交于 2019-12-05 07:54:32
I made small C module to improve performance, but GHC doesn't inline foreign functions, and calls cost eliminates the acceleration. For example, test.h : int inc (int x); test.c : #include "test.h" int inc(int x) {return x + 1;} Test.hc : {-# LANGUAGE ForeignFunctionInterface #-} module Test (inc) where import Foreign import Foreign.C foreign import ccall unsafe "test.h inc" c_inc :: CInt -> CInt inc = fromIntegral . c_inc . fromIntegral {-# INLINE c_inc #-} {-# INLINE inc #-} Main.hs : import System.Environment import Test main = do {args <- getArgs; putStrLn . show . inc . read . head $ args