kphp icon indicating copy to clipboard operation
kphp copied to clipboard

[compiler, runtime] add FFI callbacks support

Open quasilyte opened this issue 3 years ago • 0 comments

  • Improved PHP FFI nullptr handling (+ added tests #456)
  • Added FFI callbacks support (#450 #425)
  • Added unmanaged FFI memory allocation
  • Fixed double-vs-float token issue in FFI (#584)
  • Added @kphp-ffi-signalsafe annotation for C headers (cdef #586)
  • Fixed imprecise C parser error locations (#446)
  • Added ffi_cast_addr2ptr and ffi_cast_ptr2addr functions

Unmanaged memory is handles like this:

  • When unmanaged chunk is allocated, a refcnt is 2 instead of 1
  • So, this memory is never deallocated via that object
  • When free is used on such object, refcnt is decreased
  • If that was the last reference, the memory will be deallocated
    • Otherwise it will be deallocated when the last reference dies
    • It's UB to call free on the same memory chunk more than once

The memory can be deallocated via the non-owning pointer like CDataPtr; it's UB to call free on the owning object after that. There should be exactly 1 free call per every allocated memory chunk. The FFI documentation describes that and shares a few common patterns.

FFI callback functions have restrictions placed on them:

  • Can't throw from it
  • Can't have captured state (no instance methods, no closures)
  • Can't be resumable

Can use stateless lambdas, freestanding functions, and static methods as FFI callbacks.

Fixes #450 Fixes #425 Fixes #456 Fixes #585 Fixes #584 Fixes #586 Fixes #451 Fixes #446

quasilyte avatar Aug 05 '22 07:08 quasilyte