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 "vg/valgrind.h"
21 #if !defined(__WIN32__)
23 #include <sys/types.h>
36 #include <TargetConditionals.h>
37 #include <mach/mach_time.h>
39 #if !(TARGET_OS_IPHONE)
40 #include <crt_externs.h>
68 #if (TARGET_OS_IPHONE)
69 extern char **environ;
73 #if defined(__FreeBSD__) || defined(__linux__) || defined(__ANDROID__)
74 extern char **environ;
77 #if defined(__WIN32__)
85 #if defined(__APPLE__) && !(TARGET_OS_IPHONE)
86 char **environ = *_NSGetEnviron();
93 #if defined(__WIN32__)
94 rust_list_dir_val(WIN32_FIND_DATA* entry_ptr) {
95 return entry_ptr->cFileName;
98 rust_list_dir_val(struct dirent* entry_ptr) {
99 return entry_ptr->d_name;
104 #if defined(__WIN32__)
105 rust_list_dir_wfd_size() {
106 return sizeof(WIN32_FIND_DATAW);
109 rust_list_dir_wfd_size() {
115 #if defined(__WIN32__)
116 rust_list_dir_wfd_fp_buf(WIN32_FIND_DATAW* wfd) {
121 return wfd->cFileName;
125 rust_list_dir_wfd_fp_buf(void* wfd) {
130 #if defined(__WIN32__)
132 rust_get_time(int64_t *sec, int32_t *nsec) {
134 GetSystemTimeAsFileTime(&fileTime);
136 // A FILETIME contains a 64-bit value representing the number of
137 // hectonanosecond (100-nanosecond) intervals since 1601-01-01T00:00:00Z.
138 // http://support.microsoft.com/kb/167296/en-us
140 ul.LowPart = fileTime.dwLowDateTime;
141 ul.HighPart = fileTime.dwHighDateTime;
142 uint64_t ns_since_1601 = ul.QuadPart / 10;
144 const uint64_t NANOSECONDS_FROM_1601_TO_1970 = 11644473600000000ull;
145 uint64_t ns_since_1970 = ns_since_1601 - NANOSECONDS_FROM_1601_TO_1970;
146 *sec = ns_since_1970 / 1000000;
147 *nsec = (ns_since_1970 % 1000000) * 1000;
151 rust_get_time(int64_t *sec, int32_t *nsec) {
154 gettimeofday(&tv, NULL);
156 *nsec = tv.tv_usec * 1000;
159 clock_gettime(CLOCK_REALTIME, &ts);
166 const int64_t ns_per_s = 1000000000LL;
169 rust_precise_time_ns(uint64_t *ns) {
172 uint64_t time = mach_absolute_time();
173 mach_timebase_info_data_t info = {0, 0};
174 if (info.denom == 0) {
175 mach_timebase_info(&info);
177 uint64_t time_nano = time * (info.numer / info.denom);
180 LARGE_INTEGER ticks_per_s;
181 BOOL query_result = QueryPerformanceFrequency(&ticks_per_s);
182 assert(query_result);
183 if (ticks_per_s.QuadPart == 0LL) {
184 ticks_per_s.QuadPart = 1LL;
187 query_result = QueryPerformanceCounter(&ticks);
188 assert(query_result);
189 *ns = (uint64_t)((ticks.QuadPart * ns_per_s) / ticks_per_s.QuadPart);
192 clock_gettime(CLOCK_MONOTONIC, &ts);
193 *ns = (uint64_t)(ts.tv_sec * ns_per_s + ts.tv_nsec);
199 size_t fill; // in bytes; if zero, heapified
200 size_t alloc; // in bytes
204 typedef rust_vec rust_str;
221 void rust_tm_to_tm(rust_tm* in_tm, struct tm* out_tm) {
222 memset(out_tm, 0, sizeof(struct tm));
223 out_tm->tm_sec = in_tm->tm_sec;
224 out_tm->tm_min = in_tm->tm_min;
225 out_tm->tm_hour = in_tm->tm_hour;
226 out_tm->tm_mday = in_tm->tm_mday;
227 out_tm->tm_mon = in_tm->tm_mon;
228 out_tm->tm_year = in_tm->tm_year;
229 out_tm->tm_wday = in_tm->tm_wday;
230 out_tm->tm_yday = in_tm->tm_yday;
231 out_tm->tm_isdst = in_tm->tm_isdst;
234 void tm_to_rust_tm(struct tm* in_tm, rust_tm* out_tm, int32_t gmtoff,
235 const char *zone, int32_t nsec) {
236 out_tm->tm_sec = in_tm->tm_sec;
237 out_tm->tm_min = in_tm->tm_min;
238 out_tm->tm_hour = in_tm->tm_hour;
239 out_tm->tm_mday = in_tm->tm_mday;
240 out_tm->tm_mon = in_tm->tm_mon;
241 out_tm->tm_year = in_tm->tm_year;
242 out_tm->tm_wday = in_tm->tm_wday;
243 out_tm->tm_yday = in_tm->tm_yday;
244 out_tm->tm_isdst = in_tm->tm_isdst;
245 out_tm->tm_gmtoff = gmtoff;
246 out_tm->tm_nsec = nsec;
249 size_t size = strlen(zone);
250 assert(out_tm->tm_zone->alloc >= size);
251 memcpy(out_tm->tm_zone->data, zone, size);
252 out_tm->tm_zone->fill = size;
256 #if defined(__WIN32__)
257 #define TZSET() _tzset()
258 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
259 #define GMTIME(clock, result) gmtime_s((result), (clock))
260 #define LOCALTIME(clock, result) localtime_s((result), (clock))
261 #define TIMEGM(result) _mkgmtime64(result)
263 struct tm* GMTIME(const time_t *clock, struct tm *result) {
264 struct tm* t = gmtime(clock);
265 if (t == NULL || result == NULL) { return NULL; }
269 struct tm* LOCALTIME(const time_t *clock, struct tm *result) {
270 struct tm* t = localtime(clock);
271 if (t == NULL || result == NULL) { return NULL; }
275 #define TIMEGM(result) mktime((result)) - _timezone
278 #define TZSET() tzset()
279 #define GMTIME(clock, result) gmtime_r((clock), (result))
280 #define LOCALTIME(clock, result) localtime_r((clock), (result))
281 #define TIMEGM(result) timegm(result)
290 rust_gmtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
295 tm_to_rust_tm(&tm, timeptr, 0, "UTC", nsec);
299 rust_localtime(int64_t sec, int32_t nsec, rust_tm *timeptr) {
304 const char* zone = NULL;
305 #if defined(__WIN32__)
306 int32_t gmtoff = -timezone;
307 wchar_t wbuffer[64] = {0};
308 char buffer[256] = {0};
309 // strftime("%Z") can contain non-UTF-8 characters on non-English locale (issue #9418),
310 // so time zone should be converted from UTF-16 string.
311 // Since wcsftime depends on setlocale() result,
312 // instead we convert it using MultiByteToWideChar.
313 if (strftime(buffer, sizeof(buffer) / sizeof(char), "%Z", &tm) > 0) {
315 MultiByteToWideChar(CP_ACP, 0, buffer, -1, wbuffer, sizeof(wbuffer) / sizeof(wchar_t));
317 WideCharToMultiByte(CP_UTF8, 0, wbuffer, -1, buffer, sizeof(buffer), NULL, NULL);
321 int32_t gmtoff = tm.tm_gmtoff;
325 tm_to_rust_tm(&tm, timeptr, gmtoff, zone, nsec);
329 rust_timegm(rust_tm* timeptr) {
331 rust_tm_to_tm(timeptr, &t);
336 rust_mktime(rust_tm* timeptr) {
338 rust_tm_to_tm(timeptr, &t);
345 rust_opendir(char *dirname) {
346 return opendir(dirname);
350 rust_readdir(DIR *dirp) {
351 return readdir(dirp);
367 rust_running_on_valgrind() {
368 return RUNNING_ON_VALGRIND;
371 #if defined(__WIN32__)
375 GetSystemInfo(&sysinfo);
377 return (int) sysinfo.dwNumberOfProcessors;
379 #elif defined(__BSD__)
382 /* swiped from http://stackoverflow.com/questions/150355/
383 programmatically-find-the-number-of-cores-on-a-machine */
387 size_t len = sizeof(numCPU);
389 /* set the mib for hw.ncpu */
391 mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
393 /* get the number of CPUs from the system */
394 sysctl(mib, 2, &numCPU, &len, NULL, 0);
398 sysctl( mib, 2, &numCPU, &len, NULL, 0 );
406 #elif defined(__GNUC__)
409 return sysconf(_SC_NPROCESSORS_ONLN);
414 rust_get_num_cpus() {
415 return get_num_cpus();
419 rust_valgrind_stack_register(void *start, void *end) {
420 return VALGRIND_STACK_REGISTER(start, end);
424 rust_valgrind_stack_deregister(unsigned int id) {
425 VALGRIND_STACK_DEREGISTER(id);
428 #if defined(__WIN32__)
431 rust_unset_sigprocmask() {
432 // empty stub for windows to keep linker happy
438 rust_unset_sigprocmask() {
439 // this can't be safely converted to rust code because the
440 // representation of sigset_t is platform-dependent
443 sigprocmask(SIG_SETMASK, &sset, NULL);
448 #if defined(__WIN32__)
450 win32_require(LPCTSTR fn, BOOL ok) {
453 DWORD err = GetLastError();
454 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
455 FORMAT_MESSAGE_FROM_SYSTEM |
456 FORMAT_MESSAGE_IGNORE_INSERTS,
458 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
459 (LPTSTR) &buf, 0, NULL );
460 fprintf(stderr, "%s failed with error %ld: %s", fn, err, buf);
461 LocalFree((HLOCAL)buf);
467 rust_win32_rand_acquire(HCRYPTPROV* phProv) {
469 (_T("CryptAcquireContext"),
470 // changes to the parameters here should be reflected in the docs of
471 // std::rand::os::OSRng
472 CryptAcquireContext(phProv, NULL, NULL, PROV_RSA_FULL,
473 CRYPT_VERIFYCONTEXT|CRYPT_SILENT));
477 rust_win32_rand_gen(HCRYPTPROV hProv, DWORD dwLen, BYTE* pbBuffer) {
479 (_T("CryptGenRandom"), CryptGenRandom(hProv, dwLen, pbBuffer));
482 rust_win32_rand_release(HCRYPTPROV hProv) {
484 (_T("CryptReleaseContext"), CryptReleaseContext(hProv, 0));
489 // these symbols are listed in rustrt.def.in, so they need to exist; but they
490 // should never be called.
493 rust_win32_rand_acquire() {
497 rust_win32_rand_gen() {
501 rust_win32_rand_release() {
507 #if defined(__WIN32__)
510 rust_crit_section_size() { return sizeof(CRITICAL_SECTION); }
512 rust_pthread_mutex_t_size() { return 0; }
514 rust_pthread_cond_t_size() { return 0; }
519 rust_crit_section_size() { return 0; }
521 rust_pthread_mutex_t_size() { return sizeof(pthread_mutex_t); }
523 rust_pthread_cond_t_size() { return sizeof(pthread_cond_t); }
531 // indent-tabs-mode: nil
533 // buffer-file-coding-system: utf-8-unix