1 /* src/vm/os.cpp - system (OS) functions
3 Copyright (C) 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
28 /* NOTE: In this file we check for all system headers, because we wrap
29 all system calls into functions for better portability. */
31 #if defined(HAVE_ERRNO_H)
35 #if defined(HAVE_STDINT_H)
39 #if defined(HAVE_STRING_H)
43 #if defined(HAVE_UNISTD_H)
47 #if defined(HAVE_SYS_MMAN_H)
48 # include <sys/mman.h>
51 #if defined(__DARWIN__)
52 # include <mach/mach.h>
53 # include <mach/mach_host.h>
54 # include <mach/host_info.h>
57 /* this should work on BSD */
58 /* #include <sys/sysctl.h> */
64 * Maps anonymous memory, even on systems not defining
69 void* os::mmap_anonymous(void *addr, size_t len, int prot, int flags)
73 #if defined(MAP_ANON) || defined(MAP_ANONYMOUS)
74 p = mmap(addr, len, prot,
75 # if defined(MAP_ANON)
78 MAP_ANONYMOUS | flags,
84 fd = open("/dev/zero", O_RDONLY, 0);
87 vm_abort("os::mmap_anonymous: open failed: %s", os::strerror(errno));
89 p = mmap(addr, len, prot, flags, fd, 0);
92 #if defined(MAP_FAILED)
97 vm_abort("os::mmap_anonymous: mmap failed: %s", os::strerror(errno));
104 * Returns the number of online processors in the system.
106 * @return Number of online processors.
108 int os::processors_online(void)
110 #if defined(_SC_NPROC_ONLN)
112 return (int) sysconf(_SC_NPROC_ONLN);
114 #elif defined(_SC_NPROCESSORS_ONLN)
116 return (int) sysconf(_SC_NPROCESSORS_ONLN);
118 #elif defined(__DARWIN__)
120 host_basic_info_data_t hinfo;
121 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
124 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
125 (host_info_t) &hinfo, &hinfo_count);
127 if (rc != KERN_SUCCESS) {
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;
135 #elif defined(__FREEBSD__)
136 # error IMPLEMENT ME!
138 /* this should work in BSD */
140 int ncpu, mib[2], rc;
146 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
148 return (int32_t) ncpu;
159 // Legacy C interface.
162 void* os_mmap_anonymous(void *addr, size_t len, int prot, int flags) { return os::mmap_anonymous(addr, len, prot, flags); }
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); }
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); }
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 * ---------------------------------------------------------------------
200 * indent-tabs-mode: t
204 * vim:noexpandtab:sw=4:ts=4: