* src/native/vm/openjdk/management.cpp: Added support for verbose attributes.
[cacao.git] / src / vm / os.hpp
1 /* src/vm/os.hpp - system (OS) functions
2
3    Copyright (C) 2007, 2008, 2009
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_LOADAVG_H)
93 # include <sys/loadavg.h>
94 #endif
95
96 #if defined(HAVE_SYS_MMAN_H)
97 # include <sys/mman.h>
98 #endif
99
100 #if defined(HAVE_SYS_SOCKET_H)
101 # include <sys/socket.h>
102 #endif
103
104 #if defined(HAVE_SYS_STAT_H)
105 # include <sys/stat.h>
106 #endif
107
108 #if defined(HAVE_SYS_TYPES_H)
109 # include <sys/types.h>
110 #endif
111
112
113 #ifdef __cplusplus
114
115 // Class wrapping system (OS) functions.
116 class os {
117 public:
118         // Inline functions.
119         static inline void    abort();
120         static inline int     accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
121         static inline int     access(const char *pathname, int mode);
122         static inline int     atoi(const char* nptr);
123         static inline int     backtrace(void** array, int size);
124         static inline char**  backtrace_symbols(void* const* array, int size) throw ();
125         static inline void*   calloc(size_t nmemb, size_t size);
126         static inline int     close(int fd);
127         static inline int     connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen);
128 #if defined(ENABLE_JRE_LAYOUT)
129         static inline char*   dirname(char* path);
130 #endif
131         static inline int     dlclose(void* handle);
132         static inline char*   dlerror(void);
133         static inline void*   dlopen(const char* filename, int flag);
134         static inline void*   dlsym(void* handle, const char* symbol);
135         static inline int     fclose(FILE* fp);
136         static inline FILE*   fopen(const char* path, const char* mode);
137         static inline int     fprintf(FILE* stream, const char* format, ...);
138         static inline size_t  fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
139         static inline void    free(void* ptr);
140         static inline char*   getenv(const char* name);
141         static inline int     gethostname(char* name, size_t len);
142         static inline int     getloadavg(double loadavg[], int nelem);
143         static inline int     getpagesize(void);
144         static inline pid_t   getpid(void);
145         static inline int     getsockname(int s, struct sockaddr* name, socklen_t* namelen);
146         static inline int     getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
147         static inline int     listen(int sockfd, int backlog);
148         static inline void*   malloc(size_t size);
149         static inline void*   memcpy(void* dest, const void* src, size_t n);
150         static inline void*   memset(void* s, int c, size_t n);
151         static inline int     mprotect(void* addr, size_t len, int prot);
152         static inline ssize_t readlink(const char* path, char* buf, size_t bufsiz);
153         static inline int     scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
154         static inline ssize_t send(int s, const void* buf, size_t len, int flags);
155         static inline int     setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
156         static inline int     shutdown(int s, int how);
157         static inline int     socket(int domain, int type, int protocol);
158         static inline int     stat(const char* path, struct stat* buf);
159 #if defined(__SOLARIS__)
160         static inline int     str2sig(const char* str, int* signum);
161 #endif
162         static inline char*   strcat(char* dest, const char* src);
163         static inline int     strcmp(const char* s1, const char* s2);
164         static inline char*   strcpy(char* dest, const char* src);
165         static inline char*   strdup(const char* s);
166         static inline size_t  strlen(const char* s);
167         static inline char*   strerror(int errnum);
168
169         // Convenience functions.
170         static void  abort(const char* text, ...);
171         static void  abort_errnum(int errnum, const char* text, ...);
172         static void  abort_errno(const char* text, ...);
173         static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
174         static void  print_backtrace();
175         static int   processors_online();
176
177         // Template helper
178         template<class F1, class F2>
179         static int call_scandir(int (*scandir)(const char *, struct dirent ***, F1, F2), const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *));
180 };
181
182
183 inline void os::abort(void)
184 {
185 #if defined(HAVE_ABORT)
186         ::abort();
187 #else
188 # error abort not available
189 #endif
190 }
191
192 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
193 {
194 #if defined(HAVE_ACCEPT)
195         return ::accept(sockfd, addr, addrlen);
196 #else
197 # error accept not available
198 #endif
199 }
200
201 inline int os::access(const char* pathname, int mode)
202 {
203 #if defined(HAVE_ACCESS)
204         return ::access(pathname, mode);
205 #else
206 # error access not available
207 #endif
208 }
209
210 inline int os::atoi(const char* nptr)
211 {
212 #if defined(HAVE_ATOI)
213         return ::atoi(nptr);
214 #else
215 # error atoi not available
216 #endif
217 }
218
219 inline int os::backtrace(void** array, int size)
220 {
221 #if defined(HAVE_BACKTRACE)
222         return ::backtrace(array, size);
223 #else
224         fprintf(stderr, "os::backtrace: Not available.");
225         return 0;
226 #endif
227 }
228
229 inline char** os::backtrace_symbols(void* const* array, int size) throw ()
230 {
231 #if defined(HAVE_BACKTRACE_SYMBOLS)
232         return ::backtrace_symbols(array, size);
233 #else
234         fprintf(stderr, "os::backtrace_symbols: Not available.");
235         return NULL;
236 #endif
237 }
238
239 inline void* os::calloc(size_t nmemb, size_t size)
240 {
241 #if defined(HAVE_CALLOC)
242         return ::calloc(nmemb, size);
243 #else
244 # error calloc not available
245 #endif
246 }
247
248 inline int os::close(int fd)
249 {
250 #if defined(HAVE_CLOSE)
251         return ::close(fd);
252 #else
253 # error close not available
254 #endif
255 }
256
257 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
258 {
259 #if defined(HAVE_CONNECT)
260         return ::connect(sockfd, serv_addr, addrlen);
261 #else
262 # error connect not available
263 #endif
264 }
265
266 #if defined(ENABLE_JRE_LAYOUT)
267 inline char* os::dirname(char* path)
268 {
269 #if defined(HAVE_DIRNAME)
270         return ::dirname(path);
271 #else
272 # error dirname not available
273 #endif
274 }
275 #endif
276
277 inline int os::dlclose(void* handle)
278 {
279 #if defined(HAVE_DLCLOSE)
280         return ::dlclose(handle);
281 #else
282 # error dlclose not available
283 #endif
284 }
285
286 inline char* os::dlerror(void)
287 {
288 #if defined(HAVE_DLERROR)
289         // At least FreeBSD defines dlerror() to return a const char*, so
290         // we simply cast it.
291         return (char*) ::dlerror();
292 #else
293 # error dlerror not available
294 #endif
295 }
296
297 inline void* os::dlopen(const char* filename, int flag)
298 {
299 #if defined(HAVE_DLOPEN)
300         return ::dlopen(filename, flag);
301 #else
302 # error dlopen not available
303 #endif
304 }
305
306 inline void* os::dlsym(void* handle, const char* symbol)
307 {
308 #if defined(HAVE_DLSYM)
309         return ::dlsym(handle, symbol);
310 #else
311 # error dlsym not available
312 #endif
313 }
314
315 inline int os::fclose(FILE* fp)
316 {
317 #if defined(HAVE_FCLOSE)
318         return ::fclose(fp);
319 #else
320 # error fclose not available
321 #endif
322 }
323
324 inline FILE* os::fopen(const char* path, const char* mode)
325 {
326 #if defined(HAVE_FOPEN)
327         return ::fopen(path, mode);
328 #else
329 # error fopen not available
330 #endif
331 }
332
333 inline int os::fprintf(FILE* stream, const char* format, ...)
334 {
335 #if defined(HAVE_FPRINTF)
336         va_list ap;
337         va_start(ap, format);
338         int result = ::vfprintf(stream, format, ap);
339         va_end(ap);
340         return result;
341 #else
342 # error fprintf not available
343 #endif
344 }
345
346 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
347 {
348 #if defined(HAVE_FREAD)
349         return ::fread(ptr, size, nmemb, stream);
350 #else
351 # error fread not available
352 #endif
353 }
354
355 inline void os::free(void* ptr)
356 {
357 #if defined(HAVE_FREE)
358         ::free(ptr);
359 #else
360 # error free not available
361 #endif
362 }
363
364 inline static int system_fsync(int fd)
365 {
366 #if defined(HAVE_FSYNC)
367         return fsync(fd);
368 #else
369 # error fsync not available
370 #endif
371 }
372
373 inline static int system_ftruncate(int fd, off_t length)
374 {
375 #if defined(HAVE_FTRUNCATE)
376         return ftruncate(fd, length);
377 #else
378 # error ftruncate not available
379 #endif
380 }
381
382 inline char* os::getenv(const char* name)
383 {
384 #if defined(HAVE_GETENV)
385         return ::getenv(name);
386 #else
387 # error getenv not available
388 #endif
389 }
390
391 inline int os::gethostname(char* name, size_t len)
392 {
393 #if defined(HAVE_GETHOSTNAME)
394         return ::gethostname(name, len);
395 #else
396 # error gethostname not available
397 #endif
398 }
399
400 inline int os::getloadavg(double loadavg[], int nelem)
401 {
402 #if defined(HAVE_GETLOADAVG)
403         return ::getloadavg(loadavg, nelem);
404 #else
405 # error getloadavg not available
406 #endif
407 }
408
409 inline int os::getpagesize(void)
410 {
411 #if defined(HAVE_GETPAGESIZE)
412         return ::getpagesize();
413 #else
414 # error getpagesize not available
415 #endif
416 }
417
418 inline pid_t os::getpid(void)
419 {
420 #if defined(HAVE_GETPID)
421         return ::getpid();
422 #else
423 # error getpid not available
424 #endif
425 }
426
427 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
428 {
429 #if defined(HAVE_GETSOCKNAME)
430         return ::getsockname(s, name, namelen);
431 #else
432 # error getsockname not available
433 #endif
434 }
435
436 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
437 {
438 #if defined(HAVE_GETSOCKOPT)
439         return ::getsockopt(s, level, optname, optval, optlen);
440 #else
441 # error getsockopt not available
442 #endif
443 }
444
445 inline int os::listen(int sockfd, int backlog)
446 {
447 #if defined(HAVE_LISTEN)
448         return ::listen(sockfd, backlog);
449 #else
450 # error listen not available
451 #endif
452 }
453
454 inline static off_t system_lseek(int fildes, off_t offset, int whence)
455 {
456 #if defined(HAVE_LSEEK)
457         return lseek(fildes, offset, whence);
458 #else
459 # error lseek not available
460 #endif
461 }
462
463 inline void* os::malloc(size_t size)
464 {
465 #if defined(HAVE_MALLOC)
466         return ::malloc(size);
467 #else
468 # error malloc not available
469 #endif
470 }
471
472 inline void* os::memcpy(void* dest, const void* src, size_t n)
473 {
474 #if defined(HAVE_MEMCPY)
475         return ::memcpy(dest, src, n);
476 #else
477 # error memcpy not available
478 #endif
479 }
480
481 inline void* os::memset(void* s, int c, size_t n)
482 {
483 #if defined(HAVE_MEMSET)
484         return ::memset(s, c, n);
485 #else
486 # error memset not available
487 #endif
488 }
489
490 inline int os::mprotect(void* addr, size_t len, int prot)
491 {
492 #if defined(HAVE_MPROTECT)
493         return ::mprotect(addr, len, prot);
494 #else
495 # error mprotect not available
496 #endif
497 }
498
499 inline static int system_open(const char *pathname, int flags, mode_t mode)
500 {
501 #if defined(HAVE_OPEN)
502         return open(pathname, flags, mode);
503 #else
504 # error open not available
505 #endif
506 }
507
508 inline static ssize_t system_read(int fd, void *buf, size_t count)
509 {
510 #if defined(HAVE_READ)
511         return read(fd, buf, count);
512 #else
513 # error read not available
514 #endif
515 }
516
517 inline ssize_t os::readlink(const char* path, char* buf, size_t bufsiz)
518 {
519 #if defined(HAVE_READLINK)
520         return ::readlink(path, buf, bufsiz);
521 #else
522 # error readlink not available
523 #endif
524 }
525
526 inline static void *system_realloc(void *ptr, size_t size)
527 {
528 #if defined(HAVE_REALLOC)
529         return realloc(ptr, size);
530 #else
531 # error realloc not available
532 #endif
533 }
534
535 template<class F1, class F2>
536 inline int os::call_scandir(int (*scandir)(const char *, struct dirent ***, F1, F2), const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
537 {
538         return scandir(dir, namelist, (F1) filter, (F2) compar);
539 }
540
541 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
542 {
543 #if defined(HAVE_SCANDIR)
544         return call_scandir(::scandir, dir, namelist, filter, compar);
545 #else
546 # error scandir not available
547 #endif
548 }
549
550 inline ssize_t os::send(int s, const void* buf, size_t len, int flags)
551 {
552         // TODO Should be restartable on Linux and interruptible on Solaris.
553 #if defined(HAVE_SEND)
554         return ::send(s, buf, len, flags);
555 #else
556 # error send not available
557 #endif
558 }
559
560 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
561 {
562 #if defined(HAVE_SETSOCKOPT)
563         return ::setsockopt(s, level, optname, optval, optlen);
564 #else
565 # error setsockopt not available
566 #endif
567 }
568
569 inline int os::shutdown(int s, int how)
570 {
571 #if defined(HAVE_SHUTDOWN)
572         return ::shutdown(s, how);
573 #else
574 # error shutdown not available
575 #endif
576 }
577
578 inline int os::socket(int domain, int type, int protocol)
579 {
580 #if defined(HAVE_SOCKET)
581         return ::socket(domain, type, protocol);
582 #else
583 # error socket not available
584 #endif
585 }
586
587 inline int os::stat(const char* path, struct stat* buf)
588 {
589 #if defined(HAVE_STAT)
590         return ::stat(path, buf);
591 #else
592 # error stat not available
593 #endif
594 }
595
596 #if defined(__SOLARIS__)
597 inline int os::str2sig(const char* str, int* signum)
598 {
599 #if defined(HAVE_STR2SIG)
600         return ::str2sig(str, signum);
601 #else
602 # error str2sig not available
603 #endif
604 }
605 #endif
606
607 inline char* os::strcat(char* dest, const char* src)
608 {
609 #if defined(HAVE_STRCAT)
610         return ::strcat(dest, src);
611 #else
612 # error strcat not available
613 #endif
614 }
615
616 inline int os::strcmp(const char* s1, const char* s2)
617 {
618 #if defined(HAVE_STRCMP)
619         return ::strcmp(s1, s2);
620 #else
621 # error strcmp not available
622 #endif
623 }
624
625 inline char* os::strcpy(char* dest, const char* src)
626 {
627 #if defined(HAVE_STRCPY)
628         return ::strcpy(dest, src);
629 #else
630 # error strcpy not available
631 #endif
632 }
633
634 inline char* os::strdup(const char* s)
635 {
636 #if defined(HAVE_STRDUP)
637         return ::strdup(s);
638 #else
639 # error strdup not available
640 #endif
641 }
642
643 inline char* os::strerror(int errnum)
644 {
645 #if defined(HAVE_STRERROR)
646         return ::strerror(errnum);
647 #else
648 # error strerror not available
649 #endif
650 }
651
652 inline size_t os::strlen(const char* s)
653 {
654 #if defined(HAVE_STRLEN)
655         return ::strlen(s);
656 #else
657 # error strlen not available
658 #endif
659 }
660
661 inline static ssize_t system_write(int fd, const void *buf, size_t count)
662 {
663 #if defined(HAVE_WRITE)
664         return write(fd, buf, count);
665 #else
666 # error write not available
667 #endif
668 }
669
670 #else
671
672 void*  os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
673
674 void   os_abort(void);
675 int    os_access(const char* pathname, int mode);
676 int    os_atoi(const char* nptr);
677 void*  os_calloc(size_t nmemb, size_t size);
678 char*  os_dirname(char* path);
679 char*  os_dlerror(void);
680 void*  os_dlsym(void* handle, const char* symbol);
681 int    os_fclose(FILE* fp);
682 FILE*  os_fopen(const char* path, const char* mode);
683 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
684 void   os_free(void* ptr);
685 int    os_getpagesize(void);
686 void*  os_memcpy(void* dest, const void* src, size_t n);
687 void*  os_memset(void* s, int c, size_t n);
688 int    os_mprotect(void* addr, size_t len, int prot);
689 int    os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
690 int    os_stat(const char* path, struct stat* buf);
691 char*  os_strcat(char* dest, const char* src);
692 char*  os_strcpy(char* dest, const char* src);
693 char*  os_strdup(const char* s);
694 int    os_strlen(const char* s);
695
696 #endif
697
698 #endif // _OS_HPP
699
700
701 /*
702  * These are local overrides for various environment variables in Emacs.
703  * Please do not remove this and leave it at the end of the file, where
704  * Emacs will automagically detect them.
705  * ---------------------------------------------------------------------
706  * Local variables:
707  * mode: c++
708  * indent-tabs-mode: t
709  * c-basic-offset: 4
710  * tab-width: 4
711  * End:
712  * vim:noexpandtab:sw=4:ts=4:
713  */