ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
Revision: 1.44
Committed: Fri May 11 08:00:00 2007 UTC (17 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.43: +7 -0 lines
Log Message:
- introduce a notion of cpu load average within the server
- use it to more gracefully increase swap intervals in the map-scheduler
- add clip and lerp utility functions.

File Contents

# User Rev Content
1 root 1.1 #ifndef UTIL_H__
2     #define UTIL_H__
3    
4 root 1.36 //#define PREFER_MALLOC
5    
6 root 1.2 #if __GNUC__ >= 3
7     # define is_constant(c) __builtin_constant_p (c)
8     #else
9     # define is_constant(c) 0
10     #endif
11    
12 root 1.11 #include <cstddef>
13 root 1.28 #include <cmath>
14 root 1.25 #include <new>
15     #include <vector>
16 root 1.11
17     #include <glib.h>
18    
19 root 1.25 #include <shstr.h>
20     #include <traits.h>
21    
22 root 1.14 // use a gcc extension for auto declarations until ISO C++ sanctifies them
23 root 1.40 #define auto(var,expr) typeof(expr) var = (expr)
24 root 1.14
25 root 1.26 // very ugly macro that basicaly declares and initialises a variable
26     // that is in scope for the next statement only
27     // works only for stuff that can be assigned 0 and converts to false
28     // (note: works great for pointers)
29     // most ugly macro I ever wrote
30     #define declvar(type, name, value) if (type name = 0) { } else if (((name) = (value)), 1)
31    
32 root 1.27 // in range including end
33     #define IN_RANGE_INC(val,beg,end) \
34     ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg))
35    
36     // in range excluding end
37     #define IN_RANGE_EXC(val,beg,end) \
38     ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg))
39    
40 root 1.31 void fork_abort (const char *msg);
41    
42 root 1.35 // rationale for using (U) not (T) is to reduce signed/unsigned issues,
43     // as a is often a constant while b is the variable. it is still a bug, though.
44     template<typename T, typename U> static inline T min (T a, U b) { return (U)a < b ? (U)a : b; }
45     template<typename T, typename U> static inline T max (T a, U b) { return (U)a > b ? (U)a : b; }
46     template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? (T)a : v >(T)b ? (T)b : v; }
47 root 1.32
48     template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
49    
50 root 1.44 template<typename T>
51     static inline T
52     lerp (T val, T min_in, T max_in, T min_out, T max_out)
53     {
54     return (val - min_in) * (max_out - min_out) / (max_in - min_in) + min_out;
55     }
56    
57 root 1.37 // lots of stuff taken from FXT
58    
59     /* Rotate right. This is used in various places for checksumming */
60 root 1.38 //TODO: that sucks, use a better checksum algo
61 root 1.37 static inline uint32_t
62 root 1.38 rotate_right (uint32_t c, uint32_t count = 1)
63 root 1.37 {
64 root 1.38 return (c << (32 - count)) | (c >> count);
65     }
66    
67     static inline uint32_t
68     rotate_left (uint32_t c, uint32_t count = 1)
69     {
70     return (c >> (32 - count)) | (c << count);
71 root 1.37 }
72    
73     // Return abs(a-b)
74     // Both a and b must not have the most significant bit set
75     static inline uint32_t
76     upos_abs_diff (uint32_t a, uint32_t b)
77     {
78     long d1 = b - a;
79     long d2 = (d1 & (d1 >> 31)) << 1;
80    
81     return d1 - d2; // == (b - d) - (a + d);
82     }
83    
84     // Both a and b must not have the most significant bit set
85     static inline uint32_t
86     upos_min (uint32_t a, uint32_t b)
87     {
88     int32_t d = b - a;
89     d &= d >> 31;
90     return a + d;
91     }
92    
93     // Both a and b must not have the most significant bit set
94     static inline uint32_t
95     upos_max (uint32_t a, uint32_t b)
96     {
97     int32_t d = b - a;
98     d &= d >> 31;
99     return b - d;
100     }
101    
102 root 1.28 // this is much faster than crossfires original algorithm
103     // on modern cpus
104     inline int
105     isqrt (int n)
106     {
107     return (int)sqrtf ((float)n);
108     }
109    
110     // this is only twice as fast as naive sqrtf (dx*dy+dy*dy)
111     #if 0
112     // and has a max. error of 6 in the range -100..+100.
113     #else
114     // and has a max. error of 9 in the range -100..+100.
115     #endif
116     inline int
117     idistance (int dx, int dy)
118     {
119     unsigned int dx_ = abs (dx);
120     unsigned int dy_ = abs (dy);
121    
122     #if 0
123     return dx_ > dy_
124     ? (dx_ * 61685 + dy_ * 26870) >> 16
125     : (dy_ * 61685 + dx_ * 26870) >> 16;
126     #else
127 root 1.30 return dx_ + dy_ - min (dx_, dy_) * 5 / 8;
128 root 1.28 #endif
129     }
130    
131 root 1.29 /*
132     * absdir(int): Returns a number between 1 and 8, which represent
133     * the "absolute" direction of a number (it actually takes care of
134     * "overflow" in previous calculations of a direction).
135     */
136     inline int
137     absdir (int d)
138     {
139     return ((d - 1) & 7) + 1;
140     }
141 root 1.28
142 root 1.1 // makes dynamically allocated objects zero-initialised
143     struct zero_initialised
144     {
145 root 1.11 void *operator new (size_t s, void *p)
146     {
147     memset (p, 0, s);
148     return p;
149     }
150    
151     void *operator new (size_t s)
152     {
153     return g_slice_alloc0 (s);
154     }
155    
156     void *operator new[] (size_t s)
157     {
158     return g_slice_alloc0 (s);
159     }
160    
161     void operator delete (void *p, size_t s)
162     {
163     g_slice_free1 (s, p);
164     }
165    
166     void operator delete[] (void *p, size_t s)
167     {
168     g_slice_free1 (s, p);
169     }
170     };
171    
172 root 1.20 void *salloc_ (int n) throw (std::bad_alloc);
173     void *salloc_ (int n, void *src) throw (std::bad_alloc);
174    
175 root 1.12 // strictly the same as g_slice_alloc, but never returns 0
176 root 1.20 template<typename T>
177     inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
178    
179 root 1.17 // also copies src into the new area, like "memdup"
180 root 1.18 // if src is 0, clears the memory
181     template<typename T>
182 root 1.20 inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
183 root 1.18
184 root 1.21 // clears the memory
185     template<typename T>
186     inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
187    
188 root 1.12 // for symmetry
189 root 1.18 template<typename T>
190 root 1.20 inline void sfree (T *ptr, int n = 1) throw ()
191 root 1.12 {
192 root 1.36 #ifdef PREFER_MALLOC
193     free (ptr);
194     #else
195 root 1.20 g_slice_free1 (n * sizeof (T), (void *)ptr);
196 root 1.36 #endif
197 root 1.12 }
198 root 1.11
199     // a STL-compatible allocator that uses g_slice
200     // boy, this is verbose
201     template<typename Tp>
202     struct slice_allocator
203     {
204     typedef size_t size_type;
205     typedef ptrdiff_t difference_type;
206     typedef Tp *pointer;
207     typedef const Tp *const_pointer;
208     typedef Tp &reference;
209     typedef const Tp &const_reference;
210     typedef Tp value_type;
211    
212     template <class U>
213     struct rebind
214     {
215     typedef slice_allocator<U> other;
216     };
217    
218     slice_allocator () throw () { }
219     slice_allocator (const slice_allocator &o) throw () { }
220     template<typename Tp2>
221     slice_allocator (const slice_allocator<Tp2> &) throw () { }
222    
223     ~slice_allocator () { }
224    
225     pointer address (reference x) const { return &x; }
226     const_pointer address (const_reference x) const { return &x; }
227    
228     pointer allocate (size_type n, const_pointer = 0)
229     {
230 root 1.18 return salloc<Tp> (n);
231 root 1.11 }
232    
233     void deallocate (pointer p, size_type n)
234     {
235 root 1.19 sfree<Tp> (p, n);
236 root 1.11 }
237    
238     size_type max_size ()const throw ()
239     {
240     return size_t (-1) / sizeof (Tp);
241     }
242    
243     void construct (pointer p, const Tp &val)
244     {
245     ::new (p) Tp (val);
246     }
247    
248     void destroy (pointer p)
249     {
250     p->~Tp ();
251     }
252 root 1.1 };
253    
254 root 1.32 // P. L'Ecuyer, “Maximally Equidistributed Combined Tausworthe Generators”, Mathematics of Computation, 65, 213 (1996), 203–213.
255     // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
256     // http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
257     struct tausworthe_random_generator
258     {
259 root 1.34 // generator
260 root 1.32 uint32_t state [4];
261    
262 root 1.34 void operator =(const tausworthe_random_generator &src)
263     {
264     state [0] = src.state [0];
265     state [1] = src.state [1];
266     state [2] = src.state [2];
267     state [3] = src.state [3];
268     }
269    
270     void seed (uint32_t seed);
271 root 1.32 uint32_t next ();
272    
273 root 1.34 // uniform distribution
274 root 1.42 uint32_t operator ()(uint32_t num)
275 root 1.32 {
276 root 1.42 return is_constant (num)
277     ? (next () * (uint64_t)num) >> 32U
278     : get_range (num);
279 root 1.32 }
280    
281     // return a number within (min .. max)
282     int operator () (int r_min, int r_max)
283     {
284 root 1.42 return is_constant (r_min) && is_constant (r_max) && r_min <= r_max
285     ? r_min + operator ()(r_max - r_min + 1)
286 root 1.34 : get_range (r_min, r_max);
287 root 1.32 }
288    
289     double operator ()()
290     {
291 root 1.34 return this->next () / (double)0xFFFFFFFFU;
292 root 1.32 }
293 root 1.34
294     protected:
295     uint32_t get_range (uint32_t r_max);
296     int get_range (int r_min, int r_max);
297 root 1.32 };
298    
299     typedef tausworthe_random_generator rand_gen;
300    
301     extern rand_gen rndm;
302    
303 root 1.7 template<class T>
304     struct refptr
305     {
306     T *p;
307    
308     refptr () : p(0) { }
309     refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); }
310     refptr (T *p) : p(p) { if (p) p->refcnt_inc (); }
311     ~refptr () { if (p) p->refcnt_dec (); }
312    
313     const refptr<T> &operator =(T *o)
314     {
315     if (p) p->refcnt_dec ();
316     p = o;
317     if (p) p->refcnt_inc ();
318    
319     return *this;
320     }
321    
322     const refptr<T> &operator =(const refptr<T> o)
323     {
324     *this = o.p;
325     return *this;
326     }
327    
328     T &operator * () const { return *p; }
329     T *operator ->() const { return p; }
330    
331     operator T *() const { return p; }
332     };
333    
334 root 1.24 typedef refptr<maptile> maptile_ptr;
335 root 1.22 typedef refptr<object> object_ptr;
336     typedef refptr<archetype> arch_ptr;
337 root 1.24 typedef refptr<client> client_ptr;
338     typedef refptr<player> player_ptr;
339 root 1.22
340 root 1.4 struct str_hash
341     {
342     std::size_t operator ()(const char *s) const
343     {
344     unsigned long hash = 0;
345    
346     /* use the one-at-a-time hash function, which supposedly is
347     * better than the djb2-like one used by perl5.005, but
348     * certainly is better then the bug used here before.
349     * see http://burtleburtle.net/bob/hash/doobs.html
350     */
351     while (*s)
352     {
353     hash += *s++;
354     hash += hash << 10;
355     hash ^= hash >> 6;
356     }
357    
358     hash += hash << 3;
359     hash ^= hash >> 11;
360     hash += hash << 15;
361    
362     return hash;
363     }
364     };
365    
366     struct str_equal
367     {
368     bool operator ()(const char *a, const char *b) const
369     {
370     return !strcmp (a, b);
371     }
372     };
373    
374 root 1.26 template<class T>
375     struct unordered_vector : std::vector<T, slice_allocator<T> >
376 root 1.6 {
377 root 1.11 typedef typename unordered_vector::iterator iterator;
378 root 1.6
379     void erase (unsigned int pos)
380     {
381     if (pos < this->size () - 1)
382     (*this)[pos] = (*this)[this->size () - 1];
383    
384     this->pop_back ();
385     }
386    
387     void erase (iterator i)
388     {
389     erase ((unsigned int )(i - this->begin ()));
390     }
391     };
392    
393 root 1.26 template<class T, int T::* index>
394     struct object_vector : std::vector<T *, slice_allocator<T *> >
395     {
396     void insert (T *obj)
397     {
398     assert (!(obj->*index));
399     push_back (obj);
400     obj->*index = this->size ();
401     }
402    
403     void insert (T &obj)
404     {
405     insert (&obj);
406     }
407    
408     void erase (T *obj)
409     {
410     assert (obj->*index);
411 pippijn 1.39 unsigned int pos = obj->*index;
412 root 1.26 obj->*index = 0;
413    
414     if (pos < this->size ())
415     {
416     (*this)[pos - 1] = (*this)[this->size () - 1];
417     (*this)[pos - 1]->*index = pos;
418     }
419    
420     this->pop_back ();
421     }
422    
423     void erase (T &obj)
424     {
425     errase (&obj);
426     }
427     };
428    
429 root 1.10 // basically does what strncpy should do, but appends "..." to strings exceeding length
430     void assign (char *dst, const char *src, int maxlen);
431    
432     // type-safe version of assign
433 root 1.9 template<int N>
434     inline void assign (char (&dst)[N], const char *src)
435     {
436 root 1.10 assign ((char *)&dst, src, N);
437 root 1.9 }
438    
439 root 1.17 typedef double tstamp;
440    
441     // return current time as timestampe
442     tstamp now ();
443    
444 root 1.25 int similar_direction (int a, int b);
445    
446 root 1.43 // like printf, but returns a std::string
447     const std::string format (const char *format, ...);
448    
449 root 1.1 #endif
450