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