Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constify C constructors and flags tables #1007

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/unix/lwt_unix_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_access_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
4 changes: 2 additions & 2 deletions src/unix/unix_c/unix_getaddrinfo_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ 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++)
if (n == tbl[i]) return Val_int(i);
return Val_int(deflt);
}

static value convert_addrinfo(struct addrinfo *a)
static value convert_addrinfo(const struct addrinfo *a)
{
CAMLparam0();
CAMLlocal3(vres, vaddr, vcanonname);
Expand Down
4 changes: 2 additions & 2 deletions src/unix/unix_c/unix_getnameinfo_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions src/unix/unix_c/unix_lockf_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_lseek_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_madvise.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <caml/bigarray.h>
#include <sys/mman.h>

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,
Expand Down
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_open_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_recv_send_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 7 additions & 1 deletion src/unix/unix_c/unix_recv_send_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,15 @@
#define caml_unix_socket_type_table socket_type_table
#endif

extern 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);
Expand Down
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_tcflow_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_tcflush_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_tcsetattr_job.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
22 changes: 8 additions & 14 deletions src/unix/unix_c/unix_termios_conversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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},
Expand Down Expand Up @@ -152,10 +152,7 @@ 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++);
Expand All @@ -168,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;
Expand All @@ -190,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;
Expand All @@ -209,10 +206,7 @@ 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++);
Expand All @@ -228,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 {
Expand All @@ -242,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:
Expand Down
2 changes: 1 addition & 1 deletion src/unix/unix_c/unix_wait4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Loading