* Moved all files from vmcore/ to vm/.
[cacao.git] / src / vm / os.cpp
1 /* src/vm/os.cpp - 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 #include "config.h"
27
28 /* NOTE: In this file we check for all system headers, because we wrap
29    all system calls into functions for better portability. */
30
31 #if defined(HAVE_ERRNO_H)
32 # include <errno.h>
33 #endif
34
35 #if defined(HAVE_STDINT_H)
36 # include <stdint.h>
37 #endif
38
39 #if defined(HAVE_STRING_H)
40 # include <string.h>
41 #endif
42
43 #if defined(HAVE_UNISTD_H)
44 # include <unistd.h>
45 #endif
46
47 #if defined(HAVE_SYS_MMAN_H)
48 # include <sys/mman.h>
49 #endif
50
51 #if defined(__DARWIN__)
52 # include <mach/mach.h>
53 # include <mach/mach_host.h>
54 # include <mach/host_info.h>
55 #endif
56
57 /* this should work on BSD */
58 /* #include <sys/sysctl.h> */
59
60 #include "vm/vm.hpp"
61
62
63 /**
64  * Maps anonymous memory, even on systems not defining
65  * MAP_ANON(YMOUS).
66  *
67  * @param ...
68  */
69 void* os::mmap_anonymous(void *addr, size_t len, int prot, int flags)
70 {
71         void* p;
72
73 #if defined(MAP_ANON) || defined(MAP_ANONYMOUS)
74         p = mmap(addr, len, prot,
75 # if defined(MAP_ANON)
76                          MAP_ANON | flags,
77 # else
78                          MAP_ANONYMOUS | flags,
79 # endif
80                          -1, 0);
81 #else
82         int fd;
83
84         fd = open("/dev/zero", O_RDONLY, 0);
85
86         if (fd == -1)
87                 vm_abort("os::mmap_anonymous: open failed: %s", os::strerror(errno));
88
89         p = mmap(addr, len, prot, flags, fd, 0);
90 #endif
91
92 #if defined(MAP_FAILED)
93         if (p == MAP_FAILED)
94 #else
95         if (p == (void *) -1)
96 #endif
97                 vm_abort("os::mmap_anonymous: mmap failed: %s", os::strerror(errno));
98
99         return p;
100 }
101
102
103 /**
104  * Returns the number of online processors in the system.
105  *
106  * @return Number of online processors.
107  */
108 int os::processors_online(void)
109 {
110 #if defined(_SC_NPROC_ONLN)
111
112         return (int) sysconf(_SC_NPROC_ONLN);
113
114 #elif defined(_SC_NPROCESSORS_ONLN)
115
116         return (int) sysconf(_SC_NPROCESSORS_ONLN);
117
118 #elif defined(__DARWIN__)
119
120         host_basic_info_data_t hinfo;
121         mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
122         kern_return_t rc;
123
124         rc = host_info(mach_host_self(), HOST_BASIC_INFO,
125                                    (host_info_t) &hinfo, &hinfo_count);
126  
127         if (rc != KERN_SUCCESS) {
128                 return -1;
129         }
130
131         /* XXX michi: according to my infos this should be
132            hinfo.max_cpus, can someone please confirm or deny that? */
133         return (int) hinfo.avail_cpus;
134
135 #elif defined(__FREEBSD__)
136 # error IMPLEMENT ME!
137
138         /* this should work in BSD */
139         /*
140         int ncpu, mib[2], rc;
141         size_t len;
142
143         mib[0] = CTL_HW;
144         mib[1] = HW_NCPU;
145         len = sizeof(ncpu);
146         rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
147
148         return (int32_t) ncpu;
149         */
150
151 #else
152
153         return 1;
154
155 #endif
156 }
157
158
159 // Legacy C interface.
160
161 extern "C" {
162         void*  os_mmap_anonymous(void *addr, size_t len, int prot, int flags) { return os::mmap_anonymous(addr, len, prot, flags); }
163
164         void   os_abort(void) { os::abort(); }
165         int    os_access(const char* pathname, int mode) { return os::access(pathname, mode); }
166         int    os_atoi(const char* nptr) { return os::atoi(nptr); }
167         void*  os_calloc(size_t nmemb, size_t size) { return os::calloc(nmemb, size); }
168 #if defined(ENABLE_JRE_LAYOUT)
169         char*  os_dirname(char* path) { return os::dirname(path); }
170 #endif
171         int    os_dlclose(void* handle) { return os::dlclose(handle); }
172         char*  os_dlerror(void) { return os::dlerror(); }
173         void*  os_dlopen(const char* filename, int flag) { return os::dlopen(filename, flag); }
174         void*  os_dlsym(void* handle, const char* symbol) { return os::dlsym(handle, symbol); }
175         int    os_fclose(FILE* fp) { return os::fclose(fp); }
176         FILE*  os_fopen(const char* path, const char* mode) { return os::fopen(path, mode); }
177         size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream) { return os::fread(ptr, size, nmemb, stream); }
178         void   os_free(void* ptr) { os::free(ptr); }
179         int    os_getpagesize(void) { return os::getpagesize(); }
180         void*  os_memcpy(void* dest, const void* src, size_t n) { return os::memcpy(dest, src, n); }
181         void*  os_memset(void* s, int c, size_t n) { return os::memset(s, c, n); }
182         int    os_mprotect(void* addr, size_t len, int prot) { return os::mprotect(addr, len, prot); }
183         int    os_scandir(const char* dir, struct dirent*** namelist, int(*filter)(const struct dirent*), int(*compar)(const void*, const void*)) { return os::scandir(dir, namelist, filter, compar); }
184         int    os_stat(const char* path, struct stat* buf) { return os::stat(path, buf); }
185         char*  os_strcat(char* dest, const char* src) { return os::strcat(dest, src); }
186         char*  os_strcpy(char* dest, const char* src) { return os::strcpy(dest, src); }
187         char*  os_strdup(const char* s) { return os::strdup(s); }
188         int    os_strlen(const char* s) { return os::strlen(s); }
189
190 }
191
192
193 /*
194  * These are local overrides for various environment variables in Emacs.
195  * Please do not remove this and leave it at the end of the file, where
196  * Emacs will automagically detect them.
197  * ---------------------------------------------------------------------
198  * Local variables:
199  * mode: c++
200  * indent-tabs-mode: t
201  * c-basic-offset: 4
202  * tab-width: 4
203  * End:
204  * vim:noexpandtab:sw=4:ts=4:
205  */