… | |
… | |
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 *alloc (int s) 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 | |
|
|
54 | // also copies src into the new area, like "memdup" |
|
|
55 | // if src is 0, clears the memory |
|
|
56 | template<typename T> |
|
|
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); } |
|
|
62 | |
49 | // for symmetry |
63 | // for symmetry |
50 | inline void dealloc (void *p, int s) throw () |
64 | template<typename T> |
|
|
65 | inline void sfree (T *ptr, int n = 1) throw () |
51 | { |
66 | { |
52 | g_slice_free1 (s, p); |
67 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
53 | } |
68 | } |
54 | |
69 | |
55 | // a STL-compatible allocator that uses g_slice |
70 | // a STL-compatible allocator that uses g_slice |
56 | // boy, this is verbose |
71 | // boy, this is verbose |
57 | template<typename Tp> |
72 | template<typename Tp> |
… | |
… | |
81 | pointer address (reference x) const { return &x; } |
96 | pointer address (reference x) const { return &x; } |
82 | const_pointer address (const_reference x) const { return &x; } |
97 | const_pointer address (const_reference x) const { return &x; } |
83 | |
98 | |
84 | pointer allocate (size_type n, const_pointer = 0) |
99 | pointer allocate (size_type n, const_pointer = 0) |
85 | { |
100 | { |
86 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
101 | return salloc<Tp> (n); |
87 | } |
102 | } |
88 | |
103 | |
89 | void deallocate (pointer p, size_type n) |
104 | void deallocate (pointer p, size_type n) |
90 | { |
105 | { |
91 | dealloc (static_cast<void *>(p), n * sizeof (Tp)); |
106 | sfree<Tp> (p, n); |
92 | } |
107 | } |
93 | |
108 | |
94 | size_type max_size ()const throw () |
109 | size_type max_size ()const throw () |
95 | { |
110 | { |
96 | return size_t (-1) / sizeof (Tp); |
111 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
108 | }; |
123 | }; |
109 | |
124 | |
110 | struct refcounted |
125 | struct refcounted |
111 | { |
126 | { |
112 | refcounted () : refcnt (0) { } |
127 | refcounted () : refcnt (0) { } |
113 | // virtual ~refcounted (); |
128 | virtual ~refcounted (); |
114 | void refcnt_inc () { ++refcnt; } |
129 | void refcnt_inc () { ++refcnt; } |
115 | void refcnt_dec () { --refcnt; } |
130 | void refcnt_dec () { --refcnt; } |
116 | bool dead () { return refcnt == 0; } |
131 | bool dead () { return refcnt == 0; } |
117 | mutable int refcnt; |
132 | mutable int refcnt; |
118 | #if 0 |
133 | #if 0 |
… | |
… | |
150 | T &operator * () const { return *p; } |
165 | T &operator * () const { return *p; } |
151 | T *operator ->() const { return p; } |
166 | T *operator ->() const { return p; } |
152 | |
167 | |
153 | operator T *() const { return p; } |
168 | operator T *() const { return p; } |
154 | }; |
169 | }; |
|
|
170 | |
|
|
171 | typedef refptr<player> player_ptr; |
|
|
172 | typedef refptr<object> object_ptr; |
|
|
173 | typedef refptr<archetype> arch_ptr; |
155 | |
174 | |
156 | struct str_hash |
175 | struct str_hash |
157 | { |
176 | { |
158 | std::size_t operator ()(const char *s) const |
177 | std::size_t operator ()(const char *s) const |
159 | { |
178 | { |
… | |
… | |
222 | inline void assign (char (&dst)[N], const char *src) |
241 | inline void assign (char (&dst)[N], const char *src) |
223 | { |
242 | { |
224 | assign ((char *)&dst, src, N); |
243 | assign ((char *)&dst, src, N); |
225 | } |
244 | } |
226 | |
245 | |
|
|
246 | typedef double tstamp; |
|
|
247 | |
|
|
248 | // return current time as timestampe |
|
|
249 | tstamp now (); |
|
|
250 | |
227 | #endif |
251 | #endif |
228 | |
252 | |