From cb1d6b11e1ab48442a7f42b32da5ca6ade50dbe0 Mon Sep 17 00:00:00 2001 From: Christophe Raffalli Date: Sun, 6 Aug 2023 13:56:29 -1000 Subject: [PATCH 1/5] noalloc --- lib/polly.ml | 26 +++++++++++++++++--------- lib/polly_stubs.c | 22 ++++++++++++++-------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/polly.ml b/lib/polly.ml index b96c5db..a9b7c96 100644 --- a/lib/polly.ml +++ b/lib/polly.ml @@ -40,14 +40,16 @@ end type t = Unix.file_descr (* epoll fd *) -external caml_polly_add : t -> Unix.file_descr -> Events.t -> unit - = "caml_polly_add" +external caml_raise_unix_error : string -> 'a = "caml_raise_unix_error" -external caml_polly_del : t -> Unix.file_descr -> Events.t -> unit - = "caml_polly_del" +external caml_polly_add : t -> Unix.file_descr -> Events.t -> int = + "caml_polly_add" [@@noalloc] -external caml_polly_mod : t -> Unix.file_descr -> Events.t -> unit - = "caml_polly_mod" +external caml_polly_del : t -> Unix.file_descr -> Events.t -> int + = "caml_polly_del" [@@noalloc] + +external caml_polly_mod : t -> Unix.file_descr -> Events.t -> int + = "caml_polly_mod" [@@noalloc] external caml_polly_create1 : unit -> t = "caml_polly_create1" @@ -70,11 +72,17 @@ let create = caml_polly_create1 let close t = Unix.close t -let add = caml_polly_add +let add t fd e = + let r = caml_polly_add t fd e in + if r = -1 then caml_raise_unix_error "Polly.caml_polly_add" -let del t fd = caml_polly_del t fd Events.empty +let del t fd = + let r = caml_polly_del t fd Events.empty in + if r = -1 then caml_raise_unix_error "Polly.caml_polly_del" -let upd = caml_polly_mod +let upd t fd e = + let r = caml_polly_mod t fd e in + if r = -1 then caml_raise_unix_error "Polly.caml_polly_mod" let wait = caml_polly_wait diff --git a/lib/polly_stubs.c b/lib/polly_stubs.c index cb590bb..90a8f1d 100644 --- a/lib/polly_stubs.c +++ b/lib/polly_stubs.c @@ -35,19 +35,25 @@ CAMLprim value caml_polly_create1(value val_unit) CAMLreturn(val_res); } +/* necessary because of changes from 4.X to 5.X in ocaml, + uerror is a macro in 5.0 */ +CAMLprim void caml_raise_unix_error(value funname) { + CAMLparam1(funname); + uerror(String_val(funname),Nothing); +} + + static value caml_polly_ctl(value val_epfd, value val_fd, value val_events, int op) { - CAMLparam3(val_epfd, val_fd, val_events); - struct epoll_event event = { - .events = (uint32_t) Int_val(val_events), - .data.fd = Int_val(val_fd) - }; + CAMLparam3(val_epfd, val_fd, val_events); + struct epoll_event event = { + .events = (uint32_t) Int_val(val_events), + .data.fd = Int_val(val_fd) + }; - if (epoll_ctl(Int_val(val_epfd), op, Int_val(val_fd), &event) == -1) - uerror(__FUNCTION__, Nothing); - CAMLreturn(Val_unit); + CAMLreturn(Val_int(epoll_ctl(Int_val(val_epfd), op, Int_val(val_fd), &event))); } CAMLprim value caml_polly_add(value val_epfd, value val_fd, value val_events) From d63e5c3fcf2db295d6b2c232c215854310965e4e Mon Sep 17 00:00:00 2001 From: Christophe Raffalli Date: Sun, 6 Aug 2023 14:16:39 -1000 Subject: [PATCH 2/5] format with 0.26 --- lib/polly.ml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/polly.ml b/lib/polly.ml index a9b7c96..5ec0db7 100644 --- a/lib/polly.ml +++ b/lib/polly.ml @@ -42,14 +42,17 @@ type t = Unix.file_descr (* epoll fd *) external caml_raise_unix_error : string -> 'a = "caml_raise_unix_error" -external caml_polly_add : t -> Unix.file_descr -> Events.t -> int = - "caml_polly_add" [@@noalloc] +external caml_polly_add : t -> Unix.file_descr -> Events.t -> int + = "caml_polly_add" +[@@noalloc] external caml_polly_del : t -> Unix.file_descr -> Events.t -> int - = "caml_polly_del" [@@noalloc] + = "caml_polly_del" +[@@noalloc] external caml_polly_mod : t -> Unix.file_descr -> Events.t -> int - = "caml_polly_mod" [@@noalloc] + = "caml_polly_mod" +[@@noalloc] external caml_polly_create1 : unit -> t = "caml_polly_create1" From d1e7c7eed9e16bb13a3f9fee00beded2e13b7559 Mon Sep 17 00:00:00 2001 From: Christophe Raffalli Date: Sun, 6 Aug 2023 22:38:05 -1000 Subject: [PATCH 3/5] Bad function name in error --- lib/polly.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/polly.ml b/lib/polly.ml index 5ec0db7..065ea39 100644 --- a/lib/polly.ml +++ b/lib/polly.ml @@ -77,15 +77,15 @@ let close t = Unix.close t let add t fd e = let r = caml_polly_add t fd e in - if r = -1 then caml_raise_unix_error "Polly.caml_polly_add" + if r = -1 then caml_raise_unix_error "Polly.add" let del t fd = let r = caml_polly_del t fd Events.empty in - if r = -1 then caml_raise_unix_error "Polly.caml_polly_del" + if r = -1 then caml_raise_unix_error "Polly.del" let upd t fd e = let r = caml_polly_mod t fd e in - if r = -1 then caml_raise_unix_error "Polly.caml_polly_mod" + if r = -1 then caml_raise_unix_error "Polly.mod" let wait = caml_polly_wait From 19c9a1c50c605cadcff3961e1224bd144eae83f8 Mon Sep 17 00:00:00 2001 From: Christophe Raffalli Date: Sun, 6 Aug 2023 22:43:52 -1000 Subject: [PATCH 4/5] r < 0 --- lib/polly.ml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/polly.ml b/lib/polly.ml index 065ea39..146ec4e 100644 --- a/lib/polly.ml +++ b/lib/polly.ml @@ -77,15 +77,15 @@ let close t = Unix.close t let add t fd e = let r = caml_polly_add t fd e in - if r = -1 then caml_raise_unix_error "Polly.add" + if r < 0 then caml_raise_unix_error "Polly.add" let del t fd = let r = caml_polly_del t fd Events.empty in - if r = -1 then caml_raise_unix_error "Polly.del" + if r < 0 then caml_raise_unix_error "Polly.del" let upd t fd e = let r = caml_polly_mod t fd e in - if r = -1 then caml_raise_unix_error "Polly.mod" + if r < 0 then caml_raise_unix_error "Polly.mod" let wait = caml_polly_wait From a8d598c4eb329877059fbe00eb1e86e05a4a421c Mon Sep 17 00:00:00 2001 From: Christophe Raffalli Date: Sun, 6 Aug 2023 22:46:08 -1000 Subject: [PATCH 5/5] update comment about uerror --- lib/polly_stubs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/polly_stubs.c b/lib/polly_stubs.c index 90a8f1d..c953c19 100644 --- a/lib/polly_stubs.c +++ b/lib/polly_stubs.c @@ -35,8 +35,8 @@ CAMLprim value caml_polly_create1(value val_unit) CAMLreturn(val_res); } -/* necessary because of changes from 4.X to 5.X in ocaml, - uerror is a macro in 5.0 */ +/* beware, uerror is a macro in 5.0 and a function in 4.X, the new function + name is caml_uerror. uerror might be deprecated in the future. */ CAMLprim void caml_raise_unix_error(value funname) { CAMLparam1(funname); uerror(String_val(funname),Nothing);