1 // ===================================================================== 2 // 3 // util.h 4 // 5 // Author: Dave Freese, W1HKJ 6 // Copyright: 2010 7 // 8 // This file is part of FLCLUSTER. 9 // 10 // This is free software; you can redistribute it and/or modify 11 // it under the terms of the GNU General Public License as published by 12 // the Free Software Foundation; either version 3 of the License, or 13 // (at your option) any later version. 14 // 15 // This software is distributed in the hope that it will be useful, 16 // but WITHOUT ANY WARRANTY; without even the implied warranty of 17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 // GNU General Public License for more details. 19 // 20 // You should have received a copy of the GNU General Public License 21 // along with this program. If not, see <http://www.gnu.org/licenses/>. 22 // 23 // ===================================================================== 24 25 /* This file is included by config.h */ 26 27 #ifndef UTIL_H 28 #define UTIL_H 29 30 #include <string> 31 #include "config.h" 32 33 #ifdef __cplusplus 34 extern "C" { 35 #endif 36 37 #ifndef __STDC_FORMAT_MACROS 38 # define __STDC_FORMAT_MACROS 1 39 #endif 40 #include <inttypes.h> 41 42 #ifndef powerof2 43 # define powerof2(n) ((((n) - 1) & (n)) == 0) 44 #endif 45 #ifndef MAX 46 # define MAX(a, b) (((a) > (b)) ? (a) : (b)) 47 #endif 48 #ifndef MIN 49 # define MIN(a, b) (((a) < (b)) ? (a) : (b)) 50 #endif 51 #ifndef CLAMP 52 # define CLAMP(x, low, high) (((x)>(high))?(high):(((x)<(low))?(low):(x))) 53 #endif 54 #define WCLAMP(x, low, high) (((x)>(high))?(low):(((x)<(low))?(high):(x))) 55 56 #ifdef __GNUC__ 57 # if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) 58 # define full_memory_barrier() __sync_synchronize() 59 # define read_memory_barrier() full_memory_barrier() 60 # define write_memory_barrier() full_memory_barrier() 61 # elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(__x86_64__) 62 # define full_memory_barrier() asm volatile ("lock; addl $0,0(%%esp)":::"memory") 63 # define read_memory_barrier() full_memory_barrier() 64 # define write_memory_barrier() full_memory_barrier() 65 /* 66 These would be faster on SSE2-capable processors: 67 # define full_memory_barrier() asm volatile ("mfence":::"memory") 68 # define read_memory_barrier() asm volatile ("lfence":::"memory") 69 # define write_memory_barrier() asm volatile ("sfence":::"memory") 70 */ 71 # elif defined(__ppc__) || defined(__powerpc__) || defined(__PPC__) 72 # define full_memory_barrier() asm volatile("sync":::"memory") 73 # define read_memory_barrier() full_memory_barrier() 74 # define write_memory_barrier() full_memory_barrier() 75 # else 76 # warning Memory barriers not defined on this system 77 # define full_memory_barrier() ((void)0) 78 # define read_memory_barrier() full_memory_barrier() 79 # define write_memory_barrier() full_memory_barrier() 80 # endif 81 #else 82 # warning Memory barriers not defined on this system 83 # define full_memory_barrier() ((void)0) 84 # define read_memory_barrier() full_memory_barrier() 85 # define write_memory_barrier() full_memory_barrier() 86 #endif 87 88 /* http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html */ 89 #if defined(__GNUC__) && (__GNUC__ >= 3) 90 # define likely(x) __builtin_expect (!!(x), 1) 91 # define unlikely(x) __builtin_expect (!!(x), 0) 92 # define used__ __attribute__ ((__used__)) 93 # define unused__ __attribute__ ((__unused__)) 94 # define must_check__ __attribute__ ((__warn_unused_result__)) 95 # define deprecated__ __attribute__ ((__deprecated__)) 96 # define noreturn__ __attribute__ ((__noreturn__)) 97 # define pure__ __attribute__ ((__pure__)) 98 # define const__ __attribute__ ((__const__)) 99 # define malloc__ __attribute__ ((__malloc__)) 100 # define packed__ __attribute__ ((__packed__)) 101 # define inline__ inline __attribute__ ((__always_inline__)) 102 # define noinline__ __attribute__ ((__noinline__)) 103 # define nonnull__(x) __attribute__ ((__nonnull__(x))) 104 # define format__(type_, index_, first_) __attribute__ ((format(type_, index_, first_))) 105 #else 106 # define likely(x) (x) 107 # define unlikely(x) (x) 108 # define used__ 109 # define unused__ 110 # define must_check__ 111 # define deprecated__ 112 # define noreturn__ 113 # define pure__ 114 # define const__ 115 # define malloc__ 116 # define packed__ 117 # define inline__ 118 # define noinline__ 119 # define nonnull__(x) 120 # define format__(type_, index_, first_) 121 #endif 122 123 #if defined(__GNUC__) && ((__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) 124 # define hot__ __attribute__ ((__hot__)) 125 # define cold__ __attribute__ ((__cold__)) 126 #else 127 # define hot__ 128 # define cold__ 129 #endif 130 131 #include <stddef.h> 132 133 const__ uint32_t ceil2(uint32_t n); 134 const__ uint32_t floor2(uint32_t n); 135 136 //#if !HAVE_STRCASESTR 137 //char* strcasestr(const char* haystack, const char* needle); 138 //#endif 139 140 //#if !HAVE_STRLCPY 141 //size_t strlcpy(char* dest, const char* src, size_t size); 142 //#endif 143 144 int set_cloexec(int fd, unsigned char v); 145 int set_nonblock(int fd, unsigned char v); 146 int set_nodelay(int fd, unsigned char v); 147 int get_bufsize(int fd, int dir, int* len); 148 int set_bufsize(int fd, int dir, int len); 149 150 unsigned long ver2int(const char* version); 151 152 void save_signals(void); 153 void restore_signals(void); 154 155 void MilliSleep(long msecs); 156 157 #ifdef __cplusplus 158 } // extern "C" 159 #endif 160 161 #ifdef __cplusplus 162 uint32_t simple_hash_data(const unsigned char* buf, size_t len, uint32_t code = 0); 163 uint32_t simple_hash_str(const unsigned char* str, uint32_t code = 0); 164 #endif 165 166 #ifdef __cplusplus 167 const char* str2hex(const unsigned char* str, size_t len); 168 const char* str2hex(const char* str, size_t len = 0); 169 #else 170 const char* str2hex(const unsigned* str, size_t len); 171 #endif 172 173 const char* uint2bin(unsigned u, size_t len); 174 void MilliSleep(long msecs); 175 176 #if !defined(NDEBUG) && defined(deprecated__) && defined(__GNUC__) && !defined(__WIN32__) 177 #include <stdio.h> 178 #include <string.h> 179 deprecated__ typeof(sprintf) sprintf; 180 /* there are far too many of these in the qrz code 181 deprecated__ typeof(strcpy) strcpy; 182 deprecated__ typeof(strcat) strcat; 183 */ 184 #endif 185 186 #ifdef __WIN32__ 187 # define NOMINMAX 1 188 #endif 189 190 #ifndef __WIN32__ 191 # define PRIuSZ "zu" 192 # define PRIdSZ "zd" 193 #else 194 # define PRIuSZ "Iu" 195 # define PRIdSZ "Id" 196 #endif 197 198 # define PATH_SEP "/" 199 # define PATH_CHAR_SEP '/' 200 201 202 //====================================================================== 203 204 extern void ucase(std::string &); 205 extern void strip_spaces(std::string &); 206 extern void strip_leading_zeros(std::string &); 207 extern void strip_lfs(std::string &); 208 extern std::string wordwrap(std::string &s, int cnt); 209 210 #ifndef HAVE_STRNLEN 211 extern int strnlen (const char *c, int limit); 212 #endif 213 214 #endif /* UTIL_H */ 215 216 /* 217 Local Variables: 218 mode: c++ 219 c-file-style: "linux" 220 End: 221 */ 222