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