123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- #ifndef GC_ALLOCATOR_H
- #define GC_ALLOCATOR_H
- #include "gc.h"
- #include <new>
- #ifndef GC_ATTR_EXPLICIT
- # if (__cplusplus >= 201103L) || defined(CPPCHECK)
- # define GC_ATTR_EXPLICIT explicit
- # else
- # define GC_ATTR_EXPLICIT
- # endif
- #endif
- struct GC_true_type {};
- struct GC_false_type {};
- template <class GC_tp>
- struct GC_type_traits {
- GC_false_type GC_is_ptr_free;
- };
- # define GC_DECLARE_PTRFREE(T) \
- template<> struct GC_type_traits<T> { GC_true_type GC_is_ptr_free; }
- GC_DECLARE_PTRFREE(char);
- GC_DECLARE_PTRFREE(signed char);
- GC_DECLARE_PTRFREE(unsigned char);
- GC_DECLARE_PTRFREE(signed short);
- GC_DECLARE_PTRFREE(unsigned short);
- GC_DECLARE_PTRFREE(signed int);
- GC_DECLARE_PTRFREE(unsigned int);
- GC_DECLARE_PTRFREE(signed long);
- GC_DECLARE_PTRFREE(unsigned long);
- GC_DECLARE_PTRFREE(float);
- GC_DECLARE_PTRFREE(double);
- GC_DECLARE_PTRFREE(long double);
- template <class GC_Tp>
- inline void * GC_selective_alloc(size_t n, GC_Tp, bool ignore_off_page) {
- return ignore_off_page?GC_MALLOC_IGNORE_OFF_PAGE(n):GC_MALLOC(n);
- }
- template <>
- inline void * GC_selective_alloc<GC_true_type>(size_t n, GC_true_type,
- bool ignore_off_page) {
- return ignore_off_page? GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(n)
- : GC_MALLOC_ATOMIC(n);
- }
- template <class GC_Tp>
- class gc_allocator {
- public:
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef GC_Tp* pointer;
- typedef const GC_Tp* const_pointer;
- typedef GC_Tp& reference;
- typedef const GC_Tp& const_reference;
- typedef GC_Tp value_type;
- template <class GC_Tp1> struct rebind {
- typedef gc_allocator<GC_Tp1> other;
- };
- gc_allocator() {}
- gc_allocator(const gc_allocator&) throw() {}
- # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
-
- template <class GC_Tp1> GC_ATTR_EXPLICIT
- gc_allocator(const gc_allocator<GC_Tp1>&) throw() {}
- # endif
- ~gc_allocator() throw() {}
- pointer address(reference GC_x) const { return &GC_x; }
- const_pointer address(const_reference GC_x) const { return &GC_x; }
-
-
- GC_Tp* allocate(size_type GC_n, const void* = 0) {
- GC_type_traits<GC_Tp> traits;
- return static_cast<GC_Tp *>
- (GC_selective_alloc(GC_n * sizeof(GC_Tp),
- traits.GC_is_ptr_free, false));
- }
-
- void deallocate(pointer __p, size_type )
- { GC_FREE(__p); }
- size_type max_size() const throw()
- { return size_t(-1) / sizeof(GC_Tp); }
- void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
- void destroy(pointer __p) { __p->~GC_Tp(); }
- };
- template<>
- class gc_allocator<void> {
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef void* pointer;
- typedef const void* const_pointer;
- typedef void value_type;
- template <class GC_Tp1> struct rebind {
- typedef gc_allocator<GC_Tp1> other;
- };
- };
- template <class GC_T1, class GC_T2>
- inline bool operator==(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
- {
- return true;
- }
- template <class GC_T1, class GC_T2>
- inline bool operator!=(const gc_allocator<GC_T1>&, const gc_allocator<GC_T2>&)
- {
- return false;
- }
- template <class GC_Tp>
- class gc_allocator_ignore_off_page {
- public:
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef GC_Tp* pointer;
- typedef const GC_Tp* const_pointer;
- typedef GC_Tp& reference;
- typedef const GC_Tp& const_reference;
- typedef GC_Tp value_type;
- template <class GC_Tp1> struct rebind {
- typedef gc_allocator_ignore_off_page<GC_Tp1> other;
- };
- gc_allocator_ignore_off_page() {}
- gc_allocator_ignore_off_page(const gc_allocator_ignore_off_page&) throw() {}
- # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
-
- template <class GC_Tp1> GC_ATTR_EXPLICIT
- gc_allocator_ignore_off_page(const gc_allocator_ignore_off_page<GC_Tp1>&)
- throw() {}
- # endif
- ~gc_allocator_ignore_off_page() throw() {}
- pointer address(reference GC_x) const { return &GC_x; }
- const_pointer address(const_reference GC_x) const { return &GC_x; }
-
-
- GC_Tp* allocate(size_type GC_n, const void* = 0) {
- GC_type_traits<GC_Tp> traits;
- return static_cast<GC_Tp *>
- (GC_selective_alloc(GC_n * sizeof(GC_Tp),
- traits.GC_is_ptr_free, true));
- }
-
- void deallocate(pointer __p, size_type )
- { GC_FREE(__p); }
- size_type max_size() const throw()
- { return size_t(-1) / sizeof(GC_Tp); }
- void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
- void destroy(pointer __p) { __p->~GC_Tp(); }
- };
- template<>
- class gc_allocator_ignore_off_page<void> {
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef void* pointer;
- typedef const void* const_pointer;
- typedef void value_type;
- template <class GC_Tp1> struct rebind {
- typedef gc_allocator_ignore_off_page<GC_Tp1> other;
- };
- };
- template <class GC_T1, class GC_T2>
- inline bool operator==(const gc_allocator_ignore_off_page<GC_T1>&, const gc_allocator_ignore_off_page<GC_T2>&)
- {
- return true;
- }
- template <class GC_T1, class GC_T2>
- inline bool operator!=(const gc_allocator_ignore_off_page<GC_T1>&, const gc_allocator_ignore_off_page<GC_T2>&)
- {
- return false;
- }
- template <class GC_Tp>
- class traceable_allocator {
- public:
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef GC_Tp* pointer;
- typedef const GC_Tp* const_pointer;
- typedef GC_Tp& reference;
- typedef const GC_Tp& const_reference;
- typedef GC_Tp value_type;
- template <class GC_Tp1> struct rebind {
- typedef traceable_allocator<GC_Tp1> other;
- };
- traceable_allocator() throw() {}
- traceable_allocator(const traceable_allocator&) throw() {}
- # if !(GC_NO_MEMBER_TEMPLATES || 0 < _MSC_VER && _MSC_VER <= 1200)
-
- template <class GC_Tp1> GC_ATTR_EXPLICIT
- traceable_allocator(const traceable_allocator<GC_Tp1>&) throw() {}
- # endif
- ~traceable_allocator() throw() {}
- pointer address(reference GC_x) const { return &GC_x; }
- const_pointer address(const_reference GC_x) const { return &GC_x; }
-
-
- GC_Tp* allocate(size_type GC_n, const void* = 0) {
- return static_cast<GC_Tp*>(GC_MALLOC_UNCOLLECTABLE(GC_n * sizeof(GC_Tp)));
- }
-
- void deallocate(pointer __p, size_type )
- { GC_FREE(__p); }
- size_type max_size() const throw()
- { return size_t(-1) / sizeof(GC_Tp); }
- void construct(pointer __p, const GC_Tp& __val) { new(__p) GC_Tp(__val); }
- void destroy(pointer __p) { __p->~GC_Tp(); }
- };
- template<>
- class traceable_allocator<void> {
- typedef size_t size_type;
- typedef ptrdiff_t difference_type;
- typedef void* pointer;
- typedef const void* const_pointer;
- typedef void value_type;
- template <class GC_Tp1> struct rebind {
- typedef traceable_allocator<GC_Tp1> other;
- };
- };
- template <class GC_T1, class GC_T2>
- inline bool operator==(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
- {
- return true;
- }
- template <class GC_T1, class GC_T2>
- inline bool operator!=(const traceable_allocator<GC_T1>&, const traceable_allocator<GC_T2>&)
- {
- return false;
- }
- #endif
|