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.21 by root, Thu Dec 21 06:12:36 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);
104 } 122 }
105}; 123};
106 124
107struct refcounted 125struct refcounted
108{ 126{
127 refcounted () : refcnt (0) { }
128// virtual ~refcounted ();
129 void refcnt_inc () { ++refcnt; }
130 void refcnt_dec () { --refcnt; }
131 bool dead () { return refcnt == 0; }
109 mutable int refcnt; 132 mutable int refcnt;
110 refcounted () : refcnt (0) { } 133#if 0
111 void refcnt_inc () { ++refcnt; } 134private:
112 void refcnt_dec () { --refcnt; 135 static refcounted *rc_first;
113 if (refcnt < 0)abort();}//D 136 refcounted *rc_next;
137#endif
114}; 138};
115 139
116template<class T> 140template<class T>
117struct refptr 141struct refptr
118{ 142{
213inline void assign (char (&dst)[N], const char *src) 237inline void assign (char (&dst)[N], const char *src)
214{ 238{
215 assign ((char *)&dst, src, N); 239 assign ((char *)&dst, src, N);
216} 240}
217 241
242typedef double tstamp;
243
244// return current time as timestampe
245tstamp now ();
246
218#endif 247#endif
219 248

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines