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>
112 #if defined(HAVE_SYS_UTSNAME_H)
113 # include <sys/utsname.h>
119 // Class wrapping system (OS) functions.
123 static inline void abort();
124 static inline int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
125 static inline int access(const char *pathname, int mode);
126 static inline int atoi(const char* nptr);
127 static inline int backtrace(void** array, int size);
128 static inline char** backtrace_symbols(void* const* array, int size) throw ();
129 static inline void* calloc(size_t nmemb, size_t size);
130 static inline int close(int fd);
131 static inline int connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen);
132 #if defined(ENABLE_JRE_LAYOUT)
133 static inline char* dirname(char* path);
135 static inline int dlclose(void* handle);
136 static inline char* dlerror(void);
137 static inline void* dlopen(const char* filename, int flag);
138 static inline void* dlsym(void* handle, const char* symbol);
139 static inline int fclose(FILE* fp);
140 static inline FILE* fopen(const char* path, const char* mode);
141 static inline int fprintf(FILE* stream, const char* format, ...);
142 static inline size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
143 static inline void free(void* ptr);
144 static inline char* getcwd(char* buf, size_t size);
145 static inline char* getenv(const char* name);
146 static inline int gethostname(char* name, size_t len);
147 static inline int getloadavg(double loadavg[], int nelem);
148 static inline int getpagesize(void);
149 static inline pid_t getpid(void);
150 static inline int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
151 static inline int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
152 static inline int listen(int sockfd, int backlog);
153 static inline void* malloc(size_t size);
154 static inline void* memcpy(void* dest, const void* src, size_t n);
155 static inline void* memset(void* s, int c, size_t n);
156 static inline int mprotect(void* addr, size_t len, int prot);
157 static inline ssize_t readlink(const char* path, char* buf, size_t bufsiz);
158 static inline int scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
159 static inline ssize_t send(int s, const void* buf, size_t len, int flags);
160 static inline int setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
161 static inline int shutdown(int s, int how);
162 static inline int socket(int domain, int type, int protocol);
163 static inline int stat(const char* path, struct stat* buf);
164 #if defined(__SOLARIS__)
165 static inline int str2sig(const char* str, int* signum);
167 static inline char* strcat(char* dest, const char* src);
168 static inline int strcmp(const char* s1, const char* s2);
169 static inline char* strcpy(char* dest, const char* src);
170 static inline char* strdup(const char* s);
171 static inline size_t strlen(const char* s);
172 static inline char* strerror(int errnum);
174 // Convenience functions.
175 static void abort(const char* text, ...);
176 static void abort_errnum(int errnum, const char* text, ...);
177 static void abort_errno(const char* text, ...);
178 static char* getcwd(void);
179 static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
180 static void print_backtrace();
181 static int processors_online();
184 template<class F1, class F2>
185 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 *));
189 inline void os::abort(void)
191 #if defined(HAVE_ABORT)
194 # error abort not available
198 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
200 #if defined(HAVE_ACCEPT)
201 return ::accept(sockfd, addr, addrlen);
203 # error accept not available
207 inline int os::access(const char* pathname, int mode)
209 #if defined(HAVE_ACCESS)
210 return ::access(pathname, mode);
212 # error access not available
216 inline int os::atoi(const char* nptr)
218 #if defined(HAVE_ATOI)
221 # error atoi not available
225 inline int os::backtrace(void** array, int size)
227 #if defined(HAVE_BACKTRACE)
228 return ::backtrace(array, size);
230 fprintf(stderr, "os::backtrace: Not available.");
235 inline char** os::backtrace_symbols(void* const* array, int size) throw ()
237 #if defined(HAVE_BACKTRACE_SYMBOLS)
238 return ::backtrace_symbols(array, size);
240 fprintf(stderr, "os::backtrace_symbols: Not available.");
245 inline void* os::calloc(size_t nmemb, size_t size)
247 #if defined(HAVE_CALLOC)
248 return ::calloc(nmemb, size);
250 # error calloc not available
254 inline int os::close(int fd)
256 #if defined(HAVE_CLOSE)
259 # error close not available
263 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
265 #if defined(HAVE_CONNECT)
266 return ::connect(sockfd, serv_addr, addrlen);
268 # error connect not available
272 #if defined(ENABLE_JRE_LAYOUT)
273 inline char* os::dirname(char* path)
275 #if defined(HAVE_DIRNAME)
276 return ::dirname(path);
278 # error dirname not available
283 inline int os::dlclose(void* handle)
285 #if defined(HAVE_DLCLOSE)
286 return ::dlclose(handle);
288 # error dlclose not available
292 inline char* os::dlerror(void)
294 #if defined(HAVE_DLERROR)
295 // At least FreeBSD defines dlerror() to return a const char*, so
296 // we simply cast it.
297 return (char*) ::dlerror();
299 # error dlerror not available
303 inline void* os::dlopen(const char* filename, int flag)
305 #if defined(HAVE_DLOPEN)
306 return ::dlopen(filename, flag);
308 # error dlopen not available
312 inline void* os::dlsym(void* handle, const char* symbol)
314 #if defined(HAVE_DLSYM)
315 return ::dlsym(handle, symbol);
317 # error dlsym not available
321 inline int os::fclose(FILE* fp)
323 #if defined(HAVE_FCLOSE)
326 # error fclose not available
330 inline FILE* os::fopen(const char* path, const char* mode)
332 #if defined(HAVE_FOPEN)
333 return ::fopen(path, mode);
335 # error fopen not available
339 inline int os::fprintf(FILE* stream, const char* format, ...)
341 #if defined(HAVE_FPRINTF)
343 va_start(ap, format);
344 int result = ::vfprintf(stream, format, ap);
348 # error fprintf not available
352 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
354 #if defined(HAVE_FREAD)
355 return ::fread(ptr, size, nmemb, stream);
357 # error fread not available
361 inline void os::free(void* ptr)
363 #if defined(HAVE_FREE)
366 # error free not available
370 inline static int system_fsync(int fd)
372 #if defined(HAVE_FSYNC)
375 # error fsync not available
379 inline static int system_ftruncate(int fd, off_t length)
381 #if defined(HAVE_FTRUNCATE)
382 return ftruncate(fd, length);
384 # error ftruncate not available
388 inline char* os::getcwd(char* buf, size_t size)
390 #if defined(HAVE_GETCWD)
391 return ::getcwd(buf, size);
393 # error getcwd not available
397 inline char* os::getenv(const char* name)
399 #if defined(HAVE_GETENV)
400 return ::getenv(name);
402 # error getenv not available
406 inline int os::gethostname(char* name, size_t len)
408 #if defined(HAVE_GETHOSTNAME)
409 return ::gethostname(name, len);
411 # error gethostname not available
415 inline int os::getloadavg(double loadavg[], int nelem)
417 #if defined(HAVE_GETLOADAVG)
418 return ::getloadavg(loadavg, nelem);
420 # error getloadavg not available
424 inline int os::getpagesize(void)
426 #if defined(HAVE_GETPAGESIZE)
427 return ::getpagesize();
429 # error getpagesize not available
433 inline pid_t os::getpid(void)
435 #if defined(HAVE_GETPID)
438 # error getpid not available
442 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
444 #if defined(HAVE_GETSOCKNAME)
445 return ::getsockname(s, name, namelen);
447 # error getsockname not available
451 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
453 #if defined(HAVE_GETSOCKOPT)
454 return ::getsockopt(s, level, optname, optval, optlen);
456 # error getsockopt not available
460 inline int os::listen(int sockfd, int backlog)
462 #if defined(HAVE_LISTEN)
463 return ::listen(sockfd, backlog);
465 # error listen not available
469 inline static off_t system_lseek(int fildes, off_t offset, int whence)
471 #if defined(HAVE_LSEEK)
472 return lseek(fildes, offset, whence);
474 # error lseek not available
478 inline void* os::malloc(size_t size)
480 #if defined(HAVE_MALLOC)
481 return ::malloc(size);
483 # error malloc not available
487 inline void* os::memcpy(void* dest, const void* src, size_t n)
489 #if defined(HAVE_MEMCPY)
490 return ::memcpy(dest, src, n);
492 # error memcpy not available
496 inline void* os::memset(void* s, int c, size_t n)
498 #if defined(HAVE_MEMSET)
499 return ::memset(s, c, n);
501 # error memset not available
505 inline int os::mprotect(void* addr, size_t len, int prot)
507 #if defined(HAVE_MPROTECT)
508 return ::mprotect(addr, len, prot);
510 # error mprotect not available
514 inline static int system_open(const char *pathname, int flags, mode_t mode)
516 #if defined(HAVE_OPEN)
517 return open(pathname, flags, mode);
519 # error open not available
523 inline static ssize_t system_read(int fd, void *buf, size_t count)
525 #if defined(HAVE_READ)
526 return read(fd, buf, count);
528 # error read not available
532 inline ssize_t os::readlink(const char* path, char* buf, size_t bufsiz)
534 #if defined(HAVE_READLINK)
535 return ::readlink(path, buf, bufsiz);
537 # error readlink not available
541 inline static void *system_realloc(void *ptr, size_t size)
543 #if defined(HAVE_REALLOC)
544 return realloc(ptr, size);
546 # error realloc not available
550 template<class F1, class F2>
551 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 *))
553 return scandir(dir, namelist, (F1) filter, (F2) compar);
556 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
558 #if defined(HAVE_SCANDIR)
559 return call_scandir(::scandir, dir, namelist, filter, compar);
561 # error scandir not available
565 inline ssize_t os::send(int s, const void* buf, size_t len, int flags)
567 // TODO Should be restartable on Linux and interruptible on Solaris.
568 #if defined(HAVE_SEND)
569 return ::send(s, buf, len, flags);
571 # error send not available
575 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
577 #if defined(HAVE_SETSOCKOPT)
578 return ::setsockopt(s, level, optname, optval, optlen);
580 # error setsockopt not available
584 inline int os::shutdown(int s, int how)
586 #if defined(HAVE_SHUTDOWN)
587 return ::shutdown(s, how);
589 # error shutdown not available
593 inline int os::socket(int domain, int type, int protocol)
595 #if defined(HAVE_SOCKET)
596 return ::socket(domain, type, protocol);
598 # error socket not available
602 inline int os::stat(const char* path, struct stat* buf)
604 #if defined(HAVE_STAT)
605 return ::stat(path, buf);
607 # error stat not available
611 #if defined(__SOLARIS__)
612 inline int os::str2sig(const char* str, int* signum)
614 #if defined(HAVE_STR2SIG)
615 return ::str2sig(str, signum);
617 # error str2sig not available
622 inline char* os::strcat(char* dest, const char* src)
624 #if defined(HAVE_STRCAT)
625 return ::strcat(dest, src);
627 # error strcat not available
631 inline int os::strcmp(const char* s1, const char* s2)
633 #if defined(HAVE_STRCMP)
634 return ::strcmp(s1, s2);
636 # error strcmp not available
640 inline char* os::strcpy(char* dest, const char* src)
642 #if defined(HAVE_STRCPY)
643 return ::strcpy(dest, src);
645 # error strcpy not available
649 inline char* os::strdup(const char* s)
651 #if defined(HAVE_STRDUP)
654 # error strdup not available
658 inline char* os::strerror(int errnum)
660 #if defined(HAVE_STRERROR)
661 return ::strerror(errnum);
663 # error strerror not available
667 inline size_t os::strlen(const char* s)
669 #if defined(HAVE_STRLEN)
672 # error strlen not available
676 inline static ssize_t system_write(int fd, const void *buf, size_t count)
678 #if defined(HAVE_WRITE)
679 return write(fd, buf, count);
681 # error write not available
687 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
689 int os_atoi(const char* nptr);
690 int os_getpagesize(void);
691 void* os_memcpy(void* dest, const void* src, size_t n);
692 void* os_memset(void* s, int c, size_t n);
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: