Merged subtype and current trunk.
[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    Copyright (C) 2008 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License as
11    published by the Free Software Foundation; either version 2, or (at
12    your option) any later version.
13
14    This program is distributed in the hope that it will be useful, but
15    WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22    02110-1301, USA.
23
24 */
25
26
27 #ifndef _OS_HPP
28 #define _OS_HPP
29
30 #include "config.h"
31
32 // NOTE: In this file we check for all system headers, because we wrap
33 // all system calls into inline functions for better portability.
34
35 // Please don't include CACAO headers here as this header should be a
36 // very low-level one.
37
38 #if defined(HAVE_DIRENT_H)
39 # include <dirent.h>
40 #endif
41
42 #if defined(HAVE_DLFCN_H)
43 # include <dlfcn.h>
44 #endif
45
46 #if defined(HAVE_ERRNO_H)
47 # include <errno.h>
48 #endif
49
50 #if defined(HAVE_EXECINFO_H)
51 # include <execinfo.h>
52 #endif
53
54 #if defined(HAVE_FCNTL_H)
55 # include <fcntl.h>
56 #endif
57
58 #if defined(ENABLE_JRE_LAYOUT)
59 # if defined(HAVE_LIBGEN_H)
60 #  include <libgen.h>
61 # endif
62 #endif
63
64 #if defined(HAVE_SIGNAL_H)
65 # include <signal.h>
66 #endif
67
68 #if defined(HAVE_STDARG_H)
69 # include <stdarg.h>
70 #endif
71
72 #if defined(HAVE_STDINT_H)
73 # include <stdint.h>
74 #endif
75
76 #if defined(HAVE_STDIO_H)
77 # include <stdio.h>
78 #endif
79
80 #if defined(HAVE_STDLIB_H)
81 # include <stdlib.h>
82 #endif
83
84 #if defined(HAVE_STRING_H)
85 # include <string.h>
86 #endif
87
88 #if defined(HAVE_UNISTD_H)
89 # include <unistd.h>
90 #endif
91
92 #if defined(HAVE_SYS_MMAN_H)
93 # include <sys/mman.h>
94 #endif
95
96 #if defined(HAVE_SYS_SOCKET_H)
97 # include <sys/socket.h>
98 #endif
99
100 #if defined(HAVE_SYS_STAT_H)
101 # include <sys/stat.h>
102 #endif
103
104 #if defined(HAVE_SYS_TYPES_H)
105 # include <sys/types.h>
106 #endif
107
108
109 #ifdef __cplusplus
110
111 // Class wrapping system (OS) functions.
112 class os {
113 public:
114         // Inline functions.
115         static inline void    abort();
116         static inline int     accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
117         static inline int     access(const char *pathname, int mode);
118         static inline int     atoi(const char* nptr);
119         static inline int     backtrace(void** array, int size);
120         static inline char**  backtrace_symbols(void* const* array, int size) throw ();
121         static inline void*   calloc(size_t nmemb, size_t size);
122         static inline int     close(int fd);
123         static inline int     connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen);
124 #if defined(ENABLE_JRE_LAYOUT)
125         static inline char*   dirname(char* path);
126 #endif
127         static inline int     dlclose(void* handle);
128         static inline char*   dlerror(void);
129         static inline void*   dlopen(const char* filename, int flag);
130         static inline void*   dlsym(void* handle, const char* symbol);
131         static inline int     fclose(FILE* fp);
132         static inline FILE*   fopen(const char* path, const char* mode);
133         static inline int     fprintf(FILE* stream, const char* format, ...);
134         static inline size_t  fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
135         static inline void    free(void* ptr);
136         static inline char*   getenv(const char* name);
137         static inline int     gethostname(char* name, size_t len);
138         static inline int     getpagesize(void);
139         static inline int     getsockname(int s, struct sockaddr* name, socklen_t* namelen);
140         static inline int     getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
141         static inline int     listen(int sockfd, int backlog);
142         static inline void*   malloc(size_t size);
143         static inline void*   memcpy(void* dest, const void* src, size_t n);
144         static inline void*   memset(void* s, int c, size_t n);
145         static inline int     mprotect(void* addr, size_t len, int prot);
146         static inline ssize_t readlink(const char* path, char* buf, size_t bufsiz);
147         static inline int     scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
148         static inline int     setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
149         static inline int     shutdown(int s, int how);
150         static inline int     socket(int domain, int type, int protocol);
151         static inline int     stat(const char* path, struct stat* buf);
152 #if defined(__SOLARIS__)
153         static inline int     str2sig(const char* str, int* signum);
154 #endif
155         static inline char*   strcat(char* dest, const char* src);
156         static inline int     strcmp(const char* s1, const char* s2);
157         static inline char*   strcpy(char* dest, const char* src);
158         static inline char*   strdup(const char* s);
159         static inline size_t  strlen(const char* s);
160         static inline char*   strerror(int errnum);
161
162         // Convenience functions.
163         static void  abort(const char* text, ...);
164         static void  abort_errnum(int errnum, const char* text, ...);
165         static void  abort_errno(const char* text, ...);
166         static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
167         static void  print_backtrace();
168         static int   processors_online();
169 };
170
171
172 inline void os::abort(void)
173 {
174 #if defined(HAVE_ABORT)
175         ::abort();
176 #else
177 # error abort not available
178 #endif
179 }
180
181 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
182 {
183 #if defined(HAVE_ACCEPT)
184         return ::accept(sockfd, addr, addrlen);
185 #else
186 # error accept not available
187 #endif
188 }
189
190 inline int os::access(const char* pathname, int mode)
191 {
192 #if defined(HAVE_ACCESS)
193         return ::access(pathname, mode);
194 #else
195 # error access not available
196 #endif
197 }
198
199 inline int os::atoi(const char* nptr)
200 {
201 #if defined(HAVE_ATOI)
202         return ::atoi(nptr);
203 #else
204 # error atoi not available
205 #endif
206 }
207
208 inline int os::backtrace(void** array, int size)
209 {
210 #if defined(HAVE_BACKTRACE)
211         return ::backtrace(array, size);
212 #else
213         fprintf(stderr, "os::backtrace: Not available.");
214         return 0;
215 #endif
216 }
217
218 inline char** os::backtrace_symbols(void* const* array, int size) throw ()
219 {
220 #if defined(HAVE_BACKTRACE_SYMBOLS)
221         return ::backtrace_symbols(array, size);
222 #else
223         fprintf(stderr, "os::backtrace_symbols: Not available.");
224         return NULL;
225 #endif
226 }
227
228 inline void* os::calloc(size_t nmemb, size_t size)
229 {
230 #if defined(HAVE_CALLOC)
231         return ::calloc(nmemb, size);
232 #else
233 # error calloc not available
234 #endif
235 }
236
237 inline int os::close(int fd)
238 {
239 #if defined(HAVE_CLOSE)
240         return ::close(fd);
241 #else
242 # error close not available
243 #endif
244 }
245
246 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
247 {
248 #if defined(HAVE_CONNECT)
249         return ::connect(sockfd, serv_addr, addrlen);
250 #else
251 # error connect not available
252 #endif
253 }
254
255 #if defined(ENABLE_JRE_LAYOUT)
256 inline char* os::dirname(char* path)
257 {
258 #if defined(HAVE_DIRNAME)
259         return ::dirname(path);
260 #else
261 # error dirname not available
262 #endif
263 }
264 #endif
265
266 inline int os::dlclose(void* handle)
267 {
268 #if defined(HAVE_DLCLOSE)
269         return ::dlclose(handle);
270 #else
271 # error dlclose not available
272 #endif
273 }
274
275 inline char* os::dlerror(void)
276 {
277 #if defined(HAVE_DLERROR)
278         return ::dlerror();
279 #else
280 # error dlerror not available
281 #endif
282 }
283
284 inline void* os::dlopen(const char* filename, int flag)
285 {
286 #if defined(HAVE_DLOPEN)
287         return ::dlopen(filename, flag);
288 #else
289 # error dlopen not available
290 #endif
291 }
292
293 inline void* os::dlsym(void* handle, const char* symbol)
294 {
295 #if defined(HAVE_DLSYM)
296         return ::dlsym(handle, symbol);
297 #else
298 # error dlsym not available
299 #endif
300 }
301
302 inline int os::fclose(FILE* fp)
303 {
304 #if defined(HAVE_FCLOSE)
305         return ::fclose(fp);
306 #else
307 # error fclose not available
308 #endif
309 }
310
311 inline FILE* os::fopen(const char* path, const char* mode)
312 {
313 #if defined(HAVE_FOPEN)
314         return ::fopen(path, mode);
315 #else
316 # error fopen not available
317 #endif
318 }
319
320 inline int os::fprintf(FILE* stream, const char* format, ...)
321 {
322 #if defined(HAVE_FPRINTF)
323         va_list ap;
324         va_start(ap, format);
325         int result = ::fprintf(stream, format, ap);
326         va_end(ap);
327         return result;
328 #else
329 # error fprintf not available
330 #endif
331 }
332
333 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
334 {
335 #if defined(HAVE_FREAD)
336         return ::fread(ptr, size, nmemb, stream);
337 #else
338 # error fread not available
339 #endif
340 }
341
342 inline void os::free(void* ptr)
343 {
344 #if defined(HAVE_FREE)
345         ::free(ptr);
346 #else
347 # error free not available
348 #endif
349 }
350
351 inline static int system_fsync(int fd)
352 {
353 #if defined(HAVE_FSYNC)
354         return fsync(fd);
355 #else
356 # error fsync not available
357 #endif
358 }
359
360 inline static int system_ftruncate(int fd, off_t length)
361 {
362 #if defined(HAVE_FTRUNCATE)
363         return ftruncate(fd, length);
364 #else
365 # error ftruncate not available
366 #endif
367 }
368
369 inline char* os::getenv(const char* name)
370 {
371 #if defined(HAVE_GETENV)
372         return ::getenv(name);
373 #else
374 # error getenv not available
375 #endif
376 }
377
378 inline int os::gethostname(char* name, size_t len)
379 {
380 #if defined(HAVE_GETHOSTNAME)
381         return ::gethostname(name, len);
382 #else
383 # error gethostname not available
384 #endif
385 }
386
387 inline int os::getpagesize(void)
388 {
389 #if defined(HAVE_GETPAGESIZE)
390         return ::getpagesize();
391 #else
392 # error getpagesize not available
393 #endif
394 }
395
396 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
397 {
398 #if defined(HAVE_GETSOCKNAME)
399         return ::getsockname(s, name, namelen);
400 #else
401 # error getsockname not available
402 #endif
403 }
404
405 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
406 {
407 #if defined(HAVE_GETSOCKOPT)
408         return ::getsockopt(s, level, optname, optval, optlen);
409 #else
410 # error getsockopt not available
411 #endif
412 }
413
414 inline int os::listen(int sockfd, int backlog)
415 {
416 #if defined(HAVE_LISTEN)
417         return ::listen(sockfd, backlog);
418 #else
419 # error listen not available
420 #endif
421 }
422
423 inline static off_t system_lseek(int fildes, off_t offset, int whence)
424 {
425 #if defined(HAVE_LSEEK)
426         return lseek(fildes, offset, whence);
427 #else
428 # error lseek not available
429 #endif
430 }
431
432 inline void* os::malloc(size_t size)
433 {
434 #if defined(HAVE_MALLOC)
435         return ::malloc(size);
436 #else
437 # error malloc not available
438 #endif
439 }
440
441 inline void* os::memcpy(void* dest, const void* src, size_t n)
442 {
443 #if defined(HAVE_MEMCPY)
444         return ::memcpy(dest, src, n);
445 #else
446 # error memcpy not available
447 #endif
448 }
449
450 inline void* os::memset(void* s, int c, size_t n)
451 {
452 #if defined(HAVE_MEMSET)
453         return ::memset(s, c, n);
454 #else
455 # error memset not available
456 #endif
457 }
458
459 inline int os::mprotect(void* addr, size_t len, int prot)
460 {
461 #if defined(HAVE_MPROTECT)
462         return ::mprotect(addr, len, prot);
463 #else
464 # error mprotect not available
465 #endif
466 }
467
468 inline static int system_open(const char *pathname, int flags, mode_t mode)
469 {
470 #if defined(HAVE_OPEN)
471         return open(pathname, flags, mode);
472 #else
473 # error open not available
474 #endif
475 }
476
477 inline static ssize_t system_read(int fd, void *buf, size_t count)
478 {
479 #if defined(HAVE_READ)
480         return read(fd, buf, count);
481 #else
482 # error read not available
483 #endif
484 }
485
486 inline ssize_t os::readlink(const char* path, char* buf, size_t bufsiz)
487 {
488 #if defined(HAVE_READLINK)
489         return ::readlink(path, buf, bufsiz);
490 #else
491 # error readlink not available
492 #endif
493 }
494
495 inline static void *system_realloc(void *ptr, size_t size)
496 {
497 #if defined(HAVE_REALLOC)
498         return realloc(ptr, size);
499 #else
500 # error realloc not available
501 #endif
502 }
503
504 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
505 /*
506 #elif defined(__SOLARIS__)
507 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
508 #elif defined(__IRIX__)
509 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
510 #else
511 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
512 #endif
513 */
514 {
515 #if defined(HAVE_SCANDIR)
516 # if defined(__LINUX__)
517         return ::scandir(dir, namelist, filter, compar);
518 #elif defined(__SOLARIS__)
519         return ::scandir(dir, namelist, filter, (int (*)(const dirent**, const dirent**)) compar);
520 # else
521         return ::scandir(dir, namelist, (int (*)(struct dirent*)) filter, compar);
522 # endif
523 #else
524 # error scandir not available
525 #endif
526 }
527
528 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
529 {
530 #if defined(HAVE_SETSOCKOPT)
531         return ::setsockopt(s, level, optname, optval, optlen);
532 #else
533 # error setsockopt not available
534 #endif
535 }
536
537 inline int os::shutdown(int s, int how)
538 {
539 #if defined(HAVE_SHUTDOWN)
540         return ::shutdown(s, how);
541 #else
542 # error shutdown not available
543 #endif
544 }
545
546 inline int os::socket(int domain, int type, int protocol)
547 {
548 #if defined(HAVE_SOCKET)
549         return ::socket(domain, type, protocol);
550 #else
551 # error socket not available
552 #endif
553 }
554
555 inline int os::stat(const char* path, struct stat* buf)
556 {
557 #if defined(HAVE_STAT)
558         return ::stat(path, buf);
559 #else
560 # error stat not available
561 #endif
562 }
563
564 #if defined(__SOLARIS__)
565 inline int os::str2sig(const char* str, int* signum)
566 {
567 #if defined(HAVE_STR2SIG)
568         return ::str2sig(str, signum);
569 #else
570 # error str2sig not available
571 #endif
572 }
573 #endif
574
575 inline char* os::strcat(char* dest, const char* src)
576 {
577 #if defined(HAVE_STRCAT)
578         return ::strcat(dest, src);
579 #else
580 # error strcat not available
581 #endif
582 }
583
584 inline int os::strcmp(const char* s1, const char* s2)
585 {
586 #if defined(HAVE_STRCMP)
587         return ::strcmp(s1, s2);
588 #else
589 # error strcmp not available
590 #endif
591 }
592
593 inline char* os::strcpy(char* dest, const char* src)
594 {
595 #if defined(HAVE_STRCPY)
596         return ::strcpy(dest, src);
597 #else
598 # error strcpy not available
599 #endif
600 }
601
602 inline char* os::strdup(const char* s)
603 {
604 #if defined(HAVE_STRDUP)
605         return ::strdup(s);
606 #else
607 # error strdup not available
608 #endif
609 }
610
611 inline char* os::strerror(int errnum)
612 {
613 #if defined(HAVE_STRERROR)
614         return ::strerror(errnum);
615 #else
616 # error strerror not available
617 #endif
618 }
619
620 inline size_t os::strlen(const char* s)
621 {
622 #if defined(HAVE_STRLEN)
623         return ::strlen(s);
624 #else
625 # error strlen not available
626 #endif
627 }
628
629 inline static ssize_t system_write(int fd, const void *buf, size_t count)
630 {
631 #if defined(HAVE_WRITE)
632         return write(fd, buf, count);
633 #else
634 # error write not available
635 #endif
636 }
637
638 #else
639
640 void*  os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
641
642 void   os_abort(void);
643 int    os_access(const char* pathname, int mode);
644 int    os_atoi(const char* nptr);
645 void*  os_calloc(size_t nmemb, size_t size);
646 char*  os_dirname(char* path);
647 char*  os_dlerror(void);
648 void*  os_dlsym(void* handle, const char* symbol);
649 int    os_fclose(FILE* fp);
650 FILE*  os_fopen(const char* path, const char* mode);
651 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
652 void   os_free(void* ptr);
653 int    os_getpagesize(void);
654 void*  os_memcpy(void* dest, const void* src, size_t n);
655 void*  os_memset(void* s, int c, size_t n);
656 int    os_mprotect(void* addr, size_t len, int prot);
657 int    os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
658 int    os_stat(const char* path, struct stat* buf);
659 char*  os_strcat(char* dest, const char* src);
660 char*  os_strcpy(char* dest, const char* src);
661 char*  os_strdup(const char* s);
662 int    os_strlen(const char* s);
663
664 #endif
665
666 #endif // _OS_HPP
667
668
669 /*
670  * These are local overrides for various environment variables in Emacs.
671  * Please do not remove this and leave it at the end of the file, where
672  * Emacs will automagically detect them.
673  * ---------------------------------------------------------------------
674  * Local variables:
675  * mode: c++
676  * indent-tabs-mode: t
677  * c-basic-offset: 4
678  * tab-width: 4
679  * End:
680  * vim:noexpandtab:sw=4:ts=4:
681  */