]> git.lizzy.rs Git - rust.git/blob - src/rt/rust_builtin.c
Auto merge of #21452 - bleibig:bison-grammar, r=nikomatsakis
[rust.git] / src / rt / rust_builtin.c
1 // Copyright 2012 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__) || defined(__DragonFly__)
51 extern char **environ;
52 #endif
53
54 #if defined(__WIN32__)
55 char**
56 rust_env_pairs() {
57     return 0;
58 }
59 #else
60 char**
61 rust_env_pairs() {
62 #if defined(__APPLE__) && !(TARGET_OS_IPHONE)
63     char **environ = *_NSGetEnviron();
64 #endif
65     return environ;
66 }
67 #endif
68
69 char*
70 #if defined(__WIN32__)
71 rust_list_dir_val(WIN32_FIND_DATA* entry_ptr) {
72     return entry_ptr->cFileName;
73 }
74 #else
75 rust_list_dir_val(struct dirent* entry_ptr) {
76     return entry_ptr->d_name;
77 }
78 #endif
79
80 #ifndef _WIN32
81
82 DIR*
83 rust_opendir(char *dirname) {
84     return opendir(dirname);
85 }
86
87 int
88 rust_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
89     return readdir_r(dirp, entry, result);
90 }
91
92 int
93 rust_dirent_t_size() {
94     return sizeof(struct dirent);
95 }
96
97 #else
98
99 void
100 rust_opendir() {
101 }
102
103 void
104 rust_readdir() {
105 }
106
107 void
108 rust_dirent_t_size() {
109 }
110
111 #endif
112
113 uintptr_t
114 rust_running_on_valgrind() {
115     return RUNNING_ON_VALGRIND;
116 }
117
118 #if defined(__WIN32__)
119 int
120 get_num_cpus() {
121     SYSTEM_INFO sysinfo;
122     GetSystemInfo(&sysinfo);
123
124     return (int) sysinfo.dwNumberOfProcessors;
125 }
126 #elif defined(__BSD__)
127 int
128 get_num_cpus() {
129     /* swiped from http://stackoverflow.com/questions/150355/
130        programmatically-find-the-number-of-cores-on-a-machine */
131
132     unsigned int numCPU;
133     int mib[4];
134     size_t len = sizeof(numCPU);
135
136     /* set the mib for hw.ncpu */
137     mib[0] = CTL_HW;
138     mib[1] = HW_AVAILCPU;  // alternatively, try HW_NCPU;
139
140     /* get the number of CPUs from the system */
141     sysctl(mib, 2, &numCPU, &len, NULL, 0);
142
143     if( numCPU < 1 ) {
144         mib[1] = HW_NCPU;
145         sysctl( mib, 2, &numCPU, &len, NULL, 0 );
146
147         if( numCPU < 1 ) {
148             numCPU = 1;
149         }
150     }
151     return numCPU;
152 }
153 #elif defined(__GNUC__)
154 int
155 get_num_cpus() {
156     return sysconf(_SC_NPROCESSORS_ONLN);
157 }
158 #endif
159
160 uintptr_t
161 rust_get_num_cpus() {
162     return get_num_cpus();
163 }
164
165 unsigned int
166 rust_valgrind_stack_register(void *start, void *end) {
167   return VALGRIND_STACK_REGISTER(start, end);
168 }
169
170 void
171 rust_valgrind_stack_deregister(unsigned int id) {
172   VALGRIND_STACK_DEREGISTER(id);
173 }
174
175 #if defined(__WIN32__)
176
177 void
178 rust_unset_sigprocmask() {
179     // empty stub for windows to keep linker happy
180 }
181
182 #else
183
184 void
185 rust_unset_sigprocmask() {
186     // this can't be safely converted to rust code because the
187     // representation of sigset_t is platform-dependent
188     sigset_t sset;
189     sigemptyset(&sset);
190     sigprocmask(SIG_SETMASK, &sset, NULL);
191 }
192
193 #endif
194
195 #if defined(__DragonFly__)
196 #include <errno.h>
197 // In DragonFly __error() is an inline function and as such
198 // no symbol exists for it.
199 int *__dfly_error(void) { return __error(); }
200 #endif
201
202 //
203 // Local Variables:
204 // mode: C++
205 // fill-column: 78;
206 // indent-tabs-mode: nil
207 // c-basic-offset: 4
208 // buffer-file-coding-system: utf-8-unix
209 // End:
210 //