Debian Codegraph @

gc/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 #define GC_ALLOCATOR_H
42
43 #include "gc.h"
44 #include <new> // for placement new and bad_alloc
45
46 #if !defined(GC_NO_MEMBER_TEMPLATES) && defined(_MSC_VER) && _MSC_VER <= 1200
47 // MSVC++ 6.0 do not support member templates.
48 # define GC_NO_MEMBER_TEMPLATES
49 #endif
50
51 #if defined(GC_NEW_ABORTS_ON_OOM) || defined(_LIBCPP_NO_EXCEPTIONS)
52 # define GC_ALLOCATOR_THROW_OR_ABORT() GC_abort_on_oom()
53 #else
54 # define GC_ALLOCATOR_THROW_OR_ABORT() throw std::bad_alloc()
55 #endif
56
57 /* First some helpers to allow us to dispatch on whether or not a type
58 * is known to be pointer-free.
59 * These are private, except that the client may invoke the
60 * GC_DECLARE_PTRFREE macro.
61 */
62
63 struct GC_true_type {};
64 struct GC_false_type {};
65
66 template <class GC_tp>
67 struct GC_type_traits {
68 GC_false_type GC_is_ptr_free;
69 };
70
71 # define GC_DECLARE_PTRFREE(T) \
72 template<> struct GC_type_traits<T> { GC_true_type GC_is_ptr_free; }
73
74 GC_DECLARE_PTRFREE(char);
75 GC_DECLARE_PTRFREE(signed char);
76 GC_DECLARE_PTRFREE(unsigned char);
77 GC_DECLARE_PTRFREE(signed short);
78 GC_DECLARE_PTRFREE(unsigned short);
79 GC_DECLARE_PTRFREE(signed int);
80 GC_DECLARE_PTRFREE(unsigned int);
81 GC_DECLARE_PTRFREE(signed long);
82 GC_DECLARE_PTRFREE(unsigned long);
83 GC_DECLARE_PTRFREE(float);
84 GC_DECLARE_PTRFREE(double);
85 GC_DECLARE_PTRFREE(long double);
86 /* The client may want to add others. */
87
88 // In the following GC_Tp is GC_true_type if we are allocating a
89 // pointer-free object.
90 template <class GC_Tp>
91 inline void * GC_selective_alloc(size_t n, GC_Tp, bool ignore_off_page) {
92 void *obj = ignore_off_page ? GC_MALLOC_IGNORE_OFF_PAGE(n) : GC_MALLOC(n);
93 if (0 == obj)
94 GC_ALLOCATOR_THROW_OR_ABORT();
95 return obj;
96 }
97
98 #if !defined(__WATCOMC__)
99 /* Note: template-id not supported in this context by Watcom compiler. */
100 template <>
101 inline void * GC_selective_alloc<GC_true_type>(size_t n, GC_true_type,
102 bool ignore_off_page) {
103 void * obj = ignore_off_page ? GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(n)
104 : GC_MALLOC_ATOMIC(n);
105 if (0 == obj)
106 GC_ALLOCATOR_THROW_OR_ABORT();
107 return obj;
108 }
109 #endif
110
111 // Now the public gc_allocator<T> class.
112 template <class GC_Tp>
113 class gc_allocator {
114 public:
115 typedef size_t size_type;
116 typedef ptrdiff_t difference_type;
117 typedef GC_Tp* pointer;
118 typedef const GC_Tp* const_pointer;
119 typedef GC_Tp& reference;
120 typedef const GC_Tp& const_reference;
121 typedef GC_Tp value_type;
122
123 template <class GC_Tp1> struct rebind {
124 typedef gc_allocator<GC_Tp1> other;
125 };
126
127 GC_CONSTEXPR gc_allocator() GC_NOEXCEPT {}
128 GC_CONSTEXPR gc_allocator(const gc_allocator&) GC_NOEXCEPT {}
129 # ifndef GC_NO_MEMBER_TEMPLATES
130 template <class GC_Tp1> GC_ATTR_EXPLICIT
131 GC_CONSTEXPR gc_allocator(const gc_allocator<GC_Tp1>&) GC_NOEXCEPT {}
132 # endif
133 GC_CONSTEXPR ~gc_allocator() GC_NOEXCEPT {}
134
135 GC_CONSTEXPR pointer address(reference GC_x) const { return &GC_x; }
136 GC_CONSTEXPR const_pointer address(const_reference GC_x) const {
137 return &GC_x;
138 }
139
140 // GC_n is permitted to be 0. The C++ standard says nothing about what
141 // the return value is when GC_n == 0.
142 GC_CONSTEXPR GC_Tp* allocate(size_type GC_n, const void* = 0) {
143 GC_type_traits<GC_Tp> traits;
144 return static_cast<GC_Tp *>(GC_selective_alloc(GC_n * sizeof(GC_Tp),
145 traits.GC_is_ptr_free, false));
146 }
147
148 GC_CONSTEXPR void deallocate(pointer __p, size_type /* GC_n */) GC_NOEXCEPT {
149 GC_FREE(__p);
150 }
151
152 GC_CONSTEXPR size_type max_size() const GC_NOEXCEPT
153 { return size_t(-1) / sizeof(GC_Tp); }
154
155 GC_CONSTEXPR void construct(pointer __p, const GC_Tp& __val) {
156 new(__p) GC_Tp(__val);
157 }
158
159 GC_CONSTEXPR void destroy(pointer __p) { __p->~GC_Tp(); }
160 };
161
162 template<>
163 class gc_allocator<void> {
164 public:
165 typedef size_t size_type;
166 typedef ptrdiff_t difference_type;
167 typedef void* pointer;
168 typedef const void* const_pointer;
169 typedef void value_type;
170
171 template <class GC_Tp1> struct rebind {
172 typedef gc_allocator<GC_Tp1> other;
173 };
174 };
175
176
177 template <class GC_T1, class GC_T2>
178 GC_CONSTEXPR inline bool operator==(const gc_allocator<GC_T1>&,
179 const gc_allocator<GC_T2>&) GC_NOEXCEPT {
180 return true;
181 }
182
183 template <class GC_T1, class GC_T2>
184 GC_CONSTEXPR inline bool operator!=(const gc_allocator<GC_T1>&,
185 const gc_allocator<GC_T2>&) GC_NOEXCEPT {
186 return false;
187 }
188
189
190 // Now the public gc_allocator_ignore_off_page<T> class.
191 template <class GC_Tp>
192 class gc_allocator_ignore_off_page {
193 public:
194 typedef size_t size_type;
195 typedef ptrdiff_t difference_type;
196 typedef GC_Tp* pointer;
197 typedef const GC_Tp* const_pointer;
198 typedef GC_Tp& reference;
199 typedef const GC_Tp& const_reference;
200 typedef GC_Tp value_type;
201
202 template <class GC_Tp1> struct rebind {
203 typedef gc_allocator_ignore_off_page<GC_Tp1> other;
204 };
205
206 GC_CONSTEXPR gc_allocator_ignore_off_page() GC_NOEXCEPT {}
207 GC_CONSTEXPR gc_allocator_ignore_off_page(
208 const gc_allocator_ignore_off_page&) GC_NOEXCEPT {}
209 # ifndef GC_NO_MEMBER_TEMPLATES
210 template <class GC_Tp1> GC_ATTR_EXPLICIT
211 GC_CONSTEXPR gc_allocator_ignore_off_page(
212 const gc_allocator_ignore_off_page<GC_Tp1>&) GC_NOEXCEPT {}
213 # endif
214 GC_CONSTEXPR ~gc_allocator_ignore_off_page() GC_NOEXCEPT {}
215
216 GC_CONSTEXPR pointer address(reference GC_x) const { return &GC_x; }
217 GC_CONSTEXPR const_pointer address(const_reference GC_x) const {
218 return &GC_x;
219 }
220
221 // GC_n is permitted to be 0. The C++ standard says nothing about what
222 // the return value is when GC_n == 0.
223 GC_CONSTEXPR GC_Tp* allocate(size_type GC_n, const void* = 0) {
224 GC_type_traits<GC_Tp> traits;
225 return static_cast<GC_Tp *>(GC_selective_alloc(GC_n * sizeof(GC_Tp),
226 traits.GC_is_ptr_free, true));
227 }
228
229 GC_CONSTEXPR void deallocate(pointer __p, size_type /* GC_n */) GC_NOEXCEPT {
230 GC_FREE(__p);
231 }
232
233 GC_CONSTEXPR size_type max_size() const GC_NOEXCEPT
234 { return size_t(-1) / sizeof(GC_Tp); }
235
236 GC_CONSTEXPR void construct(pointer __p, const GC_Tp& __val) {
237 new(__p) GC_Tp(__val);
238 }
239
240 GC_CONSTEXPR void destroy(pointer __p) { __p->~GC_Tp(); }
241 };
242
243 template<>
244 class gc_allocator_ignore_off_page<void> {
245 public:
246 typedef size_t size_type;
247 typedef ptrdiff_t difference_type;
248 typedef void* pointer;
249 typedef const void* const_pointer;
250 typedef void value_type;
251
252 template <class GC_Tp1> struct rebind {
253 typedef gc_allocator_ignore_off_page<GC_Tp1> other;
254 };
255 };
256
257 template <class GC_T1, class GC_T2>
258 GC_CONSTEXPR inline bool operator==(const gc_allocator_ignore_off_page<GC_T1>&,
259 const gc_allocator_ignore_off_page<GC_T2>&) GC_NOEXCEPT {
260 return true;
261 }
262
263 template <class GC_T1, class GC_T2>
264 GC_CONSTEXPR inline bool operator!=(const gc_allocator_ignore_off_page<GC_T1>&,
265 const gc_allocator_ignore_off_page<GC_T2>&) GC_NOEXCEPT {
266 return false;
267 }
268
269 // And the public traceable_allocator class.
270
271 /* Note that we currently don't specialize the pointer-free case, since a
272 * pointer-free traceable container doesn't make that much sense,
273 * though it could become an issue due to abstraction boundaries.
274 */
275
276 template <class GC_Tp>
277 class traceable_allocator {
278 public:
279 typedef size_t size_type;
280 typedef ptrdiff_t difference_type;
281 typedef GC_Tp* pointer;
282 typedef const GC_Tp* const_pointer;
283 typedef GC_Tp& reference;
284 typedef const GC_Tp& const_reference;
285 typedef GC_Tp value_type;
286
287 template <class GC_Tp1> struct rebind {
288 typedef traceable_allocator<GC_Tp1> other;
289 };
290
291 GC_CONSTEXPR traceable_allocator() GC_NOEXCEPT {}
292 GC_CONSTEXPR traceable_allocator(const traceable_allocator&) GC_NOEXCEPT {}
293 # ifndef GC_NO_MEMBER_TEMPLATES
294 template <class GC_Tp1> GC_ATTR_EXPLICIT
295 GC_CONSTEXPR traceable_allocator(
296 const traceable_allocator<GC_Tp1>&) GC_NOEXCEPT {}
297 # endif
298 GC_CONSTEXPR ~traceable_allocator() GC_NOEXCEPT {}
299
300 GC_CONSTEXPR pointer address(reference GC_x) const { return &GC_x; }
301 GC_CONSTEXPR const_pointer address(const_reference GC_x) const {
302 return &GC_x;
303 }
304
305 // GC_n is permitted to be 0. The C++ standard says nothing about what
306 // the return value is when GC_n == 0.
307 GC_CONSTEXPR GC_Tp* allocate(size_type GC_n, const void* = 0) {
308 void * obj = GC_MALLOC_UNCOLLECTABLE(GC_n * sizeof(GC_Tp));
309 if (0 == obj)
310 GC_ALLOCATOR_THROW_OR_ABORT();
311 return static_cast<GC_Tp*>(obj);
312 }
313
314 GC_CONSTEXPR void deallocate(pointer __p, size_type /* GC_n */) GC_NOEXCEPT {
315 GC_FREE(__p);
316 }
317
318 GC_CONSTEXPR size_type max_size() const GC_NOEXCEPT
319 { return size_t(-1) / sizeof(GC_Tp); }
320
321 GC_CONSTEXPR void construct(pointer __p, const GC_Tp& __val) {
322 new(__p) GC_Tp(__val);
323 }
324
325 GC_CONSTEXPR void destroy(pointer __p) { __p->~GC_Tp(); }
326 };
327
328 template<>
329 class traceable_allocator<void> {
330 public:
331 typedef size_t size_type;
332 typedef ptrdiff_t difference_type;
333 typedef void* pointer;
334 typedef const void* const_pointer;
335 typedef void value_type;
336
337 template <class GC_Tp1> struct rebind {
338 typedef traceable_allocator<GC_Tp1> other;
339 };
340 };
341
342
343 template <class GC_T1, class GC_T2>
344 GC_CONSTEXPR inline bool operator==(const traceable_allocator<GC_T1>&,
345 const traceable_allocator<GC_T2>&) GC_NOEXCEPT {
346 return true;
347 }
348
349 template <class GC_T1, class GC_T2>
350 GC_CONSTEXPR inline bool operator!=(const traceable_allocator<GC_T1>&,
351 const traceable_allocator<GC_T2>&) GC_NOEXCEPT {
352 return false;
353 }
354
355 #endif /* GC_ALLOCATOR_H */