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.
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.
17 #if !defined(__WIN32__)
19 #include <sys/types.h>
32 #include <TargetConditionals.h>
33 #include <mach/mach_time.h>
35 #if !(TARGET_OS_IPHONE)
36 #include <crt_externs.h>
40 /* Foreign builtins. */
41 //include valgrind.h after stdint.h so that uintptr_t is defined for msys2 w64
42 #include "valgrind/valgrind.h"
45 #if (TARGET_OS_IPHONE)
46 extern char **environ;
50 #if defined(__FreeBSD__) || defined(__linux__) || defined(__ANDROID__) || defined(__DragonFly__) || defined(__OpenBSD__)
51 extern char **environ;
54 #if defined(__WIN32__)
62 #if defined(__APPLE__) && !(TARGET_OS_IPHONE)
63 char **environ = *_NSGetEnviron();
70 #if defined(__WIN32__)
71 rust_list_dir_val(WIN32_FIND_DATA* entry_ptr) {
72 return entry_ptr->cFileName;
75 rust_list_dir_val(struct dirent* entry_ptr) {
76 return entry_ptr->d_name;
83 rust_opendir(char *dirname) {
84 return opendir(dirname);
88 rust_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
89 return readdir_r(dirp, entry, result);
93 rust_dirent_t_size() {
94 return sizeof(struct dirent);
108 rust_dirent_t_size() {
114 rust_running_on_valgrind() {
115 return RUNNING_ON_VALGRIND;
118 #if defined(__WIN32__)
122 GetSystemInfo(&sysinfo);
124 return (int) sysinfo.dwNumberOfProcessors;
126 #elif defined(__BSD__)
129 /* swiped from http://stackoverflow.com/questions/150355/
130 programmatically-find-the-number-of-cores-on-a-machine */
134 size_t len = sizeof(numCPU);
136 /* set the mib for hw.ncpu */
138 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
140 /* get the number of CPUs from the system */
141 sysctl(mib, 2, &numCPU, &len, NULL, 0);
145 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
153 #elif defined(__GNUC__)
156 return sysconf(_SC_NPROCESSORS_ONLN);
161 rust_get_num_cpus() {
162 return get_num_cpus();
166 rust_valgrind_stack_register(void *start, void *end) {
167 return VALGRIND_STACK_REGISTER(start, end);
171 rust_valgrind_stack_deregister(unsigned int id) {
172 VALGRIND_STACK_DEREGISTER(id);
175 #if defined(__WIN32__)
178 rust_unset_sigprocmask() {
179 // empty stub for windows to keep linker happy
185 rust_unset_sigprocmask() {
186 // this can't be safely converted to rust code because the
187 // representation of sigset_t is platform-dependent
190 sigprocmask(SIG_SETMASK, &sset, NULL);
195 #if defined(__DragonFly__)
197 // In DragonFly __error() is an inline function and as such
198 // no symbol exists for it.
199 int *__dfly_error(void) { return __error(); }
202 #if defined(__OpenBSD__)
203 #include <sys/param.h>
204 #include <sys/sysctl.h>
207 const char * __load_self() {
208 static char *self = NULL;
217 mib[1] = KERN_PROC_ARGS;
219 mib[3] = KERN_PROC_ARGV;
221 /* request KERN_PROC_ARGV size */
223 if (sysctl(mib, 4, NULL, &argv_len, NULL, 0) == -1)
227 if ((argv = malloc(argv_len)) == NULL)
230 /* request KERN_PROC_ARGV */
231 if (sysctl(mib, 4, argv, &argv_len, NULL, 0) == -1) {
236 /* get realpath if possible */
237 if ((argv[0] != NULL) && ((*argv[0] == '.') || (*argv[0] == '/')
238 || (strstr(argv[0], "/") != NULL)))
240 self = realpath(argv[0], NULL);
256 // indent-tabs-mode: nil
258 // buffer-file-coding-system: utf-8-unix