neuray API Programmer's Manual

std_allocator.h File Reference

Description

Standard STL allocator implementation. The implementation is based on the mi::base::IAllocator interface. See Interface Framework Technology.

Code Example

std_allocator.h

‎/***************************************************************************************************
 * Copyright 2024 NVIDIA Corporation. All rights reserved.
 **************************************************************************************************/

#ifndef MI_BASE_STD_ALLOCATOR_H
#define MI_BASE_STD_ALLOCATOR_H

#include <mi/base/types.h>
#include <mi/base/iallocator.h>
#include <mi/base/default_allocator.h>

namespace mi {

namespace base {

template <class T>
class Std_allocator
{
    // Allocator interface used for memory management.
    IAllocator* m_alloc;
public:

    typedef T              value_type;        
    typedef T*             pointer;           
    typedef const T*       const_pointer;     
    typedef T&             reference;         
    typedef const T&       const_reference;   
    typedef std::size_t    size_type;         
    typedef std::ptrdiff_t difference_type;   

    template <class T1> struct rebind {
        typedef Std_allocator< T1> 
               other;
    };

    Std_allocator() throw() 
        : m_alloc( Default_allocator::get_instance()) {}

    Std_allocator( base::IAllocator* allocator) throw() 
        : m_alloc( allocator ? allocator : Default_allocator::get_instance()) {}

    template <class T1> 
    Std_allocator(const Std_allocator< T1>& other) throw()
        : m_alloc( other.get_allocator()) {}

    pointer  
               address( reference x) const { return &x;}

    const_pointer 
               address(const_reference x) const { return &x; }

    T* allocate( size_type n, const void* = 0) throw() {
        return reinterpret_cast<T*>( m_alloc->malloc( n * sizeof(value_type)));
    }

    //           the standard allocator concept \p p must not be \c NULL.
    void deallocate(pointer p, size_type) {
        m_alloc->free( p);
    }

    size_type 
               max_size() const throw()  { return SIZE_MAX_VALUE / sizeof(value_type); }

    void construct(pointer p, const_reference value) { new(p) T(value); }

    void destroy(pointer p) { p->~T(); }

    IAllocator* get_allocator() const { return m_alloc; }

    template <class T2> 
    bool operator==( Std_allocator< T2> other) const throw() {
        return m_alloc == other.get_allocator();
    }

    template <class T2> 
    bool operator!=( Std_allocator< T2> other) const throw() { 
        return ! ((*this) == other); 
    }
};
 // end group mi_base_iallocator

} // namespace base

} // namespace mi

#endif // MI_BASE_STD_ALLOCATOR_H

Namespaces

namespace 
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH. More...
namespace 
Namespace for the Base API. More...

Classes

struct 
Rebind helper struct to define a new class instance of this allocator template instantiated for the new value type T1. More...
class 
An adaptor class template that implements a standard STL allocator. More...