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.18 by root, Sat Dec 16 21:40:26 2006 UTC vs.
Revision 1.22 by root, Sat Dec 23 06:21:02 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"
50// if src is 0, clears the memory 55// 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> 56template<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); } 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); }
58 62
59// for symmetry 63// for symmetry
60template<typename T> 64template<typename T>
61inline void sfree (T *ptr, int size) throw () 65inline void sfree (T *ptr, int n = 1) throw ()
62{ 66{
63 g_slice_free1 (size * sizeof (T), (void *)ptr); 67 g_slice_free1 (n * sizeof (T), (void *)ptr);
64} 68}
65 69
66// a STL-compatible allocator that uses g_slice 70// a STL-compatible allocator that uses g_slice
67// boy, this is verbose 71// boy, this is verbose
68template<typename Tp> 72template<typename Tp>
97 return salloc<Tp> (n); 101 return salloc<Tp> (n);
98 } 102 }
99 103
100 void deallocate (pointer p, size_type n) 104 void deallocate (pointer p, size_type n)
101 { 105 {
102 sfree (p, n); 106 sfree<Tp> (p, n);
103 } 107 }
104 108
105 size_type max_size ()const throw () 109 size_type max_size ()const throw ()
106 { 110 {
107 return size_t (-1) / sizeof (Tp); 111 return size_t (-1) / sizeof (Tp);
162 T *operator ->() const { return p; } 166 T *operator ->() const { return p; }
163 167
164 operator T *() const { return p; } 168 operator T *() const { return p; }
165}; 169};
166 170
171typedef refptr<player> player_ptr;
172typedef refptr<object> object_ptr;
173typedef refptr<archetype> arch_ptr;
174
167struct str_hash 175struct str_hash
168{ 176{
169 std::size_t operator ()(const char *s) const 177 std::size_t operator ()(const char *s) const
170 { 178 {
171 unsigned long hash = 0; 179 unsigned long hash = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines