… | |
… | |
5 | # define is_constant(c) __builtin_constant_p (c) |
5 | # define is_constant(c) __builtin_constant_p (c) |
6 | #else |
6 | #else |
7 | # define is_constant(c) 0 |
7 | # define is_constant(c) 0 |
8 | #endif |
8 | #endif |
9 | |
9 | |
|
|
10 | #include <cstddef> |
|
|
11 | |
|
|
12 | #include <glib.h> |
|
|
13 | |
|
|
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
|
|
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
|
|
16 | |
10 | // makes dynamically allocated objects zero-initialised |
17 | // makes dynamically allocated objects zero-initialised |
11 | struct zero_initialised |
18 | struct zero_initialised |
12 | { |
19 | { |
|
|
20 | void *operator new (size_t s, void *p) |
|
|
21 | { |
|
|
22 | memset (p, 0, s); |
|
|
23 | return p; |
|
|
24 | } |
|
|
25 | |
13 | void *operator new (size_t s); |
26 | void *operator new (size_t s) |
|
|
27 | { |
|
|
28 | return g_slice_alloc0 (s); |
|
|
29 | } |
|
|
30 | |
14 | void *operator new [] (size_t s); |
31 | void *operator new[] (size_t s) |
|
|
32 | { |
|
|
33 | return g_slice_alloc0 (s); |
|
|
34 | } |
|
|
35 | |
15 | void operator delete (void *p, size_t s); |
36 | void operator delete (void *p, size_t s) |
|
|
37 | { |
|
|
38 | g_slice_free1 (s, p); |
|
|
39 | } |
|
|
40 | |
16 | void operator delete [] (void *p, size_t s); |
41 | void operator delete[] (void *p, size_t s) |
|
|
42 | { |
|
|
43 | g_slice_free1 (s, p); |
|
|
44 | } |
|
|
45 | }; |
|
|
46 | |
|
|
47 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
48 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
|
|
49 | |
|
|
50 | // strictly the same as g_slice_alloc, but never returns 0 |
|
|
51 | template<typename T> |
|
|
52 | inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); } |
|
|
53 | |
|
|
54 | // also copies src into the new area, like "memdup" |
|
|
55 | // if src is 0, clears the memory |
|
|
56 | template<typename T> |
|
|
57 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
58 | |
|
|
59 | // for symmetry |
|
|
60 | template<typename T> |
|
|
61 | inline void sfree (T *ptr, int n = 1) throw () |
|
|
62 | { |
|
|
63 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
64 | } |
|
|
65 | |
|
|
66 | // a STL-compatible allocator that uses g_slice |
|
|
67 | // boy, this is verbose |
|
|
68 | template<typename Tp> |
|
|
69 | struct slice_allocator |
|
|
70 | { |
|
|
71 | typedef size_t size_type; |
|
|
72 | typedef ptrdiff_t difference_type; |
|
|
73 | typedef Tp *pointer; |
|
|
74 | typedef const Tp *const_pointer; |
|
|
75 | typedef Tp &reference; |
|
|
76 | typedef const Tp &const_reference; |
|
|
77 | typedef Tp value_type; |
|
|
78 | |
|
|
79 | template <class U> |
|
|
80 | struct rebind |
|
|
81 | { |
|
|
82 | typedef slice_allocator<U> other; |
|
|
83 | }; |
|
|
84 | |
|
|
85 | slice_allocator () throw () { } |
|
|
86 | slice_allocator (const slice_allocator &o) throw () { } |
|
|
87 | template<typename Tp2> |
|
|
88 | slice_allocator (const slice_allocator<Tp2> &) throw () { } |
|
|
89 | |
|
|
90 | ~slice_allocator () { } |
|
|
91 | |
|
|
92 | pointer address (reference x) const { return &x; } |
|
|
93 | const_pointer address (const_reference x) const { return &x; } |
|
|
94 | |
|
|
95 | pointer allocate (size_type n, const_pointer = 0) |
|
|
96 | { |
|
|
97 | return salloc<Tp> (n); |
|
|
98 | } |
|
|
99 | |
|
|
100 | void deallocate (pointer p, size_type n) |
|
|
101 | { |
|
|
102 | sfree<Tp> (p, n); |
|
|
103 | } |
|
|
104 | |
|
|
105 | size_type max_size ()const throw () |
|
|
106 | { |
|
|
107 | return size_t (-1) / sizeof (Tp); |
|
|
108 | } |
|
|
109 | |
|
|
110 | void construct (pointer p, const Tp &val) |
|
|
111 | { |
|
|
112 | ::new (p) Tp (val); |
|
|
113 | } |
|
|
114 | |
|
|
115 | void destroy (pointer p) |
|
|
116 | { |
|
|
117 | p->~Tp (); |
|
|
118 | } |
|
|
119 | }; |
|
|
120 | |
|
|
121 | struct refcounted |
|
|
122 | { |
|
|
123 | refcounted () : refcnt (0) { } |
|
|
124 | // virtual ~refcounted (); |
|
|
125 | void refcnt_inc () { ++refcnt; } |
|
|
126 | void refcnt_dec () { --refcnt; } |
|
|
127 | bool dead () { return refcnt == 0; } |
|
|
128 | mutable int refcnt; |
|
|
129 | #if 0 |
|
|
130 | private: |
|
|
131 | static refcounted *rc_first; |
|
|
132 | refcounted *rc_next; |
|
|
133 | #endif |
|
|
134 | }; |
|
|
135 | |
|
|
136 | template<class T> |
|
|
137 | struct refptr |
|
|
138 | { |
|
|
139 | T *p; |
|
|
140 | |
|
|
141 | refptr () : p(0) { } |
|
|
142 | refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); } |
|
|
143 | refptr (T *p) : p(p) { if (p) p->refcnt_inc (); } |
|
|
144 | ~refptr () { if (p) p->refcnt_dec (); } |
|
|
145 | |
|
|
146 | const refptr<T> &operator =(T *o) |
|
|
147 | { |
|
|
148 | if (p) p->refcnt_dec (); |
|
|
149 | p = o; |
|
|
150 | if (p) p->refcnt_inc (); |
|
|
151 | |
|
|
152 | return *this; |
|
|
153 | } |
|
|
154 | |
|
|
155 | const refptr<T> &operator =(const refptr<T> o) |
|
|
156 | { |
|
|
157 | *this = o.p; |
|
|
158 | return *this; |
|
|
159 | } |
|
|
160 | |
|
|
161 | T &operator * () const { return *p; } |
|
|
162 | T *operator ->() const { return p; } |
|
|
163 | |
|
|
164 | operator T *() const { return p; } |
17 | }; |
165 | }; |
18 | |
166 | |
19 | struct str_hash |
167 | struct str_hash |
20 | { |
168 | { |
21 | std::size_t operator ()(const char *s) const |
169 | std::size_t operator ()(const char *s) const |
… | |
… | |
48 | { |
196 | { |
49 | return !strcmp (a, b); |
197 | return !strcmp (a, b); |
50 | } |
198 | } |
51 | }; |
199 | }; |
52 | |
200 | |
|
|
201 | #include <vector> |
|
|
202 | |
|
|
203 | template<class obj> |
|
|
204 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
|
|
205 | { |
|
|
206 | typedef typename unordered_vector::iterator iterator; |
|
|
207 | |
|
|
208 | void erase (unsigned int pos) |
|
|
209 | { |
|
|
210 | if (pos < this->size () - 1) |
|
|
211 | (*this)[pos] = (*this)[this->size () - 1]; |
|
|
212 | |
|
|
213 | this->pop_back (); |
|
|
214 | } |
|
|
215 | |
|
|
216 | void erase (iterator i) |
|
|
217 | { |
|
|
218 | erase ((unsigned int )(i - this->begin ())); |
|
|
219 | } |
|
|
220 | }; |
|
|
221 | |
|
|
222 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
223 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
224 | 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; } |
|
|
225 | |
|
|
226 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
227 | |
|
|
228 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
|
|
229 | void assign (char *dst, const char *src, int maxlen); |
|
|
230 | |
|
|
231 | // type-safe version of assign |
|
|
232 | template<int N> |
|
|
233 | inline void assign (char (&dst)[N], const char *src) |
|
|
234 | { |
|
|
235 | assign ((char *)&dst, src, N); |
|
|
236 | } |
|
|
237 | |
|
|
238 | typedef double tstamp; |
|
|
239 | |
|
|
240 | // return current time as timestampe |
|
|
241 | tstamp now (); |
|
|
242 | |
53 | #endif |
243 | #endif |
54 | |
244 | |