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.14 by root, Thu Sep 14 18:13:02 2006 UTC vs.
Revision 1.19 by root, Sun Dec 17 19:07:23 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines