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* 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);
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);
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();
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 *));
183 inline void os::abort(void)
185 #if defined(HAVE_ABORT)
188 # error abort not available
192 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
194 #if defined(HAVE_ACCEPT)
195 return ::accept(sockfd, addr, addrlen);
197 # error accept not available
201 inline int os::access(const char* pathname, int mode)
203 #if defined(HAVE_ACCESS)
204 return ::access(pathname, mode);
206 # error access not available
210 inline int os::atoi(const char* nptr)
212 #if defined(HAVE_ATOI)
215 # error atoi not available
219 inline int os::backtrace(void** array, int size)
221 #if defined(HAVE_BACKTRACE)
222 return ::backtrace(array, size);
224 fprintf(stderr, "os::backtrace: Not available.");
229 inline char** os::backtrace_symbols(void* const* array, int size) throw ()
231 #if defined(HAVE_BACKTRACE_SYMBOLS)
232 return ::backtrace_symbols(array, size);
234 fprintf(stderr, "os::backtrace_symbols: Not available.");
239 inline void* os::calloc(size_t nmemb, size_t size)
241 #if defined(HAVE_CALLOC)
242 return ::calloc(nmemb, size);
244 # error calloc not available
248 inline int os::close(int fd)
250 #if defined(HAVE_CLOSE)
253 # error close not available
257 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
259 #if defined(HAVE_CONNECT)
260 return ::connect(sockfd, serv_addr, addrlen);
262 # error connect not available
266 #if defined(ENABLE_JRE_LAYOUT)
267 inline char* os::dirname(char* path)
269 #if defined(HAVE_DIRNAME)
270 return ::dirname(path);
272 # error dirname not available
277 inline int os::dlclose(void* handle)
279 #if defined(HAVE_DLCLOSE)
280 return ::dlclose(handle);
282 # error dlclose not available
286 inline char* os::dlerror(void)
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();
293 # error dlerror not available
297 inline void* os::dlopen(const char* filename, int flag)
299 #if defined(HAVE_DLOPEN)
300 return ::dlopen(filename, flag);
302 # error dlopen not available
306 inline void* os::dlsym(void* handle, const char* symbol)
308 #if defined(HAVE_DLSYM)
309 return ::dlsym(handle, symbol);
311 # error dlsym not available
315 inline int os::fclose(FILE* fp)
317 #if defined(HAVE_FCLOSE)
320 # error fclose not available
324 inline FILE* os::fopen(const char* path, const char* mode)
326 #if defined(HAVE_FOPEN)
327 return ::fopen(path, mode);
329 # error fopen not available
333 inline int os::fprintf(FILE* stream, const char* format, ...)
335 #if defined(HAVE_FPRINTF)
337 va_start(ap, format);
338 int result = ::vfprintf(stream, format, ap);
342 # error fprintf not available
346 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
348 #if defined(HAVE_FREAD)
349 return ::fread(ptr, size, nmemb, stream);
351 # error fread not available
355 inline void os::free(void* ptr)
357 #if defined(HAVE_FREE)
360 # error free not available
364 inline static int system_fsync(int fd)
366 #if defined(HAVE_FSYNC)
369 # error fsync not available
373 inline static int system_ftruncate(int fd, off_t length)
375 #if defined(HAVE_FTRUNCATE)
376 return ftruncate(fd, length);
378 # error ftruncate not available
382 inline char* os::getenv(const char* name)
384 #if defined(HAVE_GETENV)
385 return ::getenv(name);
387 # error getenv not available
391 inline int os::gethostname(char* name, size_t len)
393 #if defined(HAVE_GETHOSTNAME)
394 return ::gethostname(name, len);
396 # error gethostname not available
400 inline int os::getloadavg(double loadavg[], int nelem)
402 #if defined(HAVE_GETLOADAVG)
403 return ::getloadavg(loadavg, nelem);
405 # error getloadavg not available
409 inline int os::getpagesize(void)
411 #if defined(HAVE_GETPAGESIZE)
412 return ::getpagesize();
414 # error getpagesize not available
418 inline pid_t os::getpid(void)
420 #if defined(HAVE_GETPID)
423 # error getpid not available
427 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
429 #if defined(HAVE_GETSOCKNAME)
430 return ::getsockname(s, name, namelen);
432 # error getsockname not available
436 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
438 #if defined(HAVE_GETSOCKOPT)
439 return ::getsockopt(s, level, optname, optval, optlen);
441 # error getsockopt not available
445 inline int os::listen(int sockfd, int backlog)
447 #if defined(HAVE_LISTEN)
448 return ::listen(sockfd, backlog);
450 # error listen not available
454 inline static off_t system_lseek(int fildes, off_t offset, int whence)
456 #if defined(HAVE_LSEEK)
457 return lseek(fildes, offset, whence);
459 # error lseek not available
463 inline void* os::malloc(size_t size)
465 #if defined(HAVE_MALLOC)
466 return ::malloc(size);
468 # error malloc not available
472 inline void* os::memcpy(void* dest, const void* src, size_t n)
474 #if defined(HAVE_MEMCPY)
475 return ::memcpy(dest, src, n);
477 # error memcpy not available
481 inline void* os::memset(void* s, int c, size_t n)
483 #if defined(HAVE_MEMSET)
484 return ::memset(s, c, n);
486 # error memset not available
490 inline int os::mprotect(void* addr, size_t len, int prot)
492 #if defined(HAVE_MPROTECT)
493 return ::mprotect(addr, len, prot);
495 # error mprotect not available
499 inline static int system_open(const char *pathname, int flags, mode_t mode)
501 #if defined(HAVE_OPEN)
502 return open(pathname, flags, mode);
504 # error open not available
508 inline static ssize_t system_read(int fd, void *buf, size_t count)
510 #if defined(HAVE_READ)
511 return read(fd, buf, count);
513 # error read not available
517 inline ssize_t os::readlink(const char* path, char* buf, size_t bufsiz)
519 #if defined(HAVE_READLINK)
520 return ::readlink(path, buf, bufsiz);
522 # error readlink not available
526 inline static void *system_realloc(void *ptr, size_t size)
528 #if defined(HAVE_REALLOC)
529 return realloc(ptr, size);
531 # error realloc not available
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 *))
538 return scandir(dir, namelist, (F1) filter, (F2) compar);
541 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
543 #if defined(HAVE_SCANDIR)
544 return call_scandir(::scandir, dir, namelist, filter, compar);
546 # error scandir not available
550 inline ssize_t os::send(int s, const void* buf, size_t len, int flags)
552 // TODO Should be restartable on Linux and interruptible on Solaris.
553 #if defined(HAVE_SEND)
554 return ::send(s, buf, len, flags);
556 # error send not available
560 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
562 #if defined(HAVE_SETSOCKOPT)
563 return ::setsockopt(s, level, optname, optval, optlen);
565 # error setsockopt not available
569 inline int os::shutdown(int s, int how)
571 #if defined(HAVE_SHUTDOWN)
572 return ::shutdown(s, how);
574 # error shutdown not available
578 inline int os::socket(int domain, int type, int protocol)
580 #if defined(HAVE_SOCKET)
581 return ::socket(domain, type, protocol);
583 # error socket not available
587 inline int os::stat(const char* path, struct stat* buf)
589 #if defined(HAVE_STAT)
590 return ::stat(path, buf);
592 # error stat not available
596 #if defined(__SOLARIS__)
597 inline int os::str2sig(const char* str, int* signum)
599 #if defined(HAVE_STR2SIG)
600 return ::str2sig(str, signum);
602 # error str2sig not available
607 inline char* os::strcat(char* dest, const char* src)
609 #if defined(HAVE_STRCAT)
610 return ::strcat(dest, src);
612 # error strcat not available
616 inline int os::strcmp(const char* s1, const char* s2)
618 #if defined(HAVE_STRCMP)
619 return ::strcmp(s1, s2);
621 # error strcmp not available
625 inline char* os::strcpy(char* dest, const char* src)
627 #if defined(HAVE_STRCPY)
628 return ::strcpy(dest, src);
630 # error strcpy not available
634 inline char* os::strdup(const char* s)
636 #if defined(HAVE_STRDUP)
639 # error strdup not available
643 inline char* os::strerror(int errnum)
645 #if defined(HAVE_STRERROR)
646 return ::strerror(errnum);
648 # error strerror not available
652 inline size_t os::strlen(const char* s)
654 #if defined(HAVE_STRLEN)
657 # error strlen not available
661 inline static ssize_t system_write(int fd, const void *buf, size_t count)
663 #if defined(HAVE_WRITE)
664 return write(fd, buf, count);
666 # error write not available
672 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
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);
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 * ---------------------------------------------------------------------
708 * indent-tabs-mode: t
712 * vim:noexpandtab:sw=4:ts=4: