Back to index

plt-scheme  4.2.1
gc_allocator.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1996-1997
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Permission to use, copy, modify, distribute and sell this software
00006  * and its documentation for any purpose is hereby granted without fee,
00007  * provided that the above copyright notice appear in all copies and
00008  * that both that copyright notice and this permission notice appear
00009  * in supporting documentation.  Silicon Graphics makes no
00010  * representations about the suitability of this software for any
00011  * purpose.  It is provided "as is" without express or implied warranty.
00012  *
00013  * Copyright (c) 2002
00014  * Hewlett-Packard Company
00015  *
00016  * Permission to use, copy, modify, distribute and sell this software
00017  * and its documentation for any purpose is hereby granted without fee,
00018  * provided that the above copyright notice appear in all copies and
00019  * that both that copyright notice and this permission notice appear
00020  * in supporting documentation.  Hewlett-Packard Company makes no
00021  * representations about the suitability of this software for any
00022  * purpose.  It is provided "as is" without express or implied warranty.
00023  */
00024 
00025 /*
00026  * This implements standard-conforming allocators that interact with
00027  * the garbage collector.  Gc_alloctor<T> allocates garbage-collectable
00028  * objects of type T.  Traceable_allocator<T> allocates objects that
00029  * are not temselves garbage collected, but are scanned by the
00030  * collector for pointers to collectable objects.  Traceable_alloc
00031  * should be used for explicitly managed STL containers that may
00032  * point to collectable objects.
00033  *
00034  * This code was derived from an earlier version of the GNU C++ standard
00035  * library, which itself was derived from the SGI STL implementation.
00036  */
00037 
00038 #ifndef GC_ALLOCATOR_H
00039 
00040 #define GC_ALLOCATOR_H
00041 
00042 #include "gc.h"
00043 
00044 #if defined(__GNUC__)
00045 #  define GC_ATTR_UNUSED __attribute__((unused))
00046 #else
00047 #  define GC_ATTR_UNUSED
00048 #endif
00049 
00050 /* First some helpers to allow us to dispatch on whether or not a type
00051  * is known to be pointerfree.
00052  * These are private, except that the client may invoke the
00053  * GC_DECLARE_PTRFREE macro.
00054  */
00055 
00056 struct GC_true_type {};
00057 struct GC_false_type {};
00058 
00059 template <class GC_tp>
00060 struct GC_type_traits {
00061   GC_false_type GC_is_ptr_free;
00062 };
00063 
00064 # define GC_DECLARE_PTRFREE(T) \
00065 template<> struct GC_type_traits<T> { GC_true_type GC_is_ptr_free; }
00066 
00067 GC_DECLARE_PTRFREE(signed char);
00068 GC_DECLARE_PTRFREE(unsigned char);
00069 GC_DECLARE_PTRFREE(signed short);
00070 GC_DECLARE_PTRFREE(unsigned short);
00071 GC_DECLARE_PTRFREE(signed int);
00072 GC_DECLARE_PTRFREE(unsigned int);
00073 GC_DECLARE_PTRFREE(signed long);
00074 GC_DECLARE_PTRFREE(unsigned long);
00075 GC_DECLARE_PTRFREE(float);
00076 GC_DECLARE_PTRFREE(double);
00077 /* The client may want to add others.     */
00078 
00079 // In the following GC_Tp is GC_true_type iff we are allocating a
00080 // pointerfree object.
00081 template <class GC_Tp>
00082 inline void * GC_selective_alloc(size_t n, GC_Tp) {
00083     return GC_MALLOC(n);
00084 }
00085 
00086 template <>
00087 inline void * GC_selective_alloc<GC_true_type>(size_t n, GC_true_type) {
00088     return GC_MALLOC_ATOMIC(n);
00089 }
00090 
00091 /* Now the public gc_allocator<T> class:
00092  */
00093 template <class GC_Tp>
00094 class gc_allocator {
00095 public:
00096   typedef size_t     size_type;
00097   typedef ptrdiff_t  difference_type;
00098   typedef GC_Tp*       pointer;
00099   typedef const GC_Tp* const_pointer;
00100   typedef GC_Tp&       reference;
00101   typedef const GC_Tp& const_reference;
00102   typedef GC_Tp        value_type;
00103 
00104   template <class GC_Tp1> struct rebind {
00105     typedef gc_allocator<GC_Tp1> other;
00106   };
00107 
00108   gc_allocator()  {}
00109 # ifndef _MSC_VER
00110     // I'm not sure why this is needed here in addition to the following.
00111     // The standard specifies it for the standard allocator, but VC++ rejects
00112     // it.    -HB
00113     gc_allocator(const gc_allocator&) throw() {}
00114 # endif
00115   template <class GC_Tp1> gc_allocator(const gc_allocator<GC_Tp1>&) throw() {}
00116   ~gc_allocator() throw() {}
00117 
00118   pointer address(reference GC_x) const { return &GC_x; }
00119   const_pointer address(const_reference GC_x) const { return &GC_x; }
00120 
00121   // GC_n is permitted to be 0.  The C++ standard says nothing about what
00122   // the return value is when GC_n == 0.
00123   GC_Tp* allocate(size_type GC_n, const void* = 0) {
00124     GC_type_traits<GC_Tp> traits;
00125     return static_cast<GC_Tp *>
00126            (GC_selective_alloc(GC_n * sizeof(GC_Tp),
00127                              traits.GC_is_ptr_free));
00128   }
00129 
00130   // __p is not permitted to be a null pointer.
00131   void deallocate(pointer __p, size_type GC_ATTR_UNUSED GC_n)
00132     { GC_FREE(__p); }
00133 
00134   size_type max_size() const throw()
00135     { return size_t(-1) / sizeof(GC_Tp); }
00136 
00137   void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
00138   void destroy(pointer __p) { __p->~GC_Tp(); }
00139 };
00140 
00141 template<>
00142 class gc_allocator<void> {
00143   typedef size_t      size_type;
00144   typedef ptrdiff_t   difference_type;
00145   typedef void*       pointer;
00146   typedef const void* const_pointer;
00147   typedef void        value_type;
00148 
00149   template <class GC_Tp1> struct rebind {
00150     typedef gc_allocator<GC_Tp1> other;
00151   };
00152 };
00153 
00154 
00155 template <class GC_T1, class GC_T2>
00156 inline bool operator==(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
00157 {
00158   return true;
00159 }
00160 
00161 template <class GC_T1, class GC_T2>
00162 inline bool operator!=(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
00163 {
00164   return false;
00165 }
00166 
00167 /*
00168  * And the public traceable_allocator class.
00169  */
00170 
00171 // Note that we currently don't specialize the pointer-free case, since a
00172 // pointer-free traceable container doesn't make that much sense,
00173 // though it could become an issue due to abstraction boundaries.
00174 template <class GC_Tp>
00175 class traceable_allocator {
00176 public:
00177   typedef size_t     size_type;
00178   typedef ptrdiff_t  difference_type;
00179   typedef GC_Tp*       pointer;
00180   typedef const GC_Tp* const_pointer;
00181   typedef GC_Tp&       reference;
00182   typedef const GC_Tp& const_reference;
00183   typedef GC_Tp        value_type;
00184 
00185   template <class GC_Tp1> struct rebind {
00186     typedef traceable_allocator<GC_Tp1> other;
00187   };
00188 
00189   traceable_allocator() throw() {}
00190 # ifndef _MSC_VER
00191     traceable_allocator(const traceable_allocator&) throw() {}
00192 # endif
00193   template <class GC_Tp1> traceable_allocator
00194          (const traceable_allocator<GC_Tp1>&) throw() {}
00195   ~traceable_allocator() throw() {}
00196 
00197   pointer address(reference GC_x) const { return &GC_x; }
00198   const_pointer address(const_reference GC_x) const { return &GC_x; }
00199 
00200   // GC_n is permitted to be 0.  The C++ standard says nothing about what
00201   // the return value is when GC_n == 0.
00202   GC_Tp* allocate(size_type GC_n, const void* = 0) {
00203     return static_cast<GC_Tp*>(GC_MALLOC_UNCOLLECTABLE(GC_n * sizeof(GC_Tp)));
00204   }
00205 
00206   // __p is not permitted to be a null pointer.
00207   void deallocate(pointer __p, size_type GC_ATTR_UNUSED GC_n)
00208     { GC_FREE(__p); }
00209 
00210   size_type max_size() const throw()
00211     { return size_t(-1) / sizeof(GC_Tp); }
00212 
00213   void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
00214   void destroy(pointer __p) { __p->~GC_Tp(); }
00215 };
00216 
00217 template<>
00218 class traceable_allocator<void> {
00219   typedef size_t      size_type;
00220   typedef ptrdiff_t   difference_type;
00221   typedef void*       pointer;
00222   typedef const void* const_pointer;
00223   typedef void        value_type;
00224 
00225   template <class GC_Tp1> struct rebind {
00226     typedef traceable_allocator<GC_Tp1> other;
00227   };
00228 };
00229 
00230 
00231 template <class GC_T1, class GC_T2>
00232 inline bool operator==(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
00233 {
00234   return true;
00235 }
00236 
00237 template <class GC_T1, class GC_T2>
00238 inline bool operator!=(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
00239 {
00240   return false;
00241 }
00242 
00243 #endif /* GC_ALLOCATOR_H */