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