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.13 by root, Thu Sep 14 00:07:15 2006 UTC vs.
Revision 1.24 by root, Mon Dec 25 11:25:49 2006 UTC

9 9
10#include <cstddef> 10#include <cstddef>
11 11
12#include <glib.h> 12#include <glib.h>
13 13
14// use a gcc extension for auto declarations until ISO C++ sanctifies them
15#define AUTODECL(var,expr) typeof(expr) var = (expr)
16
14// makes dynamically allocated objects zero-initialised 17// makes dynamically allocated objects zero-initialised
15struct zero_initialised 18struct zero_initialised
16{ 19{
17 void *operator new (size_t s, void *p) 20 void *operator new (size_t s, void *p)
18 { 21 {
39 { 42 {
40 g_slice_free1 (s, p); 43 g_slice_free1 (s, p);
41 } 44 }
42}; 45};
43 46
47void *salloc_ (int n) throw (std::bad_alloc);
48void *salloc_ (int n, void *src) throw (std::bad_alloc);
49
44// strictly the same as g_slice_alloc, but never returns 0 50// strictly the same as g_slice_alloc, but never returns 0
45void *alloc (int s) throw (std::bad_alloc); 51template<typename T>
52inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
53
54// also copies src into the new area, like "memdup"
55// if src is 0, clears the memory
56template<typename T>
57inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); }
58
59// clears the memory
60template<typename T>
61inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); }
62
46// for symmetry 63// for symmetry
47inline void dealloc (void *p, int s) throw () 64template<typename T>
65inline void sfree (T *ptr, int n = 1) throw ()
48{ 66{
49 g_slice_free1 (s, p); 67 g_slice_free1 (n * sizeof (T), (void *)ptr);
50} 68}
51 69
52// a STL-compatible allocator that uses g_slice 70// a STL-compatible allocator that uses g_slice
53// boy, this is verbose 71// boy, this is verbose
54template<typename Tp> 72template<typename Tp>
78 pointer address (reference x) const { return &x; } 96 pointer address (reference x) const { return &x; }
79 const_pointer address (const_reference x) const { return &x; } 97 const_pointer address (const_reference x) const { return &x; }
80 98
81 pointer allocate (size_type n, const_pointer = 0) 99 pointer allocate (size_type n, const_pointer = 0)
82 { 100 {
83 return static_cast<pointer>(alloc (n * sizeof (Tp))); 101 return salloc<Tp> (n);
84 } 102 }
85 103
86 void deallocate (pointer p, size_type n) 104 void deallocate (pointer p, size_type n)
87 { 105 {
88 dealloc (static_cast<void *>(p), n * sizeof (Tp)); 106 sfree<Tp> (p, n);
89 } 107 }
90 108
91 size_type max_size ()const throw () 109 size_type max_size ()const throw ()
92 { 110 {
93 return size_t (-1) / sizeof (Tp); 111 return size_t (-1) / sizeof (Tp);
100 118
101 void destroy (pointer p) 119 void destroy (pointer p)
102 { 120 {
103 p->~Tp (); 121 p->~Tp ();
104 } 122 }
105};
106
107struct refcounted
108{
109 mutable int refcnt;
110 refcounted () : refcnt (0) { }
111 void refcnt_inc () { ++refcnt; }
112 void refcnt_dec () { --refcnt;
113 if (refcnt < 0)abort();}//D
114}; 123};
115 124
116template<class T> 125template<class T>
117struct refptr 126struct refptr
118{ 127{
141 T &operator * () const { return *p; } 150 T &operator * () const { return *p; }
142 T *operator ->() const { return p; } 151 T *operator ->() const { return p; }
143 152
144 operator T *() const { return p; } 153 operator T *() const { return p; }
145}; 154};
155
156typedef refptr<maptile> maptile_ptr;
157typedef refptr<object> object_ptr;
158typedef refptr<archetype> arch_ptr;
159typedef refptr<client> client_ptr;
160typedef refptr<player> player_ptr;
146 161
147struct str_hash 162struct str_hash
148{ 163{
149 std::size_t operator ()(const char *s) const 164 std::size_t operator ()(const char *s) const
150 { 165 {
213inline void assign (char (&dst)[N], const char *src) 228inline void assign (char (&dst)[N], const char *src)
214{ 229{
215 assign ((char *)&dst, src, N); 230 assign ((char *)&dst, src, N);
216} 231}
217 232
233typedef double tstamp;
234
235// return current time as timestampe
236tstamp now ();
237
218#endif 238#endif
219 239

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines