misc.h
Raw
1#ifndef MISC_H
2#define 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
22#if __STDC_VERSION__ >= 199901L
23 #ifdef UINT32_MAX
24 uint32_t ntoh32(uint32_t const net);
25 #endif
26 HEDLEY_INLINE
27 static void memqswap_stack(void* item1, void* item2, size_t length)
28 {
29 unsigned char tmp[length];
30 memcpy(tmp, item1, length);
31 memcpy(item1, item2, length);
32 memcpy(item2, tmp, length);
33 free(tmp);
34 }
35#endif
36
37HEDLEY_INLINE
38static void memswap(void* item1, void* item2, size_t length)
39{
40 unsigned char *item1_tmp, *item2_tmp;
41 unsigned char tmp;
42
43 item1_tmp = item1;
44 item2_tmp = item2;
45
46 while(length--)
47 {
48 tmp = item1_tmp[length];
49 item1_tmp[length] = item2_tmp[length];
50 item2_tmp[length] = tmp;
51 }
52}
53
54HEDLEY_INLINE
55static void memqswap(void* item1, void* item2, void* tmp, size_t length)
56{
57 memcpy(tmp, item1, length);
58 memcpy(item1, item2, length);
59 memcpy(item2, tmp, length);
60}
61
62#if __STDC_VERSION__ >= 201112L
63 #include <time.h>
64#endif
65
66void sleep_ms(unsigned int milliseconds);
67
68#if __STDC_VERSION__ >= 201112L
69 struct timespec timespec_diff(const struct timespec* old_ts, const struct timespec* new_ts);
70 struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2);
71 uintmax_t timespec_ms(const struct timespec* ts);
72#endif
73
74#endif /* MISC_H */
75