Base API nvidia_logo_transpbg.gif Up
default_allocator.h
Go to the documentation of this file.
1/***************************************************************************************************
2 * Copyright 2024 NVIDIA Corporation. All rights reserved.
3 **************************************************************************************************/
8
9#ifndef MI_BASE_DEFAULT_ALLOCATOR_H
10#define MI_BASE_DEFAULT_ALLOCATOR_H
11
12#include <mi/base/types.h>
13#include <mi/base/iallocator.h>
15#include <new>
16
17namespace mi
18{
19
20namespace base
21{
22
38{
41public:
42
51 virtual void* malloc(Size size) {
52 // Use non-throwing new call, which may return NULL instead
53 return ::new(std::nothrow) char[size];
54 }
55
64 virtual void free(void* memory) {
65 ::delete[] reinterpret_cast<char*>(memory);
66 }
67
70 // We claim that this is multithreading safe because the
71 // Default_allocator has an empty default constructor.
72 // Whatever number of threads gets into the constructor, there
73 // should be no way to screw up the initialization in each
74 // thread. The optimizer might even be able to eliminate all
75 // code here.
76 static Default_allocator allocator;
77 return &allocator;
78 }
79};
80 // end group mi_base_iallocator
82
83} // namespace base
84} // namespace mi
85
86#endif // MI_BASE_DEFAULT_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
Mixin class template for deriving singleton interface implementations, where the reference count is f...
Definition: interface_implement.h:304
virtual void free(void *memory)
Releases the given memory block.
Definition: default_allocator.h:64
static IAllocator * get_instance()
Returns the single instance of the default allocator.
Definition: default_allocator.h:69
virtual void * malloc(Size size)
Allocates a memory block of the given size.
Definition: default_allocator.h:51
Uint64 Size
Unsigned integral type that is large enough to hold the size of all types.
Definition: types.h:112
Allocator interface class to dynamically allocate and deallocate memory.
Mixin class template for deriving interface implementations.
Common namespace for APIs of NVIDIA Advanced Rendering Center GmbH.
Definition: base.h:34
Basic types.