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_STDINT_H)
60 #if defined(HAVE_STDIO_H)
64 #if defined(HAVE_STDLIB_H)
68 #if defined(HAVE_STRING_H)
72 #if defined(HAVE_UNISTD_H)
76 #if defined(HAVE_SYS_MMAN_H)
77 # include <sys/mman.h>
80 #if defined(HAVE_SYS_SOCKET_H)
81 # include <sys/socket.h>
84 #if defined(HAVE_SYS_STAT_H)
85 # include <sys/stat.h>
88 #if defined(HAVE_SYS_TYPES_H)
89 # include <sys/types.h>
95 // Class wrapping system (OS) functions.
99 static inline void abort();
100 static inline int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
101 static inline int access(const char *pathname, int mode);
102 static inline int atoi(const char* nptr);
103 static inline void* calloc(size_t nmemb, size_t size);
104 static inline int close(int fd);
105 static inline int connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen);
106 #if defined(ENABLE_JRE_LAYOUT)
107 static inline char* dirname(char* path);
109 static inline int dlclose(void* handle);
110 static inline char* dlerror(void);
111 static inline void* dlopen(const char* filename, int flag);
112 static inline void* dlsym(void* handle, const char* symbol);
113 static inline int fclose(FILE* fp);
114 static inline FILE* fopen(const char* path, const char* mode);
115 static inline size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
116 static inline void free(void* ptr);
117 static inline int gethostname(char* name, size_t len);
118 static inline int getpagesize(void);
119 static inline int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
120 static inline int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
121 static inline int listen(int sockfd, int backlog);
122 static inline void* malloc(size_t size);
123 static inline void* memcpy(void* dest, const void* src, size_t n);
124 static inline void* memset(void* s, int c, size_t n);
125 static inline int mprotect(void* addr, size_t len, int prot);
126 static inline int scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
127 static inline int setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
128 static inline int shutdown(int s, int how);
129 static inline int socket(int domain, int type, int protocol);
130 static inline int stat(const char* path, struct stat* buf);
131 static inline char* strcat(char* dest, const char* src);
132 static inline char* strcpy(char* dest, const char* src);
133 static inline char* strdup(const char* s);
134 static inline size_t strlen(const char* s);
135 static inline char* strerror(int errnum);
137 static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
138 static int processors_online(void);
142 inline void os::abort(void)
144 #if defined(HAVE_ABORT)
147 # error abort not available
151 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
153 #if defined(HAVE_ACCEPT)
154 return ::accept(sockfd, addr, addrlen);
156 # error accept not available
160 inline int os::access(const char* pathname, int mode)
162 #if defined(HAVE_ACCESS)
163 return ::access(pathname, mode);
165 # error access not available
169 inline int os::atoi(const char* nptr)
171 #if defined(HAVE_ATOI)
174 # error atoi not available
178 inline void* os::calloc(size_t nmemb, size_t size)
180 #if defined(HAVE_CALLOC)
181 return ::calloc(nmemb, size);
183 # error calloc not available
187 inline int os::close(int fd)
189 #if defined(HAVE_CLOSE)
192 # error close not available
196 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
198 #if defined(HAVE_CONNECT)
199 return ::connect(sockfd, serv_addr, addrlen);
201 # error connect not available
205 #if defined(ENABLE_JRE_LAYOUT)
206 inline char* os::dirname(char* path)
208 #if defined(HAVE_DIRNAME)
209 return ::dirname(path);
211 # error dirname not available
216 inline int os::dlclose(void* handle)
218 #if defined(HAVE_DLCLOSE)
219 return ::dlclose(handle);
221 # error dlclose not available
225 inline char* os::dlerror(void)
227 #if defined(HAVE_DLERROR)
230 # error dlerror not available
234 inline void* os::dlopen(const char* filename, int flag)
236 #if defined(HAVE_DLOPEN)
237 return ::dlopen(filename, flag);
239 # error dlopen not available
243 inline void* os::dlsym(void* handle, const char* symbol)
245 #if defined(HAVE_DLSYM)
246 return ::dlsym(handle, symbol);
248 # error dlsym not available
252 inline int os::fclose(FILE* fp)
254 #if defined(HAVE_FCLOSE)
257 # error fclose not available
261 inline FILE* os::fopen(const char* path, const char* mode)
263 #if defined(HAVE_FOPEN)
264 return ::fopen(path, mode);
266 # error fopen not available
270 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
272 #if defined(HAVE_FREAD)
273 return ::fread(ptr, size, nmemb, stream);
275 # error fread not available
279 inline void os::free(void* ptr)
281 #if defined(HAVE_FREE)
284 # error free not available
288 inline static int system_fsync(int fd)
290 #if defined(HAVE_FSYNC)
293 # error fsync not available
297 inline static int system_ftruncate(int fd, off_t length)
299 #if defined(HAVE_FTRUNCATE)
300 return ftruncate(fd, length);
302 # error ftruncate not available
306 inline int os::gethostname(char* name, size_t len)
308 #if defined(HAVE_GETHOSTNAME)
309 return ::gethostname(name, len);
311 # error gethostname not available
315 inline int os::getpagesize(void)
317 #if defined(HAVE_GETPAGESIZE)
318 return ::getpagesize();
320 # error getpagesize not available
324 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
326 #if defined(HAVE_GETSOCKNAME)
327 return ::getsockname(s, name, namelen);
329 # error getsockname not available
333 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
335 #if defined(HAVE_GETSOCKOPT)
336 return ::getsockopt(s, level, optname, optval, optlen);
338 # error getsockopt not available
342 inline int os::listen(int sockfd, int backlog)
344 #if defined(HAVE_LISTEN)
345 return ::listen(sockfd, backlog);
347 # error listen not available
351 inline static off_t system_lseek(int fildes, off_t offset, int whence)
353 #if defined(HAVE_LSEEK)
354 return lseek(fildes, offset, whence);
356 # error lseek not available
360 inline void* os::malloc(size_t size)
362 #if defined(HAVE_MALLOC)
363 return ::malloc(size);
365 # error malloc not available
369 inline void* os::memcpy(void* dest, const void* src, size_t n)
371 #if defined(HAVE_MEMCPY)
372 return ::memcpy(dest, src, n);
374 # error memcpy not available
378 inline void* os::memset(void* s, int c, size_t n)
380 #if defined(HAVE_MEMSET)
381 return ::memset(s, c, n);
383 # error memset not available
387 inline int os::mprotect(void* addr, size_t len, int prot)
389 #if defined(HAVE_MPROTECT)
390 return ::mprotect(addr, len, prot);
392 # error mprotect not available
396 inline static int system_open(const char *pathname, int flags, mode_t mode)
398 #if defined(HAVE_OPEN)
399 return open(pathname, flags, mode);
401 # error open not available
405 inline static ssize_t system_read(int fd, void *buf, size_t count)
407 #if defined(HAVE_READ)
408 return read(fd, buf, count);
410 # error read not available
414 inline static void *system_realloc(void *ptr, size_t size)
416 #if defined(HAVE_REALLOC)
417 return realloc(ptr, size);
419 # error realloc not available
423 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
425 #elif defined(__SOLARIS__)
426 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
427 #elif defined(__IRIX__)
428 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
430 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
434 #if defined(HAVE_SCANDIR)
435 # if defined(__LINUX__)
436 return ::scandir(dir, namelist, filter, compar);
438 return ::scandir(dir, namelist, (int (*)(struct dirent*)) filter, compar);
441 # error scandir not available
445 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
447 #if defined(HAVE_SETSOCKOPT)
448 return ::setsockopt(s, level, optname, optval, optlen);
450 # error setsockopt not available
454 inline int os::shutdown(int s, int how)
456 #if defined(HAVE_SHUTDOWN)
457 return ::shutdown(s, how);
459 # error shutdown not available
463 inline int os::socket(int domain, int type, int protocol)
465 #if defined(HAVE_SOCKET)
466 return ::socket(domain, type, protocol);
468 # error socket not available
472 inline int os::stat(const char* path, struct stat* buf)
474 #if defined(HAVE_STAT)
475 return ::stat(path, buf);
477 # error stat not available
481 inline char* os::strcat(char* dest, const char* src)
483 #if defined(HAVE_STRCAT)
484 return ::strcat(dest, src);
486 # error strcat not available
490 inline char* os::strcpy(char* dest, const char* src)
492 #if defined(HAVE_STRCPY)
493 return ::strcpy(dest, src);
495 # error strcpy not available
499 inline char* os::strdup(const char* s)
501 #if defined(HAVE_STRDUP)
504 # error strdup not available
508 inline char* os::strerror(int errnum)
510 #if defined(HAVE_STRERROR)
511 return ::strerror(errnum);
513 # error strerror not available
517 inline size_t os::strlen(const char* s)
519 #if defined(HAVE_STRLEN)
522 # error strlen not available
526 inline static ssize_t system_write(int fd, const void *buf, size_t count)
528 #if defined(HAVE_WRITE)
529 return write(fd, buf, count);
531 # error write not available
537 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
540 int os_access(const char* pathname, int mode);
541 int os_atoi(const char* nptr);
542 void* os_calloc(size_t nmemb, size_t size);
543 char* os_dirname(char* path);
544 int os_dlclose(void* handle);
545 char* os_dlerror(void);
546 void* os_dlopen(const char* filename, int flag);
547 void* os_dlsym(void* handle, const char* symbol);
548 int os_fclose(FILE* fp);
549 FILE* os_fopen(const char* path, const char* mode);
550 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
551 void os_free(void* ptr);
552 int os_getpagesize(void);
553 void* os_memcpy(void* dest, const void* src, size_t n);
554 void* os_memset(void* s, int c, size_t n);
555 int os_mprotect(void* addr, size_t len, int prot);
556 int os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
557 int os_stat(const char* path, struct stat* buf);
558 char* os_strcat(char* dest, const char* src);
559 char* os_strcpy(char* dest, const char* src);
560 char* os_strdup(const char* s);
561 int os_strlen(const char* s);
569 * These are local overrides for various environment variables in Emacs.
570 * Please do not remove this and leave it at the end of the file, where
571 * Emacs will automagically detect them.
572 * ---------------------------------------------------------------------
575 * indent-tabs-mode: t
579 * vim:noexpandtab:sw=4:ts=4: