… | |
… | |
13 | |
13 | |
14 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
14 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
15 | |
15 | |
16 | #if __GNUC__ >= 4 |
16 | #if __GNUC__ >= 4 |
17 | # define rxvt_attribute(x) __attribute__(x) |
17 | # define rxvt_attribute(x) __attribute__(x) |
|
|
18 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
18 | #else |
19 | #else |
19 | # define rxvt_attribute(x) |
20 | # define rxvt_attribute(x) |
|
|
21 | # define expect(expr,value) (expr) |
20 | #endif |
22 | #endif |
|
|
23 | |
|
|
24 | // put into ifs if you are very sure that the expression |
|
|
25 | // is mostly true or mostly false. note that these return |
|
|
26 | // booleans, not the expression. |
|
|
27 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
28 | #define expect_true(expr) expect ((expr) != 0, 1) |
21 | |
29 | |
22 | #define NORETURN rxvt_attribute ((noreturn)) |
30 | #define NORETURN rxvt_attribute ((noreturn)) |
23 | #define UNUSED rxvt_attribute ((unused)) |
31 | #define UNUSED rxvt_attribute ((unused)) |
24 | #define CONST rxvt_attribute ((const)) |
32 | #define CONST rxvt_attribute ((const)) |
25 | |
33 | |
… | |
… | |
83 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
91 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
84 | } |
92 | } |
85 | |
93 | |
86 | // some bit functions, xft fuck me plenty |
94 | // some bit functions, xft fuck me plenty |
87 | #if HAVE_GCC_BUILTINS |
95 | #if HAVE_GCC_BUILTINS |
|
|
96 | /* netbsd stupidly defines popcount itself and puts it into string.h */ |
88 | static inline int ctz (unsigned int x) { return __builtin_ctz (x); } |
97 | static inline int rxvt_ctz (unsigned int x) { return __builtin_ctz (x); } |
89 | static inline int popcount (unsigned int x) { return __builtin_popcount (x); } |
98 | static inline int rxvt_popcount (unsigned int x) { return __builtin_popcount (x); } |
90 | #else |
99 | #else |
91 | // count trailing zero bits and count # of one bits |
100 | // count trailing zero bits and count # of one bits |
92 | int ctz (unsigned int x) CONST; |
101 | int rxvt_ctz (unsigned int x) CONST; |
93 | int popcount (unsigned int x) CONST; |
102 | int rxvt_popcount (unsigned int x) CONST; |
94 | #endif |
103 | #endif |
95 | |
104 | |
96 | // in range including end |
105 | // in range including end |
97 | #define IN_RANGE_INC(val,beg,end) \ |
106 | #define IN_RANGE_INC(val,beg,end) \ |
98 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
107 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
99 | |
108 | |
100 | // in range excluding end |
109 | // in range excluding end |
101 | #define IN_RANGE_EXC(val,beg,end) \ |
110 | #define IN_RANGE_EXC(val,beg,end) \ |
102 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
111 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
103 | |
112 | |
|
|
113 | // for m >= -n, ensure remainder lies between 0..n-1 |
|
|
114 | #define MOD(m,n) (((m) + (n)) % (n)) |
|
|
115 | |
104 | // makes dynamically allocated objects zero-initialised |
116 | // makes dynamically allocated objects zero-initialised |
105 | struct zero_initialized { |
117 | struct zero_initialized |
|
|
118 | { |
106 | void *operator new (size_t s); |
119 | void *operator new (size_t s); |
107 | void operator delete (void *p, size_t s); |
120 | void operator delete (void *p, size_t s); |
108 | }; |
121 | }; |
109 | |
122 | |
110 | /* simplevec taken (and heavily modified), from: |
123 | /* simplevec taken (and heavily modified), from: |
111 | * |
124 | * |
112 | * MICO --- a free CORBA implementation |
125 | * MICO --- a free CORBA implementation |
113 | * Copyright (C) 1997-98 Kay Roemer & Arno Puder |
126 | * Copyright (C) 1997-98 Kay Roemer & Arno Puder |
114 | */ |
127 | */ |
115 | template<class T> |
128 | template<class T> |
116 | struct simplevec { |
129 | struct simplevec |
|
|
130 | { |
117 | typedef T* iterator; |
131 | typedef T* iterator; |
118 | typedef const T* const_iterator; |
132 | typedef const T* const_iterator; |
119 | typedef unsigned long size_type; |
133 | typedef unsigned long size_type; |
120 | |
134 | |
121 | private: |
135 | private: |
… | |
… | |
362 | } |
376 | } |
363 | |
377 | |
364 | |
378 | |
365 | template<typename T> |
379 | template<typename T> |
366 | struct vector : simplevec<T> |
380 | struct vector : simplevec<T> |
367 | { }; |
381 | { |
|
|
382 | }; |
368 | |
383 | |
369 | struct stringvec : simplevec<char *> |
384 | struct stringvec : simplevec<char *> |
370 | { |
385 | { |
371 | ~stringvec () |
386 | ~stringvec () |
372 | { |
387 | { |
… | |
… | |
375 | } |
390 | } |
376 | }; |
391 | }; |
377 | |
392 | |
378 | #if 0 |
393 | #if 0 |
379 | template<typename T> |
394 | template<typename T> |
380 | struct rxvt_vec : simplevec<void *> { |
395 | struct rxvt_vec : simplevec<void *> |
|
|
396 | { |
381 | typedef T *iterator; |
397 | typedef T *iterator; |
382 | |
398 | |
383 | void push_back (T d) { simplevec<void *>::push_back ((void *)d); } |
399 | void push_back (T d) { simplevec<void *>::push_back ((void *)d); } |
384 | T pop_back () { return (T*)simplevec<void *>::pop_back (); } |
400 | T pop_back () { return (T*)simplevec<void *>::pop_back (); } |
385 | void erase (int i) { erase (begin () + i); } |
401 | void erase (int i) { erase (begin () + i); } |
… | |
… | |
387 | iterator begin () const { return (iterator)simplevec<void *>::begin (); } |
403 | iterator begin () const { return (iterator)simplevec<void *>::begin (); } |
388 | iterator end () const { return (iterator)simplevec<void *>::end (); } |
404 | iterator end () const { return (iterator)simplevec<void *>::end (); } |
389 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
405 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
390 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
406 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
391 | }; |
407 | }; |
|
|
408 | #endif |
392 | |
409 | |
393 | template<typename T> |
410 | template<typename T> |
394 | struct auto_ptr { |
411 | struct auto_ptr |
|
|
412 | { |
395 | T *p; |
413 | T *p; |
396 | |
414 | |
397 | auto_ptr () : p (0) { } |
415 | auto_ptr () : p (0) { } |
398 | auto_ptr (T *a) : p (a) { } |
416 | auto_ptr (T *a) : p (a) { } |
399 | |
417 | |
… | |
… | |
447 | return r; |
465 | return r; |
448 | } |
466 | } |
449 | }; |
467 | }; |
450 | |
468 | |
451 | typedef auto_ptr<char> auto_str; |
469 | typedef auto_ptr<char> auto_str; |
|
|
470 | |
452 | #endif |
471 | #endif |
453 | |
472 | |
454 | #endif |
|
|
455 | |
|
|