misc.h
| 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 | #include <stdarg.h> |
| 22 | |
| 23 | typedef unsigned char byte; |
| 24 | |
| 25 | #ifndef CUTILS_NO_SHORTHANDLES |
| 26 | typedef unsigned char uchar; |
| 27 | typedef unsigned short ushort; |
| 28 | typedef unsigned int uint; |
| 29 | typedef unsigned long ulong; |
| 30 | #ifdef ULLONG_MAX |
| 31 | typedef unsigned long long ullong; |
| 32 | #endif /* ULLONG_MAX */ |
| 33 | #endif /* CUTILS_NO_SHORTHANDLES */ |
| 34 | |
| 35 | #define CUTIL_MAX(x,y) ((x) > (y) ? (x) : (y)) |
| 36 | #define CUTIL_MIN(x,y) ((x) < (y) ? (x) : (y)) |
| 37 | #define CUTIL_LOOP while(1) |
| 38 | #define CUTIL_RANGE(i, start, end) \ |
| 39 | for(((i) = (start)); ((start) < (end)) ? ((i) < (end)) : ((i) > (end)); ((start) < (end)) ? (i++) : (i--)) |
| 40 | #define CUTIL_NEW(type) (malloc(sizeof(type))) |
| 41 | #define CUTIL_NEW_N(type, n) (malloc(n * sizeof(type))) |
| 42 | |
| 43 | #ifndef CUTILS_NO_MACROS |
| 44 | #define MAX(x,y) CUTIL_MAX(x,y) |
| 45 | #define MIN(x,y) CUTIL_MIN(x,y) |
| 46 | #define LOOP CUTIL_LOOP |
| 47 | #define RANGE(i, start, end) CUTIL_RANGE(i, start, end) |
| 48 | #define NEW(type) CUTIL_NEW(type) |
| 49 | #define NEW_N(type, n) CUTIL_NEW_N(type, n) |
| 50 | #endif /* CUTILS_NO_MACROS */ |
| 51 | |
| 52 | #ifdef DEBUG |
| 53 | #define DEBUG_ASSERT(x) assert(x) |
| 54 | #else |
| 55 | #define DEBUG_ASSERT(x) |
| 56 | #endif /* DEBUG */ |
| 57 | |
| 58 | |
| 59 | #if __STDC_VERSION__ >= 199901L |
| 60 | #ifdef UINT64_MAX |
| 61 | |
| 62 | enum endianess {CUTIL_ENDIAN_ERROR = 0, CUTIL_BIG_ENDIAN = 1, CUTIL_LITTLE_ENDIAN = 2, CUTIL_MID_BIG_ENDIAN = 3, CUTIL_MID_LITTLE_ENDIAN = 4}; |
| 63 | static const char** endianess_strings = (const char *[]){"ENDIAN_ERROR","BIG_ENDIAN","LITTLE_ENDIAN","MID_BIG_ENDIAN","MID_LITTLE_ENDIAN"}; |
| 64 | HEDLEY_INLINE |
| 65 | static enum endianess check_endianess(void) |
| 66 | { |
| 67 | union |
| 68 | { |
| 69 | uint32_t t1; |
| 70 | uint8_t t2[4]; |
| 71 | } test; |
| 72 | test.t1 = 255; |
| 73 | |
| 74 | if(test.t2[0] == 255) |
| 75 | { |
| 76 | return CUTIL_LITTLE_ENDIAN; |
| 77 | } else if(test.t2[1] == 255) { |
| 78 | return CUTIL_MID_LITTLE_ENDIAN; |
| 79 | } else if(test.t2[2] == 255) { |
| 80 | return CUTIL_MID_BIG_ENDIAN; |
| 81 | } else if(test.t2[3] == 255) { |
| 82 | return CUTIL_BIG_ENDIAN; |
| 83 | } else { |
| 84 | return CUTIL_ENDIAN_ERROR; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | HEDLEY_INLINE |
| 89 | static uint64_t nethost64(uint64_t const from) |
| 90 | { |
| 91 | uint8_t data[4]; |
| 92 | memcpy(&data, &from, sizeof(data)); |
| 93 | |
| 94 | return ((uint64_t) data[7] << 0) |
| 95 | | ((uint64_t) data[6] << 8) |
| 96 | | ((uint64_t) data[5] << 16) |
| 97 | | ((uint64_t) data[4] << 24) |
| 98 | | ((uint64_t) data[3] << 32) |
| 99 | | ((uint64_t) data[2] << 40) |
| 100 | | ((uint64_t) data[1] << 48) |
| 101 | | ((uint64_t) data[0] << 54); |
| 102 | } |
| 103 | #endif |
| 104 | #ifdef UINT32_MAX |
| 105 | HEDLEY_INLINE |
| 106 | static uint32_t nethost32(uint32_t const from) |
| 107 | { |
| 108 | uint8_t data[4]; |
| 109 | memcpy(&data, &from, sizeof(data)); |
| 110 | |
| 111 | return ((uint32_t) data[3] << 0) |
| 112 | | ((uint32_t) data[2] << 8) |
| 113 | | ((uint32_t) data[1] << 16) |
| 114 | | ((uint32_t) data[0] << 24); |
| 115 | } |
| 116 | #endif |
| 117 | #ifdef UINT16_MAX |
| 118 | HEDLEY_INLINE |
| 119 | static uint16_t nethost16(uint16_t const from) |
| 120 | { |
| 121 | uint8_t data[2]; |
| 122 | memcpy(&data, &from, sizeof(data)); |
| 123 | |
| 124 | return (uint16_t)( |
| 125 | ((uint16_t) data[1] << 0) |
| 126 | | ((uint16_t) data[0] << 8)); |
| 127 | } |
| 128 | #endif |
| 129 | HEDLEY_INLINE |
| 130 | static void memqswap_stack(void* item1, void* item2, size_t length) |
| 131 | { |
| 132 | byte tmp[length]; |
| 133 | memcpy(tmp, item1, length); |
| 134 | memcpy(item1, item2, length); |
| 135 | memcpy(item2, tmp, length); |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | HEDLEY_INLINE |
| 140 | static void memswap(void* item1, void* item2, size_t length) |
| 141 | { |
| 142 | byte *item1_tmp, *item2_tmp; |
| 143 | byte tmp; |
| 144 | |
| 145 | item1_tmp = item1; |
| 146 | item2_tmp = item2; |
| 147 | |
| 148 | while(length--) |
| 149 | { |
| 150 | tmp = item1_tmp[length]; |
| 151 | item1_tmp[length] = item2_tmp[length]; |
| 152 | item2_tmp[length] = tmp; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | HEDLEY_INLINE |
| 157 | static void memqswap(void* item1, void* item2, void* tmp, size_t length) |
| 158 | { |
| 159 | memcpy(tmp, item1, length); |
| 160 | memcpy(item1, item2, length); |
| 161 | memcpy(item2, tmp, length); |
| 162 | } |
| 163 | |
| 164 | void sleep_ms(unsigned int milliseconds); |
| 165 | |
| 166 | #if __STDC_VERSION__ >= 201112L |
| 167 | #include <time.h> |
| 168 | bool timespec_bigger(const struct timespec* ts1, const struct timespec* ts2); |
| 169 | bool timespec_equal(const struct timespec* ts1, const struct timespec* ts2); |
| 170 | bool timespec_smaller(const struct timespec* ts1, const struct timespec* ts2); |
| 171 | /* subtracts ts2 from ts1, if ts2 > ts1 then zero is returned */ |
| 172 | struct timespec timespec_diff(const struct timespec* ts1, const struct timespec* ts2); |
| 173 | struct timespec timespec_add(const struct timespec* ts_1, const struct timespec* ts_2); |
| 174 | HEDLEY_INLINE |
| 175 | static uintmax_t timespec_ms(const struct timespec* ts) |
| 176 | { |
| 177 | return (uintmax_t)ts->tv_sec * 1000 + (uintmax_t)ts->tv_nsec/1000000; |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | #endif /* CUTILS_MISC_H */ |
| 182 |