* merged with tip (040f180a056b)
[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 static int system_fseek(FILE *stream, off_t offset, int whence)
287 {
288 #if defined(HAVE_FSEEK)
289         return fseek(stream, offset, whence);
290 #else
291 # error fseek not available
292 #endif
293 }
294
295 inline void os::free(void* ptr)
296 {
297 #if defined(HAVE_FREE)
298         ::free(ptr);
299 #else
300 # error free not available
301 #endif
302 }
303
304 inline static int system_fsync(int fd)
305 {
306 #if defined(HAVE_FSYNC)
307         return fsync(fd);
308 #else
309 # error fsync not available
310 #endif
311 }
312
313 inline static int system_ftruncate(int fd, off_t length)
314 {
315 #if defined(HAVE_FTRUNCATE)
316         return ftruncate(fd, length);
317 #else
318 # error ftruncate not available
319 #endif
320 }
321
322 inline int os::gethostname(char* name, size_t len)
323 {
324 #if defined(HAVE_GETHOSTNAME)
325         return ::gethostname(name, len);
326 #else
327 # error gethostname not available
328 #endif
329 }
330
331 inline int os::getpagesize(void)
332 {
333 #if defined(HAVE_GETPAGESIZE)
334         return ::getpagesize();
335 #else
336 # error getpagesize not available
337 #endif
338 }
339
340 inline int os::getsockname(int s, struct sockaddr* name, socklen_t* namelen)
341 {
342 #if defined(HAVE_GETSOCKNAME)
343         return ::getsockname(s, name, namelen);
344 #else
345 # error getsockname not available
346 #endif
347 }
348
349 inline int os::getsockopt(int s, int level, int optname, void* optval, socklen_t* optlen)
350 {
351 #if defined(HAVE_GETSOCKOPT)
352         return ::getsockopt(s, level, optname, optval, optlen);
353 #else
354 # error getsockopt not available
355 #endif
356 }
357
358 inline int os::listen(int sockfd, int backlog)
359 {
360 #if defined(HAVE_LISTEN)
361         return ::listen(sockfd, backlog);
362 #else
363 # error listen not available
364 #endif
365 }
366
367 inline static off_t system_lseek(int fildes, off_t offset, int whence)
368 {
369 #if defined(HAVE_LSEEK)
370         return lseek(fildes, offset, whence);
371 #else
372 # error lseek not available
373 #endif
374 }
375
376 inline void* os::malloc(size_t size)
377 {
378 #if defined(HAVE_MALLOC)
379         return ::malloc(size);
380 #else
381 # error malloc not available
382 #endif
383 }
384
385 inline void* os::memcpy(void* dest, const void* src, size_t n)
386 {
387 #if defined(HAVE_MEMCPY)
388         return ::memcpy(dest, src, n);
389 #else
390 # error memcpy not available
391 #endif
392 }
393
394 inline void* os::memset(void* s, int c, size_t n)
395 {
396 #if defined(HAVE_MEMSET)
397         return ::memset(s, c, n);
398 #else
399 # error memset not available
400 #endif
401 }
402
403 inline int os::mprotect(void* addr, size_t len, int prot)
404 {
405 #if defined(HAVE_MPROTECT)
406         return ::mprotect(addr, len, prot);
407 #else
408 # error mprotect not available
409 #endif
410 }
411
412 inline static int system_open(const char *pathname, int flags, mode_t mode)
413 {
414 #if defined(HAVE_OPEN)
415         return open(pathname, flags, mode);
416 #else
417 # error open not available
418 #endif
419 }
420
421 inline static ssize_t system_read(int fd, void *buf, size_t count)
422 {
423 #if defined(HAVE_READ)
424         return read(fd, buf, count);
425 #else
426 # error read not available
427 #endif
428 }
429
430 inline static void *system_realloc(void *ptr, size_t size)
431 {
432 #if defined(HAVE_REALLOC)
433         return realloc(ptr, size);
434 #else
435 # error realloc not available
436 #endif
437 }
438
439 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const void *, const void *))
440 /*
441 #elif defined(__SOLARIS__)
442 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(const struct dirent *), int(*compar)(const struct dirent **, const struct dirent **))
443 #elif defined(__IRIX__)
444 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(dirent_t *), int(*compar)(dirent_t **, dirent_t **))
445 #else
446 inline int os::scandir(const char *dir, struct dirent ***namelist, int(*filter)(struct dirent *), int(*compar)(const void *, const void *))
447 #endif
448 */
449 {
450 #if defined(HAVE_SCANDIR)
451 # if defined(__LINUX__)
452         return ::scandir(dir, namelist, filter, compar);
453 #elif defined(__SOLARIS__)
454         return ::scandir(dir, namelist, filter, (int (*)(const dirent**, const dirent**)) compar);
455 # else
456         return ::scandir(dir, namelist, (int (*)(struct dirent*)) filter, compar);
457 # endif
458 #else
459 # error scandir not available
460 #endif
461 }
462
463 inline int os::setsockopt(int s, int level, int optname, const void* optval, socklen_t optlen)
464 {
465 #if defined(HAVE_SETSOCKOPT)
466         return ::setsockopt(s, level, optname, optval, optlen);
467 #else
468 # error setsockopt not available
469 #endif
470 }
471
472 inline int os::shutdown(int s, int how)
473 {
474 #if defined(HAVE_SHUTDOWN)
475         return ::shutdown(s, how);
476 #else
477 # error shutdown not available
478 #endif
479 }
480
481 inline int os::socket(int domain, int type, int protocol)
482 {
483 #if defined(HAVE_SOCKET)
484         return ::socket(domain, type, protocol);
485 #else
486 # error socket not available
487 #endif
488 }
489
490 inline int os::stat(const char* path, struct stat* buf)
491 {
492 #if defined(HAVE_STAT)
493         return ::stat(path, buf);
494 #else
495 # error stat not available
496 #endif
497 }
498
499 #if defined(__SOLARIS__)
500 inline int os::str2sig(const char* str, int* signum)
501 {
502 #if defined(HAVE_STR2SIG)
503         return ::str2sig(str, signum);
504 #else
505 # error str2sig not available
506 #endif
507 }
508 #endif
509
510 inline char* os::strcat(char* dest, const char* src)
511 {
512 #if defined(HAVE_STRCAT)
513         return ::strcat(dest, src);
514 #else
515 # error strcat not available
516 #endif
517 }
518
519 inline char* os::strcpy(char* dest, const char* src)
520 {
521 #if defined(HAVE_STRCPY)
522         return ::strcpy(dest, src);
523 #else
524 # error strcpy not available
525 #endif
526 }
527
528 inline char* os::strdup(const char* s)
529 {
530 #if defined(HAVE_STRDUP)
531         return ::strdup(s);
532 #else
533 # error strdup not available
534 #endif
535 }
536
537 inline char* os::strerror(int errnum)
538 {
539 #if defined(HAVE_STRERROR)
540         return ::strerror(errnum);
541 #else
542 # error strerror not available
543 #endif
544 }
545
546 inline size_t os::strlen(const char* s)
547 {
548 #if defined(HAVE_STRLEN)
549         return ::strlen(s);
550 #else
551 # error strlen not available
552 #endif
553 }
554
555 inline static ssize_t system_write(int fd, const void *buf, size_t count)
556 {
557 #if defined(HAVE_WRITE)
558         return write(fd, buf, count);
559 #else
560 # error write not available
561 #endif
562 }
563
564 #else
565
566 void*  os_mmap_anonymous(void *addr, size_t len, int prot, int flags);
567
568 void   os_abort(void);
569 int    os_access(const char* pathname, int mode);
570 int    os_atoi(const char* nptr);
571 void*  os_calloc(size_t nmemb, size_t size);
572 char*  os_dirname(char* path);
573 int    os_dlclose(void* handle);
574 char*  os_dlerror(void);
575 void*  os_dlopen(const char* filename, int flag);
576 void*  os_dlsym(void* handle, const char* symbol);
577 int    os_fclose(FILE* fp);
578 FILE*  os_fopen(const char* path, const char* mode);
579 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream);
580 void   os_free(void* ptr);
581 int    os_getpagesize(void);
582 void*  os_memcpy(void* dest, const void* src, size_t n);
583 void*  os_memset(void* s, int c, size_t n);
584 int    os_mprotect(void* addr, size_t len, int prot);
585 int    os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*));
586 int    os_stat(const char* path, struct stat* buf);
587 char*  os_strcat(char* dest, const char* src);
588 char*  os_strcpy(char* dest, const char* src);
589 char*  os_strdup(const char* s);
590 int    os_strlen(const char* s);
591
592 #endif
593
594 #endif // _OS_HPP
595
596
597 /*
598  * These are local overrides for various environment variables in Emacs.
599  * Please do not remove this and leave it at the end of the file, where
600  * Emacs will automagically detect them.
601  * ---------------------------------------------------------------------
602  * Local variables:
603  * mode: c++
604  * indent-tabs-mode: t
605  * c-basic-offset: 4
606  * tab-width: 4
607  * End:
608  * vim:noexpandtab:sw=4:ts=4:
609  */