Debian Codegraph @

otherlibs/unix/caml/unixsupport.h

raw
1 /**************************************************************************/
2 /* */
3 /* OCaml */
4 /* */
5 /* Xavier Leroy, projet Cristal, INRIA Rocquencourt */
6 /* */
7 /* Copyright 1996 Institut National de Recherche en Informatique et */
8 /* en Automatique. */
9 /* */
10 /* All rights reserved. This file is distributed under the terms of */
11 /* the GNU Lesser General Public License version 2.1, with the */
12 /* special exception on linking described in the file LICENSE. */
13 /* */
14 /**************************************************************************/
15
16 #ifndef CAML_UNIXSUPPORT_H
17 #define CAML_UNIXSUPPORT_H
18
19 #include <caml/misc.h>
20
21 #ifdef _WIN32 /* Windows */
22 #define WIN32_LEAN_AND_MEAN
23 #include <wtypes.h>
24 #include <winbase.h>
25 #include <stdlib.h>
26 #include <direct.h>
27 #include <process.h>
28 #include <sys/types.h>
29 #include <winsock2.h>
30 #include <ws2tcpip.h>
31 #include <wspiapi.h>
32 #else /* Unix */
33 #ifndef _WIN32
34 #include <unistd.h>
35 #endif
36 #endif
37
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41
42 #ifdef _WIN32
43 struct filedescr {
44 union {
45 HANDLE handle;
46 SOCKET socket;
47 } fd; /* Real windows handle */
48 enum { KIND_HANDLE, KIND_SOCKET } kind;
49 _Atomic int crt_fd; /* C runtime descriptor */
50 unsigned int flags_fd; /* See FLAGS_FD_* */
51 };
52
53 /* Information stored in flags_fd, describing more precisely the socket
54 * and its status. The whole flags_fd is initialized to 0.
55 */
56
57 /* Blocking or nonblocking. By default a filedescr is in blocking state */
58 #define FLAGS_FD_IS_BLOCKING (1<<0)
59
60
61 #define Handle_val(v) (((struct filedescr *) Data_custom_val(v))->fd.handle)
62 #define Socket_val(v) (((struct filedescr *) Data_custom_val(v))->fd.socket)
63 #define Descr_kind_val(v) (((struct filedescr *) Data_custom_val(v))->kind)
64 #define Flags_fd_val(v) (((struct filedescr *) Data_custom_val(v))->flags_fd)
65
66 extern value caml_win32_alloc_handle(HANDLE);
67 extern value caml_win32_alloc_socket(SOCKET);
68
69 #define NO_CRT_FD (-1)
70 #define GETTING_CRT_FD (-2)
71
72 /* Returns the C file descriptor associated with the handle, allocating it if
73 necessary. */
74 extern int caml_win32_CRT_fd_of_filedescr(value handle);
75
76 /* Returns the C file descriptor associated with the handle, or NO_CRT_FD
77 if none was allocated. The crt_fd field should only be accessed through this
78 function and not directly, to avoid race conditions with
79 win_CRT_fd_of_filedescr. */
80 extern int caml_win32_get_CRT_fd(value handle);
81
82 // Export this macro as an alias for the getter function, for compatibility
83 #define CRT_fd_val caml_win32_get_CRT_fd
84
85 extern SOCKET caml_win32_socket(int domain, int type, int protocol,
86 LPWSAPROTOCOL_INFO info,
87 BOOL inherit);
88
89 extern void caml_win32_maperr(DWORD errcode);
90
91 /* There are 11644473600 seconds between 1 January 1601 (the NT Epoch) and 1
92 * January 1970 (the Unix Epoch). FILETIME is measured in 100ns ticks.
93 */
94 #define CAML_NT_EPOCH_100ns_TICKS 116444736000000000ULL
95
96 #endif /* _WIN32 */
97
98 #define Nothing ((value) 0)
99
100 extern value caml_unix_error_of_code (int errcode);
101 extern int caml_unix_code_of_unix_error (value error);
102
103 CAMLnoret extern
104 void caml_unix_error (int errcode, const char * cmdname, value arg);
105
106 CAMLnoret extern void caml_uerror (const char * cmdname, value arg);
107
108 extern void caml_unix_check_path(value path, const char * cmdname);
109
110 #define UNIX_BUFFER_SIZE IO_BUFFER_SIZE
111
112 #define DIR_Val(v) *((DIR **) &Field(v, 0))
113
114 extern char_os ** caml_unix_cstringvect(value arg, const char * cmdname);
115 extern void caml_unix_cstringvect_free(char_os **);
116
117 extern int caml_unix_cloexec_default;
118 extern int caml_unix_cloexec_p(value cloexec);
119
120 #ifdef _WIN32
121 extern int caml_win32_set_inherit(HANDLE fd, BOOL inherit);
122 /* This is a best effort, not guaranteed to work, so don't fail on error */
123 #define caml_win32_set_cloexec(fd, cloexec) \
124 caml_win32_set_inherit((fd), ! caml_unix_cloexec_p((cloexec)))
125 #else
126 extern void caml_unix_set_cloexec(int fd, const char * cmdname, value arg);
127 extern void caml_unix_clear_cloexec(int fd, const char * cmdname, value arg);
128 #endif /* _WIN32 */
129
130 /* Compatibility definitions for the pre-5.0 names of these functions */
131 #ifndef CAML_BUILDING_UNIX
132 #ifdef _WIN32
133 #define win_alloc_handle caml_win32_alloc_handle
134 #define win_alloc_socket caml_win32_alloc_socket
135 #define win_CRT_fd_of_filedescr caml_win32_CRT_fd_of_filedescr
136 #define win32_maperr caml_win32_maperr
137 #endif /* _WIN32 */
138
139 #define unix_error_of_code caml_unix_error_of_code
140 #define code_of_unix_error caml_unix_code_of_unix_error
141
142 #define uerror caml_uerror
143 #define unix_error caml_unix_error
144
145 #define cstringvect caml_unix_cstringvect
146 #define cstringvect_free caml_unix_cstringvect_free
147
148 #define unix_cloexec_default caml_unix_cloexec_default
149 #define unix_cloexec_p caml_unix_cloexec_p
150
151 #ifdef _WIN32
152 #define win_set_inherit caml_win32_set_inherit
153 #define win_set_cloexec caml_win32_set_cloexec
154 #else
155 #define unix_set_cloexec caml_unix_set_cloexec
156 #define unix_clear_cloexec caml_unix_clear_cloexec
157 #endif /* _WIN32 */
158 #endif /* CAML_BUILDING_UNIX */
159
160 #ifdef __cplusplus
161 }
162 #endif
163
164 #ifdef _WIN32
165 #define EXECV_CAST (const char_os * const *)
166 #else
167 #define EXECV_CAST
168 #endif
169
170 #ifdef CAML_INTERNALS
171 #include <time.h>
172 #include <math.h>
173 #ifndef _WIN32
174 #include <sys/time.h>
175 #endif
176
177 Caml_inline struct timespec caml_timespec_of_sec(double sec)
178 {
179 double int_sec, frac_sec;
180 frac_sec = modf(sec, &int_sec);
181 return (struct timespec)
182 { .tv_sec = (time_t) int_sec,
183 #if __STDC_VERSION__ >= 202311L
184 .tv_nsec = (typeof((struct timespec){0}.tv_nsec))
185 #else
186 .tv_nsec = (long)
187 #endif
188 (frac_sec * NSEC_PER_SEC) };
189 }
190
191 Caml_inline struct timeval caml_timeval_of_sec(double sec)
192 {
193 double int_sec, frac_sec;
194 frac_sec = modf(sec, &int_sec);
195 return (struct timeval)
196 #ifdef _WIN32
197 { .tv_sec = (long) int_sec,
198 .tv_usec = (long) (frac_sec * USEC_PER_SEC) };
199 #else
200 { .tv_sec = (time_t) int_sec,
201 .tv_usec = (suseconds_t) (frac_sec * USEC_PER_SEC) };
202 #endif
203 }
204 #endif /* CAML_INTERNALS */
205
206 #endif /* CAML_UNIXSUPPORT_H */