* src/vm/os.hpp: Indent changes.
[cacao.git] / src / vm / os.hpp
1 /* src/vm/os.hpp - system (OS) functions
2
3    Copyright (C) 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5    Copyright (C) 2008 Theobroma Systems Ltd.
6
7    This file is part of CACAO.
8
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.
13
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.
18
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
22    02110-1301, USA.
23
24 */
25
26
27 #ifndef _OS_HPP
28 #define _OS_HPP
29
30 #include "config.h"
31
32 // NOTE: In this file we check for all system headers, because we wrap
33 // all system calls into inline functions for better portability.
34
35 // Please don't include CACAO headers here as this header should be a
36 // very low-level one.
37
38 #if defined(HAVE_DIRENT_H)
39 # include <dirent.h>
40 #endif
41
42 #if defined(HAVE_DLFCN_H)
43 # include <dlfcn.h>
44 #endif
45
46 #if defined(HAVE_ERRNO_H)
47 # include <errno.h>
48 #endif
49
50 #if defined(HAVE_EXECINFO_H)
51 # include <execinfo.h>
52 #endif
53
54 #if defined(HAVE_FCNTL_H)
55 # include <fcntl.h>
56 #endif
57
58 #if defined(ENABLE_JRE_LAYOUT)
59 # if defined(HAVE_LIBGEN_H)
60 #  include <libgen.h>
61 # endif
62 #endif
63
64 #if defined(HAVE_SIGNAL_H)
65 # include <signal.h>
66 #endif
67
68 #if defined(HAVE_STDARG_H)
69 # include <stdarg.h>
70 #endif
71
72 #if defined(HAVE_STDINT_H)
73 # include <stdint.h>
74 #endif
75
76 #if defined(HAVE_STDIO_H)
77 # include <stdio.h>
78 #endif
79
80 #if defined(HAVE_STDLIB_H)
81 # include <stdlib.h>
82 #endif
83
84 #if defined(HAVE_STRING_H)
85 # include <string.h>
86 #endif
87
88 #if defined(HAVE_UNISTD_H)
89 # include <unistd.h>
90 #endif
91
92 #if defined(HAVE_SYS_MMAN_H)
93 # include <sys/mman.h>
94 #endif
95
96 #if defined(HAVE_SYS_SOCKET_H)
97 # include <sys/socket.h>
98 #endif
99
100 #if defined(HAVE_SYS_STAT_H)
101 # include <sys/stat.h>
102 #endif
103
104 #if defined(HAVE_SYS_TYPES_H)
105 # include <sys/types.h>
106 #endif
107
108
109 #ifdef __cplusplus
110
111 // Class wrapping system (OS) functions.
112 class os {
113 public:
114         // Inline functions.
115         static inline void    abort();
116         static inline int     accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen);
117         static inline int     access(const char *pathname, int mode);
118         static inline int     atoi(const char* nptr);
119         static inline int     backtrace(void** array, int size);
120         static inline char**  backtrace_symbols(void* const* array, int size) throw ();
121         static inline void*   calloc(size_t nmemb, size_t size);
122         static inline int     close(int fd);
123         static inline int     connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen);
124 #if defined(ENABLE_JRE_LAYOUT)
125         static inline char*   dirname(char* path);
126 #endif
127         static inline int     dlclose(void* handle);
128         static inline char*   dlerror(void);
129         static inline void*   dlopen(const char* filename, int flag);
130         static inline void*   dlsym(void* handle, const char* symbol);
131         static inline int     fclose(FILE* fp);
132         static inline FILE*   fopen(const char* path, const char* mode);
133         static inline int     fprintf(FILE* stream, const char* format, ...);
134         static inline size_t  fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
135         static inline void    free(void* ptr);
136         static inline char*   getenv(const char* name);
137         static inline int     gethostname(char* name, size_t len);
138         static inline int     getpagesize(void);
139         static inline int     getsockname(int s, struct sockaddr* name, socklen_t* namelen);
140         static inline int     getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen);
141         static inline int     listen(int sockfd, int backlog);
142         static inline void*   malloc(size_t size);
143         static inline void*   memcpy(void* dest, const void* src, size_t n);
144         static inline void*   memset(void* s, int c, size_t n);
145         static inline int     mprotect(void* addr, size_t len, int prot);
146         static inline ssize_t readlink(const char* path, char* buf, size_t bufsiz);
147         static inline int     scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
148         static inline int     setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen);
149         static inline int     shutdown(int s, int how);
150         static inline int     socket(int domain, int type, int protocol);
151         static inline int     stat(const char* path, struct stat* buf);
152 #if defined(__SOLARIS__)
153         static inline int     str2sig(const char* str, int* signum);
154 #endif
155         static inline char*   strcat(char* dest, const char* src);
156         static inline int     strcmp(const char* s1, const char* s2);
157         static inline char*   strcpy(char* dest, const char* src);
158         static inline char*   strdup(const char* s);
159         static inline size_t  strlen(const char* s);
160         static inline char*   strerror(int errnum);
161
162         // Convenience functions.
163         static void* mmap_anonymous(void *addr, size_t len, int prot, int flags);
164         static void  print_backtrace();
165         static int   processors_online();
166 };
167
168
169 inline void os::abort(void)
170 {
171 #if defined(HAVE_ABORT)
172         ::abort();
173 #else
174 # error abort not available
175 #endif
176 }
177
178 inline int os::accept(int sockfd, struct sockaddr* addr, socklen_t* addrlen)
179 {
180 #if defined(HAVE_ACCEPT)
181         return ::accept(sockfd, addr, addrlen);
182 #else
183 # error accept not available
184 #endif
185 }
186
187 inline int os::access(const char* pathname, int mode)
188 {
189 #if defined(HAVE_ACCESS)
190         return ::access(pathname, mode);
191 #else
192 # error access not available
193 #endif
194 }
195
196 inline int os::atoi(const char* nptr)
197 {
198 #if defined(HAVE_ATOI)
199         return ::atoi(nptr);
200 #else
201 # error atoi not available
202 #endif
203 }
204
205 inline int os::backtrace(void** array, int size)
206 {
207 #if defined(HAVE_BACKTRACE)
208         return ::backtrace(array, size);
209 #else
210         fprintf(stderr, "os::backtrace: Not available.");
211         return 0;
212 #endif
213 }
214
215 inline char** os::backtrace_symbols(void* const* array, int size) throw ()
216 {
217 #if defined(HAVE_BACKTRACE_SYMBOLS)
218         return ::backtrace_symbols(array, size);
219 #else
220         fprintf(stderr, "os::backtrace_symbols: Not available.");
221         return NULL;
222 #endif
223 }
224
225 inline void* os::calloc(size_t nmemb, size_t size)
226 {
227 #if defined(HAVE_CALLOC)
228         return ::calloc(nmemb, size);
229 #else
230 # error calloc not available
231 #endif
232 }
233
234 inline int os::close(int fd)
235 {
236 #if defined(HAVE_CLOSE)
237         return ::close(fd);
238 #else
239 # error close not available
240 #endif
241 }
242
243 inline int os::connect(int sockfd, const struct sockaddr* serv_addr, socklen_t addrlen)
244 {
245 #if defined(HAVE_CONNECT)
246         return ::connect(sockfd, serv_addr, addrlen);
247 #else
248 # error connect not available
249 #endif
250 }
251
252 #if defined(ENABLE_JRE_LAYOUT)
253 inline char* os::dirname(char* path)
254 {
255 #if defined(HAVE_DIRNAME)
256         return ::dirname(path);
257 #else
258 # error dirname not available
259 #endif
260 }
261 #endif
262
263 inline int os::dlclose(void* handle)
264 {
265 #if defined(HAVE_DLCLOSE)
266         return ::dlclose(handle);
267 #else
268 # error dlclose not available
269 #endif
270 }
271
272 inline char* os::dlerror(void)
273 {
274 #if defined(HAVE_DLERROR)
275         return ::dlerror();
276 #else
277 # error dlerror not available
278 #endif
279 }
280
281 inline void* os::dlopen(const char* filename, int flag)
282 {
283 #if defined(HAVE_DLOPEN)
284         return ::dlopen(filename, flag);
285 #else
286 # error dlopen not available
287 #endif
288 }
289
290 inline void* os::dlsym(void* handle, const char* symbol)
291 {
292 #if defined(HAVE_DLSYM)
293         return ::dlsym(handle, symbol);
294 #else
295 # error dlsym not available
296 #endif
297 }
298
299 inline int os::fclose(FILE* fp)
300 {
301 #if defined(HAVE_FCLOSE)
302         return ::fclose(fp);
303 #else
304 # error fclose not available
305 #endif
306 }
307
308 inline FILE* os::fopen(const char* path, const char* mode)
309 {
310 #if defined(HAVE_FOPEN)
311         return ::fopen(path, mode);
312 #else
313 # error fopen not available
314 #endif
315 }
316
317 inline int os::fprintf(FILE* stream, const char* format, ...)
318 {
319 #if defined(HAVE_FPRINTF)
320         va_list ap;
321         va_start(ap, format);
322         int result = ::fprintf(stream, format, ap);
323         va_end(ap);
324         return result;
325 #else
326 # error fprintf not available
327 #endif
328 }
329
330 inline size_t os::fread(void* ptr, size_t size, size_t nmemb, FILE* stream)
331 {
332 #if defined(HAVE_FREAD)
333         return ::fread(ptr, size, nmemb, stream);
334 #else
335 # error fread not available
336 #endif
337 }
338
339 inline void os::free(void* ptr)
340 {
341 #if defined(HAVE_FREE)
342         ::free(ptr);
343 #else
344 # error free not available
345 #endif
346 }
347
348 inline static int system_fsync(int fd)
349 {
350 #if defined(HAVE_FSYNC)
351         return fsync(fd);
352 #else
353 # error fsync not available
354 #endif
355 }
356
357 inline static int system_ftruncate(int fd, off_t length)
358 {
359 #if defined(HAVE_FTRUNCATE)
360         return ftruncate(fd, length);
361 #else
362 # error ftruncate not available
363 #endif
364 }
365
366 inline char* os::getenv(const char* name)
367 {
368 #if defined(HAVE_GETENV)
369         return ::getenv(name);
370 #else
371 # error getenv not available
372 #endif
373 }
374
375 inline int os::gethostname(char* name, size_t len)
376 {
377 #if defined(HAVE_GETHOSTNAME)
378         return ::gethostname(name, len);
379 #else
380 # error gethostname not available
381 #endif
382 }
383
384 inline int os::getpagesize(void)
385 {
386 #if defined(HAVE_GETPAGESIZE)
387         return ::getpagesize();
388 #else
389 # error getpagesize not available
390 #endif
391 }
392
393 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
394 {
395 #if defined(HAVE_GETSOCKNAME)
396         return ::getsockname(s, name, namelen);
397 #else
398 # error getsockname not available
399 #endif
400 }
401
402 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
403 {
404 #if defined(HAVE_GETSOCKOPT)
405         return ::getsockopt(s, level, optname, optval, optlen);
406 #else
407 # error getsockopt not available
408 #endif
409 }
410
411 inline int os::listen(int sockfd, int backlog)
412 {
413 #if defined(HAVE_LISTEN)
414         return ::listen(sockfd, backlog);
415 #else
416 # error listen not available
417 #endif
418 }
419
420 inline static off_t system_lseek(int fildes, off_t offset, int whence)
421 {
422 #if defined(HAVE_LSEEK)
423         return lseek(fildes, offset, whence);
424 #else
425 # error lseek not available
426 #endif
427 }
428
429 inline void* os::malloc(size_t size)
430 {
431 #if defined(HAVE_MALLOC)
432         return ::malloc(size);
433 #else
434 # error malloc not available
435 #endif
436 }
437
438 inline void* os::memcpy(void* dest, const void* src, size_t n)
439 {
440 #if defined(HAVE_MEMCPY)
441         return ::memcpy(dest, src, n);
442 #else
443 # error memcpy not available
444 #endif
445 }
446
447 inline void* os::memset(void* s, int c, size_t n)
448 {
449 #if defined(HAVE_MEMSET)
450         return ::memset(s, c, n);
451 #else
452 # error memset not available
453 #endif
454 }
455
456 inline int os::mprotect(void* addr, size_t len, int prot)
457 {
458 #if defined(HAVE_MPROTECT)
459         return ::mprotect(addr, len, prot);
460 #else
461 # error mprotect not available
462 #endif
463 }
464
465 inline static int system_open(const char *pathname, int flags, mode_t mode)
466 {
467 #if defined(HAVE_OPEN)
468         return open(pathname, flags, mode);
469 #else
470 # error open not available
471 #endif
472 }
473
474 inline static ssize_t system_read(int fd, void *buf, size_t count)
475 {
476 #if defined(HAVE_READ)
477         return read(fd, buf, count);
478 #else
479 # error read not available
480 #endif
481 }
482
483 inline ssize_t os::readlink(const char* path, char* buf, size_t bufsiz)
484 {
485 #if defined(HAVE_READLINK)
486         return ::readlink(path, buf, bufsiz);
487 #else
488 # error readlink not available
489 #endif
490 }
491
492 inline static void *system_realloc(void *ptr, size_t size)
493 {
494 #if defined(HAVE_REALLOC)
495         return realloc(ptr, size);
496 #else
497 # error realloc not available
498 #endif
499 }
500
501 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
502 /*
503 #elif defined(__SOLARIS__)
504 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
505 #elif defined(__IRIX__)
506 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
507 #else
508 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
509 #endif
510 */
511 {
512 #if defined(HAVE_SCANDIR)
513 # if defined(__LINUX__)
514         return ::scandir(dir, namelist, filter, compar);
515 #elif defined(__SOLARIS__)
516         return ::scandir(dir, namelist, filter, (int (*)(const dirent**, const dirent**)) compar);
517 # else
518         return ::scandir(dir, namelist, (int (*)(struct dirent*)) filter, compar);
519 # endif
520 #else
521 # error scandir not available
522 #endif
523 }
524
525 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
526 {
527 #if defined(HAVE_SETSOCKOPT)
528         return ::setsockopt(s, level, optname, optval, optlen);
529 #else
530 # error setsockopt not available
531 #endif
532 }
533
534 inline int os::shutdown(int s, int how)
535 {
536 #if defined(HAVE_SHUTDOWN)
537         return ::shutdown(s, how);
538 #else
539 # error shutdown not available
540 #endif
541 }
542
543 inline int os::socket(int domain, int type, int protocol)
544 {
545 #if defined(HAVE_SOCKET)
546         return ::socket(domain, type, protocol);
547 #else
548 # error socket not available
549 #endif
550 }
551
552 inline int os::stat(const char* path, struct stat* buf)
553 {
554 #if defined(HAVE_STAT)
555         return ::stat(path, buf);
556 #else
557 # error stat not available
558 #endif
559 }
560
561 #if defined(__SOLARIS__)
562 inline int os::str2sig(const char* str, int* signum)
563 {
564 #if defined(HAVE_STR2SIG)
565         return ::str2sig(str, signum);
566 #else
567 # error str2sig not available
568 #endif
569 }
570 #endif
571
572 inline char* os::strcat(char* dest, const char* src)
573 {
574 #if defined(HAVE_STRCAT)
575         return ::strcat(dest, src);
576 #else
577 # error strcat not available
578 #endif
579 }
580
581 inline int os::strcmp(const char* s1, const char* s2)
582 {
583 #if defined(HAVE_STRCMP)
584         return ::strcmp(s1, s2);
585 #else
586 # error strcmp not available
587 #endif
588 }
589
590 inline char* os::strcpy(char* dest, const char* src)
591 {
592 #if defined(HAVE_STRCPY)
593         return ::strcpy(dest, src);
594 #else
595 # error strcpy not available
596 #endif
597 }
598
599 inline char* os::strdup(const char* s)
600 {
601 #if defined(HAVE_STRDUP)
602         return ::strdup(s);
603 #else
604 # error strdup not available
605 #endif
606 }
607
608 inline char* os::strerror(int errnum)
609 {
610 #if defined(HAVE_STRERROR)
611         return ::strerror(errnum);
612 #else
613 # error strerror not available
614 #endif
615 }
616
617 inline size_t os::strlen(const char* s)
618 {
619 #if defined(HAVE_STRLEN)
620         return ::strlen(s);
621 #else
622 # error strlen not available
623 #endif
624 }
625
626 inline static ssize_t system_write(int fd, const void *buf, size_t count)
627 {
628 #if defined(HAVE_WRITE)
629         return write(fd, buf, count);
630 #else
631 # error write not available
632 #endif
633 }
634
635 #else
636
637 void*  os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
638
639 void   os_abort(void);
640 int    os_access(const char* pathname, int mode);
641 int    os_atoi(const char* nptr);
642 void*  os_calloc(size_t nmemb, size_t size);
643 char*  os_dirname(char* path);
644 int    os_dlclose(void* handle);
645 char*  os_dlerror(void);
646 void*  os_dlopen(const char* filename, int flag);
647 void*  os_dlsym(void* handle, const char* symbol);
648 int    os_fclose(FILE* fp);
649 FILE*  os_fopen(const char* path, const char* mode);
650 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
651 void   os_free(void* ptr);
652 int    os_getpagesize(void);
653 void*  os_memcpy(void* dest, const void* src, size_t n);
654 void*  os_memset(void* s, int c, size_t n);
655 int    os_mprotect(void* addr, size_t len, int prot);
656 int    os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
657 int    os_stat(const char* path, struct stat* buf);
658 char*  os_strcat(char* dest, const char* src);
659 char*  os_strcpy(char* dest, const char* src);
660 char*  os_strdup(const char* s);
661 int    os_strlen(const char* s);
662
663 #endif
664
665 #endif // _OS_HPP
666
667
668 /*
669  * These are local overrides for various environment variables in Emacs.
670  * Please do not remove this and leave it at the end of the file, where
671  * Emacs will automagically detect them.
672  * ---------------------------------------------------------------------
673  * Local variables:
674  * mode: c++
675  * indent-tabs-mode: t
676  * c-basic-offset: 4
677  * tab-width: 4
678  * End:
679  * vim:noexpandtab:sw=4:ts=4:
680  */