ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/util.h
Revision: 1.11
Committed: Tue Sep 12 20:55:40 2006 UTC (17 years, 9 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.10: +91 -7 lines
Log Message:
make use of slice_allocator and inline zero_initialised

File Contents

# Content
1 #ifndef UTIL_H__
2 #define UTIL_H__
3
4 #if __GNUC__ >= 3
5 # define is_constant(c) __builtin_constant_p (c)
6 #else
7 # define is_constant(c) 0
8 #endif
9
10 #include <cstddef>
11
12 #include <glib.h>
13
14 // makes dynamically allocated objects zero-initialised
15 struct zero_initialised
16 {
17 void *operator new (size_t s, void *p)
18 {
19 memset (p, 0, s);
20 return p;
21 }
22
23 void *operator new (size_t s)
24 {
25 return g_slice_alloc0 (s);
26 }
27
28 void *operator new[] (size_t s)
29 {
30 return g_slice_alloc0 (s);
31 }
32
33 void operator delete (void *p, size_t s)
34 {
35 g_slice_free1 (s, p);
36 }
37
38 void operator delete[] (void *p, size_t s)
39 {
40 g_slice_free1 (s, p);
41 }
42 };
43
44 void throw_bad_alloc () throw (std::bad_alloc);
45
46 void *alloc (int s) throw (std::bad_alloc);
47 void dealloc (void *p, int s) throw ();
48
49 // a STL-compatible allocator that uses g_slice
50 // boy, this is verbose
51 template<typename Tp>
52 struct 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
104 struct 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
113 template<class T>
114 struct 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; }
142 };
143
144 struct str_hash
145 {
146 std::size_t operator ()(const char *s) const
147 {
148 unsigned long hash = 0;
149
150 /* use the one-at-a-time hash function, which supposedly is
151 * better than the djb2-like one used by perl5.005, but
152 * certainly is better then the bug used here before.
153 * see http://burtleburtle.net/bob/hash/doobs.html
154 */
155 while (*s)
156 {
157 hash += *s++;
158 hash += hash << 10;
159 hash ^= hash >> 6;
160 }
161
162 hash += hash << 3;
163 hash ^= hash >> 11;
164 hash += hash << 15;
165
166 return hash;
167 }
168 };
169
170 struct str_equal
171 {
172 bool operator ()(const char *a, const char *b) const
173 {
174 return !strcmp (a, b);
175 }
176 };
177
178 #include <vector>
179
180 template<class obj>
181 struct 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
199 template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; }
200 template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
201 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; }
202
203 template<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
206 void assign (char *dst, const char *src, int maxlen);
207
208 // type-safe version of assign
209 template<int N>
210 inline void assign (char (&dst)[N], const char *src)
211 {
212 assign ((char *)&dst, src, N);
213 }
214
215 #endif
216