NVIDIA Iray API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
std_allocator.h
Go to the documentation of this file.
1 //*****************************************************************************
2 // Copyright 1986, 2014 NVIDIA Corporation. All rights reserved.
3 //*****************************************************************************
10 //*****************************************************************************
11 
12 #ifndef MI_BASE_STD_ALLOCATOR_H
13 #define MI_BASE_STD_ALLOCATOR_H
14 
15 #include <mi/base/types.h>
16 #include <mi/base/iallocator.h>
18 
19 namespace mi
20 {
21 
22 namespace base
23 {
24 
38 template <class T>
40 {
41  // Allocator interface used for memory management.
42  IAllocator* m_alloc;
43 public:
44 
45  typedef T value_type;
46  typedef T* pointer;
47  typedef const T* const_pointer;
48  typedef T& reference;
49  typedef const T& const_reference;
50  typedef MISTD::size_t size_type;
51  typedef MISTD::ptrdiff_t difference_type;
52 
55  template <class T1> struct rebind {
59  };
60 
64  Std_allocator() throw()
65  : m_alloc( Default_allocator::get_instance()) {}
66 
74  Std_allocator( base::IAllocator* allocator) throw()
75  : m_alloc( allocator ? allocator : Default_allocator::get_instance()) {}
76 
78  template <class T1>
79  Std_allocator(const Std_allocator<T1>& other) throw()
80  : m_alloc( other.get_allocator()) {}
81 
83  pointer address( reference x) const { return &x;}
84 
86  const_pointer address(const_reference x) const { return &x; }
87 
91  T* allocate( size_type n, const void* = 0) throw() {
92  return reinterpret_cast<T*>( m_alloc->malloc( n * sizeof(value_type)));
93  }
94 
99  // the standard allocator concept \p p must not be \c NULL.
101  m_alloc->free( p);
102  }
103 
106  size_type max_size() const throw() { return SIZE_MAX_VALUE / sizeof(value_type); }
107 
110  void construct(pointer p, const_reference value) { new(p) T(value); }
111 
113  void destroy(pointer p) { p->~T(); }
114 
116  IAllocator* get_allocator() const { return m_alloc; }
117 
123  template <class T2>
124  bool operator== ( Std_allocator<T2> other) const throw() {
125  return m_alloc == other.get_allocator();
126  }
127 
132  template <class T2>
133  bool operator!= ( Std_allocator<T2> other) const throw() {
134  return ! ((*this) == other);
135  }
136 };
137 
138  // end group mi_base_iallocator
140 
141 } // namespace base
142 } // namespace mi
143 
144 #endif // MI_BASE_STD_ALLOCATOR_H