… | |
… | |
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 | // if src is 0, clears the memory |
|
|
51 | void *salloc (int size, void *src) throw (std::bad_alloc); |
|
|
52 | |
|
|
53 | // and as a template |
|
|
54 | template<typename T> |
|
|
55 | inline T *salloc (int size) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T)); } |
|
|
56 | template<typename T> |
|
|
57 | inline T *salloc (int size, T *src) throw (std::bad_alloc) { return (T *)salloc (size * sizeof (T), (void *)src); } |
|
|
58 | |
|
|
59 | // for symmetry |
|
|
60 | template<typename T> |
|
|
61 | inline void sfree (T *ptr, int size) throw () |
|
|
62 | { |
|
|
63 | g_slice_free1 (size * sizeof (T), (void *)ptr); |
|
|
64 | } |
|
|
65 | |
|
|
66 | // a STL-compatible allocator that uses g_slice |
|
|
67 | // boy, this is verbose |
|
|
68 | template<typename Tp> |
|
|
69 | struct slice_allocator |
|
|
70 | { |
|
|
71 | typedef size_t size_type; |
|
|
72 | typedef ptrdiff_t difference_type; |
|
|
73 | typedef Tp *pointer; |
|
|
74 | typedef const Tp *const_pointer; |
|
|
75 | typedef Tp &reference; |
|
|
76 | typedef const Tp &const_reference; |
|
|
77 | typedef Tp value_type; |
|
|
78 | |
|
|
79 | template <class U> |
|
|
80 | struct rebind |
|
|
81 | { |
|
|
82 | typedef slice_allocator<U> other; |
|
|
83 | }; |
|
|
84 | |
|
|
85 | slice_allocator () throw () { } |
|
|
86 | slice_allocator (const slice_allocator &o) throw () { } |
|
|
87 | template<typename Tp2> |
|
|
88 | slice_allocator (const slice_allocator<Tp2> &) throw () { } |
|
|
89 | |
|
|
90 | ~slice_allocator () { } |
|
|
91 | |
|
|
92 | pointer address (reference x) const { return &x; } |
|
|
93 | const_pointer address (const_reference x) const { return &x; } |
|
|
94 | |
|
|
95 | pointer allocate (size_type n, const_pointer = 0) |
|
|
96 | { |
|
|
97 | return salloc<Tp> (n); |
|
|
98 | } |
|
|
99 | |
|
|
100 | void deallocate (pointer p, size_type n) |
|
|
101 | { |
|
|
102 | sfree (p, n); |
|
|
103 | } |
|
|
104 | |
|
|
105 | size_type max_size ()const throw () |
|
|
106 | { |
|
|
107 | return size_t (-1) / sizeof (Tp); |
|
|
108 | } |
|
|
109 | |
|
|
110 | void construct (pointer p, const Tp &val) |
|
|
111 | { |
|
|
112 | ::new (p) Tp (val); |
|
|
113 | } |
|
|
114 | |
|
|
115 | void destroy (pointer p) |
|
|
116 | { |
|
|
117 | p->~Tp (); |
|
|
118 | } |
18 | }; |
119 | }; |
19 | |
120 | |
20 | struct refcounted |
121 | struct refcounted |
21 | { |
122 | { |
|
|
123 | refcounted () : refcnt (0) { } |
|
|
124 | // virtual ~refcounted (); |
|
|
125 | void refcnt_inc () { ++refcnt; } |
|
|
126 | void refcnt_dec () { --refcnt; } |
|
|
127 | bool dead () { return refcnt == 0; } |
22 | mutable int refcnt; |
128 | mutable int refcnt; |
23 | refcounted () : refcnt (0) { } |
129 | #if 0 |
24 | void refcnt_inc () { ++refcnt; } |
130 | private: |
25 | void refcnt_dec () { --refcnt; |
131 | static refcounted *rc_first; |
26 | if (refcnt < 0)abort();}//D |
132 | refcounted *rc_next; |
|
|
133 | #endif |
27 | }; |
134 | }; |
28 | |
135 | |
29 | template<class T> |
136 | template<class T> |
30 | struct refptr |
137 | struct refptr |
31 | { |
138 | { |
… | |
… | |
92 | }; |
199 | }; |
93 | |
200 | |
94 | #include <vector> |
201 | #include <vector> |
95 | |
202 | |
96 | template<class obj> |
203 | template<class obj> |
97 | struct unordered_vector : std::vector<obj> |
204 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
98 | { |
205 | { |
99 | typedef typename std::vector<obj>::iterator iterator; |
206 | typedef typename unordered_vector::iterator iterator; |
100 | |
207 | |
101 | void erase (unsigned int pos) |
208 | void erase (unsigned int pos) |
102 | { |
209 | { |
103 | if (pos < this->size () - 1) |
210 | if (pos < this->size () - 1) |
104 | (*this)[pos] = (*this)[this->size () - 1]; |
211 | (*this)[pos] = (*this)[this->size () - 1]; |
… | |
… | |
116 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
223 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
117 | 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; } |
224 | 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; } |
118 | |
225 | |
119 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
226 | template<typename T, typename U> static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } |
120 | |
227 | |
121 | // basically does what strncpy should do |
228 | // basically does what strncpy should do, but appends "..." to strings exceeding length |
|
|
229 | void assign (char *dst, const char *src, int maxlen); |
|
|
230 | |
|
|
231 | // type-safe version of assign |
122 | template<int N> |
232 | template<int N> |
123 | inline void assign (char (&dst)[N], const char *src) |
233 | inline void assign (char (&dst)[N], const char *src) |
124 | { |
234 | { |
125 | // should be optimised at some point, maybe should also add "..." |
235 | assign ((char *)&dst, src, N); |
126 | // when buffer is too small. |
|
|
127 | snprintf (dst, N, "%s", src); |
|
|
128 | } |
236 | } |
129 | |
237 | |
|
|
238 | typedef double tstamp; |
|
|
239 | |
|
|
240 | // return current time as timestampe |
|
|
241 | tstamp now (); |
|
|
242 | |
130 | #endif |
243 | #endif |
131 | |
244 | |