1 | #ifndef RXVT_UTIL_H |
1 | #ifndef RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
3 | |
3 | |
|
|
4 | #include <new> |
4 | #include <cstdlib> |
5 | #include <stdlib.h> |
5 | #include <cstring> |
6 | #include <string.h> |
6 | #include <inttypes.h> |
7 | #include "ecb.h" |
7 | |
8 | #include "estl.h" |
8 | using namespace std; |
|
|
9 | |
|
|
10 | #define ARRAY_LENGTH(v) (sizeof (v) / sizeof ((v)[0])) |
|
|
11 | |
|
|
12 | #define PP_CONCAT_(a, b) a ## b |
|
|
13 | #define PP_CONCAT(a, b) PP_CONCAT_(a, b) |
|
|
14 | #define PP_STRINGIFY_(a) #a |
|
|
15 | #define PP_STRINGIFY(a) PP_STRINGIFY_(a) |
|
|
16 | |
|
|
17 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
|
|
18 | |
|
|
19 | #if __GNUC__ >= 4 |
|
|
20 | # define rxvt_attribute(x) __attribute__(x) |
|
|
21 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
22 | #else |
|
|
23 | # define rxvt_attribute(x) |
|
|
24 | # define expect(expr,value) (expr) |
|
|
25 | #endif |
|
|
26 | |
|
|
27 | // put into ifs if you are very sure that the expression |
|
|
28 | // is mostly true or mostly false. note that these return |
|
|
29 | // booleans, not the expression. |
|
|
30 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
31 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
32 | |
|
|
33 | #define NORETURN rxvt_attribute ((noreturn)) |
|
|
34 | #define UNUSED rxvt_attribute ((unused)) |
|
|
35 | #define CONST rxvt_attribute ((const)) |
|
|
36 | |
9 | |
37 | // increases code size unless -fno-enforce-eh-specs |
10 | // increases code size unless -fno-enforce-eh-specs |
38 | #if __GNUC__ |
11 | #if __GNUC__ |
39 | # define NOTHROW |
12 | # define NOTHROW |
40 | # define THROW(x) |
13 | # define THROW(x) |
41 | #else |
14 | #else |
42 | # define NOTHROW throw() |
15 | # define NOTHROW throw() |
43 | # define THROW(x) throw x |
16 | # define THROW(x) throw x |
44 | #endif |
17 | #endif |
45 | |
18 | |
46 | namespace byteorder { |
|
|
47 | static unsigned char e () |
|
|
48 | { |
|
|
49 | const uint32_t u = 0x11223344; |
|
|
50 | return *(unsigned char *)u; |
|
|
51 | } |
|
|
52 | |
|
|
53 | static bool big_endian () { return e () == 0x11; }; |
|
|
54 | static bool network () { return big_endian (); }; |
|
|
55 | static bool little_endian () { return e () == 0x44; }; |
|
|
56 | static bool vax () { return little_endian (); }; |
|
|
57 | }; |
|
|
58 | |
|
|
59 | // various utility functions |
19 | // various utility functions |
60 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
61 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
20 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
62 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
63 | 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; } |
64 | |
22 | |
65 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
23 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
66 | template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = v < (T)a ? a : v >(T)b ? b : v; } |
24 | template<typename T, typename U, typename V> static inline void clamp_it (T &v, U a, V b) { v = v < (T)a ? a : v >(T)b ? b : v; } |
67 | |
25 | |
68 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
69 | |
|
|
70 | template<typename T> static inline T squared_diff (T a, T b) { return (a-b)*(a-b); } |
26 | template<typename T> static inline T squared_diff (T a, T b) { return (a - b) * (a - b); } |
71 | |
27 | |
72 | // linear interpolation |
28 | // linear interpolation |
73 | template<typename T, typename U, typename P> |
29 | template<typename T, typename U, typename P> |
74 | static inline |
30 | static inline T |
75 | T lerp (T a, U b, P p) |
31 | lerp (T a, U b, P p) |
76 | { |
32 | { |
77 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
33 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
78 | } |
|
|
79 | |
|
|
80 | template <typename I, typename T> |
|
|
81 | I find (I first, I last, const T& value) |
|
|
82 | { |
|
|
83 | while (first != last && *first != value) |
|
|
84 | ++first; |
|
|
85 | |
|
|
86 | return first; |
|
|
87 | } |
34 | } |
88 | |
35 | |
89 | // return a very temporary (and never deallocated) buffer. keep small. |
36 | // return a very temporary (and never deallocated) buffer. keep small. |
90 | void *rxvt_temp_buf (int len); |
37 | void *rxvt_temp_buf (int len); |
91 | |
38 | |
… | |
… | |
93 | static inline T * |
40 | static inline T * |
94 | rxvt_temp_buf (int len) |
41 | rxvt_temp_buf (int len) |
95 | { |
42 | { |
96 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
43 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
97 | } |
44 | } |
98 | |
|
|
99 | // some bit functions, xft fuck me plenty |
|
|
100 | #if HAVE_GCC_BUILTINS |
|
|
101 | /* netbsd stupidly defines popcount itself and puts it into string.h */ |
|
|
102 | static inline int rxvt_ctz (unsigned int x) { return __builtin_ctz (x); } |
|
|
103 | static inline int rxvt_popcount (unsigned int x) { return __builtin_popcount (x); } |
|
|
104 | #else |
|
|
105 | // count trailing zero bits and count # of one bits |
|
|
106 | int rxvt_ctz (unsigned int x) CONST; |
|
|
107 | int rxvt_popcount (unsigned int x) CONST; |
|
|
108 | #endif |
|
|
109 | |
45 | |
110 | // in range including end |
46 | // in range including end |
111 | #define IN_RANGE_INC(val,beg,end) \ |
47 | #define IN_RANGE_INC(val,beg,end) \ |
112 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
48 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
113 | |
49 | |
… | |
… | |
121 | // makes dynamically allocated objects zero-initialised |
57 | // makes dynamically allocated objects zero-initialised |
122 | struct zero_initialized |
58 | struct zero_initialized |
123 | { |
59 | { |
124 | void *operator new (size_t s); |
60 | void *operator new (size_t s); |
125 | void operator delete (void *p, size_t s); |
61 | void operator delete (void *p, size_t s); |
126 | }; |
|
|
127 | |
|
|
128 | /* simplevec taken (and heavily modified), from: |
|
|
129 | * |
|
|
130 | * MICO --- a free CORBA implementation |
|
|
131 | * Copyright (C) 1997-98 Kay Roemer & Arno Puder |
|
|
132 | */ |
|
|
133 | template<class T> |
|
|
134 | struct simplevec |
|
|
135 | { |
|
|
136 | typedef T* iterator; |
|
|
137 | typedef const T* const_iterator; |
|
|
138 | typedef unsigned long size_type; |
|
|
139 | |
|
|
140 | private: |
|
|
141 | size_type _last, _size; |
|
|
142 | T *_buf; |
|
|
143 | |
|
|
144 | public: |
|
|
145 | const_iterator begin () const |
|
|
146 | { |
|
|
147 | return &_buf[0]; |
|
|
148 | } |
|
|
149 | iterator begin () |
|
|
150 | { |
|
|
151 | return &_buf[0]; |
|
|
152 | } |
|
|
153 | const_iterator end () const |
|
|
154 | { |
|
|
155 | return &_buf[_last]; |
|
|
156 | } |
|
|
157 | iterator end () |
|
|
158 | { |
|
|
159 | return &_buf[_last]; |
|
|
160 | } |
|
|
161 | size_type capacity () const |
|
|
162 | { |
|
|
163 | return _size; |
|
|
164 | } |
|
|
165 | size_type size () const |
|
|
166 | { |
|
|
167 | return _last; |
|
|
168 | } |
|
|
169 | |
|
|
170 | private: |
|
|
171 | static T *alloc (size_type n) |
|
|
172 | { |
|
|
173 | return (T *)::operator new ((size_t) (n * sizeof (T))); |
|
|
174 | } |
|
|
175 | static void dealloc (T *buf) |
|
|
176 | { |
|
|
177 | if (buf) |
|
|
178 | ::operator delete (buf); |
|
|
179 | } |
|
|
180 | |
|
|
181 | void reserve (iterator where, size_type n) |
|
|
182 | { |
|
|
183 | if (_last + n <= _size) { |
|
|
184 | memmove (where+n, where, (end ()-where)*sizeof (T)); |
|
|
185 | } else { |
|
|
186 | size_type sz = _last+n; |
|
|
187 | sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); |
|
|
188 | T *nbuf = alloc (sz); |
|
|
189 | if (_buf) { |
|
|
190 | memcpy (nbuf, begin (), (where-begin ())*sizeof (T)); |
|
|
191 | memcpy (nbuf + (where-begin ()) + n, where, |
|
|
192 | (end ()-where)*sizeof (T)); |
|
|
193 | dealloc (_buf); |
|
|
194 | } |
|
|
195 | _buf = nbuf; |
|
|
196 | _size = sz; |
|
|
197 | } |
|
|
198 | } |
|
|
199 | |
|
|
200 | public: |
|
|
201 | void reserve (size_type sz) |
|
|
202 | { |
|
|
203 | if (_size < sz) { |
|
|
204 | sz = (_size == 0) ? max (sz, 5) : max (sz, 2*_size); |
|
|
205 | T *nbuf = alloc (sz); |
|
|
206 | if (_buf) { |
|
|
207 | memcpy (nbuf, begin (), size ()*sizeof (T)); |
|
|
208 | dealloc (_buf); |
|
|
209 | } |
|
|
210 | _buf = nbuf; |
|
|
211 | _size = sz; |
|
|
212 | } |
|
|
213 | } |
|
|
214 | simplevec () |
|
|
215 | : _last(0), _size(0), _buf(0) |
|
|
216 | { |
|
|
217 | } |
|
|
218 | simplevec (size_type n, const T& t = T ()) |
|
|
219 | : _last(0), _size(0), _buf(0) |
|
|
220 | { |
|
|
221 | insert (begin (), n, t); |
|
|
222 | } |
|
|
223 | simplevec (const_iterator first, const_iterator last) |
|
|
224 | : _last(0), _size(0), _buf(0) |
|
|
225 | { |
|
|
226 | insert (begin (), first, last); |
|
|
227 | } |
|
|
228 | simplevec (const simplevec<T> &v) |
|
|
229 | : _last(0), _size(0), _buf(0) |
|
|
230 | { |
|
|
231 | reserve (v._last); |
|
|
232 | memcpy (_buf, v.begin (), v.size ()*sizeof (T)); |
|
|
233 | _last = v._last; |
|
|
234 | } |
|
|
235 | simplevec<T> &operator= (const simplevec<T> &v) |
|
|
236 | { |
|
|
237 | if (this != &v) { |
|
|
238 | _last = 0; |
|
|
239 | reserve (v._last); |
|
|
240 | memcpy (_buf, v.begin (), v.size ()*sizeof (T)); |
|
|
241 | _last = v._last; |
|
|
242 | } |
|
|
243 | return *this; |
|
|
244 | } |
|
|
245 | ~simplevec () |
|
|
246 | { |
|
|
247 | dealloc (_buf); |
|
|
248 | } |
|
|
249 | const T &front () const |
|
|
250 | { |
|
|
251 | //ministl_assert (size () > 0); |
|
|
252 | return _buf[0]; |
|
|
253 | } |
|
|
254 | T &front () |
|
|
255 | { |
|
|
256 | //ministl_assert (size () > 0); |
|
|
257 | return _buf[0]; |
|
|
258 | } |
|
|
259 | const T &back () const |
|
|
260 | { |
|
|
261 | //ministl_assert (size () > 0); |
|
|
262 | return _buf[_last-1]; |
|
|
263 | } |
|
|
264 | T &back () |
|
|
265 | { |
|
|
266 | //ministl_assert (size () > 0); |
|
|
267 | return _buf[_last-1]; |
|
|
268 | } |
|
|
269 | bool empty () const |
|
|
270 | { |
|
|
271 | return _last == 0; |
|
|
272 | } |
|
|
273 | void clear () |
|
|
274 | { |
|
|
275 | _last = 0; |
|
|
276 | } |
|
|
277 | void push_back (const T &t) |
|
|
278 | { |
|
|
279 | reserve (_last+1); |
|
|
280 | *end () = t; |
|
|
281 | ++_last; |
|
|
282 | } |
|
|
283 | void push_back (T &t) |
|
|
284 | { |
|
|
285 | reserve (_last+1); |
|
|
286 | *end () = t; |
|
|
287 | ++_last; |
|
|
288 | } |
|
|
289 | void pop_back () |
|
|
290 | { |
|
|
291 | //ministl_assert (size () > 0); |
|
|
292 | --_last; |
|
|
293 | } |
|
|
294 | const T &operator[] (size_type idx) const |
|
|
295 | { |
|
|
296 | //ministl_assert (idx < size ()); |
|
|
297 | return _buf[idx]; |
|
|
298 | } |
|
|
299 | T &operator[] (size_type idx) |
|
|
300 | { |
|
|
301 | //ministl_assert (idx < size ()); |
|
|
302 | return _buf[idx]; |
|
|
303 | } |
|
|
304 | iterator insert (iterator pos, const T &t) |
|
|
305 | { |
|
|
306 | //ministl_assert (pos <= end ()); |
|
|
307 | long at = pos - begin (); |
|
|
308 | reserve (pos, 1); |
|
|
309 | pos = begin ()+at; |
|
|
310 | *pos = t; |
|
|
311 | ++_last; |
|
|
312 | return pos; |
|
|
313 | } |
|
|
314 | iterator insert (iterator pos, const_iterator first, const_iterator last) |
|
|
315 | { |
|
|
316 | //ministl_assert (pos <= end ()); |
|
|
317 | long n = last - first; |
|
|
318 | long at = pos - begin (); |
|
|
319 | if (n > 0) { |
|
|
320 | reserve (pos, n); |
|
|
321 | pos = begin ()+at; |
|
|
322 | memcpy (pos, first, (last-first)*sizeof (T)); |
|
|
323 | _last += n; |
|
|
324 | } |
|
|
325 | return pos; |
|
|
326 | } |
|
|
327 | iterator insert (iterator pos, size_type n, const T &t) |
|
|
328 | { |
|
|
329 | //ministl_assert (pos <= end ()); |
|
|
330 | long at = pos - begin (); |
|
|
331 | if (n > 0) { |
|
|
332 | reserve (pos, n); |
|
|
333 | pos = begin ()+at; |
|
|
334 | for (int i = 0; i < n; ++i) |
|
|
335 | pos[i] = t; |
|
|
336 | _last += n; |
|
|
337 | } |
|
|
338 | return pos; |
|
|
339 | } |
|
|
340 | void erase (iterator first, iterator last) |
|
|
341 | { |
|
|
342 | if (last != first) { |
|
|
343 | memmove (first, last, (end () - last) * sizeof (T)); |
|
|
344 | _last -= last - first; |
|
|
345 | } |
|
|
346 | } |
|
|
347 | void erase (iterator pos) |
|
|
348 | { |
|
|
349 | if (pos != end ()) { |
|
|
350 | memmove (pos, pos+1, (end () - (pos+1)) * sizeof (T)); |
|
|
351 | --_last; |
|
|
352 | } |
|
|
353 | } |
|
|
354 | void swap (simplevec<T> &t) |
|
|
355 | { |
|
|
356 | ::swap(_last, t._last); |
|
|
357 | ::swap(_size, t._size); |
|
|
358 | ::swap(_buf, t._buf); |
|
|
359 | } |
|
|
360 | }; |
|
|
361 | |
|
|
362 | template<class T> |
|
|
363 | bool operator== (const simplevec<T> &v1, const simplevec<T> &v2) |
|
|
364 | { |
|
|
365 | if (v1.size () != v2.size ()) |
|
|
366 | return false; |
|
|
367 | return !v1.size () || !memcmp (&v1[0], &v2[0], v1.size ()*sizeof (T)); |
|
|
368 | } |
|
|
369 | |
|
|
370 | template<class T> |
|
|
371 | bool operator< (const simplevec<T> &v1, const simplevec<T> &v2) |
|
|
372 | { |
|
|
373 | unsigned long minlast = min (v1.size (), v2.size ()); |
|
|
374 | for (unsigned long i = 0; i < minlast; ++i) { |
|
|
375 | if (v1[i] < v2[i]) |
|
|
376 | return true; |
|
|
377 | if (v2[i] < v1[i]) |
|
|
378 | return false; |
|
|
379 | } |
|
|
380 | return v1.size () < v2.size (); |
|
|
381 | } |
|
|
382 | |
|
|
383 | |
|
|
384 | template<typename T> |
|
|
385 | struct vector : simplevec<T> |
|
|
386 | { |
|
|
387 | }; |
62 | }; |
388 | |
63 | |
389 | struct stringvec : simplevec<char *> |
64 | struct stringvec : simplevec<char *> |
390 | { |
65 | { |
391 | ~stringvec () |
66 | ~stringvec () |
… | |
… | |
410 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
85 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
411 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
86 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
412 | }; |
87 | }; |
413 | #endif |
88 | #endif |
414 | |
89 | |
|
|
90 | inline void * |
|
|
91 | operator new (size_t size) |
|
|
92 | { |
|
|
93 | // TODO: use rxvt_malloc |
|
|
94 | return malloc (size); |
|
|
95 | } |
|
|
96 | |
|
|
97 | inline void |
|
|
98 | operator delete (void *p) |
|
|
99 | { |
|
|
100 | free (p); |
|
|
101 | } |
|
|
102 | |
415 | template<typename T> |
103 | template<typename T> |
416 | struct auto_ptr |
104 | struct auto_ptr |
417 | { |
105 | { |
418 | T *p; |
106 | T *p; |
419 | |
107 | |
420 | auto_ptr () : p (0) { } |
108 | auto_ptr () : p (0) { } |
|
|
109 | |
|
|
110 | explicit |
421 | auto_ptr (T *a) : p (a) { } |
111 | auto_ptr (T *a) : p (a) { } |
422 | |
112 | |
423 | auto_ptr (auto_ptr<T> &a) |
113 | auto_ptr (auto_ptr<T> &a) |
424 | { |
114 | { |
425 | p = a.p; |
115 | p = a.p; |
… | |
… | |
436 | ~auto_ptr () |
126 | ~auto_ptr () |
437 | { |
127 | { |
438 | delete p; |
128 | delete p; |
439 | } |
129 | } |
440 | |
130 | |
441 | // void because it makes sense in our context |
131 | void reset (T *a) |
442 | void operator = (T *a) |
|
|
443 | { |
132 | { |
444 | delete p; |
133 | delete p; |
445 | p = a; |
134 | p = a; |
446 | } |
135 | } |
447 | |
136 | |
|
|
137 | // void because it makes sense in our context |
448 | void operator = (auto_ptr &a) |
138 | void operator =(auto_ptr &a) |
449 | { |
139 | { |
450 | *this = a.p; |
140 | *this = a.p; |
451 | a.p = 0; |
141 | a.p = 0; |
452 | } |
142 | } |
453 | |
143 | |
454 | template<typename A> |
144 | template<typename A> |
455 | void operator = (auto_ptr<A> &a) |
145 | void operator =(auto_ptr<A> &a) |
456 | { |
146 | { |
457 | *this = a.p; |
147 | *this = a.p; |
458 | a.p = 0; |
148 | a.p = 0; |
459 | } |
149 | } |
460 | |
150 | |
|
|
151 | T *operator ->() const { return p; } |
461 | operator T * () const { return p; } |
152 | T &operator *() const { return *p; } |
462 | |
153 | |
|
|
154 | operator T *() { return p; } |
463 | T *operator -> () const { return p; } |
155 | T *get () const { return p; } |
464 | T &operator * () const { return *p; } |
|
|
465 | |
156 | |
466 | T *get () |
157 | T *release() |
467 | { |
158 | { |
468 | T *r = p; |
159 | T *r = p; |
469 | p = 0; |
160 | p = 0; |
470 | return r; |
161 | return r; |
471 | } |
162 | } |