Debian Codegraph @

external/bdwgc/include/gc_allocator.h

raw
1 /*
2 * Copyright (c) 1996-1997
3 * Silicon Graphics Computer Systems, Inc.
4 *
5 * Permission to use, copy, modify, distribute and sell this software
6 * and its documentation for any purpose is hereby granted without fee,
7 * provided that the above copyright notice appear in all copies and
8 * that both that copyright notice and this permission notice appear
9 * in supporting documentation. Silicon Graphics makes no
10 * representations about the suitability of this software for any
11 * purpose. It is provided "as is" without express or implied warranty.
12 *
13 * Copyright (c) 2002
14 * Hewlett-Packard Company
15 *
16 * Permission to use, copy, modify, distribute and sell this software
17 * and its documentation for any purpose is hereby granted without fee,
18 * provided that the above copyright notice appear in all copies and
19 * that both that copyright notice and this permission notice appear
20 * in supporting documentation. Hewlett-Packard Company makes no
21 * representations about the suitability of this software for any
22 * purpose. It is provided "as is" without express or implied warranty.
23 */
24
25 /*
26 * This implements standard-conforming allocators that interact with
27 * the garbage collector. Gc_allocator<T> allocates garbage-collectible
28 * objects of type T. Traceable_allocator<T> allocates objects that
29 * are not themselves garbage collected, but are scanned by the
30 * collector for pointers to collectible objects. Traceable_alloc
31 * should be used for explicitly managed STL containers that may
32 * point to collectible objects.
33 *
34 * This code was derived from an earlier version of the GNU C++ standard
35 * library, which itself was derived from the SGI STL implementation.
36 *
37 * Ignore-off-page allocator: George T. Talbot
38 */
39
40 #ifndef GC_ALLOCATOR_H
41
42 #define GC_ALLOCATOR_H
43
44 #include "gc.h"
45 #include <new> // for placement new
46
47 #ifndef GC_ATTR_EXPLICIT
48 # if (__cplusplus >= 201103L) || defined(CPPCHECK)
49 # define GC_ATTR_EXPLICIT explicit
50 # else
51 # define GC_ATTR_EXPLICIT /* empty */
52 # endif
53 #endif
54
55 /* First some helpers to allow us to dispatch on whether or not a type
56 * is known to be pointer-free.
57 * These are private, except that the client may invoke the
58 * GC_DECLARE_PTRFREE macro.
59 */
60
61 struct GC_true_type {};
62 struct GC_false_type {};
63
64 template <class GC_tp>
65 struct GC_type_traits {
66 GC_false_type GC_is_ptr_free;
67 };
68
69 # define GC_DECLARE_PTRFREE(T) \
70 template<> struct GC_type_traits<T> { GC_true_type GC_is_ptr_free; }
71
72 GC_DECLARE_PTRFREE(char);
73 GC_DECLARE_PTRFREE(signed char);
74 GC_DECLARE_PTRFREE(unsigned char);
75 GC_DECLARE_PTRFREE(signed short);
76 GC_DECLARE_PTRFREE(unsigned short);
77 GC_DECLARE_PTRFREE(signed int);
78 GC_DECLARE_PTRFREE(unsigned int);
79 GC_DECLARE_PTRFREE(signed long);
80 GC_DECLARE_PTRFREE(unsigned long);
81 GC_DECLARE_PTRFREE(float);
82 GC_DECLARE_PTRFREE(double);
83 GC_DECLARE_PTRFREE(long double);
84 /* The client may want to add others. */
85
86 // In the following GC_Tp is GC_true_type if we are allocating a
87 // pointer-free object.
88 template <class GC_Tp>
89 inline void * GC_selective_alloc(size_t n, GC_Tp, bool ignore_off_page) {
90 return ignore_off_page?GC_MALLOC_IGNORE_OFF_PAGE(n):GC_MALLOC(n);
91 }
92
93 template <>
94 inline void * GC_selective_alloc<GC_true_type>(size_t n, GC_true_type,
95 bool ignore_off_page) {
96 return ignore_off_page? GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(n)
97 : GC_MALLOC_ATOMIC(n);
98 }
99
100 /* Now the public gc_allocator<T> class:
101 */
102 template <class GC_Tp>
103 class gc_allocator {
104 public:
105 typedef size_t size_type;
106 typedef ptrdiff_t difference_type;
107 typedef GC_Tp* pointer;
108 typedef const GC_Tp* const_pointer;
109 typedef GC_Tp& reference;
110 typedef const GC_Tp& const_reference;
111 typedef GC_Tp value_type;
112
113 template <class GC_Tp1> struct rebind {
114 typedef gc_allocator<GC_Tp1> other;
115 };
116
117 gc_allocator() {}
118 gc_allocator(const gc_allocator&) throw() {}
119 # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
120 // MSVC++ 6.0 do not support member templates
121 template <class GC_Tp1> GC_ATTR_EXPLICIT
122 gc_allocator(const gc_allocator<GC_Tp1>&) throw() {}
123 # endif
124 ~gc_allocator() throw() {}
125
126 pointer address(reference GC_x) const { return &GC_x; }
127 const_pointer address(const_reference GC_x) const { return &GC_x; }
128
129 // GC_n is permitted to be 0. The C++ standard says nothing about what
130 // the return value is when GC_n == 0.
131 GC_Tp* allocate(size_type GC_n, const void* = 0) {
132 GC_type_traits<GC_Tp> traits;
133 return static_cast<GC_Tp *>
134 (GC_selective_alloc(GC_n * sizeof(GC_Tp),
135 traits.GC_is_ptr_free, false));
136 }
137
138 // __p is not permitted to be a null pointer.
139 void deallocate(pointer __p, size_type /* GC_n */)
140 { GC_FREE(__p); }
141
142 size_type max_size() const throw()
143 { return size_t(-1) / sizeof(GC_Tp); }
144
145 void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
146 void destroy(pointer __p) { __p->~GC_Tp(); }
147 };
148
149 template<>
150 class gc_allocator<void> {
151 typedef size_t size_type;
152 typedef ptrdiff_t difference_type;
153 typedef void* pointer;
154 typedef const void* const_pointer;
155 typedef void value_type;
156
157 template <class GC_Tp1> struct rebind {
158 typedef gc_allocator<GC_Tp1> other;
159 };
160 };
161
162
163 template <class GC_T1, class GC_T2>
164 inline bool operator==(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
165 {
166 return true;
167 }
168
169 template <class GC_T1, class GC_T2>
170 inline bool operator!=(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
171 {
172 return false;
173 }
174
175
176 /* Now the public gc_allocator_ignore_off_page<T> class:
177 */
178 template <class GC_Tp>
179 class gc_allocator_ignore_off_page {
180 public:
181 typedef size_t size_type;
182 typedef ptrdiff_t difference_type;
183 typedef GC_Tp* pointer;
184 typedef const GC_Tp* const_pointer;
185 typedef GC_Tp& reference;
186 typedef const GC_Tp& const_reference;
187 typedef GC_Tp value_type;
188
189 template <class GC_Tp1> struct rebind {
190 typedef gc_allocator_ignore_off_page<GC_Tp1> other;
191 };
192
193 gc_allocator_ignore_off_page() {}
194 gc_allocator_ignore_off_page(const gc_allocator_ignore_off_page&) throw() {}
195 # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
196 // MSVC++ 6.0 do not support member templates
197 template <class GC_Tp1> GC_ATTR_EXPLICIT
198 gc_allocator_ignore_off_page(const gc_allocator_ignore_off_page<GC_Tp1>&)
199 throw() {}
200 # endif
201 ~gc_allocator_ignore_off_page() throw() {}
202
203 pointer address(reference GC_x) const { return &GC_x; }
204 const_pointer address(const_reference GC_x) const { return &GC_x; }
205
206 // GC_n is permitted to be 0. The C++ standard says nothing about what
207 // the return value is when GC_n == 0.
208 GC_Tp* allocate(size_type GC_n, const void* = 0) {
209 GC_type_traits<GC_Tp> traits;
210 return static_cast<GC_Tp *>
211 (GC_selective_alloc(GC_n * sizeof(GC_Tp),
212 traits.GC_is_ptr_free, true));
213 }
214
215 // __p is not permitted to be a null pointer.
216 void deallocate(pointer __p, size_type /* GC_n */)
217 { GC_FREE(__p); }
218
219 size_type max_size() const throw()
220 { return size_t(-1) / sizeof(GC_Tp); }
221
222 void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
223 void destroy(pointer __p) { __p->~GC_Tp(); }
224 };
225
226 template<>
227 class gc_allocator_ignore_off_page<void> {
228 typedef size_t size_type;
229 typedef ptrdiff_t difference_type;
230 typedef void* pointer;
231 typedef const void* const_pointer;
232 typedef void value_type;
233
234 template <class GC_Tp1> struct rebind {
235 typedef gc_allocator_ignore_off_page<GC_Tp1> other;
236 };
237 };
238
239 template <class GC_T1, class GC_T2>
240 inline bool operator==(const gc_allocator_ignore_off_page<GC_T1>&, const gc_allocator_ignore_off_page<GC_T2>&)
241 {
242 return true;
243 }
244
245 template <class GC_T1, class GC_T2>
246 inline bool operator!=(const gc_allocator_ignore_off_page<GC_T1>&, const gc_allocator_ignore_off_page<GC_T2>&)
247 {
248 return false;
249 }
250
251 /*
252 * And the public traceable_allocator class.
253 */
254
255 // Note that we currently don't specialize the pointer-free case, since a
256 // pointer-free traceable container doesn't make that much sense,
257 // though it could become an issue due to abstraction boundaries.
258 template <class GC_Tp>
259 class traceable_allocator {
260 public:
261 typedef size_t size_type;
262 typedef ptrdiff_t difference_type;
263 typedef GC_Tp* pointer;
264 typedef const GC_Tp* const_pointer;
265 typedef GC_Tp& reference;
266 typedef const GC_Tp& const_reference;
267 typedef GC_Tp value_type;
268
269 template <class GC_Tp1> struct rebind {
270 typedef traceable_allocator<GC_Tp1> other;
271 };
272
273 traceable_allocator() throw() {}
274 traceable_allocator(const traceable_allocator&) throw() {}
275 # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
276 // MSVC++ 6.0 do not support member templates
277 template <class GC_Tp1> GC_ATTR_EXPLICIT
278 traceable_allocator(const traceable_allocator<GC_Tp1>&) throw() {}
279 # endif
280 ~traceable_allocator() throw() {}
281
282 pointer address(reference GC_x) const { return &GC_x; }
283 const_pointer address(const_reference GC_x) const { return &GC_x; }
284
285 // GC_n is permitted to be 0. The C++ standard says nothing about what
286 // the return value is when GC_n == 0.
287 GC_Tp* allocate(size_type GC_n, const void* = 0) {
288 return static_cast<GC_Tp*>(GC_MALLOC_UNCOLLECTABLE(GC_n * sizeof(GC_Tp)));
289 }
290
291 // __p is not permitted to be a null pointer.
292 void deallocate(pointer __p, size_type /* GC_n */)
293 { GC_FREE(__p); }
294
295 size_type max_size() const throw()
296 { return size_t(-1) / sizeof(GC_Tp); }
297
298 void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
299 void destroy(pointer __p) { __p->~GC_Tp(); }
300 };
301
302 template<>
303 class traceable_allocator<void> {
304 typedef size_t size_type;
305 typedef ptrdiff_t difference_type;
306 typedef void* pointer;
307 typedef const void* const_pointer;
308 typedef void value_type;
309
310 template <class GC_Tp1> struct rebind {
311 typedef traceable_allocator<GC_Tp1> other;
312 };
313 };
314
315
316 template <class GC_T1, class GC_T2>
317 inline bool operator==(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
318 {
319 return true;
320 }
321
322 template <class GC_T1, class GC_T2>
323 inline bool operator!=(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
324 {
325 return false;
326 }
327
328 #endif /* GC_ALLOCATOR_H */