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.17 by root, Sat Dec 16 03:08:26 2006 UTC vs.
Revision 1.23 by root, Sat Dec 23 16:05:19 2006 UTC

42 { 42 {
43 g_slice_free1 (s, p); 43 g_slice_free1 (s, p);
44 } 44 }
45}; 45};
46 46
47void *salloc_ (int n) throw (std::bad_alloc);
48void *salloc_ (int n, void *src) throw (std::bad_alloc);
49
47// strictly the same as g_slice_alloc, but never returns 0 50// strictly the same as g_slice_alloc, but never returns 0
48void *salloc (int size) throw (std::bad_alloc); 51template<typename T>
52inline T *salloc (int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T)); }
53
49// also copies src into the new area, like "memdup" 54// also copies src into the new area, like "memdup"
50void *salloc (int size, void *src) throw (std::bad_alloc); 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
51// for symmetry 63// for symmetry
64template<typename T>
52inline void sfree (void *ptr, int size) throw () 65inline void sfree (T *ptr, int n = 1) throw ()
53{ 66{
54 g_slice_free1 (size, ptr); 67 g_slice_free1 (n * sizeof (T), (void *)ptr);
55} 68}
56 69
57// a STL-compatible allocator that uses g_slice 70// a STL-compatible allocator that uses g_slice
58// boy, this is verbose 71// boy, this is verbose
59template<typename Tp> 72template<typename Tp>
83 pointer address (reference x) const { return &x; } 96 pointer address (reference x) const { return &x; }
84 const_pointer address (const_reference x) const { return &x; } 97 const_pointer address (const_reference x) const { return &x; }
85 98
86 pointer allocate (size_type n, const_pointer = 0) 99 pointer allocate (size_type n, const_pointer = 0)
87 { 100 {
88 return static_cast<pointer>(salloc (n * sizeof (Tp))); 101 return salloc<Tp> (n);
89 } 102 }
90 103
91 void deallocate (pointer p, size_type n) 104 void deallocate (pointer p, size_type n)
92 { 105 {
93 sfree (static_cast<void *>(p), n * sizeof (Tp)); 106 sfree<Tp> (p, n);
94 } 107 }
95 108
96 size_type max_size ()const throw () 109 size_type max_size ()const throw ()
97 { 110 {
98 return size_t (-1) / sizeof (Tp); 111 return size_t (-1) / sizeof (Tp);
110}; 123};
111 124
112struct refcounted 125struct refcounted
113{ 126{
114 refcounted () : refcnt (0) { } 127 refcounted () : refcnt (0) { }
115// virtual ~refcounted (); 128 virtual ~refcounted ();
116 void refcnt_inc () { ++refcnt; } 129 void refcnt_inc () { ++refcnt; }
117 void refcnt_dec () { --refcnt; } 130 void refcnt_dec () { --refcnt; }
118 bool dead () { return refcnt == 0; } 131 bool dead () { return refcnt == 0; }
119 mutable int refcnt; 132 mutable int refcnt;
120#if 0 133#if 0
152 T &operator * () const { return *p; } 165 T &operator * () const { return *p; }
153 T *operator ->() const { return p; } 166 T *operator ->() const { return p; }
154 167
155 operator T *() const { return p; } 168 operator T *() const { return p; }
156}; 169};
170
171typedef refptr<player> player_ptr;
172typedef refptr<object> object_ptr;
173typedef refptr<archetype> arch_ptr;
157 174
158struct str_hash 175struct str_hash
159{ 176{
160 std::size_t operator ()(const char *s) const 177 std::size_t operator ()(const char *s) const
161 { 178 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines