On this page:
ptr_  t->cpointer
cpointer->ptr_  t
9.2.0.3

3 Interoperability with ffi/unsafe🔗ℹ

The ffi2 library is designed to provide better performance than ffi/unsafe for calling foreign functions and working with pointer objects. It also relies less on dynamic code generation for calling foreign functions. The main difference is that ffi2 is more static (e.g., argument types for functions are not represented by run-time values), so it can compile foreign calls more directly, and it can generate the code for those calls when Racket programs are compiled ahead of time (by using raco make, for example). Another difference is that the representation of pointer objects is simplified, requiring less allocation to create pointer objects and less internal branching to work with them.

The ffi2 and ffi/unsafe libraries can coexist within a single Racket application. Libraries like ffi/unsafe/alloc and ffi/unsafe/atomic, and the procedure register-finalizer form ffi/unsafe remain particularly useful in an application that otherwise uses ffi2.

The main incompatibility between ffi2 and ffi/unsafe is that they have different pointer representations, but ptr_t->cpointer and cpointer->ptr_t support conversions between the representations.

procedure

(ptr_t->cpointer ptr)  cpointer?

  ptr : ptr_t?

procedure

(cpointer->ptr_t cptr)  ptr_t?

  cptr : cpointer?
The ptr_t->cpointer procedure converts a pointer object for ffi2 to a “C pointer” for ffi/unsafe. The cpointer->ptr_t function converts in the opposite direction.