Skip to content

Instantly share code, notes, and snippets.

@mseri
Created October 30, 2018 12:52
Show Gist options
  • Save mseri/36a92226d8358ff7dac45c7621c6bb9e to your computer and use it in GitHub Desktop.
Save mseri/36a92226d8358ff7dac45c7621c6bb9e to your computer and use it in GitHub Desktop.
How pkcs11 is solving the issue of ctypes Carray lifetimes
module Reachable_ptr : sig
type 'a t
val typ : 'a Ctypes_static.typ -> 'a t Ctypes_static.typ
val setf : ('b, 'c) Ctypes.structured ->
('a t, ('b, 'c) Ctypes.structured) Ctypes.field -> 'a Ctypes.ptr -> unit
val getf : ('b, 'c) Ctypes.structured ->
('a t, ('b, 'c) Ctypes.structured) Ctypes.field -> 'a Ctypes.ptr
end = struct
type 'a t = 'a ptr
let typ = Ctypes.ptr
(** Add a GC dependency from one object to another:
while [from] is reachable, [to_] is reachable too. *)
let add_gc_link ~from ~to_ =
let r = ref (Some (Obj.repr to_)) in
let finaliser _ = r := None in
Gc.finalise finaliser from
let setf s f v =
add_gc_link ~from:s ~to_:v;
Ctypes.setf s f v
let getf = Ctypes.getf
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment