PR 47802 Use builtins to check localtime_r return type
[official-gcc.git] / boehm-gc / include / gc_allocator.h
blob200f181efa6f17261b8187570389c50c96ae2814
1 /*
2 * Copyright (c) 1996-1997
3 * Silicon Graphics Computer Systems, Inc.
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.
13 * Copyright (c) 2002
14 * Hewlett-Packard Company
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.
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 temselves 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.
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.
38 #ifndef GC_ALLOCATOR_H
40 #define GC_ALLOCATOR_H
42 #include "gc.h"
44 #if defined(__GNUC__)
45 # define GC_ATTR_UNUSED __attribute__((unused))
46 #else
47 # define GC_ATTR_UNUSED
48 #endif
50 /* First some helpers to allow us to dispatch on whether or not a type
51 * is known to be pointerfree.
52 * These are private, except that the client may invoke the
53 * GC_DECLARE_PTRFREE macro.
56 struct GC_true_type {};
57 struct GC_false_type {};
59 template <class GC_tp>
60 struct GC_type_traits {
61 GC_false_type GC_is_ptr_free;
64 # define GC_DECLARE_PTRFREE(T) \
65 template<> struct GC_type_traits<T> { GC_true_type GC_is_ptr_free; }
67 GC_DECLARE_PTRFREE(signed char);
68 GC_DECLARE_PTRFREE(unsigned char);
69 GC_DECLARE_PTRFREE(signed short);
70 GC_DECLARE_PTRFREE(unsigned short);
71 GC_DECLARE_PTRFREE(signed int);
72 GC_DECLARE_PTRFREE(unsigned int);
73 GC_DECLARE_PTRFREE(signed long);
74 GC_DECLARE_PTRFREE(unsigned long);
75 GC_DECLARE_PTRFREE(float);
76 GC_DECLARE_PTRFREE(double);
77 /* The client may want to add others. */
79 // In the following GC_Tp is GC_true_type iff we are allocating a
80 // pointerfree object.
81 template <class GC_Tp>
82 inline void * GC_selective_alloc(size_t n, GC_Tp) {
83 return GC_MALLOC(n);
86 template <>
87 inline void * GC_selective_alloc<GC_true_type>(size_t n, GC_true_type) {
88 return GC_MALLOC_ATOMIC(n);
91 /* Now the public gc_allocator<T> class:
93 template <class GC_Tp>
94 class gc_allocator {
95 public:
96 typedef size_t size_type;
97 typedef ptrdiff_t difference_type;
98 typedef GC_Tp* pointer;
99 typedef const GC_Tp* const_pointer;
100 typedef GC_Tp& reference;
101 typedef const GC_Tp& const_reference;
102 typedef GC_Tp value_type;
104 template <class GC_Tp1> struct rebind {
105 typedef gc_allocator<GC_Tp1> other;
108 gc_allocator() {}
109 # ifndef _MSC_VER
110 // I'm not sure why this is needed here in addition to the following.
111 // The standard specifies it for the standard allocator, but VC++ rejects
112 // it. -HB
113 gc_allocator(const gc_allocator&) throw() {}
114 # endif
115 template <class GC_Tp1> gc_allocator(const gc_allocator<GC_Tp1>&) throw() {}
116 ~gc_allocator() throw() {}
118 pointer address(reference GC_x) const { return &GC_x; }
119 const_pointer address(const_reference GC_x) const { return &GC_x; }
121 // GC_n is permitted to be 0. The C++ standard says nothing about what
122 // the return value is when GC_n == 0.
123 GC_Tp* allocate(size_type GC_n, const void* = 0) {
124 GC_type_traits<GC_Tp> traits;
125 return static_cast<GC_Tp *>
126 (GC_selective_alloc(GC_n * sizeof(GC_Tp),
127 traits.GC_is_ptr_free));
130 // __p is not permitted to be a null pointer.
131 void deallocate(pointer __p, size_type GC_ATTR_UNUSED GC_n)
132 { GC_FREE(__p); }
134 size_type max_size() const throw()
135 { return size_t(-1) / sizeof(GC_Tp); }
137 void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
138 void destroy(pointer __p) { __p->~GC_Tp(); }
141 template<>
142 class gc_allocator<void> {
143 typedef size_t size_type;
144 typedef ptrdiff_t difference_type;
145 typedef void* pointer;
146 typedef const void* const_pointer;
147 typedef void value_type;
149 template <class GC_Tp1> struct rebind {
150 typedef gc_allocator<GC_Tp1> other;
155 template <class GC_T1, class GC_T2>
156 inline bool operator==(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
158 return true;
161 template <class GC_T1, class GC_T2>
162 inline bool operator!=(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
164 return false;
168 * And the public traceable_allocator class.
171 // Note that we currently don't specialize the pointer-free case, since a
172 // pointer-free traceable container doesn't make that much sense,
173 // though it could become an issue due to abstraction boundaries.
174 template <class GC_Tp>
175 class traceable_allocator {
176 public:
177 typedef size_t size_type;
178 typedef ptrdiff_t difference_type;
179 typedef GC_Tp* pointer;
180 typedef const GC_Tp* const_pointer;
181 typedef GC_Tp& reference;
182 typedef const GC_Tp& const_reference;
183 typedef GC_Tp value_type;
185 template <class GC_Tp1> struct rebind {
186 typedef traceable_allocator<GC_Tp1> other;
189 traceable_allocator() throw() {}
190 # ifndef _MSC_VER
191 traceable_allocator(const traceable_allocator&) throw() {}
192 # endif
193 template <class GC_Tp1> traceable_allocator
194 (const traceable_allocator<GC_Tp1>&) throw() {}
195 ~traceable_allocator() throw() {}
197 pointer address(reference GC_x) const { return &GC_x; }
198 const_pointer address(const_reference GC_x) const { return &GC_x; }
200 // GC_n is permitted to be 0. The C++ standard says nothing about what
201 // the return value is when GC_n == 0.
202 GC_Tp* allocate(size_type GC_n, const void* = 0) {
203 return static_cast<GC_Tp*>(GC_MALLOC_UNCOLLECTABLE(GC_n * sizeof(GC_Tp)));
206 // __p is not permitted to be a null pointer.
207 void deallocate(pointer __p, size_type GC_ATTR_UNUSED GC_n)
208 { GC_FREE(__p); }
210 size_type max_size() const throw()
211 { return size_t(-1) / sizeof(GC_Tp); }
213 void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
214 void destroy(pointer __p) { __p->~GC_Tp(); }
217 template<>
218 class traceable_allocator<void> {
219 typedef size_t size_type;
220 typedef ptrdiff_t difference_type;
221 typedef void* pointer;
222 typedef const void* const_pointer;
223 typedef void value_type;
225 template <class GC_Tp1> struct rebind {
226 typedef traceable_allocator<GC_Tp1> other;
231 template <class GC_T1, class GC_T2>
232 inline bool operator==(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
234 return true;
237 template <class GC_T1, class GC_T2>
238 inline bool operator!=(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
240 return false;
243 #endif /* GC_ALLOCATOR_H */