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_LOADAVG_H)
93 # include <sys/loadavg.h>
96 #if defined(HAVE_SYS_MMAN_H)
97 # include <sys/mman.h>
100 #if defined(HAVE_SYS_SOCKET_H)
101 # include <sys/socket.h>
104 #if defined(HAVE_SYS_STAT_H)
105 # include <sys/stat.h>
108 #if defined(HAVE_SYS_TYPES_H)
109 # include <sys/types.h>
115 // Class wrapping system (OS) 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);
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* getcwd(char* buf, size_t size);
141 static inline char* getenv(const char* name);
142 static inline int gethostname(char* name, size_t len);
143 static inline int getloadavg(double loadavg[], int nelem);
144 static inline int getpagesize(void);
145 static inline pid_t getpid(void);
146 static inline int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
147 static inline int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
148 static inline int listen(int sockfd, int backlog);
149 static inline void* malloc(size_t size);
150 static inline void* memcpy(void* dest, const void* src, size_t n);
151 static inline void* memset(void* s, int c, size_t n);
152 static inline int mprotect(void* addr, size_t len, int prot);
153 static inline ssize_t readlink(const char* path, char* buf, size_t bufsiz);
154 static inline int scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
155 static inline ssize_t send(int s, const void* buf, size_t len, int flags);
156 static inline int setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
157 static inline int shutdown(int s, int how);
158 static inline int socket(int domain, int type, int protocol);
159 static inline int stat(const char* path, struct stat* buf);
160 #if defined(__SOLARIS__)
161 static inline int str2sig(const char* str, int* signum);
163 static inline char* strcat(char* dest, const char* src);
164 static inline int strcmp(const char* s1, const char* s2);
165 static inline char* strcpy(char* dest, const char* src);
166 static inline char* strdup(const char* s);
167 static inline size_t strlen(const char* s);
168 static inline char* strerror(int errnum);
170 // Convenience functions.
171 static void abort(const char* text, ...);
172 static void abort_errnum(int errnum, const char* text, ...);
173 static void abort_errno(const char* text, ...);
174 static char* getcwd(void);
175 static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
176 static void print_backtrace();
177 static int processors_online();
180 template<class F1, class F2>
181 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 *));
185 inline void os::abort(void)
187 #if defined(HAVE_ABORT)
190 # error abort not available
194 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
196 #if defined(HAVE_ACCEPT)
197 return ::accept(sockfd, addr, addrlen);
199 # error accept not available
203 inline int os::access(const char* pathname, int mode)
205 #if defined(HAVE_ACCESS)
206 return ::access(pathname, mode);
208 # error access not available
212 inline int os::atoi(const char* nptr)
214 #if defined(HAVE_ATOI)
217 # error atoi not available
221 inline int os::backtrace(void** array, int size)
223 #if defined(HAVE_BACKTRACE)
224 return ::backtrace(array, size);
226 fprintf(stderr, "os::backtrace: Not available.");
231 inline char** os::backtrace_symbols(void* const* array, int size) throw ()
233 #if defined(HAVE_BACKTRACE_SYMBOLS)
234 return ::backtrace_symbols(array, size);
236 fprintf(stderr, "os::backtrace_symbols: Not available.");
241 inline void* os::calloc(size_t nmemb, size_t size)
243 #if defined(HAVE_CALLOC)
244 return ::calloc(nmemb, size);
246 # error calloc not available
250 inline int os::close(int fd)
252 #if defined(HAVE_CLOSE)
255 # error close not available
259 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
261 #if defined(HAVE_CONNECT)
262 return ::connect(sockfd, serv_addr, addrlen);
264 # error connect not available
268 #if defined(ENABLE_JRE_LAYOUT)
269 inline char* os::dirname(char* path)
271 #if defined(HAVE_DIRNAME)
272 return ::dirname(path);
274 # error dirname not available
279 inline int os::dlclose(void* handle)
281 #if defined(HAVE_DLCLOSE)
282 return ::dlclose(handle);
284 # error dlclose not available
288 inline char* os::dlerror(void)
290 #if defined(HAVE_DLERROR)
291 // At least FreeBSD defines dlerror() to return a const char*, so
292 // we simply cast it.
293 return (char*) ::dlerror();
295 # error dlerror not available
299 inline void* os::dlopen(const char* filename, int flag)
301 #if defined(HAVE_DLOPEN)
302 return ::dlopen(filename, flag);
304 # error dlopen not available
308 inline void* os::dlsym(void* handle, const char* symbol)
310 #if defined(HAVE_DLSYM)
311 return ::dlsym(handle, symbol);
313 # error dlsym not available
317 inline int os::fclose(FILE* fp)
319 #if defined(HAVE_FCLOSE)
322 # error fclose not available
326 inline FILE* os::fopen(const char* path, const char* mode)
328 #if defined(HAVE_FOPEN)
329 return ::fopen(path, mode);
331 # error fopen not available
335 inline int os::fprintf(FILE* stream, const char* format, ...)
337 #if defined(HAVE_FPRINTF)
339 va_start(ap, format);
340 int result = ::vfprintf(stream, format, ap);
344 # error fprintf not available
348 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
350 #if defined(HAVE_FREAD)
351 return ::fread(ptr, size, nmemb, stream);
353 # error fread not available
357 inline void os::free(void* ptr)
359 #if defined(HAVE_FREE)
362 # error free not available
366 inline static int system_fsync(int fd)
368 #if defined(HAVE_FSYNC)
371 # error fsync not available
375 inline static int system_ftruncate(int fd, off_t length)
377 #if defined(HAVE_FTRUNCATE)
378 return ftruncate(fd, length);
380 # error ftruncate not available
384 inline char* os::getcwd(char* buf, size_t size)
386 #if defined(HAVE_GETCWD)
387 return ::getcwd(buf, size);
389 # error getcwd not available
393 inline char* os::getenv(const char* name)
395 #if defined(HAVE_GETENV)
396 return ::getenv(name);
398 # error getenv not available
402 inline int os::gethostname(char* name, size_t len)
404 #if defined(HAVE_GETHOSTNAME)
405 return ::gethostname(name, len);
407 # error gethostname not available
411 inline int os::getloadavg(double loadavg[], int nelem)
413 #if defined(HAVE_GETLOADAVG)
414 return ::getloadavg(loadavg, nelem);
416 # error getloadavg not available
420 inline int os::getpagesize(void)
422 #if defined(HAVE_GETPAGESIZE)
423 return ::getpagesize();
425 # error getpagesize not available
429 inline pid_t os::getpid(void)
431 #if defined(HAVE_GETPID)
434 # error getpid not available
438 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
440 #if defined(HAVE_GETSOCKNAME)
441 return ::getsockname(s, name, namelen);
443 # error getsockname not available
447 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
449 #if defined(HAVE_GETSOCKOPT)
450 return ::getsockopt(s, level, optname, optval, optlen);
452 # error getsockopt not available
456 inline int os::listen(int sockfd, int backlog)
458 #if defined(HAVE_LISTEN)
459 return ::listen(sockfd, backlog);
461 # error listen not available
465 inline static off_t system_lseek(int fildes, off_t offset, int whence)
467 #if defined(HAVE_LSEEK)
468 return lseek(fildes, offset, whence);
470 # error lseek not available
474 inline void* os::malloc(size_t size)
476 #if defined(HAVE_MALLOC)
477 return ::malloc(size);
479 # error malloc not available
483 inline void* os::memcpy(void* dest, const void* src, size_t n)
485 #if defined(HAVE_MEMCPY)
486 return ::memcpy(dest, src, n);
488 # error memcpy not available
492 inline void* os::memset(void* s, int c, size_t n)
494 #if defined(HAVE_MEMSET)
495 return ::memset(s, c, n);
497 # error memset not available
501 inline int os::mprotect(void* addr, size_t len, int prot)
503 #if defined(HAVE_MPROTECT)
504 return ::mprotect(addr, len, prot);
506 # error mprotect not available
510 inline static int system_open(const char *pathname, int flags, mode_t mode)
512 #if defined(HAVE_OPEN)
513 return open(pathname, flags, mode);
515 # error open not available
519 inline static ssize_t system_read(int fd, void *buf, size_t count)
521 #if defined(HAVE_READ)
522 return read(fd, buf, count);
524 # error read not available
528 inline ssize_t os::readlink(const char* path, char* buf, size_t bufsiz)
530 #if defined(HAVE_READLINK)
531 return ::readlink(path, buf, bufsiz);
533 # error readlink not available
537 inline static void *system_realloc(void *ptr, size_t size)
539 #if defined(HAVE_REALLOC)
540 return realloc(ptr, size);
542 # error realloc not available
546 template<class F1, class F2>
547 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 *))
549 return scandir(dir, namelist, (F1) filter, (F2) compar);
552 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
554 #if defined(HAVE_SCANDIR)
555 return call_scandir(::scandir, dir, namelist, filter, compar);
557 # error scandir not available
561 inline ssize_t os::send(int s, const void* buf, size_t len, int flags)
563 // TODO Should be restartable on Linux and interruptible on Solaris.
564 #if defined(HAVE_SEND)
565 return ::send(s, buf, len, flags);
567 # error send not available
571 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
573 #if defined(HAVE_SETSOCKOPT)
574 return ::setsockopt(s, level, optname, optval, optlen);
576 # error setsockopt not available
580 inline int os::shutdown(int s, int how)
582 #if defined(HAVE_SHUTDOWN)
583 return ::shutdown(s, how);
585 # error shutdown not available
589 inline int os::socket(int domain, int type, int protocol)
591 #if defined(HAVE_SOCKET)
592 return ::socket(domain, type, protocol);
594 # error socket not available
598 inline int os::stat(const char* path, struct stat* buf)
600 #if defined(HAVE_STAT)
601 return ::stat(path, buf);
603 # error stat not available
607 #if defined(__SOLARIS__)
608 inline int os::str2sig(const char* str, int* signum)
610 #if defined(HAVE_STR2SIG)
611 return ::str2sig(str, signum);
613 # error str2sig not available
618 inline char* os::strcat(char* dest, const char* src)
620 #if defined(HAVE_STRCAT)
621 return ::strcat(dest, src);
623 # error strcat not available
627 inline int os::strcmp(const char* s1, const char* s2)
629 #if defined(HAVE_STRCMP)
630 return ::strcmp(s1, s2);
632 # error strcmp not available
636 inline char* os::strcpy(char* dest, const char* src)
638 #if defined(HAVE_STRCPY)
639 return ::strcpy(dest, src);
641 # error strcpy not available
645 inline char* os::strdup(const char* s)
647 #if defined(HAVE_STRDUP)
650 # error strdup not available
654 inline char* os::strerror(int errnum)
656 #if defined(HAVE_STRERROR)
657 return ::strerror(errnum);
659 # error strerror not available
663 inline size_t os::strlen(const char* s)
665 #if defined(HAVE_STRLEN)
668 # error strlen not available
672 inline static ssize_t system_write(int fd, const void *buf, size_t count)
674 #if defined(HAVE_WRITE)
675 return write(fd, buf, count);
677 # error write not available
683 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
685 int os_atoi(const char* nptr);
686 int os_getpagesize(void);
687 void* os_memcpy(void* dest, const void* src, size_t n);
688 void* os_memset(void* s, int c, size_t n);
689 char* os_strdup(const char* s);
690 int os_strlen(const char* s);
698 * These are local overrides for various environment variables in Emacs.
699 * Please do not remove this and leave it at the end of the file, where
700 * Emacs will automagically detect them.
701 * ---------------------------------------------------------------------
704 * indent-tabs-mode: t
708 * vim:noexpandtab:sw=4:ts=4: