Base API nvidia_logo_transpbg.gif Up
std_allocator.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2023 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
17namespace mi {
18
19namespace base {
20
34template <class T>
36{
37 // Allocator interface used for memory management.
38 IAllocator* m_alloc;
39public:
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;
46 typedef std::size_t size_type;
47 typedef std::ptrdiff_t difference_type;
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
A default allocator implementation based on global new and delete.
Definition: default_allocator.h:38
The IAllocator interface class supports allocating and releasing memory dynamically.
Definition: iallocator.h:49
An adaptor class template that implements a standard STL allocator.
Definition: std_allocator.h:36
Default allocator implementation based on global new and delete.
size_type max_size() const
Returns the maximum number of elements of type T that can be allocated using this allocator.
Definition: std_allocator.h:102
T * pointer
Pointer type.
Definition: std_allocator.h:42
Std_allocator()
Default constructor.
Definition: std_allocator.h:60
std::size_t size_type
Size type.
Definition: std_allocator.h:46
const T * const_pointer
Const pointer type.
Definition: std_allocator.h:43
void destroy(pointer p)
Calls the destructor of T on the location p.
Definition: std_allocator.h:109
pointer address(reference x) const
Returns address of object x allocated through this allocator.
Definition: std_allocator.h:79
IAllocator * get_allocator() const
Returns the interface of the underlying allocator.
Definition: std_allocator.h:112
virtual void * malloc(Size size)=0
Allocates a memory block of the given size.
T & reference
Reference type.
Definition: std_allocator.h:44
void deallocate(pointer p, size_type)
Frees uninitialized dynamic memory at location p that has previously been allocated with allocate().
Definition: std_allocator.h:96
const T & const_reference
Const reference type.
Definition: std_allocator.h:45
T value_type
Value type allocated by this allocator.
Definition: std_allocator.h:41
Std_allocator<T1> other
Rebind type, defines a new class instance of this allocator template instantiated for the new value t...
Definition: std_allocator.h:54
static IAllocator * get_instance()
Returns the single instance of the default allocator.
Definition: default_allocator.h:69
const_pointer address(const_reference x) const
Returns const address of object x allocated through this allocator.
Definition: std_allocator.h:82
void construct(pointer p, const_reference value)
Calls the copy constructor of T on the location p with the argument value.
Definition: std_allocator.h:106
Std_allocator(base::IAllocator *allocator)
Constructor.
Definition: std_allocator.h:70
T * allocate(size_type n, const void *=0)
Allocate uninitialized dynamic memory for n elements of type T.
Definition: std_allocator.h:87
bool operator!=(Std_allocator<T2> other) const
Inequality comparison.
Definition: std_allocator.h:129
std::ptrdiff_t difference_type
Difference type.
Definition: std_allocator.h:47
bool operator==(Std_allocator<T2> other) const
Equality comparison.
Definition: std_allocator.h:120
Std_allocator(const Std_allocator<T1> &other)
Copying constructor template for alike allocators of different value type.
Definition: std_allocator.h:75
virtual void free(void *memory)=0
Releases the given memory block.
static const Size SIZE_MAX_VALUE
The maximum value for Size.
Definition: types.h:140
Allocator interface class to dynamically allocate and deallocate memory.
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: base.h:34
Rebind helper struct to define a new class instance of this allocator template instantiated for the n...
Definition: std_allocator.h:51
Basic types.