]> git.lizzy.rs Git - rust.git/blob - src/rt/rust_builtin.c
Tidy fixes
[rust.git] / src / rt / rust_builtin.c
1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #include <stdint.h>
12 #include <time.h>
13 #include <string.h>
14 #include <assert.h>
15 #include <stdlib.h>
16
17 #if !defined(__WIN32__)
18 #include <sys/time.h>
19 #include <sys/types.h>
20 #include <dirent.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include <pthread.h>
24 #else
25 #include <windows.h>
26 #include <wincrypt.h>
27 #include <stdio.h>
28 #include <tchar.h>
29 #endif
30
31 #ifdef __APPLE__
32 #include <TargetConditionals.h>
33 #include <mach/mach_time.h>
34
35 #if !(TARGET_OS_IPHONE)
36 #include <crt_externs.h>
37 #endif
38 #endif
39
40 /* Foreign builtins. */
41 //include valgrind.h after stdint.h so that uintptr_t is defined for msys2 w64
42 #include "valgrind/valgrind.h"
43
44 #ifdef __APPLE__
45 #if (TARGET_OS_IPHONE)
46 extern char **environ;
47 #endif
48 #endif
49
50 #if defined(__FreeBSD__) || defined(__linux__) || defined(__ANDROID__) \
51   || defined(__DragonFly__) || defined(__OpenBSD__)
52 extern char **environ;
53 #endif
54
55 #if defined(__WIN32__)
56 char**
57 rust_env_pairs() {
58     return 0;
59 }
60 #else
61 char**
62 rust_env_pairs() {
63 #if defined(__APPLE__) && !(TARGET_OS_IPHONE)
64     char **environ = *_NSGetEnviron();
65 #endif
66     return environ;
67 }
68 #endif
69
70 char*
71 #if defined(__WIN32__)
72 rust_list_dir_val(WIN32_FIND_DATA* entry_ptr) {
73     return entry_ptr->cFileName;
74 }
75 #else
76 rust_list_dir_val(struct dirent* entry_ptr) {
77     return entry_ptr->d_name;
78 }
79 #endif
80
81 #ifndef _WIN32
82
83 DIR*
84 rust_opendir(char *dirname) {
85     return opendir(dirname);
86 }
87
88 int
89 rust_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
90     return readdir_r(dirp, entry, result);
91 }
92
93 int
94 rust_dirent_t_size() {
95     return sizeof(struct dirent);
96 }
97
98 #else
99
100 void
101 rust_opendir() {
102 }
103
104 void
105 rust_readdir() {
106 }
107
108 void
109 rust_dirent_t_size() {
110 }
111
112 #endif
113
114 uintptr_t
115 rust_running_on_valgrind() {
116     return RUNNING_ON_VALGRIND;
117 }
118
119 #if defined(__WIN32__)
120 int
121 get_num_cpus() {
122     SYSTEM_INFO sysinfo;
123     GetSystemInfo(&sysinfo);
124
125     return (int) sysinfo.dwNumberOfProcessors;
126 }
127 #elif defined(__BSD__)
128 int
129 get_num_cpus() {
130     /* swiped from http://stackoverflow.com/questions/150355/
131        programmatically-find-the-number-of-cores-on-a-machine */
132
133     unsigned int numCPU;
134     int mib[4];
135     size_t len = sizeof(numCPU);
136
137     /* set the mib for hw.ncpu */
138     mib[0] = CTL_HW;
139     mib[1] = HW_AVAILCPU;  // alternatively, try HW_NCPU;
140
141     /* get the number of CPUs from the system */
142     sysctl(mib, 2, &numCPU, &len, NULL, 0);
143
144     if( numCPU < 1 ) {
145         mib[1] = HW_NCPU;
146         sysctl( mib, 2, &numCPU, &len, NULL, 0 );
147
148         if( numCPU < 1 ) {
149             numCPU = 1;
150         }
151     }
152     return numCPU;
153 }
154 #elif defined(__GNUC__)
155 int
156 get_num_cpus() {
157     return sysconf(_SC_NPROCESSORS_ONLN);
158 }
159 #endif
160
161 uintptr_t
162 rust_get_num_cpus() {
163     return get_num_cpus();
164 }
165
166 unsigned int
167 rust_valgrind_stack_register(void *start, void *end) {
168   return VALGRIND_STACK_REGISTER(start, end);
169 }
170
171 void
172 rust_valgrind_stack_deregister(unsigned int id) {
173   VALGRIND_STACK_DEREGISTER(id);
174 }
175
176 #if defined(__WIN32__)
177
178 void
179 rust_unset_sigprocmask() {
180     // empty stub for windows to keep linker happy
181 }
182
183 #else
184
185 void
186 rust_unset_sigprocmask() {
187     // this can't be safely converted to rust code because the
188     // representation of sigset_t is platform-dependent
189     sigset_t sset;
190     sigemptyset(&sset);
191     sigprocmask(SIG_SETMASK, &sset, NULL);
192 }
193
194 #endif
195
196 #if defined(__DragonFly__)
197 #include <errno.h>
198 // In DragonFly __error() is an inline function and as such
199 // no symbol exists for it.
200 int *__dfly_error(void) { return __error(); }
201 #endif
202
203 #if defined(__OpenBSD__)
204 #include <sys/param.h>
205 #include <sys/sysctl.h>
206 #include <limits.h>
207
208 const char * rust_load_self() {
209     static char *self = NULL;
210
211     if (self == NULL) {
212         int mib[4];
213         char **argv = NULL;
214         size_t argv_len;
215
216         /* initialize mib */
217         mib[0] = CTL_KERN;
218         mib[1] = KERN_PROC_ARGS;
219         mib[2] = getpid();
220         mib[3] = KERN_PROC_ARGV;
221
222         /* request KERN_PROC_ARGV size */
223         argv_len = 0;
224         if (sysctl(mib, 4, NULL, &argv_len, NULL, 0) == -1)
225             return (NULL);
226
227         /* allocate size */
228         if ((argv = malloc(argv_len)) == NULL)
229             return (NULL);
230
231         /* request KERN_PROC_ARGV */
232         if (sysctl(mib, 4, argv, &argv_len, NULL, 0) == -1) {
233             free(argv);
234             return (NULL);
235         }
236
237         /* get realpath if possible */
238         if ((argv[0] != NULL) && ((*argv[0] == '.') || (*argv[0] == '/')
239                                 || (strstr(argv[0], "/") != NULL)))
240
241             self = realpath(argv[0], NULL);
242         else
243             self = NULL;
244
245         /* cleanup */
246         free(argv);
247     }
248
249     return (self);
250 }
251 #endif
252
253 //
254 // Local Variables:
255 // mode: C++
256 // fill-column: 78;
257 // indent-tabs-mode: nil
258 // c-basic-offset: 4
259 // buffer-file-coding-system: utf-8-unix
260 // End:
261 //