1 /* src/vmcore/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_FCNTL_H)
46 #if defined(ENABLE_JRE_LAYOUT)
47 # if defined(HAVE_LIBGEN_H)
52 #if defined(HAVE_STDINT_H)
56 #if defined(HAVE_STDIO_H)
60 #if defined(HAVE_STDLIB_H)
64 #if defined(HAVE_STRING_H)
68 #if defined(HAVE_UNISTD_H)
72 #if defined(HAVE_SYS_MMAN_H)
73 # include <sys/mman.h>
76 #if defined(HAVE_SYS_SOCKET_H)
77 # include <sys/socket.h>
80 #if defined(HAVE_SYS_STAT_H)
81 # include <sys/stat.h>
84 #if defined(HAVE_SYS_TYPES_H)
85 # include <sys/types.h>
91 // Class wrapping system (OS) functions.
95 static inline void abort();
96 static inline int accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
97 static inline int access(const char *pathname, int mode);
98 static inline int atoi(const char* nptr);
99 static inline void* calloc(size_t nmemb, size_t size);
100 static inline int close(int fd);
101 static inline int connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen);
102 #if defined(ENABLE_JRE_LAYOUT)
103 static inline char* dirname(char* path);
105 static inline int dlclose(void* handle);
106 static inline char* dlerror(void);
107 static inline void* dlopen(const char* filename, int flag);
108 static inline void* dlsym(void* handle, const char* symbol);
109 static inline int fclose(FILE* fp);
110 static inline FILE* fopen(const char* path, const char* mode);
111 static inline size_t fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
112 static inline void free(void* ptr);
113 static inline int gethostname(char* name, size_t len);
114 static inline int getpagesize(void);
115 static inline int getsockname(int s, struct sockaddr* name, socklen_t* namelen);
116 static inline int getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
117 static inline int listen(int sockfd, int backlog);
118 static inline void* malloc(size_t size);
119 static inline void* memcpy(void* dest, const void* src, size_t n);
120 static inline void* memset(void* s, int c, size_t n);
121 static inline int mprotect(void* addr, size_t len, int prot);
122 static inline int scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
123 static inline int setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
124 static inline int shutdown(int s, int how);
125 static inline int socket(int domain, int type, int protocol);
126 static inline int stat(const char* path, struct stat* buf);
127 static inline char* strcat(char* dest, const char* src);
128 static inline char* strcpy(char* dest, const char* src);
129 static inline char* strdup(const char* s);
130 static inline size_t strlen(const char* s);
131 static inline char* strerror(int errnum);
133 static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
134 static int processors_online(void);
138 inline void os::abort(void)
140 #if defined(HAVE_ABORT)
143 # error abort not available
147 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
149 #if defined(HAVE_ACCEPT)
150 return ::accept(sockfd, addr, addrlen);
152 # error accept not available
156 inline int os::access(const char* pathname, int mode)
158 #if defined(HAVE_ACCESS)
159 return ::access(pathname, mode);
161 # error access not available
165 inline int os::atoi(const char* nptr)
167 #if defined(HAVE_ATOI)
170 # error atoi not available
174 inline void* os::calloc(size_t nmemb, size_t size)
176 #if defined(HAVE_CALLOC)
177 return ::calloc(nmemb, size);
179 # error calloc not available
183 inline int os::close(int fd)
185 #if defined(HAVE_CLOSE)
188 # error close not available
192 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
194 #if defined(HAVE_CONNECT)
195 return ::connect(sockfd, serv_addr, addrlen);
197 # error connect not available
201 #if defined(ENABLE_JRE_LAYOUT)
202 inline char* os::dirname(char* path)
204 #if defined(HAVE_DIRNAME)
205 return ::dirname(path);
207 # error dirname not available
212 inline int os::dlclose(void* handle)
214 #if defined(HAVE_DLCLOSE)
215 return ::dlclose(handle);
217 # error dlclose not available
221 inline char* os::dlerror(void)
223 #if defined(HAVE_DLERROR)
226 # error dlerror not available
230 inline void* os::dlopen(const char* filename, int flag)
232 #if defined(HAVE_DLOPEN)
233 return ::dlopen(filename, flag);
235 # error dlopen not available
239 inline void* os::dlsym(void* handle, const char* symbol)
241 #if defined(HAVE_DLSYM)
242 return ::dlsym(handle, symbol);
244 # error dlsym not available
248 inline int os::fclose(FILE* fp)
250 #if defined(HAVE_FCLOSE)
253 # error fclose not available
257 inline FILE* os::fopen(const char* path, const char* mode)
259 #if defined(HAVE_FOPEN)
260 return ::fopen(path, mode);
262 # error fopen not available
266 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
268 #if defined(HAVE_FREAD)
269 return ::fread(ptr, size, nmemb, stream);
271 # error fread not available
275 inline void os::free(void* ptr)
277 #if defined(HAVE_FREE)
280 # error free not available
284 inline static int system_fsync(int fd)
286 #if defined(HAVE_FSYNC)
289 # error fsync not available
293 inline static int system_ftruncate(int fd, off_t length)
295 #if defined(HAVE_FTRUNCATE)
296 return ftruncate(fd, length);
298 # error ftruncate not available
302 inline int os::gethostname(char* name, size_t len)
304 #if defined(HAVE_GETHOSTNAME)
305 return ::gethostname(name, len);
307 # error gethostname not available
311 inline int os::getpagesize(void)
313 #if defined(HAVE_GETPAGESIZE)
314 return ::getpagesize();
316 # error getpagesize not available
320 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
322 #if defined(HAVE_GETSOCKNAME)
323 return ::getsockname(s, name, namelen);
325 # error getsockname not available
329 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
331 #if defined(HAVE_GETSOCKOPT)
332 return ::getsockopt(s, level, optname, optval, optlen);
334 # error getsockopt not available
338 inline int os::listen(int sockfd, int backlog)
340 #if defined(HAVE_LISTEN)
341 return ::listen(sockfd, backlog);
343 # error listen not available
347 inline static off_t system_lseek(int fildes, off_t offset, int whence)
349 #if defined(HAVE_LSEEK)
350 return lseek(fildes, offset, whence);
352 # error lseek not available
356 inline void* os::malloc(size_t size)
358 #if defined(HAVE_MALLOC)
359 return ::malloc(size);
361 # error malloc not available
365 inline void* os::memcpy(void* dest, const void* src, size_t n)
367 #if defined(HAVE_MEMCPY)
368 return ::memcpy(dest, src, n);
370 # error memcpy not available
374 inline void* os::memset(void* s, int c, size_t n)
376 #if defined(HAVE_MEMSET)
377 return ::memset(s, c, n);
379 # error memset not available
383 inline int os::mprotect(void* addr, size_t len, int prot)
385 #if defined(HAVE_MPROTECT)
386 return ::mprotect(addr, len, prot);
388 # error mprotect not available
392 inline static int system_open(const char *pathname, int flags, mode_t mode)
394 #if defined(HAVE_OPEN)
395 return open(pathname, flags, mode);
397 # error open not available
401 inline static ssize_t system_read(int fd, void *buf, size_t count)
403 #if defined(HAVE_READ)
404 return read(fd, buf, count);
406 # error read not available
410 inline static void *system_realloc(void *ptr, size_t size)
412 #if defined(HAVE_REALLOC)
413 return realloc(ptr, size);
415 # error realloc not available
419 #if defined(__LINUX__)
420 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
421 #elif defined(__SOLARIS__)
422 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
423 #elif defined(__IRIX__)
424 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
426 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
429 #if defined(HAVE_SCANDIR)
430 return ::scandir(dir, namelist, filter, compar);
432 # error scandir not available
436 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
438 #if defined(HAVE_SETSOCKOPT)
439 return ::setsockopt(s, level, optname, optval, optlen);
441 # error setsockopt not available
445 inline int os::shutdown(int s, int how)
447 #if defined(HAVE_SHUTDOWN)
448 return ::shutdown(s, how);
450 # error shutdown not available
454 inline int os::socket(int domain, int type, int protocol)
456 #if defined(HAVE_SOCKET)
457 return ::socket(domain, type, protocol);
459 # error socket not available
463 inline int os::stat(const char* path, struct stat* buf)
465 #if defined(HAVE_STAT)
466 return ::stat(path, buf);
468 # error stat not available
472 inline char* os::strcat(char* dest, const char* src)
474 #if defined(HAVE_STRCAT)
475 return ::strcat(dest, src);
477 # error strcat not available
481 inline char* os::strcpy(char* dest, const char* src)
483 #if defined(HAVE_STRCPY)
484 return ::strcpy(dest, src);
486 # error strcpy not available
490 inline char* os::strdup(const char* s)
492 #if defined(HAVE_STRDUP)
495 # error strdup not available
499 inline char* os::strerror(int errnum)
501 #if defined(HAVE_STRERROR)
502 return ::strerror(errnum);
504 # error strerror not available
508 inline size_t os::strlen(const char* s)
510 #if defined(HAVE_STRLEN)
513 # error strlen not available
517 inline static ssize_t system_write(int fd, const void *buf, size_t count)
519 #if defined(HAVE_WRITE)
520 return write(fd, buf, count);
522 # error write not available
528 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
531 int os_access(const char* pathname, int mode);
532 int os_atoi(const char* nptr);
533 void* os_calloc(size_t nmemb, size_t size);
534 char* os_dirname(char* path);
535 int os_dlclose(void* handle);
536 char* os_dlerror(void);
537 void* os_dlopen(const char* filename, int flag);
538 void* os_dlsym(void* handle, const char* symbol);
539 int os_fclose(FILE* fp);
540 FILE* os_fopen(const char* path, const char* mode);
541 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
542 void os_free(void* ptr);
543 int os_getpagesize(void);
544 void* os_memcpy(void* dest, const void* src, size_t n);
545 void* os_memset(void* s, int c, size_t n);
546 int os_mprotect(void* addr, size_t len, int prot);
547 int os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
548 int os_stat(const char* path, struct stat* buf);
549 char* os_strcat(char* dest, const char* src);
550 char* os_strcpy(char* dest, const char* src);
551 char* os_strdup(const char* s);
552 int os_strlen(const char* s);
560 * These are local overrides for various environment variables in Emacs.
561 * Please do not remove this and leave it at the end of the file, where
562 * Emacs will automagically detect them.
563 * ---------------------------------------------------------------------
566 * indent-tabs-mode: t
570 * vim:noexpandtab:sw=4:ts=4: