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.
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.
11 /* Foreign builtins. */
13 #include "rust_globals.h"
14 #include "vg/valgrind.h"
19 #include <TargetConditionals.h>
20 #include <mach/mach_time.h>
22 #if (TARGET_OS_IPHONE)
23 extern char **environ;
25 #include <crt_externs.h>
29 #if !defined(__WIN32__)
34 extern char **environ;
60 #if defined(__WIN32__)
68 #if defined(__APPLE__) && !(TARGET_OS_IPHONE)
69 char **environ = *_NSGetEnviron();
76 #if defined(__WIN32__)
77 rust_list_dir_val(WIN32_FIND_DATA* entry_ptr) {
78 return entry_ptr->cFileName;
81 rust_list_dir_val(struct dirent* entry_ptr) {
82 return entry_ptr->d_name;
87 #if defined(__WIN32__)
88 rust_list_dir_wfd_size() {
89 return sizeof(WIN32_FIND_DATAW);
92 rust_list_dir_wfd_size() {
98 #if defined(__WIN32__)
99 rust_list_dir_wfd_fp_buf(WIN32_FIND_DATAW* wfd) {
104 return wfd->cFileName;
108 rust_list_dir_wfd_fp_buf(void* wfd) {
113 #if defined(__WIN32__)
115 rust_get_time(int64_t *sec, int32_t *nsec) {
117 GetSystemTimeAsFileTime(&fileTime);
119 // A FILETIME contains a 64-bit value representing the number of
120 // hectonanosecond (100-nanosecond) intervals since 1601-01-01T00:00:00Z.
121 // http://support.microsoft.com/kb/167296/en-us
123 ul.LowPart = fileTime.dwLowDateTime;
124 ul.HighPart = fileTime.dwHighDateTime;
125 uint64_t ns_since_1601 = ul.QuadPart / 10;
127 const uint64_t NANOSECONDS_FROM_1601_TO_1970 = 11644473600000000ull;
128 uint64_t ns_since_1970 = ns_since_1601 - NANOSECONDS_FROM_1601_TO_1970;
129 *sec = ns_since_1970 / 1000000;
130 *nsec = (ns_since_1970 % 1000000) * 1000;
134 rust_get_time(int64_t *sec, int32_t *nsec) {
137 gettimeofday(&tv, NULL);
139 *nsec = tv.tv_usec * 1000;
142 clock_gettime(CLOCK_REALTIME, &ts);
149 const int64_t ns_per_s = 1000000000LL;
152 rust_precise_time_ns(uint64_t *ns) {
155 uint64_t time = mach_absolute_time();
156 mach_timebase_info_data_t info = {0, 0};
157 if (info.denom == 0) {
158 mach_timebase_info(&info);
160 uint64_t time_nano = time * (info.numer / info.denom);
163 LARGE_INTEGER ticks_per_s;
164 BOOL query_result = QueryPerformanceFrequency(&ticks_per_s);
165 assert(query_result);
166 if (ticks_per_s.QuadPart == 0LL) {
167 ticks_per_s.QuadPart = 1LL;
170 query_result = QueryPerformanceCounter(&ticks);
171 assert(query_result);
172 *ns = (uint64_t)((ticks.QuadPart * ns_per_s) / ticks_per_s.QuadPart);
175 clock_gettime(CLOCK_MONOTONIC, &ts);
176 *ns = (uint64_t)(ts.tv_sec * ns_per_s + ts.tv_nsec);
182 size_t fill; // in bytes; if zero, heapified
183 size_t alloc; // in bytes
187 typedef rust_vec rust_str;
204 void rust_tm_to_tm(rust_tm* in_tm, struct tm* out_tm) {
205 memset(out_tm, 0, sizeof(struct tm));
206 out_tm->tm_sec = in_tm->tm_sec;
207 out_tm->tm_min = in_tm->tm_min;
208 out_tm->tm_hour = in_tm->tm_hour;
209 out_tm->tm_mday = in_tm->tm_mday;
210 out_tm->tm_mon = in_tm->tm_mon;
211 out_tm->tm_year = in_tm->tm_year;
212 out_tm->tm_wday = in_tm->tm_wday;
213 out_tm->tm_yday = in_tm->tm_yday;
214 out_tm->tm_isdst = in_tm->tm_isdst;
217 void tm_to_rust_tm(struct tm* in_tm, rust_tm* out_tm, int32_t gmtoff,
218 const char *zone, int32_t nsec) {
219 out_tm->tm_sec = in_tm->tm_sec;
220 out_tm->tm_min = in_tm->tm_min;
221 out_tm->tm_hour = in_tm->tm_hour;
222 out_tm->tm_mday = in_tm->tm_mday;
223 out_tm->tm_mon = in_tm->tm_mon;
224 out_tm->tm_year = in_tm->tm_year;
225 out_tm->tm_wday = in_tm->tm_wday;
226 out_tm->tm_yday = in_tm->tm_yday;
227 out_tm->tm_isdst = in_tm->tm_isdst;
228 out_tm->tm_gmtoff = gmtoff;
229 out_tm->tm_nsec = nsec;
232 size_t size = strlen(zone);
233 assert(out_tm->tm_zone->alloc >= size);
234 memcpy(out_tm->tm_zone->data, zone, size);
235 out_tm->tm_zone->fill = size;
239 #if defined(__WIN32__)
240 #define TZSET() _tzset()
241 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
242 #define GMTIME(clock, result) gmtime_s((result), (clock))
243 #define LOCALTIME(clock, result) localtime_s((result), (clock))
244 #define TIMEGM(result) _mkgmtime64(result)
246 struct tm* GMTIME(const time_t *clock, struct tm *result) {
247 struct tm* t = gmtime(clock);
248 if (t == NULL || result == NULL) { return NULL; }
252 struct tm* LOCALTIME(const time_t *clock, struct tm *result) {
253 struct tm* t = localtime(clock);
254 if (t == NULL || result == NULL) { return NULL; }
258 #define TIMEGM(result) mktime((result)) - _timezone
261 #define TZSET() tzset()
262 #define GMTIME(clock, result) gmtime_r((clock), (result))
263 #define LOCALTIME(clock, result) localtime_r((clock), (result))
264 #define TIMEGM(result) timegm(result)
273 rust_gmtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
278 tm_to_rust_tm(&tm, timeptr, 0, "UTC", nsec);
282 rust_localtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
287 const char* zone = NULL;
288 #if defined(__WIN32__)
289 int32_t gmtoff = -timezone;
290 wchar_t wbuffer[64] = {0};
291 char buffer[256] = {0};
292 // strftime("%Z") can contain non-UTF-8 characters on non-English locale (issue #9418),
293 // so time zone should be converted from UTF-16 string.
294 // Since wcsftime depends on setlocale() result,
295 // instead we convert it using MultiByteToWideChar.
296 if (strftime(buffer, sizeof(buffer) / sizeof(char), "%Z", &tm) > 0) {
298 MultiByteToWideChar(CP_ACP, 0, buffer, -1, wbuffer, sizeof(wbuffer) / sizeof(wchar_t));
300 WideCharToMultiByte(CP_UTF8, 0, wbuffer, -1, buffer, sizeof(buffer), NULL, NULL);
304 int32_t gmtoff = tm.tm_gmtoff;
308 tm_to_rust_tm(&tm, timeptr, gmtoff, zone, nsec);
312 rust_timegm(rust_tm* timeptr) {
314 rust_tm_to_tm(timeptr, &t);
319 rust_mktime(rust_tm* timeptr) {
321 rust_tm_to_tm(timeptr, &t);
326 #include <sys/types.h>
330 rust_opendir(char *dirname) {
331 return opendir(dirname);
335 rust_readdir(DIR *dirp) {
336 return readdir(dirp);
352 rust_running_on_valgrind() {
353 return RUNNING_ON_VALGRIND;
356 #if defined(__WIN32__)
360 GetSystemInfo(&sysinfo);
362 return (int) sysinfo.dwNumberOfProcessors;
364 #elif defined(__BSD__)
367 /* swiped from http://stackoverflow.com/questions/150355/
368 programmatically-find-the-number-of-cores-on-a-machine */
372 size_t len = sizeof(numCPU);
374 /* set the mib for hw.ncpu */
376 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
378 /* get the number of CPUs from the system */
379 sysctl(mib, 2, &numCPU, &len, NULL, 0);
383 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
391 #elif defined(__GNUC__)
394 return sysconf(_SC_NPROCESSORS_ONLN);
399 rust_get_num_cpus() {
400 return get_num_cpus();
404 rust_valgrind_stack_register(void *start, void *end) {
405 return VALGRIND_STACK_REGISTER(start, end);
409 rust_valgrind_stack_deregister(unsigned int id) {
410 VALGRIND_STACK_DEREGISTER(id);
413 #if defined(__WIN32__)
416 rust_unset_sigprocmask() {
417 // empty stub for windows to keep linker happy
426 rust_unset_sigprocmask() {
427 // this can't be safely converted to rust code because the
428 // representation of sigset_t is platform-dependent
431 sigprocmask(SIG_SETMASK, &sset, NULL);
436 #if defined(__WIN32__)
438 win32_require(LPCTSTR fn, BOOL ok) {
441 DWORD err = GetLastError();
442 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
443 FORMAT_MESSAGE_FROM_SYSTEM |
444 FORMAT_MESSAGE_IGNORE_INSERTS,
446 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
447 (LPTSTR) &buf, 0, NULL );
448 fprintf(stderr, "%s failed with error %ld: %s", fn, err, buf);
449 LocalFree((HLOCAL)buf);
455 rust_win32_rand_acquire(HCRYPTPROV* phProv) {
457 (_T("CryptAcquireContext"),
458 // changes to the parameters here should be reflected in the docs of
459 // std::rand::os::OSRng
460 CryptAcquireContext(phProv, NULL, NULL, PROV_RSA_FULL,
461 CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
465 rust_win32_rand_gen(HCRYPTPROV hProv, DWORD dwLen, BYTE* pbBuffer) {
467 (_T("CryptGenRandom"), CryptGenRandom(hProv, dwLen, pbBuffer));
470 rust_win32_rand_release(HCRYPTPROV hProv) {
472 (_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0));
477 // these symbols are listed in rustrt.def.in, so they need to exist; but they
478 // should never be called.
481 rust_win32_rand_acquire() {
485 rust_win32_rand_gen() {
489 rust_win32_rand_release() {
495 #if defined(__WIN32__)
498 rust_crit_section_size() { return sizeof(CRITICAL_SECTION); }
500 rust_pthread_mutex_t_size() { return 0; }
502 rust_pthread_cond_t_size() { return 0; }
507 rust_crit_section_size() { return 0; }
509 rust_pthread_mutex_t_size() { return sizeof(pthread_mutex_t); }
511 rust_pthread_cond_t_size() { return sizeof(pthread_cond_t); }
519 // indent-tabs-mode: nil
521 // buffer-file-coding-system: utf-8-unix