/libs/headers/gc/gc_allocator.h

http://github.com/nddrylliog/ooc · C++ Header · 245 lines · 146 code · 40 blank · 59 comment · 6 complexity · 8a9c45413d097fbadc05e6b225faff04 MD5 · raw file

  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. * This implements standard-conforming allocators that interact with
  26. * the garbage collector. Gc_alloctor<T> allocates garbage-collectable
  27. * objects of type T. Traceable_allocator<T> allocates objects that
  28. * are not temselves garbage collected, but are scanned by the
  29. * collector for pointers to collectable objects. Traceable_alloc
  30. * should be used for explicitly managed STL containers that may
  31. * point to collectable objects.
  32. *
  33. * This code was derived from an earlier version of the GNU C++ standard
  34. * library, which itself was derived from the SGI STL implementation.
  35. */
  36. #ifndef GC_ALLOCATOR_H
  37. #define GC_ALLOCATOR_H
  38. #include "gc.h"
  39. #include <new> // for placement new
  40. #if defined(__GNUC__)
  41. # define GC_ATTR_UNUSED __attribute__((unused))
  42. #else
  43. # define GC_ATTR_UNUSED
  44. #endif
  45. /* First some helpers to allow us to dispatch on whether or not a type
  46. * is known to be pointerfree.
  47. * These are private, except that the client may invoke the
  48. * GC_DECLARE_PTRFREE macro.
  49. */
  50. struct GC_true_type {};
  51. struct GC_false_type {};
  52. template <class GC_tp>
  53. struct GC_type_traits {
  54. GC_false_type GC_is_ptr_free;
  55. };
  56. # define GC_DECLARE_PTRFREE(T) \
  57. template<> struct GC_type_traits<T> { GC_true_type GC_is_ptr_free; }
  58. GC_DECLARE_PTRFREE(char);
  59. GC_DECLARE_PTRFREE(signed char);
  60. GC_DECLARE_PTRFREE(unsigned char);
  61. GC_DECLARE_PTRFREE(signed short);
  62. GC_DECLARE_PTRFREE(unsigned short);
  63. GC_DECLARE_PTRFREE(signed int);
  64. GC_DECLARE_PTRFREE(unsigned int);
  65. GC_DECLARE_PTRFREE(signed long);
  66. GC_DECLARE_PTRFREE(unsigned long);
  67. GC_DECLARE_PTRFREE(float);
  68. GC_DECLARE_PTRFREE(double);
  69. GC_DECLARE_PTRFREE(long double);
  70. /* The client may want to add others. */
  71. // In the following GC_Tp is GC_true_type iff we are allocating a
  72. // pointerfree object.
  73. template <class GC_Tp>
  74. inline void * GC_selective_alloc(size_t n, GC_Tp) {
  75. return GC_MALLOC(n);
  76. }
  77. template <>
  78. inline void * GC_selective_alloc<GC_true_type>(size_t n, GC_true_type) {
  79. return GC_MALLOC_ATOMIC(n);
  80. }
  81. /* Now the public gc_allocator<T> class:
  82. */
  83. template <class GC_Tp>
  84. class gc_allocator {
  85. public:
  86. typedef size_t size_type;
  87. typedef ptrdiff_t difference_type;
  88. typedef GC_Tp* pointer;
  89. typedef const GC_Tp* const_pointer;
  90. typedef GC_Tp& reference;
  91. typedef const GC_Tp& const_reference;
  92. typedef GC_Tp value_type;
  93. template <class GC_Tp1> struct rebind {
  94. typedef gc_allocator<GC_Tp1> other;
  95. };
  96. gc_allocator() {}
  97. gc_allocator(const gc_allocator&) throw() {}
  98. # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
  99. // MSVC++ 6.0 do not support member templates
  100. template <class GC_Tp1> gc_allocator(const gc_allocator<GC_Tp1>&) throw() {}
  101. # endif
  102. ~gc_allocator() throw() {}
  103. pointer address(reference GC_x) const { return &GC_x; }
  104. const_pointer address(const_reference GC_x) const { return &GC_x; }
  105. // GC_n is permitted to be 0. The C++ standard says nothing about what
  106. // the return value is when GC_n == 0.
  107. GC_Tp* allocate(size_type GC_n, const void* = 0) {
  108. GC_type_traits<GC_Tp> traits;
  109. return static_cast<GC_Tp *>
  110. (GC_selective_alloc(GC_n * sizeof(GC_Tp),
  111. traits.GC_is_ptr_free));
  112. }
  113. // __p is not permitted to be a null pointer.
  114. void deallocate(pointer __p, size_type GC_ATTR_UNUSED GC_n)
  115. { GC_FREE(__p); }
  116. size_type max_size() const throw()
  117. { return size_t(-1) / sizeof(GC_Tp); }
  118. void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
  119. void destroy(pointer __p) { __p->~GC_Tp(); }
  120. };
  121. template<>
  122. class gc_allocator<void> {
  123. typedef size_t size_type;
  124. typedef ptrdiff_t difference_type;
  125. typedef void* pointer;
  126. typedef const void* const_pointer;
  127. typedef void value_type;
  128. template <class GC_Tp1> struct rebind {
  129. typedef gc_allocator<GC_Tp1> other;
  130. };
  131. };
  132. template <class GC_T1, class GC_T2>
  133. inline bool operator==(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
  134. {
  135. return true;
  136. }
  137. template <class GC_T1, class GC_T2>
  138. inline bool operator!=(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
  139. {
  140. return false;
  141. }
  142. /*
  143. * And the public traceable_allocator class.
  144. */
  145. // Note that we currently don't specialize the pointer-free case, since a
  146. // pointer-free traceable container doesn't make that much sense,
  147. // though it could become an issue due to abstraction boundaries.
  148. template <class GC_Tp>
  149. class traceable_allocator {
  150. public:
  151. typedef size_t size_type;
  152. typedef ptrdiff_t difference_type;
  153. typedef GC_Tp* pointer;
  154. typedef const GC_Tp* const_pointer;
  155. typedef GC_Tp& reference;
  156. typedef const GC_Tp& const_reference;
  157. typedef GC_Tp value_type;
  158. template <class GC_Tp1> struct rebind {
  159. typedef traceable_allocator<GC_Tp1> other;
  160. };
  161. traceable_allocator() throw() {}
  162. traceable_allocator(const traceable_allocator&) throw() {}
  163. # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
  164. // MSVC++ 6.0 do not support member templates
  165. template <class GC_Tp1> traceable_allocator
  166. (const traceable_allocator<GC_Tp1>&) throw() {}
  167. # endif
  168. ~traceable_allocator() throw() {}
  169. pointer address(reference GC_x) const { return &GC_x; }
  170. const_pointer address(const_reference GC_x) const { return &GC_x; }
  171. // GC_n is permitted to be 0. The C++ standard says nothing about what
  172. // the return value is when GC_n == 0.
  173. GC_Tp* allocate(size_type GC_n, const void* = 0) {
  174. return static_cast<GC_Tp*>(GC_MALLOC_UNCOLLECTABLE(GC_n * sizeof(GC_Tp)));
  175. }
  176. // __p is not permitted to be a null pointer.
  177. void deallocate(pointer __p, size_type GC_ATTR_UNUSED GC_n)
  178. { GC_FREE(__p); }
  179. size_type max_size() const throw()
  180. { return size_t(-1) / sizeof(GC_Tp); }
  181. void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
  182. void destroy(pointer __p) { __p->~GC_Tp(); }
  183. };
  184. template<>
  185. class traceable_allocator<void> {
  186. typedef size_t size_type;
  187. typedef ptrdiff_t difference_type;
  188. typedef void* pointer;
  189. typedef const void* const_pointer;
  190. typedef void value_type;
  191. template <class GC_Tp1> struct rebind {
  192. typedef traceable_allocator<GC_Tp1> other;
  193. };
  194. };
  195. template <class GC_T1, class GC_T2>
  196. inline bool operator==(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
  197. {
  198. return true;
  199. }
  200. template <class GC_T1, class GC_T2>
  201. inline bool operator!=(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
  202. {
  203. return false;
  204. }
  205. #endif /* GC_ALLOCATOR_H */