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__) \
51 || defined(__DragonFly__) || defined(__OpenBSD__)
52 extern char **environ;
55 #if defined(__WIN32__)
63 #if defined(__APPLE__) && !(TARGET_OS_IPHONE)
64 char **environ = *_NSGetEnviron();
71 #if defined(__WIN32__)
72 rust_list_dir_val(WIN32_FIND_DATA* entry_ptr) {
73 return entry_ptr->cFileName;
76 rust_list_dir_val(struct dirent* entry_ptr) {
77 return entry_ptr->d_name;
84 rust_opendir(char *dirname) {
85 return opendir(dirname);
89 rust_readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result) {
90 return readdir_r(dirp, entry, result);
94 rust_dirent_t_size() {
95 return sizeof(struct dirent);
109 rust_dirent_t_size() {
115 rust_running_on_valgrind() {
116 return RUNNING_ON_VALGRIND;
119 #if defined(__WIN32__)
123 GetSystemInfo(&sysinfo);
125 return (int) sysinfo.dwNumberOfProcessors;
127 #elif defined(__BSD__)
130 /* swiped from http://stackoverflow.com/questions/150355/
131 programmatically-find-the-number-of-cores-on-a-machine */
135 size_t len = sizeof(numCPU);
137 /* set the mib for hw.ncpu */
139 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
141 /* get the number of CPUs from the system */
142 sysctl(mib, 2, &numCPU, &len, NULL, 0);
146 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
154 #elif defined(__GNUC__)
157 return sysconf(_SC_NPROCESSORS_ONLN);
162 rust_get_num_cpus() {
163 return get_num_cpus();
167 rust_valgrind_stack_register(void *start, void *end) {
168 return VALGRIND_STACK_REGISTER(start, end);
172 rust_valgrind_stack_deregister(unsigned int id) {
173 VALGRIND_STACK_DEREGISTER(id);
176 #if defined(__WIN32__)
179 rust_unset_sigprocmask() {
180 // empty stub for windows to keep linker happy
186 rust_unset_sigprocmask() {
187 // this can't be safely converted to rust code because the
188 // representation of sigset_t is platform-dependent
191 sigprocmask(SIG_SETMASK, &sset, NULL);
196 #if defined(__DragonFly__)
198 // In DragonFly __error() is an inline function and as such
199 // no symbol exists for it.
200 int *__dfly_error(void) { return __error(); }
203 #if defined(__OpenBSD__)
204 #include <sys/param.h>
205 #include <sys/sysctl.h>
208 const char * rust_load_self() {
209 static char *self = NULL;
218 mib[1] = KERN_PROC_ARGS;
220 mib[3] = KERN_PROC_ARGV;
222 /* request KERN_PROC_ARGV size */
224 if (sysctl(mib, 4, NULL, &argv_len, NULL, 0) == -1)
228 if ((argv = malloc(argv_len)) == NULL)
231 /* request KERN_PROC_ARGV */
232 if (sysctl(mib, 4, argv, &argv_len, NULL, 0) == -1) {
237 /* get realpath if possible */
238 if ((argv[0] != NULL) && ((*argv[0] == '.') || (*argv[0] == '/')
239 || (strstr(argv[0], "/") != NULL)))
241 self = realpath(argv[0], NULL);
257 // indent-tabs-mode: nil
259 // buffer-file-coding-system: utf-8-unix