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.18 by root, Sat Dec 16 21:40: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"
50// if src is 0, clears the memory
51void *salloc (int size, void *src) throw (std::bad_alloc);
52
53// and as a template
54template<typename T>
55inline T *salloc (int size) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T)); }
56template<typename T>
57inline T *salloc (int size, T *src) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T), (void *)src); }
58
46// for symmetry 59// for symmetry
60template<typename T>
47inline void dealloc (void *p, int s) throw () 61inline void sfree (T *ptr, int size) throw ()
48{ 62{
49 g_slice_free1 (s, p); 63 g_slice_free1 (size * sizeof (T), (void *)ptr);
50} 64}
51 65
52// a STL-compatible allocator that uses g_slice 66// a STL-compatible allocator that uses g_slice
53// boy, this is verbose 67// boy, this is verbose
54template<typename Tp> 68template<typename Tp>
78 pointer address (reference x) const { return &x; } 92 pointer address (reference x) const { return &x; }
79 const_pointer address (const_reference x) const { return &x; } 93 const_pointer address (const_reference x) const { return &x; }
80 94
81 pointer allocate (size_type n, const_pointer = 0) 95 pointer allocate (size_type n, const_pointer = 0)
82 { 96 {
83 return static_cast<pointer>(alloc (n * sizeof (Tp))); 97 return salloc<Tp> (n);
84 } 98 }
85 99
86 void deallocate (pointer p, size_type n) 100 void deallocate (pointer p, size_type n)
87 { 101 {
88 dealloc (static_cast<void *>(p), n * sizeof (Tp)); 102 sfree (p, n);
89 } 103 }
90 104
91 size_type max_size ()const throw () 105 size_type max_size ()const throw ()
92 { 106 {
93 return size_t (-1) / sizeof (Tp); 107 return size_t (-1) / sizeof (Tp);
104 } 118 }
105}; 119};
106 120
107struct refcounted 121struct refcounted
108{ 122{
123 refcounted () : refcnt (0) { }
124// virtual ~refcounted ();
125 void refcnt_inc () { ++refcnt; }
126 void refcnt_dec () { --refcnt; }
127 bool dead () { return refcnt == 0; }
109 mutable int refcnt; 128 mutable int refcnt;
110 refcounted () : refcnt (0) { } 129#if 0
111 void refcnt_inc () { ++refcnt; } 130private:
112 void refcnt_dec () { --refcnt; 131 static refcounted *rc_first;
113 if (refcnt < 0)abort();}//D 132 refcounted *rc_next;
133#endif
114}; 134};
115 135
116template<class T> 136template<class T>
117struct refptr 137struct refptr
118{ 138{
213inline void assign (char (&dst)[N], const char *src) 233inline void assign (char (&dst)[N], const char *src)
214{ 234{
215 assign ((char *)&dst, src, N); 235 assign ((char *)&dst, src, N);
216} 236}
217 237
238typedef double tstamp;
239
240// return current time as timestampe
241tstamp now ();
242
218#endif 243#endif
219 244

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines