Skip to content

Commit fa7d66d

Browse files
committed
cpg: initial binding to libcpg
1 parent 9beb194 commit fa7d66d

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

lib/cpg.ml

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
open Ctypes
2+
open Foreign
3+
open Corotypes
4+
open CsError
5+
6+
let ( >>= ) = Result.bind
7+
8+
let cpg_handle_t = uint64_t
9+
10+
type cpg_name
11+
12+
let cpg_name : cpg_name structure typ = structure "cpg_name"
13+
14+
let cpg_name_length = field cpg_name "length" uint32_t
15+
16+
let cpg_name_value = field cpg_name "value" (ptr char)
17+
18+
let () = seal cpg_name
19+
20+
type cpg_address
21+
22+
let cpg_address : cpg_address structure typ = structure "cpg_address"
23+
24+
let nodeid = field cpg_address "nodeid" uint32_t
25+
26+
let pid = field cpg_address "pid" uint32_t
27+
28+
let reason = field cpg_address "reason" uint32_t
29+
30+
let () = seal cpg_address
31+
32+
let cpg_deliver_fn_t =
33+
cpg_handle_t @-> ptr cpg_name @-> uint32_t @-> uint32_t @-> returning void
34+
35+
let cpg_confchg_fn_t =
36+
cpg_handle_t
37+
@-> ptr cpg_name
38+
@-> ptr cpg_address
39+
@-> size_t
40+
@-> ptr cpg_address
41+
@-> size_t
42+
@-> cpg_address
43+
@-> size_t
44+
@-> returning void
45+
46+
(* struct cpg_callbacks_t *)
47+
48+
type cpg_callbacks_t
49+
50+
let cpg_callbacks_t : cpg_callbacks_t structure typ =
51+
structure "cpg_callbacks_t"
52+
53+
let cpg_deliver_fn =
54+
field cpg_callbacks_t "cpg_deliver_fn" (funptr cpg_deliver_fn_t)
55+
56+
let cpg_confchg_fn =
57+
field cpg_callbacks_t "cpg_confchg_fn" (funptr cpg_confchg_fn_t)
58+
59+
let () = seal cpg_callbacks_t
60+
61+
(* cpg bindings *)
62+
63+
let cpg_initialize =
64+
foreign "cpg_initialize"
65+
(ptr cpg_handle_t @-> ptr cpg_callbacks_t @-> returning cs_error_t)
66+
67+
let cpg_finalize = foreign "cpg_finalize" (cpg_handle_t @-> returning cs_error_t)
68+
69+
let cpg_fd_get =
70+
foreign "cpg_fd_get" (cpg_handle_t @-> ptr int @-> returning cs_error_t)
71+
72+
let cpg_join =
73+
foreign "cpg_join" (cpg_handle_t @-> ptr cpg_name @-> returning cs_error_t)
74+
75+
let cpg_leave =
76+
foreign "cpg_leave" (cpg_handle_t @-> ptr cpg_name @-> returning cs_error_t)
77+
78+
let cpg_membership_get =
79+
foreign "cpg_member_ship_get"
80+
(cpg_handle_t
81+
@-> ptr cpg_name
82+
@-> ptr cpg_address
83+
@-> ptr int
84+
@-> returning cs_error_t
85+
)
86+
87+
let cpg_local_get =
88+
foreign "cpg_local_get" (cpg_handle_t @-> ptr uint @-> returning cs_error_t)
89+
90+
(* ocaml APIs *)
91+
let with_handle f =
92+
let handle = allocate cpg_handle_t Unsigned.UInt64.zero in
93+
cpg_initialize handle (from_voidp cpg_callbacks_t null)
94+
|> CsError.to_result
95+
>>= fun () ->
96+
let r = f !@handle in
97+
cpg_finalize !@handle |> CsError.to_result >>= fun () -> r
98+
99+
let fd_get handle =
100+
let fd = allocate int 0 in
101+
cpg_fd_get handle fd |> to_result >>= fun () ->
102+
Ok !@fd
103+
104+

0 commit comments

Comments
 (0)