1 /* src/vm/os.hpp - system (OS) functions
3 Copyright (C) 2007, 2008, 2009
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2008 Theobroma Systems Ltd.
7 This file is part of CACAO.
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.
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.
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
32 // NOTE: In this file we check for all system headers, because we wrap
33 // all system calls into inline functions for better portability.
35 // Please don't include CACAO headers here as this header should be a
36 // very low-level one.
38 #if defined(HAVE_DIRENT_H)
42 #if defined(HAVE_DLFCN_H)
46 #if defined(HAVE_ERRNO_H)
50 #if defined(HAVE_EXECINFO_H)
51 # include <execinfo.h>
54 #if defined(HAVE_FCNTL_H)
58 #if defined(ENABLE_JRE_LAYOUT)
59 # if defined(HAVE_LIBGEN_H)
64 #if defined(HAVE_SIGNAL_H)
68 #if defined(HAVE_STDARG_H)
72 #if defined(HAVE_STDINT_H)
76 #if defined(HAVE_STDIO_H)
80 #if defined(HAVE_STDLIB_H)
84 #if defined(HAVE_STRING_H)
88 #if defined(HAVE_UNISTD_H)
92 #if defined(HAVE_SYS_MMAN_H)
93 # include <sys/mman.h>
96 #if defined(HAVE_SYS_SOCKET_H)
97 # include <sys/socket.h>
100 #if defined(HAVE_SYS_STAT_H)
101 # include <sys/stat.h>
104 #if defined(HAVE_SYS_TYPES_H)
105 # include <sys/types.h>
111 // Class wrapping system (OS) 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);
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 getloadavg(double loadavg[], int nelem);
139 static inline int getpagesize(void);
140 static inline int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
141 static inline int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
142 static inline int listen(int sockfd, int backlog);
143 static inline void* malloc(size_t size);
144 static inline void* memcpy(void* dest, const void* src, size_t n);
145 static inline void* memset(void* s, int c, size_t n);
146 static inline int mprotect(void* addr, size_t len, int prot);
147 static inline ssize_t readlink(const char* path, char* buf, size_t bufsiz);
148 static inline int scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
149 static inline ssize_t send(int s, const void* buf, size_t len, int flags);
150 static inline int setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
151 static inline int shutdown(int s, int how);
152 static inline int socket(int domain, int type, int protocol);
153 static inline int stat(const char* path, struct stat* buf);
154 #if defined(__SOLARIS__)
155 static inline int str2sig(const char* str, int* signum);
157 static inline char* strcat(char* dest, const char* src);
158 static inline int strcmp(const char* s1, const char* s2);
159 static inline char* strcpy(char* dest, const char* src);
160 static inline char* strdup(const char* s);
161 static inline size_t strlen(const char* s);
162 static inline char* strerror(int errnum);
164 // Convenience functions.
165 static void abort(const char* text, ...);
166 static void abort_errnum(int errnum, const char* text, ...);
167 static void abort_errno(const char* text, ...);
168 static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
169 static void print_backtrace();
170 static int processors_online();
173 template<class F1, class F2>
174 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 *));
178 inline void os::abort(void)
180 #if defined(HAVE_ABORT)
183 # error abort not available
187 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
189 #if defined(HAVE_ACCEPT)
190 return ::accept(sockfd, addr, addrlen);
192 # error accept not available
196 inline int os::access(const char* pathname, int mode)
198 #if defined(HAVE_ACCESS)
199 return ::access(pathname, mode);
201 # error access not available
205 inline int os::atoi(const char* nptr)
207 #if defined(HAVE_ATOI)
210 # error atoi not available
214 inline int os::backtrace(void** array, int size)
216 #if defined(HAVE_BACKTRACE)
217 return ::backtrace(array, size);
219 fprintf(stderr, "os::backtrace: Not available.");
224 inline char** os::backtrace_symbols(void* const* array, int size) throw ()
226 #if defined(HAVE_BACKTRACE_SYMBOLS)
227 return ::backtrace_symbols(array, size);
229 fprintf(stderr, "os::backtrace_symbols: Not available.");
234 inline void* os::calloc(size_t nmemb, size_t size)
236 #if defined(HAVE_CALLOC)
237 return ::calloc(nmemb, size);
239 # error calloc not available
243 inline int os::close(int fd)
245 #if defined(HAVE_CLOSE)
248 # error close not available
252 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
254 #if defined(HAVE_CONNECT)
255 return ::connect(sockfd, serv_addr, addrlen);
257 # error connect not available
261 #if defined(ENABLE_JRE_LAYOUT)
262 inline char* os::dirname(char* path)
264 #if defined(HAVE_DIRNAME)
265 return ::dirname(path);
267 # error dirname not available
272 inline int os::dlclose(void* handle)
274 #if defined(HAVE_DLCLOSE)
275 return ::dlclose(handle);
277 # error dlclose not available
281 inline char* os::dlerror(void)
283 #if defined(HAVE_DLERROR)
284 // At least FreeBSD defines dlerror() to return a const char*, so
285 // we simply cast it.
286 return (char*) ::dlerror();
288 # error dlerror not available
292 inline void* os::dlopen(const char* filename, int flag)
294 #if defined(HAVE_DLOPEN)
295 return ::dlopen(filename, flag);
297 # error dlopen not available
301 inline void* os::dlsym(void* handle, const char* symbol)
303 #if defined(HAVE_DLSYM)
304 return ::dlsym(handle, symbol);
306 # error dlsym not available
310 inline int os::fclose(FILE* fp)
312 #if defined(HAVE_FCLOSE)
315 # error fclose not available
319 inline FILE* os::fopen(const char* path, const char* mode)
321 #if defined(HAVE_FOPEN)
322 return ::fopen(path, mode);
324 # error fopen not available
328 inline int os::fprintf(FILE* stream, const char* format, ...)
330 #if defined(HAVE_FPRINTF)
332 va_start(ap, format);
333 int result = ::vfprintf(stream, format, ap);
337 # error fprintf not available
341 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
343 #if defined(HAVE_FREAD)
344 return ::fread(ptr, size, nmemb, stream);
346 # error fread not available
350 inline void os::free(void* ptr)
352 #if defined(HAVE_FREE)
355 # error free not available
359 inline static int system_fsync(int fd)
361 #if defined(HAVE_FSYNC)
364 # error fsync not available
368 inline static int system_ftruncate(int fd, off_t length)
370 #if defined(HAVE_FTRUNCATE)
371 return ftruncate(fd, length);
373 # error ftruncate not available
377 inline char* os::getenv(const char* name)
379 #if defined(HAVE_GETENV)
380 return ::getenv(name);
382 # error getenv not available
386 inline int os::gethostname(char* name, size_t len)
388 #if defined(HAVE_GETHOSTNAME)
389 return ::gethostname(name, len);
391 # error gethostname not available
395 inline int os::getloadavg(double loadavg[], int nelem)
397 #if defined(HAVE_GETLOADAVG)
398 return ::getloadavg(loadavg, nelem);
400 # error getloadavg not available
404 inline int os::getpagesize(void)
406 #if defined(HAVE_GETPAGESIZE)
407 return ::getpagesize();
409 # error getpagesize not available
413 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
415 #if defined(HAVE_GETSOCKNAME)
416 return ::getsockname(s, name, namelen);
418 # error getsockname not available
422 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
424 #if defined(HAVE_GETSOCKOPT)
425 return ::getsockopt(s, level, optname, optval, optlen);
427 # error getsockopt not available
431 inline int os::listen(int sockfd, int backlog)
433 #if defined(HAVE_LISTEN)
434 return ::listen(sockfd, backlog);
436 # error listen not available
440 inline static off_t system_lseek(int fildes, off_t offset, int whence)
442 #if defined(HAVE_LSEEK)
443 return lseek(fildes, offset, whence);
445 # error lseek not available
449 inline void* os::malloc(size_t size)
451 #if defined(HAVE_MALLOC)
452 return ::malloc(size);
454 # error malloc not available
458 inline void* os::memcpy(void* dest, const void* src, size_t n)
460 #if defined(HAVE_MEMCPY)
461 return ::memcpy(dest, src, n);
463 # error memcpy not available
467 inline void* os::memset(void* s, int c, size_t n)
469 #if defined(HAVE_MEMSET)
470 return ::memset(s, c, n);
472 # error memset not available
476 inline int os::mprotect(void* addr, size_t len, int prot)
478 #if defined(HAVE_MPROTECT)
479 return ::mprotect(addr, len, prot);
481 # error mprotect not available
485 inline static int system_open(const char *pathname, int flags, mode_t mode)
487 #if defined(HAVE_OPEN)
488 return open(pathname, flags, mode);
490 # error open not available
494 inline static ssize_t system_read(int fd, void *buf, size_t count)
496 #if defined(HAVE_READ)
497 return read(fd, buf, count);
499 # error read not available
503 inline ssize_t os::readlink(const char* path, char* buf, size_t bufsiz)
505 #if defined(HAVE_READLINK)
506 return ::readlink(path, buf, bufsiz);
508 # error readlink not available
512 inline static void *system_realloc(void *ptr, size_t size)
514 #if defined(HAVE_REALLOC)
515 return realloc(ptr, size);
517 # error realloc not available
521 template<class F1, class F2>
522 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 *))
524 return scandir(dir, namelist, (F1) filter, (F2) compar);
527 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
529 #if defined(HAVE_SCANDIR)
530 return call_scandir(::scandir, dir, namelist, filter, compar);
532 # error scandir not available
536 inline ssize_t os::send(int s, const void* buf, size_t len, int flags)
538 // TODO Should be restartable on Linux and interruptible on Solaris.
539 #if defined(HAVE_SEND)
540 return ::send(s, buf, len, flags);
542 # error send not available
546 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
548 #if defined(HAVE_SETSOCKOPT)
549 return ::setsockopt(s, level, optname, optval, optlen);
551 # error setsockopt not available
555 inline int os::shutdown(int s, int how)
557 #if defined(HAVE_SHUTDOWN)
558 return ::shutdown(s, how);
560 # error shutdown not available
564 inline int os::socket(int domain, int type, int protocol)
566 #if defined(HAVE_SOCKET)
567 return ::socket(domain, type, protocol);
569 # error socket not available
573 inline int os::stat(const char* path, struct stat* buf)
575 #if defined(HAVE_STAT)
576 return ::stat(path, buf);
578 # error stat not available
582 #if defined(__SOLARIS__)
583 inline int os::str2sig(const char* str, int* signum)
585 #if defined(HAVE_STR2SIG)
586 return ::str2sig(str, signum);
588 # error str2sig not available
593 inline char* os::strcat(char* dest, const char* src)
595 #if defined(HAVE_STRCAT)
596 return ::strcat(dest, src);
598 # error strcat not available
602 inline int os::strcmp(const char* s1, const char* s2)
604 #if defined(HAVE_STRCMP)
605 return ::strcmp(s1, s2);
607 # error strcmp not available
611 inline char* os::strcpy(char* dest, const char* src)
613 #if defined(HAVE_STRCPY)
614 return ::strcpy(dest, src);
616 # error strcpy not available
620 inline char* os::strdup(const char* s)
622 #if defined(HAVE_STRDUP)
625 # error strdup not available
629 inline char* os::strerror(int errnum)
631 #if defined(HAVE_STRERROR)
632 return ::strerror(errnum);
634 # error strerror not available
638 inline size_t os::strlen(const char* s)
640 #if defined(HAVE_STRLEN)
643 # error strlen not available
647 inline static ssize_t system_write(int fd, const void *buf, size_t count)
649 #if defined(HAVE_WRITE)
650 return write(fd, buf, count);
652 # error write not available
658 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
661 int os_access(const char* pathname, int mode);
662 int os_atoi(const char* nptr);
663 void* os_calloc(size_t nmemb, size_t size);
664 char* os_dirname(char* path);
665 char* os_dlerror(void);
666 void* os_dlsym(void* handle, const char* symbol);
667 int os_fclose(FILE* fp);
668 FILE* os_fopen(const char* path, const char* mode);
669 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
670 void os_free(void* ptr);
671 int os_getpagesize(void);
672 void* os_memcpy(void* dest, const void* src, size_t n);
673 void* os_memset(void* s, int c, size_t n);
674 int os_mprotect(void* addr, size_t len, int prot);
675 int os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
676 int os_stat(const char* path, struct stat* buf);
677 char* os_strcat(char* dest, const char* src);
678 char* os_strcpy(char* dest, const char* src);
679 char* os_strdup(const char* s);
680 int os_strlen(const char* s);
688 * These are local overrides for various environment variables in Emacs.
689 * Please do not remove this and leave it at the end of the file, where
690 * Emacs will automagically detect them.
691 * ---------------------------------------------------------------------
694 * indent-tabs-mode: t
698 * vim:noexpandtab:sw=4:ts=4: