* m4/assertion.m4: Fixed copyright header.
[cacao.git] / src / vmcore / system.c
1 /* src/vmcore/system.c - system (OS) functions
2
3    Copyright (C) 2007
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.h"
61
62
63 /* system_mmap_anonymous *******************************************************
64
65    Maps anonymous memory, even on systems not defining
66    MAP_ANON(YMOUS).
67
68 *******************************************************************************/
69
70 void *system_mmap_anonymous(void *addr, size_t len, int prot, int flags)
71 {
72         void *p;
73
74 #if defined(MAP_ANON) || defined(MAP_ANONYMOUS)
75         p = mmap(addr, len, prot,
76 # if defined(MAP_ANON)
77                          MAP_ANON | flags,
78 # else
79                          MAP_ANONYMOUS | flags,
80 # endif
81                          -1, 0);
82 #else
83         int fd;
84
85         fd = open("/dev/zero", O_RDONLY, 0);
86
87         if (fd == -1)
88                 vm_abort("system_mmap_anonymous: open failed: %s", strerror(errno));
89
90         p = mmap(addr, len, prot, flags, fd, 0);
91 #endif
92
93 #if defined(MAP_FAILED)
94         if (p == MAP_FAILED)
95 #else
96         if (p == (void *) -1)
97 #endif
98                 vm_abort("system_mmap_anonymous: mmap failed: %s", strerror(errno));
99
100         return p;
101 }
102
103
104 /* system_processors_online ****************************************************
105
106    Returns the number of online processors in the system.
107
108    RETURN:
109        online processor count
110
111 *******************************************************************************/
112
113 int system_processors_online(void)
114 {
115 #if defined(_SC_NPROC_ONLN)
116
117         return (int) sysconf(_SC_NPROC_ONLN);
118
119 #elif defined(_SC_NPROCESSORS_ONLN)
120
121         return (int) sysconf(_SC_NPROCESSORS_ONLN);
122
123 #elif defined(__DARWIN__)
124
125         host_basic_info_data_t hinfo;
126         mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
127         kern_return_t rc;
128
129         rc = host_info(mach_host_self(), HOST_BASIC_INFO,
130                                    (host_info_t) &hinfo, &hinfo_count);
131  
132         if (rc != KERN_SUCCESS) {
133                 return -1;
134         }
135
136         /* XXX michi: according to my infos this should be
137            hinfo.max_cpus, can someone please confirm or deny that? */
138         return (int) hinfo.avail_cpus;
139
140 #elif defined(__FREEBSD__)
141 # error IMPLEMENT ME!
142
143         /* this should work in BSD */
144         /*
145         int ncpu, mib[2], rc;
146         size_t len;
147
148         mib[0] = CTL_HW;
149         mib[1] = HW_NCPU;
150         len = sizeof(ncpu);
151         rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
152
153         return (int32_t) ncpu;
154         */
155
156 #else
157
158         return 1;
159
160 #endif
161 }
162
163
164 /*
165  * These are local overrides for various environment variables in Emacs.
166  * Please do not remove this and leave it at the end of the file, where
167  * Emacs will automagically detect them.
168  * ---------------------------------------------------------------------
169  * Local variables:
170  * mode: c
171  * indent-tabs-mode: t
172  * c-basic-offset: 4
173  * tab-width: 4
174  * End:
175  * vim:noexpandtab:sw=4:ts=4:
176  */