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.17 by root, Sat Dec 16 03:08:26 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 {
40 g_slice_free1 (s, p); 43 g_slice_free1 (s, p);
41 } 44 }
42}; 45};
43 46
44// strictly the same as g_slice_alloc, but never returns 0 47// strictly the same as g_slice_alloc, but never returns 0
45void *alloc (int s) throw (std::bad_alloc); 48void *salloc (int size) throw (std::bad_alloc);
49// also copies src into the new area, like "memdup"
50void *salloc (int size, void *src) throw (std::bad_alloc);
46// for symmetry 51// for symmetry
47inline void dealloc (void *p, int s) throw () 52inline void sfree (void *ptr, int size) throw ()
48{ 53{
49 g_slice_free1 (s, p); 54 g_slice_free1 (size, ptr);
50} 55}
51 56
52// a STL-compatible allocator that uses g_slice 57// a STL-compatible allocator that uses g_slice
53// boy, this is verbose 58// boy, this is verbose
54template<typename Tp> 59template<typename Tp>
78 pointer address (reference x) const { return &x; } 83 pointer address (reference x) const { return &x; }
79 const_pointer address (const_reference x) const { return &x; } 84 const_pointer address (const_reference x) const { return &x; }
80 85
81 pointer allocate (size_type n, const_pointer = 0) 86 pointer allocate (size_type n, const_pointer = 0)
82 { 87 {
83 return static_cast<pointer>(alloc (n * sizeof (Tp))); 88 return static_cast<pointer>(salloc (n * sizeof (Tp)));
84 } 89 }
85 90
86 void deallocate (pointer p, size_type n) 91 void deallocate (pointer p, size_type n)
87 { 92 {
88 dealloc (static_cast<void *>(p), n * sizeof (Tp)); 93 sfree (static_cast<void *>(p), n * sizeof (Tp));
89 } 94 }
90 95
91 size_type max_size ()const throw () 96 size_type max_size ()const throw ()
92 { 97 {
93 return size_t (-1) / sizeof (Tp); 98 return size_t (-1) / sizeof (Tp);
104 } 109 }
105}; 110};
106 111
107struct refcounted 112struct refcounted
108{ 113{
114 refcounted () : refcnt (0) { }
115// virtual ~refcounted ();
116 void refcnt_inc () { ++refcnt; }
117 void refcnt_dec () { --refcnt; }
118 bool dead () { return refcnt == 0; }
109 mutable int refcnt; 119 mutable int refcnt;
110 refcounted () : refcnt (0) { } 120#if 0
111 void refcnt_inc () { ++refcnt; } 121private:
112 void refcnt_dec () { --refcnt; 122 static refcounted *rc_first;
113 if (refcnt < 0)abort();}//D 123 refcounted *rc_next;
124#endif
114}; 125};
115 126
116template<class T> 127template<class T>
117struct refptr 128struct refptr
118{ 129{
213inline void assign (char (&dst)[N], const char *src) 224inline void assign (char (&dst)[N], const char *src)
214{ 225{
215 assign ((char *)&dst, src, N); 226 assign ((char *)&dst, src, N);
216} 227}
217 228
229typedef double tstamp;
230
231// return current time as timestampe
232tstamp now ();
233
218#endif 234#endif
219 235

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines