1 /* src/vm/os.hpp - system (OS) functions
3 Copyright (C) 2007, 2008
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 #if defined(HAVE_DIRENT_H)
39 #if defined(HAVE_DLFCN_H)
43 #if defined(HAVE_ERRNO_H)
47 #if defined(HAVE_EXECINFO_H)
48 # include <execinfo.h>
51 #if defined(HAVE_FCNTL_H)
55 #if defined(ENABLE_JRE_LAYOUT)
56 # if defined(HAVE_LIBGEN_H)
61 #if defined(HAVE_SIGNAL_H)
65 #if defined(HAVE_STDINT_H)
69 #if defined(HAVE_STDIO_H)
73 #if defined(HAVE_STDLIB_H)
77 #if defined(HAVE_STRING_H)
81 #if defined(HAVE_UNISTD_H)
85 #if defined(HAVE_SYS_MMAN_H)
86 # include <sys/mman.h>
89 #if defined(HAVE_SYS_SOCKET_H)
90 # include <sys/socket.h>
93 #if defined(HAVE_SYS_STAT_H)
94 # include <sys/stat.h>
97 #if defined(HAVE_SYS_TYPES_H)
98 # include <sys/types.h>
104 // Class wrapping system (OS) functions.
108 static inline void abort();
109 static inline int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
110 static inline int access(const char *pathname, int mode);
111 static inline int atoi(const char* nptr);
112 static inline int backtrace(void** array, int size);
113 static inline char** backtrace_symbols(void* const* array, int size) throw ();
114 static inline void* calloc(size_t nmemb, size_t size);
115 static inline int close(int fd);
116 static inline int connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen);
117 #if defined(ENABLE_JRE_LAYOUT)
118 static inline char* dirname(char* path);
120 static inline int dlclose(void* handle);
121 static inline char* dlerror(void);
122 static inline void* dlopen(const char* filename, int flag);
123 static inline void* dlsym(void* handle, const char* symbol);
124 static inline int fclose(FILE* fp);
125 static inline FILE* fopen(const char* path, const char* mode);
126 static inline size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
127 static inline void free(void* ptr);
128 static inline char* getenv(const char* name);
129 static inline int gethostname(char* name, size_t len);
130 static inline int getpagesize(void);
131 static inline int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
132 static inline int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
133 static inline int listen(int sockfd, int backlog);
134 static inline void* malloc(size_t size);
135 static inline void* memcpy(void* dest, const void* src, size_t n);
136 static inline void* memset(void* s, int c, size_t n);
137 static inline int mprotect(void* addr, size_t len, int prot);
138 static inline int scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
139 static inline int setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
140 static inline int shutdown(int s, int how);
141 static inline int socket(int domain, int type, int protocol);
142 static inline int stat(const char* path, struct stat* buf);
143 #if defined(__SOLARIS__)
144 static inline int str2sig(const char* str, int* signum);
146 static inline char* strcat(char* dest, const char* src);
147 static inline int strcmp(const char* s1, const char* s2);
148 static inline char* strcpy(char* dest, const char* src);
149 static inline char* strdup(const char* s);
150 static inline size_t strlen(const char* s);
151 static inline char* strerror(int errnum);
153 // Convenience functions.
154 static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
155 static void print_backtrace();
156 static int processors_online();
161 #include "toolbox/logging.h"
164 inline void os::abort(void)
166 #if defined(HAVE_ABORT)
169 # error abort not available
173 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
175 #if defined(HAVE_ACCEPT)
176 return ::accept(sockfd, addr, addrlen);
178 # error accept not available
182 inline int os::access(const char* pathname, int mode)
184 #if defined(HAVE_ACCESS)
185 return ::access(pathname, mode);
187 # error access not available
191 inline int os::atoi(const char* nptr)
193 #if defined(HAVE_ATOI)
196 # error atoi not available
200 inline int os::backtrace(void** array, int size)
202 #if defined(HAVE_BACKTRACE)
203 return ::backtrace(array, size);
205 log_println("os::backtrace: Not available.");
210 inline char** os::backtrace_symbols(void* const* array, int size) throw ()
212 #if defined(HAVE_BACKTRACE_SYMBOLS)
213 return ::backtrace_symbols(array, size);
215 log_println("os::backtrace_symbols: Not available.");
220 inline void* os::calloc(size_t nmemb, size_t size)
222 #if defined(HAVE_CALLOC)
223 return ::calloc(nmemb, size);
225 # error calloc not available
229 inline int os::close(int fd)
231 #if defined(HAVE_CLOSE)
234 # error close not available
238 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
240 #if defined(HAVE_CONNECT)
241 return ::connect(sockfd, serv_addr, addrlen);
243 # error connect not available
247 #if defined(ENABLE_JRE_LAYOUT)
248 inline char* os::dirname(char* path)
250 #if defined(HAVE_DIRNAME)
251 return ::dirname(path);
253 # error dirname not available
258 inline int os::dlclose(void* handle)
260 #if defined(HAVE_DLCLOSE)
261 return ::dlclose(handle);
263 # error dlclose not available
267 inline char* os::dlerror(void)
269 #if defined(HAVE_DLERROR)
272 # error dlerror not available
276 inline void* os::dlopen(const char* filename, int flag)
278 #if defined(HAVE_DLOPEN)
279 return ::dlopen(filename, flag);
281 # error dlopen not available
285 inline void* os::dlsym(void* handle, const char* symbol)
287 #if defined(HAVE_DLSYM)
288 return ::dlsym(handle, symbol);
290 # error dlsym not available
294 inline int os::fclose(FILE* fp)
296 #if defined(HAVE_FCLOSE)
299 # error fclose not available
303 inline FILE* os::fopen(const char* path, const char* mode)
305 #if defined(HAVE_FOPEN)
306 return ::fopen(path, mode);
308 # error fopen not available
312 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
314 #if defined(HAVE_FREAD)
315 return ::fread(ptr, size, nmemb, stream);
317 # error fread not available
321 inline void os::free(void* ptr)
323 #if defined(HAVE_FREE)
326 # error free not available
330 inline static int system_fsync(int fd)
332 #if defined(HAVE_FSYNC)
335 # error fsync not available
339 inline static int system_ftruncate(int fd, off_t length)
341 #if defined(HAVE_FTRUNCATE)
342 return ftruncate(fd, length);
344 # error ftruncate not available
348 inline char* os::getenv(const char* name)
350 #if defined(HAVE_GETENV)
351 return ::getenv(name);
353 # error getenv not available
357 inline int os::gethostname(char* name, size_t len)
359 #if defined(HAVE_GETHOSTNAME)
360 return ::gethostname(name, len);
362 # error gethostname not available
366 inline int os::getpagesize(void)
368 #if defined(HAVE_GETPAGESIZE)
369 return ::getpagesize();
371 # error getpagesize not available
375 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
377 #if defined(HAVE_GETSOCKNAME)
378 return ::getsockname(s, name, namelen);
380 # error getsockname not available
384 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
386 #if defined(HAVE_GETSOCKOPT)
387 return ::getsockopt(s, level, optname, optval, optlen);
389 # error getsockopt not available
393 inline int os::listen(int sockfd, int backlog)
395 #if defined(HAVE_LISTEN)
396 return ::listen(sockfd, backlog);
398 # error listen not available
402 inline static off_t system_lseek(int fildes, off_t offset, int whence)
404 #if defined(HAVE_LSEEK)
405 return lseek(fildes, offset, whence);
407 # error lseek not available
411 inline void* os::malloc(size_t size)
413 #if defined(HAVE_MALLOC)
414 return ::malloc(size);
416 # error malloc not available
420 inline void* os::memcpy(void* dest, const void* src, size_t n)
422 #if defined(HAVE_MEMCPY)
423 return ::memcpy(dest, src, n);
425 # error memcpy not available
429 inline void* os::memset(void* s, int c, size_t n)
431 #if defined(HAVE_MEMSET)
432 return ::memset(s, c, n);
434 # error memset not available
438 inline int os::mprotect(void* addr, size_t len, int prot)
440 #if defined(HAVE_MPROTECT)
441 return ::mprotect(addr, len, prot);
443 # error mprotect not available
447 inline static int system_open(const char *pathname, int flags, mode_t mode)
449 #if defined(HAVE_OPEN)
450 return open(pathname, flags, mode);
452 # error open not available
456 inline static ssize_t system_read(int fd, void *buf, size_t count)
458 #if defined(HAVE_READ)
459 return read(fd, buf, count);
461 # error read not available
465 inline static void *system_realloc(void *ptr, size_t size)
467 #if defined(HAVE_REALLOC)
468 return realloc(ptr, size);
470 # error realloc not available
474 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
476 #elif defined(__SOLARIS__)
477 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
478 #elif defined(__IRIX__)
479 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
481 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
485 #if defined(HAVE_SCANDIR)
486 # if defined(__LINUX__)
487 return ::scandir(dir, namelist, filter, compar);
488 #elif defined(__SOLARIS__)
489 return ::scandir(dir, namelist, filter, (int (*)(const dirent**, const dirent**)) compar);
491 return ::scandir(dir, namelist, (int (*)(struct dirent*)) filter, compar);
494 # error scandir not available
498 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
500 #if defined(HAVE_SETSOCKOPT)
501 return ::setsockopt(s, level, optname, optval, optlen);
503 # error setsockopt not available
507 inline int os::shutdown(int s, int how)
509 #if defined(HAVE_SHUTDOWN)
510 return ::shutdown(s, how);
512 # error shutdown not available
516 inline int os::socket(int domain, int type, int protocol)
518 #if defined(HAVE_SOCKET)
519 return ::socket(domain, type, protocol);
521 # error socket not available
525 inline int os::stat(const char* path, struct stat* buf)
527 #if defined(HAVE_STAT)
528 return ::stat(path, buf);
530 # error stat not available
534 #if defined(__SOLARIS__)
535 inline int os::str2sig(const char* str, int* signum)
537 #if defined(HAVE_STR2SIG)
538 return ::str2sig(str, signum);
540 # error str2sig not available
545 inline char* os::strcat(char* dest, const char* src)
547 #if defined(HAVE_STRCAT)
548 return ::strcat(dest, src);
550 # error strcat not available
554 inline int os::strcmp(const char* s1, const char* s2)
556 #if defined(HAVE_STRCMP)
557 return ::strcmp(s1, s2);
559 # error strcmp not available
563 inline char* os::strcpy(char* dest, const char* src)
565 #if defined(HAVE_STRCPY)
566 return ::strcpy(dest, src);
568 # error strcpy not available
572 inline char* os::strdup(const char* s)
574 #if defined(HAVE_STRDUP)
577 # error strdup not available
581 inline char* os::strerror(int errnum)
583 #if defined(HAVE_STRERROR)
584 return ::strerror(errnum);
586 # error strerror not available
590 inline size_t os::strlen(const char* s)
592 #if defined(HAVE_STRLEN)
595 # error strlen not available
599 inline static ssize_t system_write(int fd, const void *buf, size_t count)
601 #if defined(HAVE_WRITE)
602 return write(fd, buf, count);
604 # error write not available
610 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
613 int os_access(const char* pathname, int mode);
614 int os_atoi(const char* nptr);
615 void* os_calloc(size_t nmemb, size_t size);
616 char* os_dirname(char* path);
617 int os_dlclose(void* handle);
618 char* os_dlerror(void);
619 void* os_dlopen(const char* filename, int flag);
620 void* os_dlsym(void* handle, const char* symbol);
621 int os_fclose(FILE* fp);
622 FILE* os_fopen(const char* path, const char* mode);
623 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
624 void os_free(void* ptr);
625 int os_getpagesize(void);
626 void* os_memcpy(void* dest, const void* src, size_t n);
627 void* os_memset(void* s, int c, size_t n);
628 int os_mprotect(void* addr, size_t len, int prot);
629 int os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
630 int os_stat(const char* path, struct stat* buf);
631 char* os_strcat(char* dest, const char* src);
632 char* os_strcpy(char* dest, const char* src);
633 char* os_strdup(const char* s);
634 int os_strlen(const char* s);
642 * These are local overrides for various environment variables in Emacs.
643 * Please do not remove this and leave it at the end of the file, where
644 * Emacs will automagically detect them.
645 * ---------------------------------------------------------------------
648 * indent-tabs-mode: t
652 * vim:noexpandtab:sw=4:ts=4: