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