ffi

How to wrap a call to a FFI function that uses VarArgs in Rust?

此生再无相见时 提交于 2020-01-03 14:25:06
问题 mexPrintf , just like printf , accepts a varargs list of arguments, but I don't know what the best way to wrap this is in Rust. There is a RFC for variadic generics, but what can we do today? In this example, I want to print of the number of inputs and outputs, but the wrapped function just prints garbage. Any idea how to fix this? #![allow(non_snake_case)] #![allow(unused_variables)] extern crate mex_sys; use mex_sys::mxArray; use std::ffi::CString; use std::os::raw::c_int; use std::os::raw:

How can I make data that is allocated manually be garbage-collected in Haskell?

浪子不回头ぞ 提交于 2020-01-03 09:45:25
问题 I'm thinking about a FFI calling some C functions from Haskell. If a memory buffer is used to hold some data and is allocated "manually" and then it is used in Haskell computations, can I somehow rely on the garbage collector to free it when it is not needed anymore. As for the manual allocations, there are basically two ways (but the difference doesn't seem to be essential for my question): allocating a buffer in Haskell, then passing it to C function, like in fdRead allocating a buffer in C

How do I free a *char allocated via FFI in Rust?

[亡魂溺海] 提交于 2020-01-03 08:29:07
问题 I'm calling the LLVM API via Rust's FFI. LLVMPrintModuleToString uses strdup to create a string. However, when I wrap the pointer in a CString , I get an error when Rust drops it. #![feature(cstr_memory)] use std::ffi::CString; extern crate llvm_sys as llvm; fn main() { let llvm_ir_cstring; unsafe { let module = llvm::core::LLVMModuleCreateWithName(b"nop\0".as_ptr() as *const _); let llvm_ir_char_ptr = llvm::core::LLVMPrintModuleToString(module); llvm::core::LLVMDisposeModule(module); llvm_ir

Passing arbitrary-sized integers from Prolog to C

独自空忆成欢 提交于 2020-01-03 07:20:29
问题 Right now, I'm learning how to interface SICStus Prolog with C code. I would like to have/use/see a C implementation of "Hamming weight" of arbitrary-sized integers in SICStus Prolog version 4. It seems to me that I need C functions for testing term types (SP_is_integer) and C functions for accessing Prolog terms (SP_get_integer, SP_get_integer_bytes). However, I'm not sure how to use SP_get_integer_bytes in a portable, robust fashion. Could you please point me to some well-crafted solid C

How to install ffi on Heroku

让人想犯罪 __ 提交于 2020-01-02 08:33:43
问题 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

How to install ffi on Heroku

孤者浪人 提交于 2020-01-02 08:32:13
问题 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

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

喜欢而已 提交于 2020-01-02 04:51:26
问题 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

How do I use C preprocessor macros with Rust's FFI?

瘦欲@ 提交于 2020-01-02 00:57:35
问题 I'm writing some code that interfaces an existing library written in C. In my Rust code I'd like to be able to use values from CPP macros. If I have a C include.h that looks like this: #define INIT_FLAG 0x00000001 I'd like to be able to use it in Rust like this: #[link(name="mylib")] extern { pub static init_flag: c_int = INIT_FLAG; } I've looked at other FFI code and I see a lot of people duplicating these values in Rust instead of getting them from the FFI. This seems a little brittle, and

hsc2hs: Mutate a C struct with Haskell

荒凉一梦 提交于 2020-01-01 05:34:33
问题 I am trying to write a Haskell program that communicates with C (ultimately for iOS via GHC-iOS). I want it to pass a string from C to Haskell, have Haskell process it and then return some Data types from Haskell to C Structs via hsc2s. I have been unsuccessful at finding a clear, simple tutorial. The only thing Haskell needs from C is the String, nothing else. I have no trouble with the very first part, passing a string to Haskell. testPrint :: CString -> IO () testPrint c = do s <-

How to pass a pointer to LuaJIT ffi to be used as out argument?

流过昼夜 提交于 2020-01-01 05:18:08
问题 Assuming there is following C code: struct Foo { int dummy; } int tryToAllocateFoo(Foo ** dest); ...How to do following in LuaJIT? Foo * pFoo = NULL; tryToAllocateFoo(&pFoo); 回答1: local ffi = require 'ffi' ffi.cdef [[ struct Foo { int dummy; }; int tryToAllocateFoo(Foo ** dest); ]] local theDll = ffi.load(dllName) local pFoo = ffi.new 'struct Foo *[1]' local ok = theDll.tryToAllocateFoo(pFoo) if ok == 0 then -- Assuming it returns 0 on success print('dummy ==', pFoo[0].dummy) end 来源: https:/