v2 / vlib / v / slow_tests / keep_args_alive_test_c.h
44 lines · 41 sloc · 940 bytes · 6a32c810703f4ec0c39fe18298ebe6c40acac8f1
Raw
1#include <time.h>
2
3#if defined(_WIN32)
4#define __SLEEP_MS(n) Sleep(n)
5#elif defined(__APPLE__)
6static void __sleep_ms(int ms) {
7 struct timespec ts = {
8 .tv_sec = ms / 1000,
9 .tv_nsec = 1000000L * (ms % 1000)
10 };
11 struct timespec rem;
12 while (nanosleep(&ts, &rem) != 0) {
13 ts = rem;
14 }
15}
16#define __SLEEP_MS(n) __sleep_ms(n)
17#else
18static void __sleep_ms(int ms) {
19 struct timespec ts;
20 clock_gettime(CLOCK_MONOTONIC, &ts);
21 ts.tv_nsec += 1000000L*((ms)%1000);
22 ts.tv_sec += ms/1000;
23 if (ts.tv_nsec >= 1000000000) {
24 ts.tv_nsec -= 1000000000;
25 ++ts.tv_sec;
26 }
27 while (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) != 0);
28}
29#define __SLEEP_MS(n) __sleep_ms(n)
30#endif
31
32static volatile int** keep;
33
34static int calc_expr_after_delay(int* a, int b, int* c) {
35 keep = malloc(1000000);
36 keep[43242] = a;
37 keep[86343] = c;
38 a = NULL;
39 c = NULL;
40 __SLEEP_MS(200);
41 int z = *keep[43242] * b + *keep[86343];
42 free(keep);
43 return z;
44}
45