From fd1397b58023a19e2cb3d5267054613292dd6664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Fri, 2 Feb 2024 14:09:32 +0100 Subject: [PATCH 1/4] Constify C constructors and flags tables Now these tables will go in the readonly segment, where they belong. --- src/unix/lwt_unix_stubs.c | 7 ++++--- src/unix/unix_c/unix_access_job.c | 2 +- src/unix/unix_c/unix_getnameinfo_job.c | 4 ++-- src/unix/unix_c/unix_lockf_job.c | 4 ++-- src/unix/unix_c/unix_lseek_job.c | 2 +- src/unix/unix_c/unix_madvise.c | 2 +- src/unix/unix_c/unix_open_job.c | 2 +- src/unix/unix_c/unix_recv_send_utils.c | 2 +- src/unix/unix_c/unix_recv_send_utils.h | 2 +- src/unix/unix_c/unix_tcflow_job.c | 2 +- src/unix/unix_c/unix_tcflush_job.c | 2 +- src/unix/unix_c/unix_tcsetattr_job.c | 2 +- src/unix/unix_c/unix_termios_conversion.c | 10 ++++------ src/unix/unix_c/unix_wait4.c | 2 +- 14 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/unix/lwt_unix_stubs.c b/src/unix/lwt_unix_stubs.c index a8eedde877..443773baca 100644 --- a/src/unix/lwt_unix_stubs.c +++ b/src/unix/lwt_unix_stubs.c @@ -465,10 +465,11 @@ static void lwt_unix_socketpair(int domain, int type, int protocol, uerror("socketpair", Nothing); } -static int socket_domain_table[] = {PF_UNIX, PF_INET, PF_INET6}; +static const int socket_domain_table[] = + {PF_UNIX, PF_INET, PF_INET6}; -static int socket_type_table[] = {SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, - SOCK_SEQPACKET}; +static const int socket_type_table[] = + {SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET}; CAMLprim value lwt_unix_socketpair_stub(value cloexec, value domain, value type, value protocol) { diff --git a/src/unix/unix_c/unix_access_job.c b/src/unix/unix_c/unix_access_job.c index a054fc34cb..0a607686e7 100644 --- a/src/unix/unix_c/unix_access_job.c +++ b/src/unix/unix_c/unix_access_job.c @@ -34,7 +34,7 @@ +-----------------------------------------------------------------+ */ /* Table mapping constructors of ocaml type Unix.access_permission to C values. */ -static int access_permission_table[] = { +static const int access_permission_table[] = { /* Constructor R_OK. */ R_OK, /* Constructor W_OK. */ diff --git a/src/unix/unix_c/unix_getnameinfo_job.c b/src/unix/unix_c/unix_getnameinfo_job.c index 3ae74091df..8f5e3bbdbd 100644 --- a/src/unix/unix_c/unix_getnameinfo_job.c +++ b/src/unix/unix_c/unix_getnameinfo_job.c @@ -28,8 +28,8 @@ struct job_getnameinfo { int result; }; -static int getnameinfo_flag_table[] = {NI_NOFQDN, NI_NUMERICHOST, NI_NAMEREQD, - NI_NUMERICSERV, NI_DGRAM}; +static const int getnameinfo_flag_table[] = + {NI_NOFQDN, NI_NUMERICHOST, NI_NAMEREQD, NI_NUMERICSERV, NI_DGRAM}; static void worker_getnameinfo(struct job_getnameinfo *job) { diff --git a/src/unix/unix_c/unix_lockf_job.c b/src/unix/unix_c/unix_lockf_job.c index a0449d80fd..67732f8b01 100644 --- a/src/unix/unix_c/unix_lockf_job.c +++ b/src/unix/unix_c/unix_lockf_job.c @@ -84,8 +84,8 @@ static void worker_lockf(struct job_lockf *job) #else -static int lock_command_table[] = {F_ULOCK, F_LOCK, F_TLOCK, - F_TEST, F_LOCK, F_TLOCK}; +static const int lock_command_table[] = + {F_ULOCK, F_LOCK, F_TLOCK, F_TEST, F_LOCK, F_TLOCK}; static void worker_lockf(struct job_lockf *job) { diff --git a/src/unix/unix_c/unix_lseek_job.c b/src/unix/unix_c/unix_lseek_job.c index fe2740493d..291611a846 100644 --- a/src/unix/unix_c/unix_lseek_job.c +++ b/src/unix/unix_c/unix_lseek_job.c @@ -35,7 +35,7 @@ +-----------------------------------------------------------------+ */ /* Table mapping constructors of ocaml type Unix.seek_command to C values. */ -static int seek_command_table[] = { +static const int seek_command_table[] = { /* Constructor SEEK_SET. */ SEEK_SET, /* Constructor SEEK_CUR. */ diff --git a/src/unix/unix_c/unix_madvise.c b/src/unix/unix_c/unix_madvise.c index f619a4441e..51b1d3ce99 100644 --- a/src/unix/unix_c/unix_madvise.c +++ b/src/unix/unix_c/unix_madvise.c @@ -12,7 +12,7 @@ #include #include -static int advise_table[] = { +static const int advise_table[] = { MADV_NORMAL, MADV_RANDOM, MADV_SEQUENTIAL, MADV_WILLNEED, MADV_DONTNEED, #if defined(MADV_MERGEABLE) MADV_MERGEABLE, diff --git a/src/unix/unix_c/unix_open_job.c b/src/unix/unix_c/unix_open_job.c index a77ad1cabf..0c1be84591 100644 --- a/src/unix/unix_c/unix_open_job.c +++ b/src/unix/unix_c/unix_open_job.c @@ -35,7 +35,7 @@ #define caml_unix_cloexec_default unix_cloexec_default #endif -static int open_flag_table[] = { +static const int open_flag_table[] = { O_RDONLY, O_WRONLY, O_RDWR, O_NONBLOCK, O_APPEND, O_CREAT, O_TRUNC, O_EXCL, O_NOCTTY, O_DSYNC, O_SYNC, O_RSYNC, 0, /* O_SHARE_DELETE, Windows-only */ diff --git a/src/unix/unix_c/unix_recv_send_utils.c b/src/unix/unix_c/unix_recv_send_utils.c index f9486afe9f..bbb3d9c1f5 100644 --- a/src/unix/unix_c/unix_recv_send_utils.c +++ b/src/unix/unix_c/unix_recv_send_utils.c @@ -19,7 +19,7 @@ #include "unix_recv_send_utils.h" -int msg_flag_table[3] = {MSG_OOB, MSG_DONTROUTE, MSG_PEEK}; +const int msg_flag_table[3] = {MSG_OOB, MSG_DONTROUTE, MSG_PEEK}; value wrapper_recv_msg(int fd, int n_iovs, struct iovec *iovs) { diff --git a/src/unix/unix_c/unix_recv_send_utils.h b/src/unix/unix_c/unix_recv_send_utils.h index f4b9bd76d4..97cea4fcc8 100644 --- a/src/unix/unix_c/unix_recv_send_utils.h +++ b/src/unix/unix_c/unix_recv_send_utils.h @@ -36,7 +36,7 @@ #define caml_unix_socket_type_table socket_type_table #endif -extern int msg_flag_table[]; +extern const int msg_flag_table[]; extern int caml_unix_socket_domain_table[]; extern int caml_unix_socket_type_table[]; extern void get_sockaddr(value mladdr, union sock_addr_union *addr /*out*/, diff --git a/src/unix/unix_c/unix_tcflow_job.c b/src/unix/unix_c/unix_tcflow_job.c index 764fe3c577..24042b80c2 100644 --- a/src/unix/unix_c/unix_tcflow_job.c +++ b/src/unix/unix_c/unix_tcflow_job.c @@ -35,7 +35,7 @@ +-----------------------------------------------------------------+ */ /* Table mapping constructors of ocaml type Unix.flow_action to C values. */ -static int flow_action_table[] = { +static const int flow_action_table[] = { /* Constructor TCOOFF. */ TCOOFF, /* Constructor TCOON. */ diff --git a/src/unix/unix_c/unix_tcflush_job.c b/src/unix/unix_c/unix_tcflush_job.c index 9287ca8e86..d76bf1ee11 100644 --- a/src/unix/unix_c/unix_tcflush_job.c +++ b/src/unix/unix_c/unix_tcflush_job.c @@ -35,7 +35,7 @@ +-----------------------------------------------------------------+ */ /* Table mapping constructors of ocaml type Unix.flush_queue to C values. */ -static int flush_queue_table[] = { +static const int flush_queue_table[] = { /* Constructor TCIFLUSH. */ TCIFLUSH, /* Constructor TCOFLUSH. */ diff --git a/src/unix/unix_c/unix_tcsetattr_job.c b/src/unix/unix_c/unix_tcsetattr_job.c index b699b50692..9d3029db28 100644 --- a/src/unix/unix_c/unix_tcsetattr_job.c +++ b/src/unix/unix_c/unix_tcsetattr_job.c @@ -24,7 +24,7 @@ struct job_tcsetattr { int error_code; }; -static int when_flag_table[] = {TCSANOW, TCSADRAIN, TCSAFLUSH}; +static const int when_flag_table[] = {TCSANOW, TCSADRAIN, TCSAFLUSH}; static void worker_tcsetattr(struct job_tcsetattr *job) { diff --git a/src/unix/unix_c/unix_termios_conversion.c b/src/unix/unix_c/unix_termios_conversion.c index e9b82f8e6f..d47eb68572 100644 --- a/src/unix/unix_c/unix_termios_conversion.c +++ b/src/unix/unix_c/unix_termios_conversion.c @@ -20,7 +20,7 @@ enum { Iflags, Oflags, Cflags, Lflags }; /* Structure of the terminal_io record. Cf. unix.mli */ -static long terminal_io_descr[] = { +static const long terminal_io_descr[] = { /* Input modes */ Bool, Iflags, IGNBRK, Bool, Iflags, BRKINT, Bool, Iflags, IGNPAR, Bool, Iflags, PARMRK, Bool, Iflags, INPCK, Bool, Iflags, ISTRIP, Bool, Iflags, @@ -40,7 +40,7 @@ static long terminal_io_descr[] = { Char, VINTR, Char, VQUIT, Char, VERASE, Char, VKILL, Char, VEOF, Char, VEOL, Char, VMIN, Char, VTIME, Char, VSTART, Char, VSTOP, End}; -static struct { +static const struct { speed_t speed; int baud; } speedtable[] = {{B50, 50}, @@ -152,10 +152,9 @@ static tcflag_t *choose_field(struct termios *terminal_status, long field) void encode_terminal_status(struct termios *terminal_status, volatile value *dst) { - long *pc; int i; - for (pc = terminal_io_descr; *pc != End; dst++) { + for (const long *pc = terminal_io_descr; *pc != End; dst++) { switch (*pc++) { case Bool: { tcflag_t *src = choose_field(terminal_status, *pc++); @@ -209,10 +208,9 @@ void encode_terminal_status(struct termios *terminal_status, volatile value *dst int decode_terminal_status(struct termios *terminal_status, volatile value *src) { - long *pc; int i; - for (pc = terminal_io_descr; *pc != End; src++) { + for (const long *pc = terminal_io_descr; *pc != End; src++) { switch (*pc++) { case Bool: { tcflag_t *dst = choose_field(terminal_status, *pc++); diff --git a/src/unix/unix_c/unix_wait4.c b/src/unix/unix_c/unix_wait4.c index daa9ab4f49..eae1aa6ad3 100644 --- a/src/unix/unix_c/unix_wait4.c +++ b/src/unix/unix_c/unix_wait4.c @@ -58,7 +58,7 @@ static value alloc_process_status(int status) return st; } -static int wait_flag_table[] = {WNOHANG, WUNTRACED}; +static const int wait_flag_table[] = {WNOHANG, WUNTRACED}; value lwt_unix_wait4(value flags, value pid_req) { From 7485bbcd8d19314acce79418adde12450a1db1ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Fri, 2 Feb 2024 14:09:48 +0100 Subject: [PATCH 2/4] Reduce scope of local variable --- src/unix/unix_c/unix_termios_conversion.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/unix/unix_c/unix_termios_conversion.c b/src/unix/unix_c/unix_termios_conversion.c index d47eb68572..cee17025fa 100644 --- a/src/unix/unix_c/unix_termios_conversion.c +++ b/src/unix/unix_c/unix_termios_conversion.c @@ -152,8 +152,6 @@ static tcflag_t *choose_field(struct termios *terminal_status, long field) void encode_terminal_status(struct termios *terminal_status, volatile value *dst) { - int i; - for (const long *pc = terminal_io_descr; *pc != End; dst++) { switch (*pc++) { case Bool: { @@ -167,7 +165,7 @@ void encode_terminal_status(struct termios *terminal_status, volatile value *dst int ofs = *pc++; int num = *pc++; tcflag_t msk = *pc++; - for (i = 0; i < num; i++) { + for (int i = 0; i < num; i++) { if ((*src & msk) == pc[i]) { *dst = Val_int(i + ofs); break; @@ -189,7 +187,7 @@ void encode_terminal_status(struct termios *terminal_status, volatile value *dst speed = cfgetispeed(terminal_status); break; } - for (i = 0; i < NSPEEDS; i++) { + for (int i = 0; i < NSPEEDS; i++) { if (speed == speedtable[i].speed) { *dst = Val_int(speedtable[i].baud); break; @@ -208,8 +206,6 @@ void encode_terminal_status(struct termios *terminal_status, volatile value *dst int decode_terminal_status(struct termios *terminal_status, volatile value *src) { - int i; - for (const long *pc = terminal_io_descr; *pc != End; src++) { switch (*pc++) { case Bool: { @@ -226,7 +222,7 @@ int decode_terminal_status(struct termios *terminal_status, volatile value *src) int ofs = *pc++; int num = *pc++; tcflag_t msk = *pc++; - i = Int_val(*src) - ofs; + int i = Int_val(*src) - ofs; if (i >= 0 && i < num) { *dst = (*dst & ~msk) | pc[i]; } else { @@ -240,7 +236,7 @@ int decode_terminal_status(struct termios *terminal_status, volatile value *src) int which = *pc++; int baud = Int_val(*src); int res = 0; - for (i = 0; i < NSPEEDS; i++) { + for (int i = 0; i < NSPEEDS; i++) { if (baud == speedtable[i].baud) { switch (which) { case Output: From 5978227cc5baa3327cb365cc5e0d0cec77bc1be5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Fri, 2 Feb 2024 14:22:45 +0100 Subject: [PATCH 3/4] Constify function parameters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Antonin Décimo --- src/unix/unix_c/unix_getaddrinfo_job.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/unix/unix_c/unix_getaddrinfo_job.c b/src/unix/unix_c/unix_getaddrinfo_job.c index 395335d15f..d7b1f8d0de 100644 --- a/src/unix/unix_c/unix_getaddrinfo_job.c +++ b/src/unix/unix_c/unix_getaddrinfo_job.c @@ -29,7 +29,7 @@ struct job_getaddrinfo { char data[]; }; -static value cst_to_constr(int n, int *tbl, int size, int deflt) +static value cst_to_constr(int n, const int *tbl, int size, int deflt) { int i; for (i = 0; i < size; i++) @@ -37,7 +37,7 @@ static value cst_to_constr(int n, int *tbl, int size, int deflt) return Val_int(deflt); } -static value convert_addrinfo(struct addrinfo *a) +static value convert_addrinfo(const struct addrinfo *a) { CAMLparam0(); CAMLlocal3(vres, vaddr, vcanonname); From 98607c46c3ab253d80ef715f3028bec0ea3c574c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonin=20D=C3=A9cimo?= Date: Fri, 2 Feb 2024 14:23:42 +0100 Subject: [PATCH 4/4] Constify caml_unix_socket_{domain,type}_table in OCaml 5.03 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Antonin Décimo --- src/unix/unix_c/unix_recv_send_utils.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/unix/unix_c/unix_recv_send_utils.h b/src/unix/unix_c/unix_recv_send_utils.h index 97cea4fcc8..407091f6e2 100644 --- a/src/unix/unix_c/unix_recv_send_utils.h +++ b/src/unix/unix_c/unix_recv_send_utils.h @@ -36,9 +36,15 @@ #define caml_unix_socket_type_table socket_type_table #endif -extern const int msg_flag_table[]; +#if OCAML_VERSION < 50300 extern int caml_unix_socket_domain_table[]; extern int caml_unix_socket_type_table[]; +#else +extern const int caml_unix_socket_domain_table[]; +extern const int caml_unix_socket_type_table[]; +#endif + +extern const int msg_flag_table[]; extern void get_sockaddr(value mladdr, union sock_addr_union *addr /*out*/, socklen_t *addr_len /*out*/); value wrapper_recv_msg(int fd, int n_iovs, struct iovec *iovs);