… | |
… | |
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 | // strictly the same as g_slice_alloc, but never returns 0 |
|
|
48 | void *salloc (int size) throw (std::bad_alloc); |
|
|
49 | // also copies src into the new area, like "memdup" |
|
|
50 | void *salloc (int size, void *src) throw (std::bad_alloc); |
|
|
51 | // for symmetry |
|
|
52 | inline void sfree (void *ptr, int size) throw () |
|
|
53 | { |
|
|
54 | g_slice_free1 (size, ptr); |
|
|
55 | } |
|
|
56 | |
|
|
57 | // a STL-compatible allocator that uses g_slice |
|
|
58 | // boy, this is verbose |
|
|
59 | template<typename Tp> |
|
|
60 | struct slice_allocator |
|
|
61 | { |
|
|
62 | typedef size_t size_type; |
|
|
63 | typedef ptrdiff_t difference_type; |
|
|
64 | typedef Tp *pointer; |
|
|
65 | typedef const Tp *const_pointer; |
|
|
66 | typedef Tp &reference; |
|
|
67 | typedef const Tp &const_reference; |
|
|
68 | typedef Tp value_type; |
|
|
69 | |
|
|
70 | template <class U> |
|
|
71 | struct rebind |
|
|
72 | { |
|
|
73 | typedef slice_allocator<U> other; |
|
|
74 | }; |
|
|
75 | |
|
|
76 | slice_allocator () throw () { } |
|
|
77 | slice_allocator (const slice_allocator &o) throw () { } |
|
|
78 | template<typename Tp2> |
|
|
79 | slice_allocator (const slice_allocator<Tp2> &) throw () { } |
|
|
80 | |
|
|
81 | ~slice_allocator () { } |
|
|
82 | |
|
|
83 | pointer address (reference x) const { return &x; } |
|
|
84 | const_pointer address (const_reference x) const { return &x; } |
|
|
85 | |
|
|
86 | pointer allocate (size_type n, const_pointer = 0) |
|
|
87 | { |
|
|
88 | return static_cast<pointer>(salloc (n * sizeof (Tp))); |
|
|
89 | } |
|
|
90 | |
|
|
91 | void deallocate (pointer p, size_type n) |
|
|
92 | { |
|
|
93 | sfree (static_cast<void *>(p), n * sizeof (Tp)); |
|
|
94 | } |
|
|
95 | |
|
|
96 | size_type max_size ()const throw () |
|
|
97 | { |
|
|
98 | return size_t (-1) / sizeof (Tp); |
|
|
99 | } |
|
|
100 | |
|
|
101 | void construct (pointer p, const Tp &val) |
|
|
102 | { |
|
|
103 | ::new (p) Tp (val); |
|
|
104 | } |
|
|
105 | |
|
|
106 | void destroy (pointer p) |
|
|
107 | { |
|
|
108 | p->~Tp (); |
|
|
109 | } |
18 | }; |
110 | }; |
19 | |
111 | |
20 | struct refcounted |
112 | struct refcounted |
21 | { |
113 | { |
|
|
114 | refcounted () : refcnt (0) { } |
|
|
115 | // virtual ~refcounted (); |
|
|
116 | void refcnt_inc () { ++refcnt; } |
|
|
117 | void refcnt_dec () { --refcnt; } |
|
|
118 | bool dead () { return refcnt == 0; } |
22 | mutable int refcnt; |
119 | mutable int refcnt; |
23 | refcounted () : refcnt (0) { } |
120 | #if 0 |
24 | void refcnt_inc () { ++refcnt; } |
121 | private: |
25 | void refcnt_dec () { --refcnt; |
122 | static refcounted *rc_first; |
26 | if (refcnt < 0)abort();}//D |
123 | refcounted *rc_next; |
|
|
124 | #endif |
27 | }; |
125 | }; |
28 | |
126 | |
29 | template<class T> |
127 | template<class T> |
30 | struct refptr |
128 | struct refptr |
31 | { |
129 | { |
… | |
… | |
92 | }; |
190 | }; |
93 | |
191 | |
94 | #include <vector> |
192 | #include <vector> |
95 | |
193 | |
96 | template<class obj> |
194 | template<class obj> |
97 | struct unordered_vector : std::vector<obj> |
195 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
98 | { |
196 | { |
99 | typedef typename std::vector<obj>::iterator iterator; |
197 | typedef typename unordered_vector::iterator iterator; |
100 | |
198 | |
101 | void erase (unsigned int pos) |
199 | void erase (unsigned int pos) |
102 | { |
200 | { |
103 | if (pos < this->size () - 1) |
201 | if (pos < this->size () - 1) |
104 | (*this)[pos] = (*this)[this->size () - 1]; |
202 | (*this)[pos] = (*this)[this->size () - 1]; |
… | |
… | |
110 | { |
208 | { |
111 | erase ((unsigned int )(i - this->begin ())); |
209 | erase ((unsigned int )(i - this->begin ())); |
112 | } |
210 | } |
113 | }; |
211 | }; |
114 | |
212 | |
|
|
213 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
|
|
214 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
|
|
215 | template<typename T, typename U, typename V> static inline T clamp (T v, U a, V b) { return v < (T)a ? a : v >(T)b ? b : v; } |
|
|
216 | |
|
|
217 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
|
|
218 | |
|
|
219 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
|
|
220 | void assign (char *dst, const char *src, int maxlen); |
|
|
221 | |
|
|
222 | // type-safe version of assign |
|
|
223 | template<int N> |
|
|
224 | inline void assign (char (&dst)[N], const char *src) |
|
|
225 | { |
|
|
226 | assign ((char *)&dst, src, N); |
|
|
227 | } |
|
|
228 | |
|
|
229 | typedef double tstamp; |
|
|
230 | |
|
|
231 | // return current time as timestampe |
|
|
232 | tstamp now (); |
|
|
233 | |
115 | #endif |
234 | #endif |
116 | |
235 | |