ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
(Generate patch)

Comparing deliantra/server/include/util.h (file contents):
Revision 1.10 by root, Tue Sep 12 19:20:08 2006 UTC vs.
Revision 1.25 by root, Sat Dec 30 10:16:10 2006 UTC

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#include <new>
12#include <vector>
13
14#include <glib.h>
15
16#include <shstr.h>
17#include <traits.h>
18
19// use a gcc extension for auto declarations until ISO C++ sanctifies them
20#define AUTODECL(var,expr) typeof(expr) var = (expr)
21
10// makes dynamically allocated objects zero-initialised 22// makes dynamically allocated objects zero-initialised
11struct zero_initialised 23struct zero_initialised
12{ 24{
13 void *operator new (size_t s, void *); 25 void *operator new (size_t s, void *p)
26 {
27 memset (p, 0, s);
28 return p;
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 new [] (size_t s); 36 void *operator new[] (size_t s)
37 {
38 return g_slice_alloc0 (s);
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
17 void operator delete [] (void *p, size_t s); 46 void operator delete[] (void *p, size_t s)
47 {
48 g_slice_free1 (s, p);
49 }
18}; 50};
19 51
20struct refcounted 52void *salloc_ (int n) throw (std::bad_alloc);
53void *salloc_ (int n, void *src) throw (std::bad_alloc);
54
55// strictly the same as g_slice_alloc, but never returns 0
56template<typename T>
57inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
58
59// also copies src into the new area, like "memdup"
60// if src is 0, clears the memory
61template<typename T>
62inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
63
64// clears the memory
65template<typename T>
66inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
67
68// for symmetry
69template<typename T>
70inline void sfree (T *ptr, int n = 1) throw ()
21{ 71{
22 mutable int refcnt; 72 g_slice_free1 (n * sizeof (T), (void *)ptr);
23 refcounted () : refcnt (0) { } 73}
24 void refcnt_inc () { ++refcnt; } 74
25 void refcnt_dec () { --refcnt; 75// a STL-compatible allocator that uses g_slice
26 if (refcnt < 0)abort();}//D 76// boy, this is verbose
77template<typename Tp>
78struct slice_allocator
79{
80 typedef size_t size_type;
81 typedef ptrdiff_t difference_type;
82 typedef Tp *pointer;
83 typedef const Tp *const_pointer;
84 typedef Tp &reference;
85 typedef const Tp &const_reference;
86 typedef Tp value_type;
87
88 template <class U>
89 struct rebind
90 {
91 typedef slice_allocator<U> other;
92 };
93
94 slice_allocator () throw () { }
95 slice_allocator (const slice_allocator &o) throw () { }
96 template<typename Tp2>
97 slice_allocator (const slice_allocator<Tp2> &) throw () { }
98
99 ~slice_allocator () { }
100
101 pointer address (reference x) const { return &x; }
102 const_pointer address (const_reference x) const { return &x; }
103
104 pointer allocate (size_type n, const_pointer = 0)
105 {
106 return salloc<Tp> (n);
107 }
108
109 void deallocate (pointer p, size_type n)
110 {
111 sfree<Tp> (p, n);
112 }
113
114 size_type max_size ()const throw ()
115 {
116 return size_t (-1) / sizeof (Tp);
117 }
118
119 void construct (pointer p, const Tp &val)
120 {
121 ::new (p) Tp (val);
122 }
123
124 void destroy (pointer p)
125 {
126 p->~Tp ();
127 }
27}; 128};
28 129
29template<class T> 130template<class T>
30struct refptr 131struct refptr
31{ 132{
54 T &operator * () const { return *p; } 155 T &operator * () const { return *p; }
55 T *operator ->() const { return p; } 156 T *operator ->() const { return p; }
56 157
57 operator T *() const { return p; } 158 operator T *() const { return p; }
58}; 159};
160
161typedef refptr<maptile> maptile_ptr;
162typedef refptr<object> object_ptr;
163typedef refptr<archetype> arch_ptr;
164typedef refptr<client> client_ptr;
165typedef refptr<player> player_ptr;
59 166
60struct str_hash 167struct str_hash
61{ 168{
62 std::size_t operator ()(const char *s) const 169 std::size_t operator ()(const char *s) const
63 { 170 {
89 { 196 {
90 return !strcmp (a, b); 197 return !strcmp (a, b);
91 } 198 }
92}; 199};
93 200
94#include <vector>
95
96template<class obj> 201template<class obj>
97struct unordered_vector : std::vector<obj> 202struct unordered_vector : std::vector<obj, slice_allocator<obj> >
98{ 203{
99 typedef typename std::vector<obj>::iterator iterator; 204 typedef typename unordered_vector::iterator iterator;
100 205
101 void erase (unsigned int pos) 206 void erase (unsigned int pos)
102 { 207 {
103 if (pos < this->size () - 1) 208 if (pos < this->size () - 1)
104 (*this)[pos] = (*this)[this->size () - 1]; 209 (*this)[pos] = (*this)[this->size () - 1];
126inline void assign (char (&dst)[N], const char *src) 231inline void assign (char (&dst)[N], const char *src)
127{ 232{
128 assign ((char *)&dst, src, N); 233 assign ((char *)&dst, src, N);
129} 234}
130 235
236typedef double tstamp;
237
238// return current time as timestampe
239tstamp now ();
240
241int similar_direction (int a, int b);
242
131#endif 243#endif
132 244

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines