Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "ffi"

Title:Dynamically call C functions
Rating:Not rated. Create the first rating!
Latest version:0.8
SHA1 sum:2ec0983dfb7bc87e4eec609cd5e6196f2f3013cd
Author:Jan Wielemaker <J.Wielemaker@vu.nl>
Maintainer:Jan Wielemaker <J.Wielemaker@vu.nl>
Packager:Jan Wielemaker <J.Wielemaker@vu.nl>
Home page:https://github.com/JanWielemaker/ffi.git
Download URL:https://github.com/JanWielemaker/ffi/archive/V*.zip

Reviews

No reviews. Create the first review!.

Details by download location

VersionSHA1#DownloadsURL
0.82ec0983dfb7bc87e4eec609cd5e6196f2f3013cd13https://github.com/JanWielemaker/ffi/archive/V0.8.zip
0.59027f24316ac0adec4c9371b7310fc74b6c1867380https://github.com/JanWielemaker/ffi/archive/V0.5.zip
0.499ed6336c607385254325e3328525bb72bf140b87https://github.com/JanWielemaker/ffi/archive/V0.4.zip
0.3dfa7613cc8aab28679e333265cd1fbb6c701eca21https://github.com/JanWielemaker/ffi/archive/V0.3.zip
0.201229389fab51fca6b2383fb7383c26e668009d11https://github.com/JanWielemaker/ffi/archive/V0.2.zip
0.182265b39dee29dc6836e8f09bea879e41a93cd2c12http://github.com/JanWielemaker/ffi.git
a710ac9120ee3675ea87b96801b6495ba6cc76602http://github.com/JanWielemaker/ffi.git
81b4e9436e6f06d43bbf1c8f41786819a075a13d31http://github.com/JanWielemaker/ffi.git
63b91a242ac8bea8cb13e1e323cde7fd8ae922cc32http://github.com/JanWielemaker/ffi.git

Dynamic calling C from Prolog

This package deals with calling C functions from shared objects (DLLs) from Prolog without writing wrappers. There are several libraries that allows for creating an argument vector for a C function dynamically and call the function. One such library is libffi.

Giving such a library, calling C functions with arithmetic types (integers, floats) is easy, but we still have to deal with structs, unions, enums and the fact that most C libraries do not define the target type directly but use some typedef that binds the concrete type depending on the platform. This package parses the C header file (`.h`) or even a concrete C source file to extract the actual function return and parameter types and all type definition that are involved in these types. In addition it allows fetching constants defined using `#define` macros.

Below is an example making the POSIX statfs() function available.

:- use_module(library(ffi)).
:- use_module(library(cerror)).

:- c_import("#include <sys/vfs.h>",
            [ libc ],
            [ statfs(string, -struct(statfs), [int])
            ]).

statfs(File, FsStat) :-
    statfs(File, FsStat, Status),
    posix_status(Status, statfs, file, File).

The returned pointer may be handed to c_load/2 to extract fields. For example, to get the number of available blocks on the current file system we can make the call below. Note that we did not make any declarations about the structure or the involved integer types.

?- statfs(".", FsStat), c_load(FsStat[f_bavail], A).
FsStat = <C struct statfs[1]>(0x55a2c9d5bae0),
A = 66197957.

Installation

  • Make sure libffi is installed. This is available as package on various systems.
  • Clone this repo
  • Make a link from the `.../swipl/pack/ffi` to the cloned directory
  • Run in Prolog: ?- pack_rebuild(ffi).

Windows installation

Documentation

There is not yet public documentation. With all proper tools installed (Prolog, LaTeX) and a Linux machine you should be able to type make in the doc directory to build the (incomplete) documentation.

Here is a PDF version, created at Feb 18, 2018

Status

  • Portability
    • Tested on Linux (Fedora 26 and Ubuntu 17.10), MacOSX and Windows using MinGW. Mainly limited by libffi.
  • Functionality
    • All major foreseen features are implemented.
    • It is likely that high-level conversions such as between a Prolog list and C array will be added. Such conversions are easily written in Prolog though.
  • API Stability
    • Overall the API is not likely to change much. Possibly the * as prefix operator will be changed to postfix. One may wish to write *(Type) to avoid ambiguity.
  • Documentation
    • First incomplete draft. Please also see the directories test and examples for concrete code examples. Notably examples/python provides a rudimentary Python interface.
  • Testing
    • Only basic functionality is tested. Many more combinatins of types and different ways to define them need to be tested.

Contents of pack "ffi"

Pack contains 67 files holding a total of 398K bytes.