… | |
… | |
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 | |
|
|
47 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
48 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
|
|
49 | |
44 | // strictly the same as g_slice_alloc, but never returns 0 |
50 | // strictly the same as g_slice_alloc, but never returns 0 |
45 | 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 | |
46 | // for symmetry |
63 | // for symmetry |
47 | inline void dealloc (void *p, int s) throw () |
64 | template<typename T> |
|
|
65 | inline void sfree (T *ptr, int n = 1) throw () |
48 | { |
66 | { |
49 | g_slice_free1 (s, p); |
67 | g_slice_free1 (n * sizeof (T), (void *)ptr); |
50 | } |
68 | } |
51 | |
69 | |
52 | // a STL-compatible allocator that uses g_slice |
70 | // a STL-compatible allocator that uses g_slice |
53 | // boy, this is verbose |
71 | // boy, this is verbose |
54 | template<typename Tp> |
72 | template<typename Tp> |
… | |
… | |
78 | pointer address (reference x) const { return &x; } |
96 | pointer address (reference x) const { return &x; } |
79 | const_pointer address (const_reference x) const { return &x; } |
97 | const_pointer address (const_reference x) const { return &x; } |
80 | |
98 | |
81 | pointer allocate (size_type n, const_pointer = 0) |
99 | pointer allocate (size_type n, const_pointer = 0) |
82 | { |
100 | { |
83 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
101 | return salloc<Tp> (n); |
84 | } |
102 | } |
85 | |
103 | |
86 | void deallocate (pointer p, size_type n) |
104 | void deallocate (pointer p, size_type n) |
87 | { |
105 | { |
88 | dealloc (static_cast<void *>(p), n * sizeof (Tp)); |
106 | sfree<Tp> (p, n); |
89 | } |
107 | } |
90 | |
108 | |
91 | size_type max_size ()const throw () |
109 | size_type max_size ()const throw () |
92 | { |
110 | { |
93 | return size_t (-1) / sizeof (Tp); |
111 | return size_t (-1) / sizeof (Tp); |
… | |
… | |
100 | |
118 | |
101 | void destroy (pointer p) |
119 | void destroy (pointer p) |
102 | { |
120 | { |
103 | p->~Tp (); |
121 | p->~Tp (); |
104 | } |
122 | } |
105 | }; |
|
|
106 | |
|
|
107 | struct refcounted |
|
|
108 | { |
|
|
109 | mutable int refcnt; |
|
|
110 | refcounted () : refcnt (0) { } |
|
|
111 | void refcnt_inc () { ++refcnt; } |
|
|
112 | void refcnt_dec () { --refcnt; |
|
|
113 | if (refcnt < 0)abort();}//D |
|
|
114 | }; |
123 | }; |
115 | |
124 | |
116 | template<class T> |
125 | template<class T> |
117 | struct refptr |
126 | struct refptr |
118 | { |
127 | { |
… | |
… | |
141 | T &operator * () const { return *p; } |
150 | T &operator * () const { return *p; } |
142 | T *operator ->() const { return p; } |
151 | T *operator ->() const { return p; } |
143 | |
152 | |
144 | operator T *() const { return p; } |
153 | operator T *() const { return p; } |
145 | }; |
154 | }; |
|
|
155 | |
|
|
156 | typedef refptr<maptile> maptile_ptr; |
|
|
157 | typedef refptr<object> object_ptr; |
|
|
158 | typedef refptr<archetype> arch_ptr; |
|
|
159 | typedef refptr<client> client_ptr; |
|
|
160 | typedef refptr<player> player_ptr; |
146 | |
161 | |
147 | struct str_hash |
162 | struct str_hash |
148 | { |
163 | { |
149 | std::size_t operator ()(const char *s) const |
164 | std::size_t operator ()(const char *s) const |
150 | { |
165 | { |
… | |
… | |
213 | inline void assign (char (&dst)[N], const char *src) |
228 | inline void assign (char (&dst)[N], const char *src) |
214 | { |
229 | { |
215 | assign ((char *)&dst, src, N); |
230 | assign ((char *)&dst, src, N); |
216 | } |
231 | } |
217 | |
232 | |
|
|
233 | typedef double tstamp; |
|
|
234 | |
|
|
235 | // return current time as timestampe |
|
|
236 | tstamp now (); |
|
|
237 | |
218 | #endif |
238 | #endif |
219 | |
239 | |