1 | #ifndef RXVT_UTIL_H |
1 | #ifndef RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
3 | |
3 | |
4 | #include <cstdlib> |
4 | #include <stdlib.h> |
5 | #include <cstring> |
5 | #include <string.h> |
6 | |
6 | #include "ecb.h" |
7 | using namespace std; |
|
|
8 | |
|
|
9 | #define ARRAY_LENGTH(v) (sizeof (v) / sizeof ((v)[0])) |
|
|
10 | |
|
|
11 | #define PP_CONCAT_(a, b) a ## b |
|
|
12 | #define PP_CONCAT(a, b) PP_CONCAT_(a, b) |
|
|
13 | #define PP_STRINGIFY_(a) #a |
|
|
14 | #define PP_STRINGIFY(a) PP_STRINGIFY_(a) |
|
|
15 | |
|
|
16 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
|
|
17 | |
|
|
18 | #if __GNUC__ >= 4 |
|
|
19 | # define rxvt_attribute(x) __attribute__(x) |
|
|
20 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
21 | #else |
|
|
22 | # define rxvt_attribute(x) |
|
|
23 | # define expect(expr,value) (expr) |
|
|
24 | #endif |
|
|
25 | |
|
|
26 | // put into ifs if you are very sure that the expression |
|
|
27 | // is mostly true or mostly false. note that these return |
|
|
28 | // booleans, not the expression. |
|
|
29 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
30 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
31 | |
|
|
32 | #define NORETURN rxvt_attribute ((noreturn)) |
|
|
33 | #define UNUSED rxvt_attribute ((unused)) |
|
|
34 | #define CONST rxvt_attribute ((const)) |
|
|
35 | |
7 | |
36 | // increases code size unless -fno-enforce-eh-specs |
8 | // increases code size unless -fno-enforce-eh-specs |
37 | #if __GNUC__ |
9 | #if __GNUC__ |
38 | # define NOTHROW |
10 | # define NOTHROW |
39 | # define THROW(x) |
11 | # define THROW(x) |
40 | #else |
12 | #else |
41 | # define NOTHROW throw() |
13 | # define NOTHROW throw() |
42 | # define THROW(x) throw x |
14 | # define THROW(x) throw x |
43 | #endif |
15 | #endif |
44 | |
16 | |
45 | extern class byteorder { |
|
|
46 | static unsigned int e; // at least 32 bits |
|
|
47 | public: |
|
|
48 | byteorder (); |
|
|
49 | |
|
|
50 | static bool big_endian () { return e == 0x11223344; }; |
|
|
51 | static bool network () { return e == 0x11223344; }; |
|
|
52 | static bool little_endian () { return e == 0x44332211; }; |
|
|
53 | static bool vax () { return e == 0x44332211; }; |
|
|
54 | } byteorder; |
|
|
55 | |
|
|
56 | // various utility functions |
17 | // various utility functions |
57 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
18 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
58 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
19 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
59 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
20 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
60 | template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } |
21 | template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } |
… | |
… | |
90 | static inline T * |
51 | static inline T * |
91 | rxvt_temp_buf (int len) |
52 | rxvt_temp_buf (int len) |
92 | { |
53 | { |
93 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
54 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
94 | } |
55 | } |
95 | |
|
|
96 | // some bit functions, xft fuck me plenty |
|
|
97 | #if HAVE_GCC_BUILTINS |
|
|
98 | /* netbsd stupidly defines popcount itself and puts it into string.h */ |
|
|
99 | static inline int rxvt_ctz (unsigned int x) { return __builtin_ctz (x); } |
|
|
100 | static inline int rxvt_popcount (unsigned int x) { return __builtin_popcount (x); } |
|
|
101 | #else |
|
|
102 | // count trailing zero bits and count # of one bits |
|
|
103 | int rxvt_ctz (unsigned int x) CONST; |
|
|
104 | int rxvt_popcount (unsigned int x) CONST; |
|
|
105 | #endif |
|
|
106 | |
56 | |
107 | // in range including end |
57 | // in range including end |
108 | #define IN_RANGE_INC(val,beg,end) \ |
58 | #define IN_RANGE_INC(val,beg,end) \ |
109 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
59 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
110 | |
60 | |