misc.c
Raw
1#include <cutils/misc.h>
2
3void sleep_ms(unsigned int milliseconds)
4{
5 #ifdef __unix__
6 usleep(milliseconds * 1000);
7 #endif
8 #ifdef _WIN32
9 Sleep(milliseconds);
10 #endif
11}
12
13#if __STDC_VERSION__ >= 199901L
14#ifdef UINT32_MAX
15uint32_t ntoh32(uint32_t const net)
16{
17 uint8_t data[4];
18 memcpy(&data, &net, sizeof(data));
19
20 return ((uint32_t) data[3] << 0)
21 | ((uint32_t) data[2] << 8)
22 | ((uint32_t) data[1] << 16)
23 | ((uint32_t) data[0] << 24);
24}
25#endif
26#endif
27
28#if __STDC_VERSION__ >= 201112L
29struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts)
30{
31 struct timespec diff;
32
33 if ((new_ts->tv_nsec - old_ts->tv_nsec) < 0)
34 {
35 diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec - 1;
36 diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec + 1000000000;
37 } else {
38 diff.tv_sec = new_ts->tv_sec - old_ts->tv_sec;
39 diff.tv_nsec = new_ts->tv_nsec - old_ts->tv_nsec;
40 }
41
42 return diff;
43}
44
45struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2)
46{
47 struct timespec res;
48
49 if (999999999 - ts_1->tv_nsec < ts_2->tv_nsec)
50 {
51 res.tv_sec = ts_1->tv_sec + ts_2->tv_sec + 1;
52 res.tv_nsec = ts_1->tv_nsec + (ts_2->tv_nsec - 1000000000) ;
53 } else {
54 res.tv_sec = ts_1->tv_sec + ts_2->tv_sec;
55 res.tv_nsec = ts_1->tv_nsec + ts_2->tv_nsec;
56 }
57
58 return res;
59}
60
61
62uintmax_t timespec_ms(const struct timespec* ts)
63{
64 return (uintmax_t)ts->tv_sec * 1000 + (uintmax_t)ts->tv_nsec/1000000;
65}
66#endif
67