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 #if defined(__DARWIN__)
30 # include <mach/mach.h>
31 # include <mach/mach_host.h>
32 # include <mach/host_info.h>
35 /* this should work on BSD */
36 /* #include <sys/sysctl.h> */
38 #include "mm/memory.hpp"
45 * Prints an error message and aborts the VM.
47 * @param text Error message to print.
49 void os::abort(const char* text, ...)
53 // Print the log message.
63 os::print_backtrace();
71 * Common code for both os::abort_errnum and os::abort_errno.
73 static void abort_verrnum(int errnum, const char* text, va_list ap)
75 // Print the log message.
80 // Print the strerror-message of errnum.
81 log_print(": %s", os::strerror(errnum));
86 os::print_backtrace();
93 * Prints an error message, appends ":" plus the strerror-message of
94 * errnum and aborts the VM.
96 * @param errnum Error number.
97 * @param text Error message to print.
99 void os::abort_errnum(int errnum, const char* text, ...)
104 abort_verrnum(errnum, text, ap);
110 * Equal to abort_errnum, but uses errno to get the error number.
112 * @param text Error message to print.
114 void os::abort_errno(const char* text, ...)
119 abort_verrnum(errno, text, ap);
125 * Return the current working directory.
127 * @return Pointer to a char array allocated by MNEW, or
128 * NULL if memory could not be allocated.
130 char* os::getcwd(void)
134 char* buf = MNEW(char, size);
136 while (buf != NULL) {
137 if (getcwd(buf, size) != NULL)
140 MFREE(buf, char, size);
142 /* too small buffer or a more serious problem */
145 abort_errno("os::getcwd: getcwd failed");
147 /* double the buffer size */
151 buf = MNEW(char, size);
159 * Maps anonymous memory, even on systems not defining
164 void* os::mmap_anonymous(void *addr, size_t len, int prot, int flags)
168 #if defined(MAP_ANON) || defined(MAP_ANONYMOUS)
169 p = mmap(addr, len, prot,
170 # if defined(MAP_ANON)
173 MAP_ANONYMOUS | flags,
179 fd = open("/dev/zero", O_RDONLY, 0);
182 os::abort_errno("os::mmap_anonymous: open failed");
184 p = mmap(addr, len, prot, flags, fd, 0);
187 #if defined(MAP_FAILED)
190 if (p == (void *) -1)
192 os::abort_errno("os::mmap_anonymous: mmap failed");
199 * Print a C backtrace.
201 void os::print_backtrace()
203 #define BACKTRACE_SIZE 100
204 void** array = new void*[SIZEOF_VOID_P * BACKTRACE_SIZE];
206 // Get the backtrace.
207 int size = backtrace(array, BACKTRACE_SIZE);
209 // Resolve the symbols.
210 char** strings = backtrace_symbols(array, size);
212 log_println("Backtrace (%d stack frames):", size);
214 for (int i = 0; i < size; i++)
215 log_println("%s", strings[i]);
217 // We have to free the strings.
223 * Returns the number of online processors in the system.
225 * @return Number of online processors.
227 int os::processors_online(void)
229 #if defined(_SC_NPROC_ONLN)
231 return (int) sysconf(_SC_NPROC_ONLN);
233 #elif defined(_SC_NPROCESSORS_ONLN)
235 return (int) sysconf(_SC_NPROCESSORS_ONLN);
237 #elif defined(__DARWIN__)
239 host_basic_info_data_t hinfo;
240 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
243 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
244 (host_info_t) &hinfo, &hinfo_count);
246 if (rc != KERN_SUCCESS) {
250 /* XXX michi: according to my infos this should be
251 hinfo.max_cpus, can someone please confirm or deny that? */
252 return (int) hinfo.avail_cpus;
254 #elif defined(__FREEBSD__)
255 # error IMPLEMENT ME!
257 /* this should work in BSD */
259 int ncpu, mib[2], rc;
265 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
267 return (int32_t) ncpu;
278 // Legacy C interface.
281 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags) { return os::mmap_anonymous(addr, len, prot, flags); }
283 int os_atoi(const char* nptr) { return os::atoi(nptr); }
284 int os_getpagesize(void) { return os::getpagesize(); }
285 void* os_memcpy(void* dest, const void* src, size_t n) { return os::memcpy(dest, src, n); }
286 void* os_memset(void* s, int c, size_t n) { return os::memset(s, c, n); }
287 char* os_strdup(const char* s) { return os::strdup(s); }
288 int os_strlen(const char* s) { return os::strlen(s); }
294 * These are local overrides for various environment variables in Emacs.
295 * Please do not remove this and leave it at the end of the file, where
296 * Emacs will automagically detect them.
297 * ---------------------------------------------------------------------
300 * indent-tabs-mode: t
304 * vim:noexpandtab:sw=4:ts=4: