Skip to content

Commit

Permalink
cleanup: use Ctypes_ptr.Fat.set_managed
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Feb 6, 2024
1 parent 008970b commit 7713c8b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/bindings.ml
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,8 @@ let extism_plugin_cancel =

let extism_plugin_id = fn "extism_plugin_id" (ptr void @-> returning (ptr char))
let extism_plugin_reset = fn "extism_plugin_reset" (ptr void @-> returning bool)

let set_managed pointer t =
match pointer with
| Ctypes_static.CPointer pointer ->
Ctypes_ptr.Fat.set_managed pointer (Some (Obj.repr t))
1 change: 1 addition & 0 deletions src/function.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ let create name ?namespace ~params ~results ~user_data f =
let t =
{ pointer; user_data; name; free_lock = Mutex.create (); closure = f }
in
Bindings.set_managed pointer t;
Gc.finalise_last (fun () -> free t) t;
t

Expand Down
1 change: 1 addition & 0 deletions src/plugin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ let create ?config ?(wasi = false) ?(functions = []) wasm =
Error (`Msg s)
else
let t = { pointer; functions; free_lock = Mutex.create () } in
let () = Bindings.set_managed pointer t in
let () = Gc.finalise_last (fun () -> free t) t in
if not (set_config t config) then Error (`Msg "call to set_config failed")
else Ok t
Expand Down

0 comments on commit 7713c8b

Please sign in to comment.