LoongArch: Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook
[gcc.git] / libgm2 / libm2pim / Selective.cc
blob09c57f2f0d4966af9d2db461c1e23ef148299fa2
1 /* Selective.c provide access to timeval and select.
3 Copyright (C) 2009-2022 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 This file is part of GNU Modula-2.
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include <config.h>
28 #include <m2rts.h>
30 #define EXPORT(FUNC) m2pim ## _Selective_ ## FUNC
31 #define M2EXPORT(FUNC) m2pim ## _M2_Selective_ ## FUNC
32 #define M2LIBNAME "m2pim"
34 #if defined(HAVE_STDDEF_H)
35 /* Obtain a definition for NULL. */
36 #include <stddef.h>
37 #endif
39 #if defined(HAVE_STDIO_H)
40 /* Obtain a definition for NULL. */
41 #include <stdio.h>
42 #endif
44 #if defined(HAVE_SYS_TIME_H)
45 #include <sys/time.h>
46 #endif
48 #if defined(HAVE_TIME_H)
49 /* Obtain a definition for NULL. */
50 #include <time.h>
51 #endif
53 #if defined(HAVE_STRING_H)
54 /* Obtain a definition for NULL. */
55 #include <string.h>
56 #endif
58 #if defined(HAVE_WCHAR_H)
59 /* Obtain a definition for NULL. */
60 #include <wchar.h>
61 #endif
63 #if defined(HAVE_STDLIB_H)
64 /* Obtain a prototype for free and malloc. */
65 #include <stdlib.h>
66 #endif
68 #if defined(HAVE_SYS_TYPES_H)
69 #include <sys/types.h>
70 #endif
72 #if defined(HAVE_UNISTD_H)
73 #include <unistd.h>
74 #endif
76 #if !defined(NULL)
77 #define NULL (void *)0
78 #endif
80 #if defined(HAVE_SELECT)
81 #define FDSET_T fd_set
82 #else
83 #define FDSET_T void
84 #endif
86 /* Select wrap a call to the C select. */
88 #if defined(HAVE_STRUCT_TIMEVAL)
89 extern "C" int
90 EXPORT(Select) (int nooffds, fd_set *readfds, fd_set *writefds,
91 fd_set *exceptfds, struct timeval *timeout)
93 return select (nooffds, readfds, writefds, exceptfds, timeout);
95 #else
96 extern "C" int
97 EXPORT(Select) (int nooffds, void *readfds, void *writefds, void *exceptfds,
98 void *timeout)
100 return 0;
102 #endif
104 /* InitTime initializes a timeval structure and returns a pointer to it. */
106 #if defined(HAVE_STRUCT_TIMEVAL)
107 extern "C" struct timeval *
108 EXPORT(InitTime) (unsigned int sec, unsigned int usec)
110 struct timeval *t = (struct timeval *)malloc (sizeof (struct timeval));
112 t->tv_sec = (long int)sec;
113 t->tv_usec = (long int)usec;
114 return t;
117 extern "C" void
118 EXPORT(GetTime) (struct timeval *t, unsigned int *sec, unsigned int *usec)
120 *sec = (unsigned int)t->tv_sec;
121 *usec = (unsigned int)t->tv_usec;
124 extern "C" void
125 EXPORT(SetTime) (struct timeval *t, unsigned int sec, unsigned int usec)
127 t->tv_sec = sec;
128 t->tv_usec = usec;
131 /* KillTime frees the timeval structure and returns NULL. */
133 extern "C" struct timeval *
134 EXPORT(KillTime) (struct timeval *t)
136 #if defined(HAVE_STDLIB_H)
137 free (t);
138 #endif
139 return NULL;
142 /* InitSet returns a pointer to a FD_SET. */
144 extern "C" FDSET_T *
145 EXPORT(InitSet) (void)
147 #if defined(HAVE_STDLIB_H)
148 FDSET_T *s = (FDSET_T *)malloc (sizeof (FDSET_T));
150 return s;
151 #else
152 return NULL
153 #endif
156 /* KillSet frees the FD_SET and returns NULL. */
158 extern "C" FDSET_T *
159 EXPORT(KillSet) (FDSET_T *s)
161 #if defined(HAVE_STDLIB_H)
162 free (s);
163 #endif
164 return NULL;
167 /* FdZero generate an empty set. */
169 extern "C" void
170 EXPORT(FdZero) (FDSET_T *s)
172 FD_ZERO (s);
175 /* FS_Set include an element, fd, into set, s. */
177 extern "C" void
178 EXPORT(FdSet) (int fd, FDSET_T *s)
180 FD_SET (fd, s);
183 /* FdClr exclude an element, fd, from the set, s. */
185 extern "C" void
186 EXPORT(FdClr) (int fd, FDSET_T *s)
188 FD_CLR (fd, s);
191 /* FdIsSet return TRUE if, fd, is present in set, s. */
193 extern "C" int
194 EXPORT(FdIsSet) (int fd, FDSET_T *s)
196 return FD_ISSET (fd, s);
199 /* GetTimeOfDay fills in a record, Timeval, filled in with the
200 current system time in seconds and microseconds.
201 It returns zero (see man 3p gettimeofday). */
203 extern "C" int
204 EXPORT(GetTimeOfDay) (struct timeval *t)
206 return gettimeofday (t, NULL);
208 #else
210 extern "C" void *
211 EXPORT(InitTime) (unsigned int sec, unsigned int usec)
213 return NULL;
216 extern "C" void *
217 EXPORT(KillTime) (void *t)
219 return NULL;
222 extern "C" void
223 EXPORT(GetTime) (void *t, unsigned int *sec, unsigned int *usec)
227 extern "C" void
228 EXPORT(SetTime) (void *t, unsigned int sec, unsigned int usec)
232 extern "C" FDSET_T *
233 EXPORT(InitSet) (void)
235 return NULL;
238 extern "C" FDSET_T *
239 EXPORT(KillSet) (void)
241 return NULL;
244 extern "C" void
245 EXPORT(FdZero) (void *s)
249 extern "C" void
250 EXPORT(FdSet) (int fd, void *s)
254 extern "C" void
255 EXPORT(FdClr) (int fd, void *s)
259 extern "C" int
260 EXPORT(FdIsSet) (int fd, void *s)
262 return 0;
265 extern "C" int
266 EXPORT(GetTimeOfDay) (void *t)
268 return -1;
270 #endif
272 /* MaxFdsPlusOne returns max (a + 1, b + 1). */
274 extern "C" int
275 EXPORT(MaxFdsPlusOne) (int a, int b)
277 if (a > b)
278 return a + 1;
279 else
280 return b + 1;
283 /* WriteCharRaw writes a single character to the file descriptor. */
285 extern "C" void
286 EXPORT(WriteCharRaw) (int fd, char ch)
288 write (fd, &ch, 1);
291 /* ReadCharRaw read and return a single char from file descriptor, fd. */
293 extern "C" char
294 EXPORT(ReadCharRaw) (int fd)
296 char ch;
298 read (fd, &ch, 1);
299 return ch;
302 /* GNU Modula-2 linking hooks. */
304 extern "C" void
305 M2EXPORT(init) (int, char **, char **)
309 extern "C" void
310 M2EXPORT(fini) (int, char **, char **)
314 extern "C" void
315 M2EXPORT(dep) (void)
319 extern "C" void __attribute__((__constructor__))
320 M2EXPORT(ctor) (void)
322 m2pim_M2RTS_RegisterModule ("Selective", M2LIBNAME,
323 M2EXPORT(init), M2EXPORT(fini),
324 M2EXPORT(dep));