All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mallocHook.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_BASE_ARCH_MALLOC_HOOK_H
25 #define PXR_BASE_ARCH_MALLOC_HOOK_H
26 
30 
31 #include "pxr/pxr.h"
32 #include "pxr/base/arch/api.h"
33 
34 #include <stdlib.h>
35 #include <string>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
47 ARCH_API bool ArchIsPtmallocActive();
48 
56 ARCH_API bool ArchIsStlAllocatorOff();
57 
76 public:
88  ARCH_API
89  bool Initialize(void* (*mallocWrapper)(size_t, const void*),
90  void* (*reallocWrapper)(void*, size_t, const void*),
91  void* (*memalignWrapper)(size_t, size_t, const void*),
92  void (*freeWrapper)(void*, const void*),
93  std::string* errMsg);
94 
100  ARCH_API
101  bool IsInitialized();
102 
109  ARCH_API
110  void* Malloc(size_t nBytes) {
111  return (*_underlyingMallocFunc)(nBytes);
112  }
113 
120  ARCH_API
121  void* Realloc(void* ptr, size_t nBytes) {
122  return (*_underlyingReallocFunc)(ptr, nBytes);
123  }
124 
131  ARCH_API
132  void* Memalign(size_t alignment, size_t nBytes) {
133  return (*_underlyingMemalignFunc)(alignment, nBytes);
134  }
135 
142  ARCH_API
143  void Free(void* ptr) {
144  (*_underlyingFreeFunc)(ptr);
145  }
146 
147 private:
148  // Note: this is a POD (plain 'ol data structure) so we depend on zero
149  // initialization here to null these out. Do not add a constructor or
150  // destructor to this class.
151 
152  void* (*_underlyingMallocFunc)(size_t);
153  void* (*_underlyingReallocFunc)(void*, size_t);
154  void* (*_underlyingMemalignFunc)(size_t, size_t);
155  void (*_underlyingFreeFunc)(void*);
156 };
157 
158 PXR_NAMESPACE_CLOSE_SCOPE
159 
160 #endif // PXR_BASE_ARCH_MALLOC_HOOK_H
ARCH_API bool IsInitialized()
Return true if *this has been (successfully) initialized.
ARCH_API void * Malloc(size_t nBytes)
Call the original system malloc() function.
Definition: mallocHook.h:110
ARCH_API void Free(void *ptr)
Call the original system free() function.
Definition: mallocHook.h:143
ARCH_API void * Memalign(size_t alignment, size_t nBytes)
Call the original system memalign() function.
Definition: mallocHook.h:132
ARCH_API bool ArchIsStlAllocatorOff()
Return true if the C++ STL allocator was requested to be turned off.
ARCH_API void * Realloc(void *ptr, size_t nBytes)
Call the original system realloc() function.
Definition: mallocHook.h:121
ARCH_API bool ArchIsPtmallocActive()
Return true if ptmalloc is being used as the memory allocator.
Override default malloc() functionality.
Definition: mallocHook.h:75
ARCH_API bool Initialize(void *(*mallocWrapper)(size_t, const void *), void *(*reallocWrapper)(void *, size_t, const void *), void *(*memalignWrapper)(size_t, size_t, const void *), void(*freeWrapper)(void *, const void *), std::string *errMsg)
Initialize hooks.