All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mallocHook.h
Go to the documentation of this file.
1//
2// Copyright 2016 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_ARCH_MALLOC_HOOK_H
8#define PXR_BASE_ARCH_MALLOC_HOOK_H
9
13
14#include "pxr/pxr.h"
15#include "pxr/base/arch/api.h"
16
17#include <stdlib.h>
18#include <string>
19
20PXR_NAMESPACE_OPEN_SCOPE
21
30ARCH_API bool ArchIsPtmallocActive();
31
39ARCH_API bool ArchIsStlAllocatorOff();
40
59public:
71 ARCH_API
72 bool Initialize(void* (*mallocWrapper)(size_t, const void*),
73 void* (*reallocWrapper)(void*, size_t, const void*),
74 void* (*memalignWrapper)(size_t, size_t, const void*),
75 void (*freeWrapper)(void*, const void*),
76 std::string* errMsg);
77
83 ARCH_API
85
92 ARCH_API
93 void* Malloc(size_t nBytes) {
94 return (*_underlyingMallocFunc)(nBytes);
95 }
96
103 ARCH_API
104 void* Realloc(void* ptr, size_t nBytes) {
105 return (*_underlyingReallocFunc)(ptr, nBytes);
106 }
107
114 ARCH_API
115 void* Memalign(size_t alignment, size_t nBytes) {
116 return (*_underlyingMemalignFunc)(alignment, nBytes);
117 }
118
125 ARCH_API
126 void Free(void* ptr) {
127 (*_underlyingFreeFunc)(ptr);
128 }
129
130private:
131 // Note: this is a POD (plain 'ol data structure) so we depend on zero
132 // initialization here to null these out. Do not add a constructor or
133 // destructor to this class.
134
135 void* (*_underlyingMallocFunc)(size_t);
136 void* (*_underlyingReallocFunc)(void*, size_t);
137 void* (*_underlyingMemalignFunc)(size_t, size_t);
138 void (*_underlyingFreeFunc)(void*);
139};
140
141PXR_NAMESPACE_CLOSE_SCOPE
142
143#endif // PXR_BASE_ARCH_MALLOC_HOOK_H
Override default malloc() functionality.
Definition: mallocHook.h:58
ARCH_API void * Malloc(size_t nBytes)
Call the original system malloc() function.
Definition: mallocHook.h:93
ARCH_API void * Memalign(size_t alignment, size_t nBytes)
Call the original system memalign() function.
Definition: mallocHook.h:115
ARCH_API bool IsInitialized()
Return true if *this has been (successfully) initialized.
ARCH_API void Free(void *ptr)
Call the original system free() function.
Definition: mallocHook.h:126
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.
ARCH_API void * Realloc(void *ptr, size_t nBytes)
Call the original system realloc() function.
Definition: mallocHook.h:104
ARCH_API bool ArchIsStlAllocatorOff()
Return true if the C++ STL allocator was requested to be turned off.
ARCH_API bool ArchIsPtmallocActive()
Return true if ptmalloc is being used as the memory allocator.