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