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.11 by root, Tue Sep 12 20:55:40 2006 UTC vs.
Revision 1.22 by root, Sat Dec 23 06:21:02 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
44void throw_bad_alloc () throw (std::bad_alloc); 47void *salloc_ (int n) throw (std::bad_alloc);
45
46void *alloc (int s) throw (std::bad_alloc); 48void *salloc_ (int n, void *src) throw (std::bad_alloc);
47void dealloc (void *p, int s) throw (); 49
50// strictly the same as g_slice_alloc, but never returns 0
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
63// for symmetry
64template<typename T>
65inline void sfree (T *ptr, int n = 1) throw ()
66{
67 g_slice_free1 (n * sizeof (T), (void *)ptr);
68}
48 69
49// a STL-compatible allocator that uses g_slice 70// a STL-compatible allocator that uses g_slice
50// boy, this is verbose 71// boy, this is verbose
51template<typename Tp> 72template<typename Tp>
52struct slice_allocator 73struct slice_allocator
75 pointer address (reference x) const { return &x; } 96 pointer address (reference x) const { return &x; }
76 const_pointer address (const_reference x) const { return &x; } 97 const_pointer address (const_reference x) const { return &x; }
77 98
78 pointer allocate (size_type n, const_pointer = 0) 99 pointer allocate (size_type n, const_pointer = 0)
79 { 100 {
80 return static_cast<pointer>(alloc (n * sizeof (Tp))); 101 return salloc<Tp> (n);
81 } 102 }
82 103
83 void deallocate (pointer p, size_type n) 104 void deallocate (pointer p, size_type n)
84 { 105 {
85 dealloc (static_cast<void *>(p), n); 106 sfree<Tp> (p, n);
86 } 107 }
87 108
88 size_type max_size ()const throw () 109 size_type max_size ()const throw ()
89 { 110 {
90 return size_t (-1) / sizeof (Tp); 111 return size_t (-1) / sizeof (Tp);
101 } 122 }
102}; 123};
103 124
104struct refcounted 125struct refcounted
105{ 126{
127 refcounted () : refcnt (0) { }
128// virtual ~refcounted ();
129 void refcnt_inc () { ++refcnt; }
130 void refcnt_dec () { --refcnt; }
131 bool dead () { return refcnt == 0; }
106 mutable int refcnt; 132 mutable int refcnt;
107 refcounted () : refcnt (0) { } 133#if 0
108 void refcnt_inc () { ++refcnt; } 134private:
109 void refcnt_dec () { --refcnt; 135 static refcounted *rc_first;
110 if (refcnt < 0)abort();}//D 136 refcounted *rc_next;
137#endif
111}; 138};
112 139
113template<class T> 140template<class T>
114struct refptr 141struct refptr
115{ 142{
138 T &operator * () const { return *p; } 165 T &operator * () const { return *p; }
139 T *operator ->() const { return p; } 166 T *operator ->() const { return p; }
140 167
141 operator T *() const { return p; } 168 operator T *() const { return p; }
142}; 169};
170
171typedef refptr<player> player_ptr;
172typedef refptr<object> object_ptr;
173typedef refptr<archetype> arch_ptr;
143 174
144struct str_hash 175struct str_hash
145{ 176{
146 std::size_t operator ()(const char *s) const 177 std::size_t operator ()(const char *s) const
147 { 178 {
210inline void assign (char (&dst)[N], const char *src) 241inline void assign (char (&dst)[N], const char *src)
211{ 242{
212 assign ((char *)&dst, src, N); 243 assign ((char *)&dst, src, N);
213} 244}
214 245
246typedef double tstamp;
247
248// return current time as timestampe
249tstamp now ();
250
215#endif 251#endif
216 252

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines