* configure.ac (AC_CHECK_HEADERS): Added signal.h.
[cacao.git] / src / vm / os.hpp
1 /* src/vm/os.hpp - 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 _OS_HPP
27 #define _OS_HPP
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_DLFCN_H)
39 # include <dlfcn.h>
40 #endif
41
42 #if defined(HAVE_ERRNO_H)
43 # include <errno.h>
44 #endif
45
46 #if defined(HAVE_FCNTL_H)
47 # include <fcntl.h>
48 #endif
49
50 #if defined(ENABLE_JRE_LAYOUT)
51 # if defined(HAVE_LIBGEN_H)
52 #  include <libgen.h>
53 # endif
54 #endif
55
56 #if defined(HAVE_SIGNAL_H)
57 # include <signal.h>
58 #endif
59
60 #if defined(HAVE_STDINT_H)
61 # include <stdint.h>
62 #endif
63
64 #if defined(HAVE_STDIO_H)
65 # include <stdio.h>
66 #endif
67
68 #if defined(HAVE_STDLIB_H)
69 # include <stdlib.h>
70 #endif
71
72 #if defined(HAVE_STRING_H)
73 # include <string.h>
74 #endif
75
76 #if defined(HAVE_UNISTD_H)
77 # include <unistd.h>
78 #endif
79
80 #if defined(HAVE_SYS_MMAN_H)
81 # include <sys/mman.h>
82 #endif
83
84 #if defined(HAVE_SYS_SOCKET_H)
85 # include <sys/socket.h>
86 #endif
87
88 #if defined(HAVE_SYS_STAT_H)
89 # include <sys/stat.h>
90 #endif
91
92 #if defined(HAVE_SYS_TYPES_H)
93 # include <sys/types.h>
94 #endif
95
96
97 #ifdef __cplusplus
98
99 // Class wrapping system (OS) functions.
100 class os {
101 public:
102         // Inline functions.
103         static inline void   abort();
104         static inline int    accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
105         static inline int    access(const char *pathname, int mode);
106         static inline int    atoi(const char* nptr);
107         static inline void*  calloc(size_t nmemb, size_t size);
108         static inline int    close(int fd);
109         static inline int    connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen);
110 #if defined(ENABLE_JRE_LAYOUT)
111         static inline char*  dirname(char* path);
112 #endif
113         static inline int    dlclose(void* handle);
114         static inline char*  dlerror(void);
115         static inline void*  dlopen(const char* filename, int flag);
116         static inline void*  dlsym(void* handle, const char* symbol);
117         static inline int    fclose(FILE* fp);
118         static inline FILE*  fopen(const char* path, const char* mode);
119         static inline size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
120         static inline void   free(void* ptr);
121         static inline int    gethostname(char* name, size_t len);
122         static inline int    getpagesize(void);
123         static inline int    getsockname(int s, struct sockaddr* name, socklen_t* namelen);
124         static inline int    getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
125         static inline int    listen(int sockfd, int backlog);
126         static inline void*  malloc(size_t size);
127         static inline void*  memcpy(void* dest, const void* src, size_t n);
128         static inline void*  memset(void* s, int c, size_t n);
129         static inline int    mprotect(void* addr, size_t len, int prot);
130         static inline int    scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
131         static inline int    setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
132         static inline int    shutdown(int s, int how);
133         static inline int    socket(int domain, int type, int protocol);
134         static inline int    stat(const char* path, struct stat* buf);
135         static inline int    str2sig(const char* str, int* signum);
136         static inline char*  strcat(char* dest, const char* src);
137         static inline char*  strcpy(char* dest, const char* src);
138         static inline char*  strdup(const char* s);
139         static inline size_t strlen(const char* s);
140         static inline char*  strerror(int errnum);
141
142         static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
143         static int   processors_online(void);
144 };
145
146
147 inline void os::abort(void)
148 {
149 #if defined(HAVE_ABORT)
150         ::abort();
151 #else
152 # error abort not available
153 #endif
154 }
155
156 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
157 {
158 #if defined(HAVE_ACCEPT)
159         return ::accept(sockfd, addr, addrlen);
160 #else
161 # error accept not available
162 #endif
163 }
164
165 inline int os::access(const char* pathname, int mode)
166 {
167 #if defined(HAVE_ACCESS)
168         return ::access(pathname, mode);
169 #else
170 # error access not available
171 #endif
172 }
173
174 inline int os::atoi(const char* nptr)
175 {
176 #if defined(HAVE_ATOI)
177         return ::atoi(nptr);
178 #else
179 # error atoi not available
180 #endif
181 }
182
183 inline void* os::calloc(size_t nmemb, size_t size)
184 {
185 #if defined(HAVE_CALLOC)
186         return ::calloc(nmemb, size);
187 #else
188 # error calloc not available
189 #endif
190 }
191
192 inline int os::close(int fd)
193 {
194 #if defined(HAVE_CLOSE)
195         return ::close(fd);
196 #else
197 # error close not available
198 #endif
199 }
200
201 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
202 {
203 #if defined(HAVE_CONNECT)
204         return ::connect(sockfd, serv_addr, addrlen);
205 #else
206 # error connect not available
207 #endif
208 }
209
210 #if defined(ENABLE_JRE_LAYOUT)
211 inline char* os::dirname(char* path)
212 {
213 #if defined(HAVE_DIRNAME)
214         return ::dirname(path);
215 #else
216 # error dirname not available
217 #endif
218 }
219 #endif
220
221 inline int os::dlclose(void* handle)
222 {
223 #if defined(HAVE_DLCLOSE)
224         return ::dlclose(handle);
225 #else
226 # error dlclose not available
227 #endif
228 }
229
230 inline char* os::dlerror(void)
231 {
232 #if defined(HAVE_DLERROR)
233         return ::dlerror();
234 #else
235 # error dlerror not available
236 #endif
237 }
238
239 inline void* os::dlopen(const char* filename, int flag)
240 {
241 #if defined(HAVE_DLOPEN)
242         return ::dlopen(filename, flag);
243 #else
244 # error dlopen not available
245 #endif
246 }
247
248 inline void* os::dlsym(void* handle, const char* symbol)
249 {
250 #if defined(HAVE_DLSYM)
251         return ::dlsym(handle, symbol);
252 #else
253 # error dlsym not available
254 #endif
255 }
256
257 inline int os::fclose(FILE* fp)
258 {
259 #if defined(HAVE_FCLOSE)
260         return ::fclose(fp);
261 #else
262 # error fclose not available
263 #endif
264 }
265
266 inline FILE* os::fopen(const char* path, const char* mode)
267 {
268 #if defined(HAVE_FOPEN)
269         return ::fopen(path, mode);
270 #else
271 # error fopen not available
272 #endif
273 }
274
275 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
276 {
277 #if defined(HAVE_FREAD)
278         return ::fread(ptr, size, nmemb, stream);
279 #else
280 # error fread not available
281 #endif
282 }
283
284 inline void os::free(void* ptr)
285 {
286 #if defined(HAVE_FREE)
287         ::free(ptr);
288 #else
289 # error free not available
290 #endif
291 }
292
293 inline static int system_fsync(int fd)
294 {
295 #if defined(HAVE_FSYNC)
296         return fsync(fd);
297 #else
298 # error fsync not available
299 #endif
300 }
301
302 inline static int system_ftruncate(int fd, off_t length)
303 {
304 #if defined(HAVE_FTRUNCATE)
305         return ftruncate(fd, length);
306 #else
307 # error ftruncate not available
308 #endif
309 }
310
311 inline int os::gethostname(char* name, size_t len)
312 {
313 #if defined(HAVE_GETHOSTNAME)
314         return ::gethostname(name, len);
315 #else
316 # error gethostname not available
317 #endif
318 }
319
320 inline int os::getpagesize(void)
321 {
322 #if defined(HAVE_GETPAGESIZE)
323         return ::getpagesize();
324 #else
325 # error getpagesize not available
326 #endif
327 }
328
329 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
330 {
331 #if defined(HAVE_GETSOCKNAME)
332         return ::getsockname(s, name, namelen);
333 #else
334 # error getsockname not available
335 #endif
336 }
337
338 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
339 {
340 #if defined(HAVE_GETSOCKOPT)
341         return ::getsockopt(s, level, optname, optval, optlen);
342 #else
343 # error getsockopt not available
344 #endif
345 }
346
347 inline int os::listen(int sockfd, int backlog)
348 {
349 #if defined(HAVE_LISTEN)
350         return ::listen(sockfd, backlog);
351 #else
352 # error listen not available
353 #endif
354 }
355
356 inline static off_t system_lseek(int fildes, off_t offset, int whence)
357 {
358 #if defined(HAVE_LSEEK)
359         return lseek(fildes, offset, whence);
360 #else
361 # error lseek not available
362 #endif
363 }
364
365 inline void* os::malloc(size_t size)
366 {
367 #if defined(HAVE_MALLOC)
368         return ::malloc(size);
369 #else
370 # error malloc not available
371 #endif
372 }
373
374 inline void* os::memcpy(void* dest, const void* src, size_t n)
375 {
376 #if defined(HAVE_MEMCPY)
377         return ::memcpy(dest, src, n);
378 #else
379 # error memcpy not available
380 #endif
381 }
382
383 inline void* os::memset(void* s, int c, size_t n)
384 {
385 #if defined(HAVE_MEMSET)
386         return ::memset(s, c, n);
387 #else
388 # error memset not available
389 #endif
390 }
391
392 inline int os::mprotect(void* addr, size_t len, int prot)
393 {
394 #if defined(HAVE_MPROTECT)
395         return ::mprotect(addr, len, prot);
396 #else
397 # error mprotect not available
398 #endif
399 }
400
401 inline static int system_open(const char *pathname, int flags, mode_t mode)
402 {
403 #if defined(HAVE_OPEN)
404         return open(pathname, flags, mode);
405 #else
406 # error open not available
407 #endif
408 }
409
410 inline static ssize_t system_read(int fd, void *buf, size_t count)
411 {
412 #if defined(HAVE_READ)
413         return read(fd, buf, count);
414 #else
415 # error read not available
416 #endif
417 }
418
419 inline static void *system_realloc(void *ptr, size_t size)
420 {
421 #if defined(HAVE_REALLOC)
422         return realloc(ptr, size);
423 #else
424 # error realloc not available
425 #endif
426 }
427
428 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
429 /*
430 #elif defined(__SOLARIS__)
431 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
432 #elif defined(__IRIX__)
433 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
434 #else
435 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
436 #endif
437 */
438 {
439 #if defined(HAVE_SCANDIR)
440 # if defined(__LINUX__)
441         return ::scandir(dir, namelist, filter, compar);
442 #elif defined(__SOLARIS__)
443         return ::scandir(dir, namelist, filter, (int (*)(const dirent**, const dirent**)) compar);
444 # else
445         return ::scandir(dir, namelist, (int (*)(struct dirent*)) filter, compar);
446 # endif
447 #else
448 # error scandir not available
449 #endif
450 }
451
452 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
453 {
454 #if defined(HAVE_SETSOCKOPT)
455         return ::setsockopt(s, level, optname, optval, optlen);
456 #else
457 # error setsockopt not available
458 #endif
459 }
460
461 inline int os::shutdown(int s, int how)
462 {
463 #if defined(HAVE_SHUTDOWN)
464         return ::shutdown(s, how);
465 #else
466 # error shutdown not available
467 #endif
468 }
469
470 inline int os::socket(int domain, int type, int protocol)
471 {
472 #if defined(HAVE_SOCKET)
473         return ::socket(domain, type, protocol);
474 #else
475 # error socket not available
476 #endif
477 }
478
479 inline int os::stat(const char* path, struct stat* buf)
480 {
481 #if defined(HAVE_STAT)
482         return ::stat(path, buf);
483 #else
484 # error stat not available
485 #endif
486 }
487
488 inline int os::str2sig(const char* str, int* signum)
489 {
490 #if defined(HAVE_STR2SIG)
491         return ::str2sig(str, signum);
492 #else
493 # error str2sig not available
494 #endif
495 }
496
497 inline char* os::strcat(char* dest, const char* src)
498 {
499 #if defined(HAVE_STRCAT)
500         return ::strcat(dest, src);
501 #else
502 # error strcat not available
503 #endif
504 }
505
506 inline char* os::strcpy(char* dest, const char* src)
507 {
508 #if defined(HAVE_STRCPY)
509         return ::strcpy(dest, src);
510 #else
511 # error strcpy not available
512 #endif
513 }
514
515 inline char* os::strdup(const char* s)
516 {
517 #if defined(HAVE_STRDUP)
518         return ::strdup(s);
519 #else
520 # error strdup not available
521 #endif
522 }
523
524 inline char* os::strerror(int errnum)
525 {
526 #if defined(HAVE_STRERROR)
527         return ::strerror(errnum);
528 #else
529 # error strerror not available
530 #endif
531 }
532
533 inline size_t os::strlen(const char* s)
534 {
535 #if defined(HAVE_STRLEN)
536         return ::strlen(s);
537 #else
538 # error strlen not available
539 #endif
540 }
541
542 inline static ssize_t system_write(int fd, const void *buf, size_t count)
543 {
544 #if defined(HAVE_WRITE)
545         return write(fd, buf, count);
546 #else
547 # error write not available
548 #endif
549 }
550
551 #else
552
553 void*  os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
554
555 void   os_abort(void);
556 int    os_access(const char* pathname, int mode);
557 int    os_atoi(const char* nptr);
558 void*  os_calloc(size_t nmemb, size_t size);
559 char*  os_dirname(char* path);
560 int    os_dlclose(void* handle);
561 char*  os_dlerror(void);
562 void*  os_dlopen(const char* filename, int flag);
563 void*  os_dlsym(void* handle, const char* symbol);
564 int    os_fclose(FILE* fp);
565 FILE*  os_fopen(const char* path, const char* mode);
566 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
567 void   os_free(void* ptr);
568 int    os_getpagesize(void);
569 void*  os_memcpy(void* dest, const void* src, size_t n);
570 void*  os_memset(void* s, int c, size_t n);
571 int    os_mprotect(void* addr, size_t len, int prot);
572 int    os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
573 int    os_stat(const char* path, struct stat* buf);
574 char*  os_strcat(char* dest, const char* src);
575 char*  os_strcpy(char* dest, const char* src);
576 char*  os_strdup(const char* s);
577 int    os_strlen(const char* s);
578
579 #endif
580
581 #endif // _OS_HPP
582
583
584 /*
585  * These are local overrides for various environment variables in Emacs.
586  * Please do not remove this and leave it at the end of the file, where
587  * Emacs will automagically detect them.
588  * ---------------------------------------------------------------------
589  * Local variables:
590  * mode: c++
591  * indent-tabs-mode: t
592  * c-basic-offset: 4
593  * tab-width: 4
594  * End:
595  * vim:noexpandtab:sw=4:ts=4:
596  */