1 /* src/vm/os.hpp - system (OS) functions
3 Copyright (C) 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 /* NOTE: In this file we check for all system headers, because we wrap
32 all system calls into inline functions for better portability. */
34 #if defined(HAVE_DIRENT_H)
38 #if defined(HAVE_DLFCN_H)
42 #if defined(HAVE_ERRNO_H)
46 #if defined(HAVE_FCNTL_H)
50 #if defined(ENABLE_JRE_LAYOUT)
51 # if defined(HAVE_LIBGEN_H)
56 #if defined(HAVE_SIGNAL_H)
60 #if defined(HAVE_STDINT_H)
64 #if defined(HAVE_STDIO_H)
68 #if defined(HAVE_STDLIB_H)
72 #if defined(HAVE_STRING_H)
76 #if defined(HAVE_UNISTD_H)
80 #if defined(HAVE_SYS_MMAN_H)
81 # include <sys/mman.h>
84 #if defined(HAVE_SYS_SOCKET_H)
85 # include <sys/socket.h>
88 #if defined(HAVE_SYS_STAT_H)
89 # include <sys/stat.h>
92 #if defined(HAVE_SYS_TYPES_H)
93 # include <sys/types.h>
99 // Class wrapping system (OS) functions.
103 static inline void abort();
104 static inline int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
105 static inline int access(const char *pathname, int mode);
106 static inline int atoi(const char* nptr);
107 static inline void* calloc(size_t nmemb, size_t size);
108 static inline int close(int fd);
109 static inline int connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen);
110 #if defined(ENABLE_JRE_LAYOUT)
111 static inline char* dirname(char* path);
113 static inline int dlclose(void* handle);
114 static inline char* dlerror(void);
115 static inline void* dlopen(const char* filename, int flag);
116 static inline void* dlsym(void* handle, const char* symbol);
117 static inline int fclose(FILE* fp);
118 static inline FILE* fopen(const char* path, const char* mode);
119 static inline size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
120 static inline void free(void* ptr);
121 static inline int gethostname(char* name, size_t len);
122 static inline int getpagesize(void);
123 static inline int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
124 static inline int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
125 static inline int listen(int sockfd, int backlog);
126 static inline void* malloc(size_t size);
127 static inline void* memcpy(void* dest, const void* src, size_t n);
128 static inline void* memset(void* s, int c, size_t n);
129 static inline int mprotect(void* addr, size_t len, int prot);
130 static inline int scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
131 static inline int setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
132 static inline int shutdown(int s, int how);
133 static inline int socket(int domain, int type, int protocol);
134 static inline int stat(const char* path, struct stat* buf);
135 static inline int str2sig(const char* str, int* signum);
136 static inline char* strcat(char* dest, const char* src);
137 static inline char* strcpy(char* dest, const char* src);
138 static inline char* strdup(const char* s);
139 static inline size_t strlen(const char* s);
140 static inline char* strerror(int errnum);
142 static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
143 static int processors_online(void);
147 inline void os::abort(void)
149 #if defined(HAVE_ABORT)
152 # error abort not available
156 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
158 #if defined(HAVE_ACCEPT)
159 return ::accept(sockfd, addr, addrlen);
161 # error accept not available
165 inline int os::access(const char* pathname, int mode)
167 #if defined(HAVE_ACCESS)
168 return ::access(pathname, mode);
170 # error access not available
174 inline int os::atoi(const char* nptr)
176 #if defined(HAVE_ATOI)
179 # error atoi not available
183 inline void* os::calloc(size_t nmemb, size_t size)
185 #if defined(HAVE_CALLOC)
186 return ::calloc(nmemb, size);
188 # error calloc not available
192 inline int os::close(int fd)
194 #if defined(HAVE_CLOSE)
197 # error close not available
201 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
203 #if defined(HAVE_CONNECT)
204 return ::connect(sockfd, serv_addr, addrlen);
206 # error connect not available
210 #if defined(ENABLE_JRE_LAYOUT)
211 inline char* os::dirname(char* path)
213 #if defined(HAVE_DIRNAME)
214 return ::dirname(path);
216 # error dirname not available
221 inline int os::dlclose(void* handle)
223 #if defined(HAVE_DLCLOSE)
224 return ::dlclose(handle);
226 # error dlclose not available
230 inline char* os::dlerror(void)
232 #if defined(HAVE_DLERROR)
235 # error dlerror not available
239 inline void* os::dlopen(const char* filename, int flag)
241 #if defined(HAVE_DLOPEN)
242 return ::dlopen(filename, flag);
244 # error dlopen not available
248 inline void* os::dlsym(void* handle, const char* symbol)
250 #if defined(HAVE_DLSYM)
251 return ::dlsym(handle, symbol);
253 # error dlsym not available
257 inline int os::fclose(FILE* fp)
259 #if defined(HAVE_FCLOSE)
262 # error fclose not available
266 inline FILE* os::fopen(const char* path, const char* mode)
268 #if defined(HAVE_FOPEN)
269 return ::fopen(path, mode);
271 # error fopen not available
275 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
277 #if defined(HAVE_FREAD)
278 return ::fread(ptr, size, nmemb, stream);
280 # error fread not available
284 inline void os::free(void* ptr)
286 #if defined(HAVE_FREE)
289 # error free not available
293 inline static int system_fsync(int fd)
295 #if defined(HAVE_FSYNC)
298 # error fsync not available
302 inline static int system_ftruncate(int fd, off_t length)
304 #if defined(HAVE_FTRUNCATE)
305 return ftruncate(fd, length);
307 # error ftruncate not available
311 inline int os::gethostname(char* name, size_t len)
313 #if defined(HAVE_GETHOSTNAME)
314 return ::gethostname(name, len);
316 # error gethostname not available
320 inline int os::getpagesize(void)
322 #if defined(HAVE_GETPAGESIZE)
323 return ::getpagesize();
325 # error getpagesize not available
329 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
331 #if defined(HAVE_GETSOCKNAME)
332 return ::getsockname(s, name, namelen);
334 # error getsockname not available
338 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
340 #if defined(HAVE_GETSOCKOPT)
341 return ::getsockopt(s, level, optname, optval, optlen);
343 # error getsockopt not available
347 inline int os::listen(int sockfd, int backlog)
349 #if defined(HAVE_LISTEN)
350 return ::listen(sockfd, backlog);
352 # error listen not available
356 inline static off_t system_lseek(int fildes, off_t offset, int whence)
358 #if defined(HAVE_LSEEK)
359 return lseek(fildes, offset, whence);
361 # error lseek not available
365 inline void* os::malloc(size_t size)
367 #if defined(HAVE_MALLOC)
368 return ::malloc(size);
370 # error malloc not available
374 inline void* os::memcpy(void* dest, const void* src, size_t n)
376 #if defined(HAVE_MEMCPY)
377 return ::memcpy(dest, src, n);
379 # error memcpy not available
383 inline void* os::memset(void* s, int c, size_t n)
385 #if defined(HAVE_MEMSET)
386 return ::memset(s, c, n);
388 # error memset not available
392 inline int os::mprotect(void* addr, size_t len, int prot)
394 #if defined(HAVE_MPROTECT)
395 return ::mprotect(addr, len, prot);
397 # error mprotect not available
401 inline static int system_open(const char *pathname, int flags, mode_t mode)
403 #if defined(HAVE_OPEN)
404 return open(pathname, flags, mode);
406 # error open not available
410 inline static ssize_t system_read(int fd, void *buf, size_t count)
412 #if defined(HAVE_READ)
413 return read(fd, buf, count);
415 # error read not available
419 inline static void *system_realloc(void *ptr, size_t size)
421 #if defined(HAVE_REALLOC)
422 return realloc(ptr, size);
424 # error realloc not available
428 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
430 #elif defined(__SOLARIS__)
431 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
432 #elif defined(__IRIX__)
433 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
435 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
439 #if defined(HAVE_SCANDIR)
440 # if defined(__LINUX__)
441 return ::scandir(dir, namelist, filter, compar);
442 #elif defined(__SOLARIS__)
443 return ::scandir(dir, namelist, filter, (int (*)(const dirent**, const dirent**)) compar);
445 return ::scandir(dir, namelist, (int (*)(struct dirent*)) filter, compar);
448 # error scandir not available
452 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
454 #if defined(HAVE_SETSOCKOPT)
455 return ::setsockopt(s, level, optname, optval, optlen);
457 # error setsockopt not available
461 inline int os::shutdown(int s, int how)
463 #if defined(HAVE_SHUTDOWN)
464 return ::shutdown(s, how);
466 # error shutdown not available
470 inline int os::socket(int domain, int type, int protocol)
472 #if defined(HAVE_SOCKET)
473 return ::socket(domain, type, protocol);
475 # error socket not available
479 inline int os::stat(const char* path, struct stat* buf)
481 #if defined(HAVE_STAT)
482 return ::stat(path, buf);
484 # error stat not available
488 inline int os::str2sig(const char* str, int* signum)
490 #if defined(HAVE_STR2SIG)
491 return ::str2sig(str, signum);
493 # error str2sig not available
497 inline char* os::strcat(char* dest, const char* src)
499 #if defined(HAVE_STRCAT)
500 return ::strcat(dest, src);
502 # error strcat not available
506 inline char* os::strcpy(char* dest, const char* src)
508 #if defined(HAVE_STRCPY)
509 return ::strcpy(dest, src);
511 # error strcpy not available
515 inline char* os::strdup(const char* s)
517 #if defined(HAVE_STRDUP)
520 # error strdup not available
524 inline char* os::strerror(int errnum)
526 #if defined(HAVE_STRERROR)
527 return ::strerror(errnum);
529 # error strerror not available
533 inline size_t os::strlen(const char* s)
535 #if defined(HAVE_STRLEN)
538 # error strlen not available
542 inline static ssize_t system_write(int fd, const void *buf, size_t count)
544 #if defined(HAVE_WRITE)
545 return write(fd, buf, count);
547 # error write not available
553 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
556 int os_access(const char* pathname, int mode);
557 int os_atoi(const char* nptr);
558 void* os_calloc(size_t nmemb, size_t size);
559 char* os_dirname(char* path);
560 int os_dlclose(void* handle);
561 char* os_dlerror(void);
562 void* os_dlopen(const char* filename, int flag);
563 void* os_dlsym(void* handle, const char* symbol);
564 int os_fclose(FILE* fp);
565 FILE* os_fopen(const char* path, const char* mode);
566 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
567 void os_free(void* ptr);
568 int os_getpagesize(void);
569 void* os_memcpy(void* dest, const void* src, size_t n);
570 void* os_memset(void* s, int c, size_t n);
571 int os_mprotect(void* addr, size_t len, int prot);
572 int os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
573 int os_stat(const char* path, struct stat* buf);
574 char* os_strcat(char* dest, const char* src);
575 char* os_strcpy(char* dest, const char* src);
576 char* os_strdup(const char* s);
577 int os_strlen(const char* s);
585 * These are local overrides for various environment variables in Emacs.
586 * Please do not remove this and leave it at the end of the file, where
587 * Emacs will automagically detect them.
588 * ---------------------------------------------------------------------
591 * indent-tabs-mode: t
595 * vim:noexpandtab:sw=4:ts=4: