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