gc_allocator.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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_allocator<T> allocates garbage-collectible
  27. * objects of type T. Traceable_allocator<T> allocates objects that
  28. * are not themselves garbage collected, but are scanned by the
  29. * collector for pointers to collectible objects. Traceable_alloc
  30. * should be used for explicitly managed STL containers that may
  31. * point to collectible 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. * Ignore-off-page allocator: George T. Talbot
  37. */
  38. #ifndef GC_ALLOCATOR_H
  39. #define GC_ALLOCATOR_H
  40. #include "gc.h"
  41. #include <new> // for placement new
  42. #ifndef GC_ATTR_EXPLICIT
  43. # if (__cplusplus >= 201103L) || defined(CPPCHECK)
  44. # define GC_ATTR_EXPLICIT explicit
  45. # else
  46. # define GC_ATTR_EXPLICIT /* empty */
  47. # endif
  48. #endif
  49. /* First some helpers to allow us to dispatch on whether or not a type
  50. * is known to be pointer-free.
  51. * These are private, except that the client may invoke the
  52. * GC_DECLARE_PTRFREE macro.
  53. */
  54. struct GC_true_type {};
  55. struct GC_false_type {};
  56. template <class GC_tp>
  57. struct GC_type_traits {
  58. GC_false_type GC_is_ptr_free;
  59. };
  60. # define GC_DECLARE_PTRFREE(T) \
  61. template<> struct GC_type_traits<T> { GC_true_type GC_is_ptr_free; }
  62. GC_DECLARE_PTRFREE(char);
  63. GC_DECLARE_PTRFREE(signed char);
  64. GC_DECLARE_PTRFREE(unsigned char);
  65. GC_DECLARE_PTRFREE(signed short);
  66. GC_DECLARE_PTRFREE(unsigned short);
  67. GC_DECLARE_PTRFREE(signed int);
  68. GC_DECLARE_PTRFREE(unsigned int);
  69. GC_DECLARE_PTRFREE(signed long);
  70. GC_DECLARE_PTRFREE(unsigned long);
  71. GC_DECLARE_PTRFREE(float);
  72. GC_DECLARE_PTRFREE(double);
  73. GC_DECLARE_PTRFREE(long double);
  74. /* The client may want to add others. */
  75. // In the following GC_Tp is GC_true_type if we are allocating a
  76. // pointer-free object.
  77. template <class GC_Tp>
  78. inline void * GC_selective_alloc(size_t n, GC_Tp, bool ignore_off_page) {
  79. return ignore_off_page?GC_MALLOC_IGNORE_OFF_PAGE(n):GC_MALLOC(n);
  80. }
  81. template <>
  82. inline void * GC_selective_alloc<GC_true_type>(size_t n, GC_true_type,
  83. bool ignore_off_page) {
  84. return ignore_off_page? GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(n)
  85. : GC_MALLOC_ATOMIC(n);
  86. }
  87. /* Now the public gc_allocator<T> class:
  88. */
  89. template <class GC_Tp>
  90. class gc_allocator {
  91. public:
  92. typedef size_t size_type;
  93. typedef ptrdiff_t difference_type;
  94. typedef GC_Tp* pointer;
  95. typedef const GC_Tp* const_pointer;
  96. typedef GC_Tp& reference;
  97. typedef const GC_Tp& const_reference;
  98. typedef GC_Tp value_type;
  99. template <class GC_Tp1> struct rebind {
  100. typedef gc_allocator<GC_Tp1> other;
  101. };
  102. gc_allocator() {}
  103. gc_allocator(const gc_allocator&) throw() {}
  104. # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
  105. // MSVC++ 6.0 do not support member templates
  106. template <class GC_Tp1> GC_ATTR_EXPLICIT
  107. gc_allocator(const gc_allocator<GC_Tp1>&) throw() {}
  108. # endif
  109. ~gc_allocator() throw() {}
  110. pointer address(reference GC_x) const { return &GC_x; }
  111. const_pointer address(const_reference GC_x) const { return &GC_x; }
  112. // GC_n is permitted to be 0. The C++ standard says nothing about what
  113. // the return value is when GC_n == 0.
  114. GC_Tp* allocate(size_type GC_n, const void* = 0) {
  115. GC_type_traits<GC_Tp> traits;
  116. return static_cast<GC_Tp *>
  117. (GC_selective_alloc(GC_n * sizeof(GC_Tp),
  118. traits.GC_is_ptr_free, false));
  119. }
  120. // __p is not permitted to be a null pointer.
  121. void deallocate(pointer __p, size_type /* GC_n */)
  122. { GC_FREE(__p); }
  123. size_type max_size() const throw()
  124. { return size_t(-1) / sizeof(GC_Tp); }
  125. void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
  126. void destroy(pointer __p) { __p->~GC_Tp(); }
  127. };
  128. template<>
  129. class gc_allocator<void> {
  130. typedef size_t size_type;
  131. typedef ptrdiff_t difference_type;
  132. typedef void* pointer;
  133. typedef const void* const_pointer;
  134. typedef void value_type;
  135. template <class GC_Tp1> struct rebind {
  136. typedef gc_allocator<GC_Tp1> other;
  137. };
  138. };
  139. template <class GC_T1, class GC_T2>
  140. inline bool operator==(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
  141. {
  142. return true;
  143. }
  144. template <class GC_T1, class GC_T2>
  145. inline bool operator!=(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
  146. {
  147. return false;
  148. }
  149. /* Now the public gc_allocator_ignore_off_page<T> class:
  150. */
  151. template <class GC_Tp>
  152. class gc_allocator_ignore_off_page {
  153. public:
  154. typedef size_t size_type;
  155. typedef ptrdiff_t difference_type;
  156. typedef GC_Tp* pointer;
  157. typedef const GC_Tp* const_pointer;
  158. typedef GC_Tp& reference;
  159. typedef const GC_Tp& const_reference;
  160. typedef GC_Tp value_type;
  161. template <class GC_Tp1> struct rebind {
  162. typedef gc_allocator_ignore_off_page<GC_Tp1> other;
  163. };
  164. gc_allocator_ignore_off_page() {}
  165. gc_allocator_ignore_off_page(const gc_allocator_ignore_off_page&) throw() {}
  166. # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
  167. // MSVC++ 6.0 do not support member templates
  168. template <class GC_Tp1> GC_ATTR_EXPLICIT
  169. gc_allocator_ignore_off_page(const gc_allocator_ignore_off_page<GC_Tp1>&)
  170. throw() {}
  171. # endif
  172. ~gc_allocator_ignore_off_page() throw() {}
  173. pointer address(reference GC_x) const { return &GC_x; }
  174. const_pointer address(const_reference GC_x) const { return &GC_x; }
  175. // GC_n is permitted to be 0. The C++ standard says nothing about what
  176. // the return value is when GC_n == 0.
  177. GC_Tp* allocate(size_type GC_n, const void* = 0) {
  178. GC_type_traits<GC_Tp> traits;
  179. return static_cast<GC_Tp *>
  180. (GC_selective_alloc(GC_n * sizeof(GC_Tp),
  181. traits.GC_is_ptr_free, true));
  182. }
  183. // __p is not permitted to be a null pointer.
  184. void deallocate(pointer __p, size_type /* GC_n */)
  185. { GC_FREE(__p); }
  186. size_type max_size() const throw()
  187. { return size_t(-1) / sizeof(GC_Tp); }
  188. void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
  189. void destroy(pointer __p) { __p->~GC_Tp(); }
  190. };
  191. template<>
  192. class gc_allocator_ignore_off_page<void> {
  193. typedef size_t size_type;
  194. typedef ptrdiff_t difference_type;
  195. typedef void* pointer;
  196. typedef const void* const_pointer;
  197. typedef void value_type;
  198. template <class GC_Tp1> struct rebind {
  199. typedef gc_allocator_ignore_off_page<GC_Tp1> other;
  200. };
  201. };
  202. template <class GC_T1, class GC_T2>
  203. inline bool operator==(const gc_allocator_ignore_off_page<GC_T1>&, const gc_allocator_ignore_off_page<GC_T2>&)
  204. {
  205. return true;
  206. }
  207. template <class GC_T1, class GC_T2>
  208. inline bool operator!=(const gc_allocator_ignore_off_page<GC_T1>&, const gc_allocator_ignore_off_page<GC_T2>&)
  209. {
  210. return false;
  211. }
  212. /*
  213. * And the public traceable_allocator class.
  214. */
  215. // Note that we currently don't specialize the pointer-free case, since a
  216. // pointer-free traceable container doesn't make that much sense,
  217. // though it could become an issue due to abstraction boundaries.
  218. template <class GC_Tp>
  219. class traceable_allocator {
  220. public:
  221. typedef size_t size_type;
  222. typedef ptrdiff_t difference_type;
  223. typedef GC_Tp* pointer;
  224. typedef const GC_Tp* const_pointer;
  225. typedef GC_Tp& reference;
  226. typedef const GC_Tp& const_reference;
  227. typedef GC_Tp value_type;
  228. template <class GC_Tp1> struct rebind {
  229. typedef traceable_allocator<GC_Tp1> other;
  230. };
  231. traceable_allocator() throw() {}
  232. traceable_allocator(const traceable_allocator&) throw() {}
  233. # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
  234. // MSVC++ 6.0 do not support member templates
  235. template <class GC_Tp1> GC_ATTR_EXPLICIT
  236. traceable_allocator(const traceable_allocator<GC_Tp1>&) throw() {}
  237. # endif
  238. ~traceable_allocator() throw() {}
  239. pointer address(reference GC_x) const { return &GC_x; }
  240. const_pointer address(const_reference GC_x) const { return &GC_x; }
  241. // GC_n is permitted to be 0. The C++ standard says nothing about what
  242. // the return value is when GC_n == 0.
  243. GC_Tp* allocate(size_type GC_n, const void* = 0) {
  244. return static_cast<GC_Tp*>(GC_MALLOC_UNCOLLECTABLE(GC_n * sizeof(GC_Tp)));
  245. }
  246. // __p is not permitted to be a null pointer.
  247. void deallocate(pointer __p, size_type /* GC_n */)
  248. { GC_FREE(__p); }
  249. size_type max_size() const throw()
  250. { return size_t(-1) / sizeof(GC_Tp); }
  251. void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
  252. void destroy(pointer __p) { __p->~GC_Tp(); }
  253. };
  254. template<>
  255. class traceable_allocator<void> {
  256. typedef size_t size_type;
  257. typedef ptrdiff_t difference_type;
  258. typedef void* pointer;
  259. typedef const void* const_pointer;
  260. typedef void value_type;
  261. template <class GC_Tp1> struct rebind {
  262. typedef traceable_allocator<GC_Tp1> other;
  263. };
  264. };
  265. template <class GC_T1, class GC_T2>
  266. inline bool operator==(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
  267. {
  268. return true;
  269. }
  270. template <class GC_T1, class GC_T2>
  271. inline bool operator!=(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
  272. {
  273. return false;
  274. }
  275. #endif /* GC_ALLOCATOR_H */