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