… | |
… | |
5 | # define is_constant(c) __builtin_constant_p (c) |
5 | # define is_constant(c) __builtin_constant_p (c) |
6 | #else |
6 | #else |
7 | # define is_constant(c) 0 |
7 | # define is_constant(c) 0 |
8 | #endif |
8 | #endif |
9 | |
9 | |
|
|
10 | #include <cstddef> |
|
|
11 | |
|
|
12 | #include <glib.h> |
|
|
13 | |
|
|
14 | // use a gcc extension for auto declarations until ISO C++ sanctifies them |
|
|
15 | #define AUTODECL(var,expr) typeof(expr) var = (expr) |
|
|
16 | |
10 | // makes dynamically allocated objects zero-initialised |
17 | // makes dynamically allocated objects zero-initialised |
11 | struct zero_initialised |
18 | struct zero_initialised |
12 | { |
19 | { |
13 | void *operator new (size_t s, void *); |
20 | void *operator new (size_t s, void *p) |
|
|
21 | { |
|
|
22 | memset (p, 0, s); |
|
|
23 | return p; |
|
|
24 | } |
|
|
25 | |
14 | void *operator new (size_t s); |
26 | void *operator new (size_t s) |
|
|
27 | { |
|
|
28 | return g_slice_alloc0 (s); |
|
|
29 | } |
|
|
30 | |
15 | void *operator new [] (size_t s); |
31 | void *operator new[] (size_t s) |
|
|
32 | { |
|
|
33 | return g_slice_alloc0 (s); |
|
|
34 | } |
|
|
35 | |
16 | void operator delete (void *p, size_t s); |
36 | void operator delete (void *p, size_t s) |
|
|
37 | { |
|
|
38 | g_slice_free1 (s, p); |
|
|
39 | } |
|
|
40 | |
17 | void operator delete [] (void *p, size_t s); |
41 | void operator delete[] (void *p, size_t s) |
|
|
42 | { |
|
|
43 | g_slice_free1 (s, p); |
|
|
44 | } |
|
|
45 | }; |
|
|
46 | |
|
|
47 | void *salloc_ (int n) throw (std::bad_alloc); |
|
|
48 | void *salloc_ (int n, void *src) throw (std::bad_alloc); |
|
|
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 | } |
|
|
69 | |
|
|
70 | // a STL-compatible allocator that uses g_slice |
|
|
71 | // boy, this is verbose |
|
|
72 | template<typename Tp> |
|
|
73 | struct slice_allocator |
|
|
74 | { |
|
|
75 | typedef size_t size_type; |
|
|
76 | typedef ptrdiff_t difference_type; |
|
|
77 | typedef Tp *pointer; |
|
|
78 | typedef const Tp *const_pointer; |
|
|
79 | typedef Tp &reference; |
|
|
80 | typedef const Tp &const_reference; |
|
|
81 | typedef Tp value_type; |
|
|
82 | |
|
|
83 | template <class U> |
|
|
84 | struct rebind |
|
|
85 | { |
|
|
86 | typedef slice_allocator<U> other; |
|
|
87 | }; |
|
|
88 | |
|
|
89 | slice_allocator () throw () { } |
|
|
90 | slice_allocator (const slice_allocator &o) throw () { } |
|
|
91 | template<typename Tp2> |
|
|
92 | slice_allocator (const slice_allocator<Tp2> &) throw () { } |
|
|
93 | |
|
|
94 | ~slice_allocator () { } |
|
|
95 | |
|
|
96 | pointer address (reference x) const { return &x; } |
|
|
97 | const_pointer address (const_reference x) const { return &x; } |
|
|
98 | |
|
|
99 | pointer allocate (size_type n, const_pointer = 0) |
|
|
100 | { |
|
|
101 | return salloc<Tp> (n); |
|
|
102 | } |
|
|
103 | |
|
|
104 | void deallocate (pointer p, size_type n) |
|
|
105 | { |
|
|
106 | sfree<Tp> (p, n); |
|
|
107 | } |
|
|
108 | |
|
|
109 | size_type max_size ()const throw () |
|
|
110 | { |
|
|
111 | return size_t (-1) / sizeof (Tp); |
|
|
112 | } |
|
|
113 | |
|
|
114 | void construct (pointer p, const Tp &val) |
|
|
115 | { |
|
|
116 | ::new (p) Tp (val); |
|
|
117 | } |
|
|
118 | |
|
|
119 | void destroy (pointer p) |
|
|
120 | { |
|
|
121 | p->~Tp (); |
|
|
122 | } |
18 | }; |
123 | }; |
19 | |
124 | |
20 | struct refcounted |
125 | struct refcounted |
21 | { |
126 | { |
|
|
127 | refcounted () : refcnt (0) { } |
|
|
128 | virtual ~refcounted (); |
|
|
129 | void refcnt_inc () { ++refcnt; } |
|
|
130 | void refcnt_dec () { --refcnt; } |
|
|
131 | bool dead () { return refcnt == 0; } |
22 | mutable int refcnt; |
132 | mutable int refcnt; |
23 | refcounted () : refcnt (0) { } |
133 | #if 0 |
24 | void refcnt_inc () { ++refcnt; } |
134 | private: |
25 | void refcnt_dec () { --refcnt; |
135 | static refcounted *rc_first; |
26 | if (refcnt < 0)abort();}//D |
136 | refcounted *rc_next; |
|
|
137 | #endif |
27 | }; |
138 | }; |
28 | |
139 | |
29 | template<class T> |
140 | template<class T> |
30 | struct refptr |
141 | struct refptr |
31 | { |
142 | { |
… | |
… | |
54 | T &operator * () const { return *p; } |
165 | T &operator * () const { return *p; } |
55 | T *operator ->() const { return p; } |
166 | T *operator ->() const { return p; } |
56 | |
167 | |
57 | operator T *() const { return p; } |
168 | operator T *() const { return p; } |
58 | }; |
169 | }; |
|
|
170 | |
|
|
171 | typedef refptr<player> player_ptr; |
|
|
172 | typedef refptr<object> object_ptr; |
|
|
173 | typedef refptr<archetype> arch_ptr; |
59 | |
174 | |
60 | struct str_hash |
175 | struct str_hash |
61 | { |
176 | { |
62 | std::size_t operator ()(const char *s) const |
177 | std::size_t operator ()(const char *s) const |
63 | { |
178 | { |
… | |
… | |
92 | }; |
207 | }; |
93 | |
208 | |
94 | #include <vector> |
209 | #include <vector> |
95 | |
210 | |
96 | template<class obj> |
211 | template<class obj> |
97 | struct unordered_vector : std::vector<obj> |
212 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
98 | { |
213 | { |
99 | typedef typename std::vector<obj>::iterator iterator; |
214 | typedef typename unordered_vector::iterator iterator; |
100 | |
215 | |
101 | void erase (unsigned int pos) |
216 | void erase (unsigned int pos) |
102 | { |
217 | { |
103 | if (pos < this->size () - 1) |
218 | if (pos < this->size () - 1) |
104 | (*this)[pos] = (*this)[this->size () - 1]; |
219 | (*this)[pos] = (*this)[this->size () - 1]; |
… | |
… | |
126 | inline void assign (char (&dst)[N], const char *src) |
241 | inline void assign (char (&dst)[N], const char *src) |
127 | { |
242 | { |
128 | assign ((char *)&dst, src, N); |
243 | assign ((char *)&dst, src, N); |
129 | } |
244 | } |
130 | |
245 | |
|
|
246 | typedef double tstamp; |
|
|
247 | |
|
|
248 | // return current time as timestampe |
|
|
249 | tstamp now (); |
|
|
250 | |
131 | #endif |
251 | #endif |
132 | |
252 | |