MDL SDK API nvidia_logo_transpbg.gif Up
std_allocator.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2025 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 using value_type = T;
42 using pointer = T*;
43 using const_pointer = const T*;
44 using reference = T&;
45 using const_reference = const T&;
46 using size_type = std::size_t;
47 using difference_type = std::ptrdiff_t;
48
51 template <class T1> struct rebind
52 {
56 };
57
61 Std_allocator() noexcept
62 : m_alloc( Default_allocator::get_instance()) {}
63
71 Std_allocator( base::IAllocator* allocator) noexcept
72 : m_alloc( allocator ? allocator : Default_allocator::get_instance()) {}
73
75 template <class T1>
76 Std_allocator(const Std_allocator<T1>& other) noexcept
77 : m_alloc( other.get_allocator()) {}
78
80 pointer address( reference x) const { return &x;}
81
83 const_pointer address(const_reference x) const { return &x; }
84
88 T* allocate( size_type n, const void* = nullptr) noexcept
89 {
90 return reinterpret_cast<T*>( m_alloc->malloc( n * sizeof(value_type)));
91 }
92
97 // the standard allocator concept \p p must not be \c nullptr.
99 {
100 m_alloc->free( p);
101 }
102
105 size_type max_size() const noexcept { return SIZE_MAX_VALUE / sizeof(value_type); }
106
109 void construct(pointer p, const_reference value) { new(p) T(value); }
110
112 void destroy(pointer p) { p->~T(); }
113
115 IAllocator* get_allocator() const { return m_alloc; }
116
122 template <class T2>
123 bool operator==( Std_allocator<T2> other) const noexcept
124 {
125 return m_alloc == other.get_allocator();
126 }
127
132 template <class T2>
133 bool operator!=( Std_allocator<T2> other) const noexcept
134 {
135 return ! ((*this) == other);
136 }
137};
138 // end group mi_base_iallocator
140
141} // namespace base
142
143} // namespace mi
144
145#endif // MI_BASE_STD_ALLOCATOR_H
Allocator interface class to dynamically allocate and deallocate memory.
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.
bool operator!=(Std_allocator<T2> other) const noexcept
Inequality comparison.
Definition: std_allocator.h:133
T * pointer
Pointer type.
Definition: std_allocator.h:42
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:112
pointer address(reference x) const
Returns address of object x allocated through this allocator.
Definition: std_allocator.h:80
IAllocator * get_allocator() const
Returns the interface of the underlying allocator.
Definition: std_allocator.h:115
virtual void * malloc(Size size)=0
Allocates a memory block of the given size.
T value_type
Value type allocated by this allocator.
Definition: std_allocator.h:41
T * allocate(size_type n, const void *=nullptr) noexcept
Allocate uninitialized dynamic memory for n elements of type T.
Definition: std_allocator.h:88
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:98
Std_allocator(const Std_allocator<T1> &other) noexcept
Copying constructor template for alike allocators of different value type.
Definition: std_allocator.h:76
Std_allocator() noexcept
Default constructor.
Definition: std_allocator.h:61
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:83
std::size_t size_type
Size type.
Definition: std_allocator.h:46
std::ptrdiff_t difference_type
Difference type.
Definition: std_allocator.h:47
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:109
Std_allocator(base::IAllocator *allocator) noexcept
Constructor.
Definition: std_allocator.h:71
const T & const_reference
Const reference type.
Definition: std_allocator.h:45
bool operator==(Std_allocator<T2> other) const noexcept
Equality comparison.
Definition: std_allocator.h:123
virtual void free(void *memory)=0
Releases the given memory block.
size_type max_size() const noexcept
Returns the maximum number of elements of type T that can be allocated using this allocator.
Definition: std_allocator.h:105
static const Size SIZE_MAX_VALUE
The maximum value for Size.
Definition: types.h:140
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: example_derivatives.dox:5
Rebind helper struct to define a new class instance of this allocator template instantiated for the n...
Definition: std_allocator.h:52
Basic types.