Material Definition Language API nvidia_logo_transpbg.gif Up
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
default_allocator.h
Go to the documentation of this file.
1 /***************************************************************************************************
2  * Copyright 2020 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 
17 namespace mi
18 {
19 
20 namespace base
21 {
22 
38 {
41 public:
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