1 /* src/vm/os.cpp - system (OS) functions
3 Copyright (C) 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2008 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 /* NOTE: In this file we check for all system headers, because we wrap
30 all system calls into functions for better portability. */
32 #if defined(HAVE_ERRNO_H)
36 #if defined(HAVE_STDINT_H)
40 #if defined(HAVE_STRING_H)
44 #if defined(HAVE_UNISTD_H)
48 #if defined(HAVE_SYS_MMAN_H)
49 # include <sys/mman.h>
52 #if defined(__DARWIN__)
53 # include <mach/mach.h>
54 # include <mach/mach_host.h>
55 # include <mach/host_info.h>
58 /* this should work on BSD */
59 /* #include <sys/sysctl.h> */
65 * Prints an error message and aborts the VM.
67 * @param text Error message to print.
69 void os::abort(const char* text, ...)
73 // Print the log message.
83 os::print_backtrace();
91 * Common code for both os::abort_errnum and os::abort_errno.
93 static void abort_verrnum(int errnum, const char* text, va_list ap)
95 // Print the log message.
100 // Print the strerror-message of errnum.
101 log_print(": %s", os::strerror(errnum));
105 // Print a backtrace.
106 os::print_backtrace();
113 * Prints an error message, appends ":" plus the strerror-message of
114 * errnum and aborts the VM.
116 * @param errnum Error number.
117 * @param text Error message to print.
119 void os::abort_errnum(int errnum, const char* text, ...)
124 abort_verrnum(errnum, text, ap);
130 * Equal to abort_errnum, but uses errno to get the error number.
132 * @param text Error message to print.
134 void os::abort_errno(const char* text, ...)
139 abort_verrnum(errno, text, ap);
145 * Maps anonymous memory, even on systems not defining
150 void* os::mmap_anonymous(void *addr, size_t len, int prot, int flags)
154 #if defined(MAP_ANON) || defined(MAP_ANONYMOUS)
155 p = mmap(addr, len, prot,
156 # if defined(MAP_ANON)
159 MAP_ANONYMOUS | flags,
165 fd = open("/dev/zero", O_RDONLY, 0);
168 os::abort_errno("os::mmap_anonymous: open failed");
170 p = mmap(addr, len, prot, flags, fd, 0);
173 #if defined(MAP_FAILED)
176 if (p == (void *) -1)
178 os::abort_errno("os::mmap_anonymous: mmap failed");
185 * Print a C backtrace.
187 void os::print_backtrace()
189 #define BACKTRACE_SIZE 100
190 void** array = new void*[SIZEOF_VOID_P * BACKTRACE_SIZE];
192 // Get the backtrace.
193 int size = backtrace(array, BACKTRACE_SIZE);
195 // Resolve the symbols.
196 char** strings = backtrace_symbols(array, size);
198 log_println("Backtrace (%d stack frames):", size);
200 for (int i = 0; i < size; i++)
201 log_println("%s", strings[i]);
203 // We have to free the strings.
209 * Returns the number of online processors in the system.
211 * @return Number of online processors.
213 int os::processors_online(void)
215 #if defined(_SC_NPROC_ONLN)
217 return (int) sysconf(_SC_NPROC_ONLN);
219 #elif defined(_SC_NPROCESSORS_ONLN)
221 return (int) sysconf(_SC_NPROCESSORS_ONLN);
223 #elif defined(__DARWIN__)
225 host_basic_info_data_t hinfo;
226 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
229 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
230 (host_info_t) &hinfo, &hinfo_count);
232 if (rc != KERN_SUCCESS) {
236 /* XXX michi: according to my infos this should be
237 hinfo.max_cpus, can someone please confirm or deny that? */
238 return (int) hinfo.avail_cpus;
240 #elif defined(__FREEBSD__)
241 # error IMPLEMENT ME!
243 /* this should work in BSD */
245 int ncpu, mib[2], rc;
251 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
253 return (int32_t) ncpu;
264 // Legacy C interface.
267 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags) { return os::mmap_anonymous(addr, len, prot, flags); }
269 void os_abort(void) { os::abort(); }
270 int os_access(const char* pathname, int mode) { return os::access(pathname, mode); }
271 int os_atoi(const char* nptr) { return os::atoi(nptr); }
272 void* os_calloc(size_t nmemb, size_t size) { return os::calloc(nmemb, size); }
273 #if defined(ENABLE_JRE_LAYOUT)
274 char* os_dirname(char* path) { return os::dirname(path); }
276 char* os_dlerror(void) { return os::dlerror(); }
277 void* os_dlsym(void* handle, const char* symbol) { return os::dlsym(handle, symbol); }
278 int os_fclose(FILE* fp) { return os::fclose(fp); }
279 FILE* os_fopen(const char* path, const char* mode) { return os::fopen(path, mode); }
280 size_t os_fread(void* ptr, size_t size, size_t nmemb, FILE* stream) { return os::fread(ptr, size, nmemb, stream); }
281 void os_free(void* ptr) { os::free(ptr); }
282 int os_getpagesize(void) { return os::getpagesize(); }
283 void* os_memcpy(void* dest, const void* src, size_t n) { return os::memcpy(dest, src, n); }
284 void* os_memset(void* s, int c, size_t n) { return os::memset(s, c, n); }
285 int os_mprotect(void* addr, size_t len, int prot) { return os::mprotect(addr, len, prot); }
286 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); }
287 int os_stat(const char* path, struct stat* buf) { return os::stat(path, buf); }
288 char* os_strcat(char* dest, const char* src) { return os::strcat(dest, src); }
289 char* os_strcpy(char* dest, const char* src) { return os::strcpy(dest, src); }
290 char* os_strdup(const char* s) { return os::strdup(s); }
291 int os_strlen(const char* s) { return os::strlen(s); }
297 * These are local overrides for various environment variables in Emacs.
298 * Please do not remove this and leave it at the end of the file, where
299 * Emacs will automagically detect them.
300 * ---------------------------------------------------------------------
303 * indent-tabs-mode: t
307 * vim:noexpandtab:sw=4:ts=4: