--- rxvt-unicode/src/rxvtutil.h 2006/01/31 16:57:35 1.26 +++ rxvt-unicode/src/rxvtutil.h 2008/07/21 16:51:31 1.37 @@ -4,26 +4,32 @@ #include #include +using namespace std; + #define PP_CONCAT_(a, b) a ## b #define PP_CONCAT(a, b) PP_CONCAT_(a, b) #define PP_STRINGIFY_(a) #a #define PP_STRINGIFY(a) PP_STRINGIFY_(a) -// actually, some gcc-3.x versions work, too -#define HAVE_GCC_BUILTINS (__GNUC__ >= 4) +#define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) -#ifndef __attribute__ -# if __GNUC__ -# if (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || (__GNUC__ < 2) -# define __attribute__(x) -# endif -# endif -# define __attribute__(x) +#if __GNUC__ >= 4 +# define rxvt_attribute(x) __attribute__(x) +# define expect(expr,value) __builtin_expect ((expr),(value)) +#else +# define rxvt_attribute(x) +# define expect(expr,value) (expr) #endif -#define NORETURN __attribute__ ((noreturn)) -#define UNUSED __attribute__ ((unused)) -#define CONST __attribute__ ((const)) +// put into ifs if you are very sure that the expression +// is mostly true or mosty false. note that these return +// booleans, not the expression. +#define expect_false(expr) expect ((expr) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) + +#define NORETURN rxvt_attribute ((noreturn)) +#define UNUSED rxvt_attribute ((unused)) +#define CONST rxvt_attribute ((const)) // increases code size unless -fno-enforce-eh-specs #if __GNUC__ @@ -66,10 +72,29 @@ return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; } +template +I find (I first, I last, const T& value) +{ + while (first != last && *first != value) + ++first; + + return first; +} + +// return a very temporary (and never deallocated) buffer. keep small. +void *rxvt_temp_buf (int len); + +template +static inline T * +rxvt_temp_buf (int len) +{ + return (T *)rxvt_temp_buf (len * sizeof (T)); +} + // some bit functions, xft fuck me plenty #if HAVE_GCC_BUILTINS -static inline int ctz (unsigned int x) CONST { return __builtin_ctz (x); } -static inline int popcount (unsigned int x) CONST { return __builtin_popcount (x); } +static inline int ctz (unsigned int x) { return __builtin_ctz (x); } +static inline int popcount (unsigned int x) { return __builtin_popcount (x); } #else // count trailing zero bits and count # of one bits int ctz (unsigned int x) CONST; @@ -84,6 +109,9 @@ #define IN_RANGE_EXC(val,beg,end) \ ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) +// for m >= -n, ensure remainder lies between 0..n-1 +#define MOD(m,n) (((m) + (n)) % (n)) + // makes dynamically allocated objects zero-initialised struct zero_initialized { void *operator new (size_t s); @@ -91,7 +119,7 @@ }; /* simplevec taken (and heavily modified), from: - * + * * MICO --- a free CORBA implementation * Copyright (C) 1997-98 Kay Roemer & Arno Puder */ @@ -347,7 +375,17 @@ template struct vector : simplevec -{ }; +{ +}; + +struct stringvec : simplevec +{ + ~stringvec () + { + for (char **c = begin (); c != end (); c++) + free (*c); + } +}; #if 0 template @@ -365,15 +403,6 @@ }; #endif -template -I find (I first, I last, const T& value) -{ - while (first != last && *first != value) - ++first; - - return first; -} - template struct auto_ptr { T *p; @@ -434,24 +463,5 @@ typedef auto_ptr auto_str; -struct stringvec : simplevec -{ - ~stringvec () - { - for (char **c = begin (); c != end (); c++) - free (*c); - } -}; - -// return a very temporary (and never deallocated) buffer. keep small. -void *rxvt_temp_buf (int len); - -template -static inline T * -rxvt_temp_buf (int len) -{ - return (T *)rxvt_temp_buf (len * sizeof (T)); -} - #endif