… | |
… | |
9 | |
9 | |
10 | #include <cstddef> |
10 | #include <cstddef> |
11 | |
11 | |
12 | #include <glib.h> |
12 | #include <glib.h> |
13 | |
13 | |
|
|
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
|
|
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
|
|
16 | |
14 | // makes dynamically allocated objects zero-initialised |
17 | // makes dynamically allocated objects zero-initialised |
15 | struct zero_initialised |
18 | struct zero_initialised |
16 | { |
19 | { |
17 | void *operator new (size_t s, void *p) |
20 | void *operator new (size_t s, void *p) |
18 | { |
21 | { |
… | |
… | |
39 | { |
42 | { |
40 | g_slice_free1 (s, p); |
43 | g_slice_free1 (s, p); |
41 | } |
44 | } |
42 | }; |
45 | }; |
43 | |
46 | |
44 | void throw_bad_alloc () throw (std::bad_alloc); |
47 | void *salloc_ (int n) throw (std::bad_alloc); |
45 | |
|
|
46 | void *alloc (int s) throw (std::bad_alloc); |
48 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
47 | void dealloc (void *p, int s) throw (); |
49 | |
|
|
50 | // strictly the same as g_slice_alloc, but never returns 0 |
|
|
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 | |
|
|
63 | // for symmetry |
|
|
64 | template<typename T> |
|
|
65 | inline void sfree (T *ptr, int n = 1) throw () |
|
|
66 | { |
|
|
67 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
|
|
68 | } |
48 | |
69 | |
49 | // a STL-compatible allocator that uses g_slice |
70 | // a STL-compatible allocator that uses g_slice |
50 | // boy, this is verbose |
71 | // boy, this is verbose |
51 | template<typename Tp> |
72 | template<typename Tp> |
52 | struct slice_allocator |
73 | struct slice_allocator |
… | |
… | |
75 | pointer address (reference x) const { return &x; } |
96 | pointer address (reference x) const { return &x; } |
76 | const_pointer address (const_reference x) const { return &x; } |
97 | const_pointer address (const_reference x) const { return &x; } |
77 | |
98 | |
78 | pointer allocate (size_type n, const_pointer = 0) |
99 | pointer allocate (size_type n, const_pointer = 0) |
79 | { |
100 | { |
80 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
101 | return salloc<Tp> (n); |
81 | } |
102 | } |
82 | |
103 | |
83 | void deallocate (pointer p, size_type n) |
104 | void deallocate (pointer p, size_type n) |
84 | { |
105 | { |
85 | dealloc (static_cast<void *>(p), n); |
106 | sfree<Tp> (p, n); |
86 | } |
107 | } |
87 | |
108 | |
88 | size_type max_size ()const throw () |
109 | size_type max_size ()const throw () |
89 | { |
110 | { |
90 | return size_t (-1) / sizeof (Tp); |
111 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
101 | } |
122 | } |
102 | }; |
123 | }; |
103 | |
124 | |
104 | struct refcounted |
125 | struct refcounted |
105 | { |
126 | { |
|
|
127 | refcounted () : refcnt (0) { } |
|
|
128 | virtual ~refcounted (); |
|
|
129 | void refcnt_inc () { ++refcnt; } |
|
|
130 | void refcnt_dec () { --refcnt; } |
|
|
131 | bool dead () { return refcnt == 0; } |
106 | mutable int refcnt; |
132 | mutable int refcnt; |
107 | refcounted () : refcnt (0) { } |
133 | #if 0 |
108 | void refcnt_inc () { ++refcnt; } |
134 | private: |
109 | void refcnt_dec () { --refcnt; |
135 | static refcounted *rc_first; |
110 | if (refcnt < 0)abort();}//D |
136 | refcounted *rc_next; |
|
|
137 | #endif |
111 | }; |
138 | }; |
112 | |
139 | |
113 | template<class T> |
140 | template<class T> |
114 | struct refptr |
141 | struct refptr |
115 | { |
142 | { |
… | |
… | |
138 | T &operator * () const { return *p; } |
165 | T &operator * () const { return *p; } |
139 | T *operator ->() const { return p; } |
166 | T *operator ->() const { return p; } |
140 | |
167 | |
141 | operator T *() const { return p; } |
168 | operator T *() const { return p; } |
142 | }; |
169 | }; |
|
|
170 | |
|
|
171 | typedef refptr<player> player_ptr; |
|
|
172 | typedef refptr<object> object_ptr; |
|
|
173 | typedef refptr<archetype> arch_ptr; |
143 | |
174 | |
144 | struct str_hash |
175 | struct str_hash |
145 | { |
176 | { |
146 | std::size_t operator ()(const char *s) const |
177 | std::size_t operator ()(const char *s) const |
147 | { |
178 | { |
… | |
… | |
210 | inline void assign (char (&dst)[N], const char *src) |
241 | inline void assign (char (&dst)[N], const char *src) |
211 | { |
242 | { |
212 | assign ((char *)&dst, src, N); |
243 | assign ((char *)&dst, src, N); |
213 | } |
244 | } |
214 | |
245 | |
|
|
246 | typedef double tstamp; |
|
|
247 | |
|
|
248 | // return current time as timestampe |
|
|
249 | tstamp now (); |
|
|
250 | |
215 | #endif |
251 | #endif |
216 | |
252 | |