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.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{
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
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 }
18}; 102};
19 103
20struct refcounted 104struct refcounted
21{ 105{
22 mutable int refcnt; 106 mutable int refcnt;
92}; 176};
93 177
94#include <vector> 178#include <vector>
95 179
96template<class obj> 180template<class obj>
97struct unordered_vector : std::vector<obj> 181struct unordered_vector : std::vector<obj, slice_allocator<obj> >
98{ 182{
99 typedef typename std::vector<obj>::iterator iterator; 183 typedef typename unordered_vector::iterator iterator;
100 184
101 void erase (unsigned int pos) 185 void erase (unsigned int pos)
102 { 186 {
103 if (pos < this->size () - 1) 187 if (pos < this->size () - 1)
104 (*this)[pos] = (*this)[this->size () - 1]; 188 (*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; } 200template<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; } 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; }
118 202
119template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } 203template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; }
120 204
121// basically does what strncpy should do 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
122template<int N> 209template<int N>
123inline void assign (char (&dst)[N], const char *src) 210inline void assign (char (&dst)[N], const char *src)
124{ 211{
125 // should be optimised at some point, maybe should also add "..." 212 assign ((char *)&dst, src, N);
126 // when buffer is too small.
127 snprintf (dst, N, "%s", src);
128} 213}
129 214
130#endif 215#endif
131 216

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines