Material Definition Language API nvidia_logo_transpbg.gif Up
 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 2020 NVIDIA Corporation. All rights reserved.
3  **************************************************************************************************/
9 
10 #ifndef MI_BASE_STD_ALLOCATOR_H
11 #define MI_BASE_STD_ALLOCATOR_H
12 
13 #include <mi/base/types.h>
14 #include <mi/base/iallocator.h>
16 
17 namespace mi {
18 
19 namespace base {
20 
34 template <class T>
36 {
37  // Allocator interface used for memory management.
38  IAllocator* m_alloc;
39 public:
40 
41  typedef T value_type;
42  typedef T* pointer;
43  typedef const T* const_pointer;
44  typedef T& reference;
45  typedef const T& const_reference;
48 
51  template <class T1> struct rebind {
55  };
56 
60  Std_allocator() throw()
61  : m_alloc( Default_allocator::get_instance()) {}
62 
70  Std_allocator( base::IAllocator* allocator) throw()
71  : m_alloc( allocator ? allocator : Default_allocator::get_instance()) {}
72 
74  template <class T1>
75  Std_allocator(const Std_allocator<T1>& other) throw()
76  : m_alloc( other.get_allocator()) {}
77 
79  pointer address( reference x) const { return &x;}
80 
82  const_pointer address(const_reference x) const { return &x; }
83 
87  T* allocate( size_type n, const void* = 0) throw() {
88  return reinterpret_cast<T*>( m_alloc->malloc( n * sizeof(value_type)));
89  }
90 
95  // the standard allocator concept \p p must not be \c NULL.
97  m_alloc->free( p);
98  }
99 
102  size_type max_size() const throw() { return SIZE_MAX_VALUE / sizeof(value_type); }
103 
106  void construct(pointer p, const_reference value) { new(p) T(value); }
107 
109  void destroy(pointer p) { p->~T(); }
110 
112  IAllocator* get_allocator() const { return m_alloc; }
113 
119  template <class T2>
120  bool operator== ( Std_allocator<T2> other) const throw() {
121  return m_alloc == other.get_allocator();
122  }
123 
128  template <class T2>
129  bool operator!= ( Std_allocator<T2> other) const throw() {
130  return ! ((*this) == other);
131  }
132 };
133  // end group mi_base_iallocator
135 
136 } // namespace base
137 
138 } // namespace mi
139 
140 #endif // MI_BASE_STD_ALLOCATOR_H