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.4 by root, Mon Sep 4 17:16:19 2006 UTC vs.
Revision 1.11 by root, Tue Sep 12 20:55:40 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
12#include <glib.h>
13
10// makes dynamically allocated objects zero-initialised 14// makes dynamically allocated objects zero-initialised
11struct zero_initialised 15struct zero_initialised
12{ 16{
17 void *operator new (size_t s, void *p)
18 {
19 memset (p, 0, s);
20 return p;
21 }
22
13 void *operator new (size_t s); 23 void *operator new (size_t s)
24 {
25 return g_slice_alloc0 (s);
26 }
27
14 void *operator new [] (size_t s); 28 void *operator new[] (size_t s)
29 {
30 return g_slice_alloc0 (s);
31 }
32
15 void operator delete (void *p, size_t s); 33 void operator delete (void *p, size_t s)
34 {
35 g_slice_free1 (s, p);
36 }
37
16 void operator delete [] (void *p, size_t s); 38 void operator delete[] (void *p, size_t s)
39 {
40 g_slice_free1 (s, p);
41 }
42};
43
44void throw_bad_alloc () throw (std::bad_alloc);
45
46void *alloc (int s) throw (std::bad_alloc);
47void dealloc (void *p, int s) throw ();
48
49// a STL-compatible allocator that uses g_slice
50// boy, this is verbose
51template<typename Tp>
52struct slice_allocator
53{
54 typedef size_t size_type;
55 typedef ptrdiff_t difference_type;
56 typedef Tp *pointer;
57 typedef const Tp *const_pointer;
58 typedef Tp &reference;
59 typedef const Tp &const_reference;
60 typedef Tp value_type;
61
62 template <class U>
63 struct rebind
64 {
65 typedef slice_allocator<U> other;
66 };
67
68 slice_allocator () throw () { }
69 slice_allocator (const slice_allocator &o) throw () { }
70 template<typename Tp2>
71 slice_allocator (const slice_allocator<Tp2> &) throw () { }
72
73 ~slice_allocator () { }
74
75 pointer address (reference x) const { return &x; }
76 const_pointer address (const_reference x) const { return &x; }
77
78 pointer allocate (size_type n, const_pointer = 0)
79 {
80 return static_cast<pointer>(alloc (n * sizeof (Tp)));
81 }
82
83 void deallocate (pointer p, size_type n)
84 {
85 dealloc (static_cast<void *>(p), n);
86 }
87
88 size_type max_size ()const throw ()
89 {
90 return size_t (-1) / sizeof (Tp);
91 }
92
93 void construct (pointer p, const Tp &val)
94 {
95 ::new (p) Tp (val);
96 }
97
98 void destroy (pointer p)
99 {
100 p->~Tp ();
101 }
102};
103
104struct refcounted
105{
106 mutable int refcnt;
107 refcounted () : refcnt (0) { }
108 void refcnt_inc () { ++refcnt; }
109 void refcnt_dec () { --refcnt;
110 if (refcnt < 0)abort();}//D
111};
112
113template<class T>
114struct refptr
115{
116 T *p;
117
118 refptr () : p(0) { }
119 refptr (const refptr<T> &p) : p(p.p) { if (p) p->refcnt_inc (); }
120 refptr (T *p) : p(p) { if (p) p->refcnt_inc (); }
121 ~refptr () { if (p) p->refcnt_dec (); }
122
123 const refptr<T> &operator =(T *o)
124 {
125 if (p) p->refcnt_dec ();
126 p = o;
127 if (p) p->refcnt_inc ();
128
129 return *this;
130 }
131
132 const refptr<T> &operator =(const refptr<T> o)
133 {
134 *this = o.p;
135 return *this;
136 }
137
138 T &operator * () const { return *p; }
139 T *operator ->() const { return p; }
140
141 operator T *() const { return p; }
17}; 142};
18 143
19struct str_hash 144struct str_hash
20{ 145{
21 std::size_t operator ()(const char *s) const 146 std::size_t operator ()(const char *s) const
22 { 147 {
23 unsigned long hash = 0; 148 unsigned long hash = 0;
24 unsigned int i = 0;
25 149
26 /* use the one-at-a-time hash function, which supposedly is 150 /* use the one-at-a-time hash function, which supposedly is
27 * better than the djb2-like one used by perl5.005, but 151 * better than the djb2-like one used by perl5.005, but
28 * certainly is better then the bug used here before. 152 * certainly is better then the bug used here before.
29 * see http://burtleburtle.net/bob/hash/doobs.html 153 * see http://burtleburtle.net/bob/hash/doobs.html
49 { 173 {
50 return !strcmp (a, b); 174 return !strcmp (a, b);
51 } 175 }
52}; 176};
53 177
178#include <vector>
179
180template<class obj>
181struct unordered_vector : std::vector<obj, slice_allocator<obj> >
182{
183 typedef typename unordered_vector::iterator iterator;
184
185 void erase (unsigned int pos)
186 {
187 if (pos < this->size () - 1)
188 (*this)[pos] = (*this)[this->size () - 1];
189
190 this->pop_back ();
191 }
192
193 void erase (iterator i)
194 {
195 erase ((unsigned int )(i - this->begin ()));
196 }
197};
198
199template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
200template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
201template<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; }
202
203template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
204
205// basically does what strncpy should do, but appends "..." to strings exceeding length
206void assign (char *dst, const char *src, int maxlen);
207
208// type-safe version of assign
209template<int N>
210inline void assign (char (&dst)[N], const char *src)
211{
212 assign ((char *)&dst, src, N);
213}
214
54#endif 215#endif
55 216

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines