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.9 by root, Tue Sep 12 18:15:34 2006 UTC vs.
Revision 1.12 by root, Tue Sep 12 21:10:32 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{
13 void *operator new (size_t s, void *); 17 void *operator new (size_t s, void *p)
18 {
19 memset (p, 0, s);
20 return p;
21 }
22
14 void *operator new (size_t s); 23 void *operator new (size_t s)
24 {
25 return g_slice_alloc0 (s);
26 }
27
15 void *operator new [] (size_t s); 28 void *operator new[] (size_t s)
29 {
30 return g_slice_alloc0 (s);
31 }
32
16 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
17 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
44// strictly the same as g_slice_alloc, but never returns 0
45void *alloc (int s) throw (std::bad_alloc);
46// for symmetry
47inline void dealloc (void *p, int s) throw ()
48{
49 g_slice_free1 (s, p);
50}
51
52// a STL-compatible allocator that uses g_slice
53// boy, this is verbose
54template<typename Tp>
55struct slice_allocator
56{
57 typedef size_t size_type;
58 typedef ptrdiff_t difference_type;
59 typedef Tp *pointer;
60 typedef const Tp *const_pointer;
61 typedef Tp &reference;
62 typedef const Tp &const_reference;
63 typedef Tp value_type;
64
65 template <class U>
66 struct rebind
67 {
68 typedef slice_allocator<U> other;
69 };
70
71 slice_allocator () throw () { }
72 slice_allocator (const slice_allocator &o) throw () { }
73 template<typename Tp2>
74 slice_allocator (const slice_allocator<Tp2> &) throw () { }
75
76 ~slice_allocator () { }
77
78 pointer address (reference x) const { return &x; }
79 const_pointer address (const_reference x) const { return &x; }
80
81 pointer allocate (size_type n, const_pointer = 0)
82 {
83 return static_cast<pointer>(alloc (n * sizeof (Tp)));
84 }
85
86 void deallocate (pointer p, size_type n)
87 {
88 dealloc (static_cast<void *>(p), n);
89 }
90
91 size_type max_size ()const throw ()
92 {
93 return size_t (-1) / sizeof (Tp);
94 }
95
96 void construct (pointer p, const Tp &val)
97 {
98 ::new (p) Tp (val);
99 }
100
101 void destroy (pointer p)
102 {
103 p->~Tp ();
104 }
18}; 105};
19 106
20struct refcounted 107struct refcounted
21{ 108{
22 mutable int refcnt; 109 mutable int refcnt;
92}; 179};
93 180
94#include <vector> 181#include <vector>
95 182
96template<class obj> 183template<class obj>
97struct unordered_vector : std::vector<obj> 184struct unordered_vector : std::vector<obj, slice_allocator<obj> >
98{ 185{
99 typedef typename std::vector<obj>::iterator iterator; 186 typedef typename unordered_vector::iterator iterator;
100 187
101 void erase (unsigned int pos) 188 void erase (unsigned int pos)
102 { 189 {
103 if (pos < this->size () - 1) 190 if (pos < this->size () - 1)
104 (*this)[pos] = (*this)[this->size () - 1]; 191 (*this)[pos] = (*this)[this->size () - 1];
116template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } 203template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; }
117template<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; } 204template<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; }
118 205
119template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 206template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
120 207
121// basically does what strncpy should do 208// basically does what strncpy should do, but appends "..." to strings exceeding length
209void assign (char *dst, const char *src, int maxlen);
210
211// type-safe version of assign
122template<int N> 212template<int N>
123inline void assign (char (&dst)[N], const char *src) 213inline void assign (char (&dst)[N], const char *src)
124{ 214{
125 // should be optimised at some point, maybe should also add "..." 215 assign ((char *)&dst, src, N);
126 // when buffer is too small.
127 snprintf (dst, N, "%s", src);
128} 216}
129 217
130#endif 218#endif
131 219

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines