6d6e6838d359edbd15fd591d57185f298e5ad8ab
[cacao.git] / src / vmcore / system.h
1 /* src/vmcore/system.h - system (OS) functions
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program 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.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _VMCORE_SYSTEM_H
27 #define _VMCORE_SYSTEM_H
28
29 #include "config.h"
30
31 /* NOTE: In this file we check for all system headers, because we wrap
32    all system calls into inline functions for better portability. */
33
34 #if defined(HAVE_DIRENT_H)
35 # include <dirent.h>
36 #endif
37
38 #if defined(HAVE_FCNTL_H)
39 # include <fcntl.h>
40 #endif
41
42 #if defined(ENABLE_JRE_LAYOUT)
43 # if defined(HAVE_LIBGEN_H)
44 #  include <libgen.h>
45 # endif
46 #endif
47
48 #if defined(HAVE_STDINT_H)
49 # include <stdint.h>
50 #endif
51
52 #if defined(HAVE_STDIO_H)
53 # include <stdio.h>
54 #endif
55
56 #if defined(HAVE_STDLIB_H)
57 # include <stdlib.h>
58 #endif
59
60 #if defined(HAVE_STRING_H)
61 # include <string.h>
62 #endif
63
64 #if defined(HAVE_UNISTD_H)
65 # include <unistd.h>
66 #endif
67
68 #if defined(HAVE_SYS_MMAN_H)
69 # include <sys/mman.h>
70 #endif
71
72 #if defined(HAVE_SYS_SOCKET_H)
73 # include <sys/socket.h>
74 #endif
75
76 #if defined(HAVE_SYS_STAT_H)
77 # include <sys/stat.h>
78 #endif
79
80 #if defined(HAVE_SYS_TYPES_H)
81 # include <sys/types.h>
82 #endif
83
84
85 /* inline functions ***********************************************************/
86
87 inline static void system_abort(void)
88 {
89 #if defined(HAVE_ABORT)
90         abort();
91 #else
92 # error abort not available
93 #endif
94 }
95
96 inline static int system_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
97 {
98 #if defined(HAVE_ACCEPT)
99         return accept(sockfd, addr, addrlen);
100 #else
101 # error accept not available
102 #endif
103 }
104
105 inline static int system_access(const char *pathname, int mode)
106 {
107 #if defined(HAVE_ACCESS)
108         return access(pathname, mode);
109 #else
110 # error access not available
111 #endif
112 }
113
114 inline static int system_atoi(const char *nptr)
115 {
116 #if defined(HAVE_ATOI)
117         return atoi(nptr);
118 #else
119 # error atoi not available
120 #endif
121 }
122
123 inline static void *system_calloc(size_t nmemb, size_t size)
124 {
125 #if defined(HAVE_CALLOC)
126         return calloc(nmemb, size);
127 #else
128 # error calloc not available
129 #endif
130 }
131
132 inline static int system_close(int fd)
133 {
134 #if defined(HAVE_CLOSE)
135         return close(fd);
136 #else
137 # error close not available
138 #endif
139 }
140
141 inline static int system_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen)
142 {
143 #if defined(HAVE_CONNECT)
144         return connect(sockfd, serv_addr, addrlen);
145 #else
146 # error connect not available
147 #endif
148 }
149
150 #if defined(ENABLE_JRE_LAYOUT)
151 inline static char *system_dirname(char *path)
152 {
153 #if defined(HAVE_DIRNAME)
154         return dirname(path);
155 #else
156 # error dirname not available
157 #endif
158 }
159 #endif
160
161 inline static FILE *system_fopen(const char *path, const char *mode)
162 {
163 #if defined(HAVE_FOPEN)
164         return fopen(path, mode);
165 #else
166 # error fopen not available
167 #endif
168 }
169
170 inline static int system_fclose(FILE *fp)
171 {
172 #if defined(HAVE_FCLOSE)
173         return fclose(fp);
174 #else
175 # error fclose not available
176 #endif
177 }
178
179 inline static size_t system_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
180 {
181 #if defined(HAVE_FREAD)
182         return fread(ptr, size, nmemb, stream);
183 #else
184 # error fread not available
185 #endif
186 }
187
188 inline static void system_free(void *ptr)
189 {
190 #if defined(HAVE_FREE)
191         free(ptr);
192 #else
193 # error free not available
194 #endif
195 }
196
197 inline static int system_fsync(int fd)
198 {
199 #if defined(HAVE_FSYNC)
200         return fsync(fd);
201 #else
202 # error fsync not available
203 #endif
204 }
205
206 inline static int system_ftruncate(int fd, off_t length)
207 {
208 #if defined(HAVE_FTRUNCATE)
209         return ftruncate(fd, length);
210 #else
211 # error ftruncate not available
212 #endif
213 }
214
215 inline static int system_gethostname(char *name, size_t len)
216 {
217 #if defined(HAVE_GETHOSTNAME)
218         return gethostname(name, len);
219 #else
220 # error gethostname not available
221 #endif
222 }
223
224 inline static int system_getpagesize(void)
225 {
226 #if defined(HAVE_GETPAGESIZE)
227         return getpagesize();
228 #else
229 # error getpagesize not available
230 #endif
231 }
232
233 inline static int system_getsockname(int s, struct sockaddr *name, socklen_t *namelen)
234 {
235 #if defined(HAVE_GETSOCKNAME)
236         return getsockname(s, name, namelen);
237 #else
238 # error getsockname not available
239 #endif
240 }
241
242 inline static int system_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
243 {
244 #if defined(HAVE_GETSOCKOPT)
245         return getsockopt(s, level, optname, optval, optlen);
246 #else
247 # error getsockopt not available
248 #endif
249 }
250
251 inline static int system_listen(int sockfd, int backlog)
252 {
253 #if defined(HAVE_LISTEN)
254         return listen(sockfd, backlog);
255 #else
256 # error listen not available
257 #endif
258 }
259
260 inline static off_t system_lseek(int fildes, off_t offset, int whence)
261 {
262 #if defined(HAVE_LSEEK)
263         return lseek(fildes, offset, whence);
264 #else
265 # error lseek not available
266 #endif
267 }
268
269 inline static void *system_malloc(size_t size)
270 {
271 #if defined(HAVE_MALLOC)
272         return malloc(size);
273 #else
274 # error malloc not available
275 #endif
276 }
277
278 inline static void *system_memcpy(void *dest, const void *src, size_t n)
279 {
280 #if defined(HAVE_MEMCPY)
281         return memcpy(dest, src, n);
282 #else
283 # error memcpy not available
284 #endif
285 }
286
287 inline static void *system_memset(void *s, int c, size_t n)
288 {
289 #if defined(HAVE_MEMSET)
290         return memset(s, c, n);
291 #else
292 # error memset not available
293 #endif
294 }
295
296 inline static int system_mprotect(void *addr, size_t len, int prot)
297 {
298 #if defined(HAVE_MPROTECT)
299         return mprotect(addr, len, prot);
300 #else
301 # error mprotect not available
302 #endif
303 }
304
305 inline static int system_open(const char *pathname, int flags, mode_t mode)
306 {
307 #if defined(HAVE_OPEN)
308         return open(pathname, flags, mode);
309 #else
310 # error open not available
311 #endif
312 }
313
314 inline static ssize_t system_read(int fd, void *buf, size_t count)
315 {
316 #if defined(HAVE_READ)
317         return read(fd, buf, count);
318 #else
319 # error read not available
320 #endif
321 }
322
323 inline static void *system_realloc(void *ptr, size_t size)
324 {
325 #if defined(HAVE_REALLOC)
326         return realloc(ptr, size);
327 #else
328 # error realloc not available
329 #endif
330 }
331
332 #if defined(__LINUX__)
333 inline static int system_scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
334 #elif defined(__IRIX__)
335 inline static int system_scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
336 #else
337 inline static int system_scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
338 #endif
339 {
340 #if defined(HAVE_SCANDIR)
341         return scandir(dir, namelist, filter, compar);
342 #else
343 # error scandir not available
344 #endif
345 }
346
347 inline static int system_setsockopt(int s, int level, int optname, const void *optval, socklen_t optlen)
348 {
349 #if defined(HAVE_SETSOCKOPT)
350         return setsockopt(s, level, optname, optval, optlen);
351 #else
352 # error setsockopt not available
353 #endif
354 }
355
356 inline static int system_shutdown(int s, int how)
357 {
358 #if defined(HAVE_SHUTDOWN)
359         return shutdown(s, how);
360 #else
361 # error shutdown not available
362 #endif
363 }
364
365 inline static int system_socket(int domain, int type, int protocol)
366 {
367 #if defined(HAVE_SOCKET)
368         return socket(domain, type, protocol);
369 #else
370 # error socket not available
371 #endif
372 }
373
374 inline static int system_stat(const char *path, struct stat *buf)
375 {
376 #if defined(HAVE_STAT)
377         return stat(path, buf);
378 #else
379 # error stat not available
380 #endif
381 }
382
383 inline static char *system_strcat(char *dest, const char *src)
384 {
385 #if defined(HAVE_STRCAT)
386         return strcat(dest, src);
387 #else
388 # error strcat not available
389 #endif
390 }
391
392 inline static char *system_strcpy(char *dest, const char *src)
393 {
394 #if defined(HAVE_STRCPY)
395         return strcpy(dest, src);
396 #else
397 # error strcpy not available
398 #endif
399 }
400
401 inline static char *system_strdup(const char *s)
402 {
403 #if defined(HAVE_STRDUP)
404         return strdup(s);
405 #else
406 # error strdup not available
407 #endif
408 }
409
410 inline static char *system_strerror(int errnum)
411 {
412 #if defined(HAVE_STRERROR)
413         return strerror(errnum);
414 #else
415 # error strerror not available
416 #endif
417 }
418
419 inline static size_t system_strlen(const char *s)
420 {
421 #if defined(HAVE_STRLEN)
422         return strlen(s);
423 #else
424 # error strlen not available
425 #endif
426 }
427
428 inline static ssize_t system_write(int fd, const void *buf, size_t count)
429 {
430 #if defined(HAVE_WRITE)
431         return write(fd, buf, count);
432 #else
433 # error write not available
434 #endif
435 }
436
437
438 /* function prototypes ********************************************************/
439
440 void *system_mmap_anonymous(void *addr, size_t len, int prot, int flags);
441 int   system_processors_online(void);
442
443 #endif /* _VMCORE_SYSTEM_H */
444
445
446 /*
447  * These are local overrides for various environment variables in Emacs.
448  * Please do not remove this and leave it at the end of the file, where
449  * Emacs will automagically detect them.
450  * ---------------------------------------------------------------------
451  * Local variables:
452  * mode: c
453  * indent-tabs-mode: t
454  * c-basic-offset: 4
455  * tab-width: 4
456  * End:
457  * vim:noexpandtab:sw=4:ts=4:
458  */