… | |
… | |
42 | { |
42 | { |
43 | g_slice_free1 (s, p); |
43 | g_slice_free1 (s, p); |
44 | } |
44 | } |
45 | }; |
45 | }; |
46 | |
46 | |
|
|
47 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
48 | void *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 |
48 | void *salloc (int size) throw (std::bad_alloc); |
51 | template<typename T> |
|
|
52 | inline 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 |
51 | void *salloc (int size, void *src) throw (std::bad_alloc); |
|
|
52 | |
|
|
53 | // and as a template |
|
|
54 | template<typename T> |
56 | template<typename T> |
55 | inline T *salloc (int size) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T)); } |
|
|
56 | template<typename T> |
|
|
57 | inline T *salloc (int size, T *src) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T), (void *)src); } |
57 | inline T *salloc (int n, T *src) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), (void *)src); } |
|
|
58 | |
|
|
59 | // clears the memory |
|
|
60 | template<typename T> |
|
|
61 | inline T *salloc0(int n = 1) throw (std::bad_alloc) { return (T *)salloc_ (n * sizeof (T), 0); } |
58 | |
62 | |
59 | // for symmetry |
63 | // for symmetry |
60 | template<typename T> |
64 | template<typename T> |
61 | inline void sfree (T *ptr, int size) throw () |
65 | inline 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 |
68 | template<typename Tp> |
72 | template<typename Tp> |
… | |
… | |
119 | }; |
123 | }; |
120 | |
124 | |
121 | struct refcounted |
125 | struct refcounted |
122 | { |
126 | { |
123 | refcounted () : refcnt (0) { } |
127 | refcounted () : refcnt (0) { } |
124 | // virtual ~refcounted (); |
128 | virtual ~refcounted (); |
125 | void refcnt_inc () { ++refcnt; } |
129 | void refcnt_inc () { ++refcnt; } |
126 | void refcnt_dec () { --refcnt; } |
130 | void refcnt_dec () { --refcnt; } |
127 | bool dead () { return refcnt == 0; } |
131 | bool dead () { return refcnt == 0; } |
128 | mutable int refcnt; |
132 | mutable int refcnt; |
129 | #if 0 |
133 | #if 0 |
… | |
… | |
161 | T &operator * () const { return *p; } |
165 | T &operator * () const { return *p; } |
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 | }; |
|
|
170 | |
|
|
171 | typedef refptr<player> player_ptr; |
|
|
172 | typedef refptr<object> object_ptr; |
|
|
173 | typedef refptr<archetype> arch_ptr; |
166 | |
174 | |
167 | struct str_hash |
175 | struct str_hash |
168 | { |
176 | { |
169 | std::size_t operator ()(const char *s) const |
177 | std::size_t operator ()(const char *s) const |
170 | { |
178 | { |