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