misc.h
Raw
1#ifndef CUTILS_MISC_H
2#define CUTILS_MISC_H
3
4#ifdef __unix__
5 #ifndef _BSD_SOURCE
6 #define _BSD_SOURCE 1
7 #endif
8 #ifndef _DEFAULT_SOURCE
9 #define _DEFAULT_SOURCE 1
10 #endif
11 #include <unistd.h>
12#endif
13#ifdef _WIN32
14 #include <windows.h>
15#endif
16
17#include <string.h>
18#include <stdlib.h>
19#include <cutils/common.h>
20#include <ctype.h>
21
22typedef unsigned char byte;
23
24#ifndef CUTILS_NO_SHORTHANDLES
25typedef unsigned char uchar;
26typedef unsigned short ushort;
27typedef unsigned int uint;
28typedef unsigned long ulong;
29typedef unsigned long long ullong;
30#endif /* CUTILS_NO_SHORTHANDLES */
31
32#if __STDC_VERSION__ >= 199901L
33 #ifdef UINT32_MAX
34 uint32_t ntoh32(uint32_t const net);
35 #endif
36 HEDLEY_INLINE
37 static void memqswap_stack(void* item1, void* item2, size_t length)
38 {
39 byte tmp[length];
40 memcpy(tmp, item1, length);
41 memcpy(item1, item2, length);
42 memcpy(item2, tmp, length);
43 }
44#endif
45
46HEDLEY_INLINE
47static void memswap(void* item1, void* item2, size_t length)
48{
49 byte *item1_tmp, *item2_tmp;
50 byte tmp;
51
52 item1_tmp = item1;
53 item2_tmp = item2;
54
55 while(length--)
56 {
57 tmp = item1_tmp[length];
58 item1_tmp[length] = item2_tmp[length];
59 item2_tmp[length] = tmp;
60 }
61}
62
63HEDLEY_INLINE
64static void memqswap(void* item1, void* item2, void* tmp, size_t length)
65{
66 memcpy(tmp, item1, length);
67 memcpy(item1, item2, length);
68 memcpy(item2, tmp, length);
69}
70
71void sleep_ms(unsigned int milliseconds);
72
73#if __STDC_VERSION__ >= 201112L
74 #include <time.h>
75 bool timespec_bigger(const struct timespec* ts1, const struct timespec* ts2);
76 bool timespec_equal(const struct timespec* ts1, const struct timespec* ts2);
77 bool timespec_smaller(const struct timespec* ts1, const struct timespec* ts2);
78 /* subtracts ts2 from ts1, if ts2 > ts1 then zero is returned */
79 struct timespec timespec_diff(const struct timespec* ts1, const struct timespec* ts2);
80 struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2);
81 uintmax_t timespec_ms(const struct timespec* ts);
82#endif
83
84#endif /* CUTILS_MISC_H */
85