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#ifndef CUTILS_NO_MACROS
33#define MAX(x,y) ((x) > (y) ? (x) : (y))
34#define MIN(x,y) ((x) < (y) ? (x) : (y))
35#define LOOP while(1)
36#define RANGE(i, start, end) \
37for(((i) = (start)); ((start) < (end)) ? ((i) < (end)) : ((i) > (end)); ((start) < (end)) ? (i++) : (i--))
38#endif /* CUTILS_NO_MACROS */
39
40
41#if __STDC_VERSION__ >= 199901L
42 #ifdef UINT32_MAX
43 uint32_t nethost32(uint32_t const net);
44 #endif
45 HEDLEY_INLINE
46 static void memqswap_stack(void* item1, void* item2, size_t length)
47 {
48 byte tmp[length];
49 memcpy(tmp, item1, length);
50 memcpy(item1, item2, length);
51 memcpy(item2, tmp, length);
52 }
53#endif
54
55HEDLEY_INLINE
56static void memswap(void* item1, void* item2, size_t length)
57{
58 byte *item1_tmp, *item2_tmp;
59 byte tmp;
60
61 item1_tmp = item1;
62 item2_tmp = item2;
63
64 while(length--)
65 {
66 tmp = item1_tmp[length];
67 item1_tmp[length] = item2_tmp[length];
68 item2_tmp[length] = tmp;
69 }
70}
71
72HEDLEY_INLINE
73static void memqswap(void* item1, void* item2, void* tmp, size_t length)
74{
75 memcpy(tmp, item1, length);
76 memcpy(item1, item2, length);
77 memcpy(item2, tmp, length);
78}
79
80void sleep_ms(unsigned int milliseconds);
81
82#if __STDC_VERSION__ >= 201112L
83 #include <time.h>
84 bool timespec_bigger(const struct timespec* ts1, const struct timespec* ts2);
85 bool timespec_equal(const struct timespec* ts1, const struct timespec* ts2);
86 bool timespec_smaller(const struct timespec* ts1, const struct timespec* ts2);
87 /* subtracts ts2 from ts1, if ts2 > ts1 then zero is returned */
88 struct timespec timespec_diff(const struct timespec* ts1, const struct timespec* ts2);
89 struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2);
90 uintmax_t timespec_ms(const struct timespec* ts);
91#endif
92
93#endif /* CUTILS_MISC_H */
94