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.24 by root, Mon Dec 25 11:25:49 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);
105 118
106 void destroy (pointer p) 119 void destroy (pointer p)
107 { 120 {
108 p->~Tp (); 121 p->~Tp ();
109 } 122 }
110};
111
112struct refcounted
113{
114 refcounted () : refcnt (0) { }
115// virtual ~refcounted ();
116 void refcnt_inc () { ++refcnt; }
117 void refcnt_dec () { --refcnt; }
118 bool dead () { return refcnt == 0; }
119 mutable int refcnt;
120#if 0
121private:
122 static refcounted *rc_first;
123 refcounted *rc_next;
124#endif
125}; 123};
126 124
127template<class T> 125template<class T>
128struct refptr 126struct refptr
129{ 127{
152 T &operator * () const { return *p; } 150 T &operator * () const { return *p; }
153 T *operator ->() const { return p; } 151 T *operator ->() const { return p; }
154 152
155 operator T *() const { return p; } 153 operator T *() const { return p; }
156}; 154};
155
156typedef refptr<maptile> maptile_ptr;
157typedef refptr<object> object_ptr;
158typedef refptr<archetype> arch_ptr;
159typedef refptr<client> client_ptr;
160typedef refptr<player> player_ptr;
157 161
158struct str_hash 162struct str_hash
159{ 163{
160 std::size_t operator ()(const char *s) const 164 std::size_t operator ()(const char *s) const
161 { 165 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines