… | |
… | |
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 | |
10 | // makes dynamically allocated objects zero-initialised |
14 | // makes dynamically allocated objects zero-initialised |
11 | struct zero_initialised |
15 | struct zero_initialised |
12 | { |
16 | { |
13 | void *operator new (size_t s, void *); |
17 | void *operator new (size_t s, void *p) |
|
|
18 | { |
|
|
19 | memset (p, 0, s); |
|
|
20 | return p; |
|
|
21 | } |
|
|
22 | |
14 | void *operator new (size_t s); |
23 | void *operator new (size_t s) |
|
|
24 | { |
|
|
25 | return g_slice_alloc0 (s); |
|
|
26 | } |
|
|
27 | |
15 | void *operator new [] (size_t s); |
28 | void *operator new[] (size_t s) |
|
|
29 | { |
|
|
30 | return g_slice_alloc0 (s); |
|
|
31 | } |
|
|
32 | |
16 | void operator delete (void *p, size_t s); |
33 | void operator delete (void *p, size_t s) |
|
|
34 | { |
|
|
35 | g_slice_free1 (s, p); |
|
|
36 | } |
|
|
37 | |
17 | void operator delete [] (void *p, size_t s); |
38 | void operator delete[] (void *p, size_t s) |
|
|
39 | { |
|
|
40 | g_slice_free1 (s, p); |
|
|
41 | } |
|
|
42 | }; |
|
|
43 | |
|
|
44 | // strictly the same as g_slice_alloc, but never returns 0 |
|
|
45 | void *alloc (int s) throw (std::bad_alloc); |
|
|
46 | // for symmetry |
|
|
47 | inline void dealloc (void *p, int s) throw () |
|
|
48 | { |
|
|
49 | g_slice_free1 (s, p); |
|
|
50 | } |
|
|
51 | |
|
|
52 | // a STL-compatible allocator that uses g_slice |
|
|
53 | // boy, this is verbose |
|
|
54 | template<typename Tp> |
|
|
55 | struct slice_allocator |
|
|
56 | { |
|
|
57 | typedef size_t size_type; |
|
|
58 | typedef ptrdiff_t difference_type; |
|
|
59 | typedef Tp *pointer; |
|
|
60 | typedef const Tp *const_pointer; |
|
|
61 | typedef Tp &reference; |
|
|
62 | typedef const Tp &const_reference; |
|
|
63 | typedef Tp value_type; |
|
|
64 | |
|
|
65 | template <class U> |
|
|
66 | struct rebind |
|
|
67 | { |
|
|
68 | typedef slice_allocator<U> other; |
|
|
69 | }; |
|
|
70 | |
|
|
71 | slice_allocator () throw () { } |
|
|
72 | slice_allocator (const slice_allocator &o) throw () { } |
|
|
73 | template<typename Tp2> |
|
|
74 | slice_allocator (const slice_allocator<Tp2> &) throw () { } |
|
|
75 | |
|
|
76 | ~slice_allocator () { } |
|
|
77 | |
|
|
78 | pointer address (reference x) const { return &x; } |
|
|
79 | const_pointer address (const_reference x) const { return &x; } |
|
|
80 | |
|
|
81 | pointer allocate (size_type n, const_pointer = 0) |
|
|
82 | { |
|
|
83 | return static_cast<pointer>(alloc (n * sizeof (Tp))); |
|
|
84 | } |
|
|
85 | |
|
|
86 | void deallocate (pointer p, size_type n) |
|
|
87 | { |
|
|
88 | dealloc (static_cast<void *>(p), n); |
|
|
89 | } |
|
|
90 | |
|
|
91 | size_type max_size ()const throw () |
|
|
92 | { |
|
|
93 | return size_t (-1) / sizeof (Tp); |
|
|
94 | } |
|
|
95 | |
|
|
96 | void construct (pointer p, const Tp &val) |
|
|
97 | { |
|
|
98 | ::new (p) Tp (val); |
|
|
99 | } |
|
|
100 | |
|
|
101 | void destroy (pointer p) |
|
|
102 | { |
|
|
103 | p->~Tp (); |
|
|
104 | } |
18 | }; |
105 | }; |
19 | |
106 | |
20 | struct refcounted |
107 | struct refcounted |
21 | { |
108 | { |
22 | mutable int refcnt; |
109 | mutable int refcnt; |
… | |
… | |
92 | }; |
179 | }; |
93 | |
180 | |
94 | #include <vector> |
181 | #include <vector> |
95 | |
182 | |
96 | template<class obj> |
183 | template<class obj> |
97 | struct unordered_vector : std::vector<obj> |
184 | struct unordered_vector : std::vector<obj, slice_allocator<obj> > |
98 | { |
185 | { |
99 | typedef typename std::vector<obj>::iterator iterator; |
186 | typedef typename unordered_vector::iterator iterator; |
100 | |
187 | |
101 | void erase (unsigned int pos) |
188 | void erase (unsigned int pos) |
102 | { |
189 | { |
103 | if (pos < this->size () - 1) |
190 | if (pos < this->size () - 1) |
104 | (*this)[pos] = (*this)[this->size () - 1]; |
191 | (*this)[pos] = (*this)[this->size () - 1]; |