neuray API Programmer's Manual

default_allocator.h File Reference

Description

Default allocator implementation based on global new and delete. See Interface Framework Technology.

Code Example

default_allocator.h

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

#ifndef MI_BASE_DEFAULT_ALLOCATOR_H
#define MI_BASE_DEFAULT_ALLOCATOR_H

#include <mi/base/types.h>
#include <mi/base/iallocator.h>
#include <mi/base/interface_implement.h>
#include <new>

namespace mi
{

namespace base
{

class Default_allocator : public Interface_implement_singleton<IAllocator>
{
    Default_allocator() {}
    Default_allocator( const Default_allocator&) {}
public:

    virtual void* malloc(Size size) {
        // Use non-throwing new call, which may return NULL instead
        return ::new(std::nothrow) char[size];
    }

    virtual void free(void* memory) {
        ::delete[] reinterpret_cast<char*>(memory);
    }

    static IAllocator* get_instance() {
        // We claim that this is multithreading safe because the
        // Default_allocator has an empty default constructor.
        // Whatever number of threads gets into the constructor, there
        // should be no way to screw up the initialization in each
        // thread. The optimizer might even be able to eliminate all
        // code here.
        static Default_allocator allocator;
        return &allocator;
    }
};
 // end group mi_base_iallocator

} // namespace base
} // namespace mi

#endif // MI_BASE_DEFAULT_ALLOCATOR_H

Namespaces

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

Classes

class 
A default allocator implementation based on global new and delete. More...