Pixar Arch 0.25.8
Loading...
Searching...
No Matches
mallocHook.h
Go to the documentation of this file.
1// Copyright 2016 Pixar
2//
3// Licensed under the terms set forth in the LICENSE.txt file available at
4// https://openusd.org/license.
5//
6// Modified by Jeremy Retailleau.
7
8#ifndef PXR_ARCH_MALLOC_HOOK_H
9#define PXR_ARCH_MALLOC_HOOK_H
10
13
14#include "pxr/arch/pxr.h"
15#include "pxr/arch/api.h"
16
17#include <stdlib.h>
18#include <string>
19
20ARCH_NAMESPACE_OPEN_SCOPE
21
29ARCH_API bool ArchIsPtmallocActive();
30
37ARCH_API bool ArchIsStlAllocatorOff();
38
56public:
68 ARCH_API
69 bool Initialize(void* (*mallocWrapper)(size_t, const void*),
70 void* (*reallocWrapper)(void*, size_t, const void*),
71 void* (*memalignWrapper)(size_t, size_t, const void*),
72 void (*freeWrapper)(void*, const void*),
73 std::string* errMsg);
74
80 ARCH_API
82
89 ARCH_API
90 void* Malloc(size_t nBytes) {
91 return (*_underlyingMallocFunc)(nBytes);
92 }
93
100 ARCH_API
101 void* Realloc(void* ptr, size_t nBytes) {
102 return (*_underlyingReallocFunc)(ptr, nBytes);
103 }
104
111 ARCH_API
112 void* Memalign(size_t alignment, size_t nBytes) {
113 return (*_underlyingMemalignFunc)(alignment, nBytes);
114 }
115
122 ARCH_API
123 void Free(void* ptr) {
124 (*_underlyingFreeFunc)(ptr);
125 }
126
127private:
128 // Note: this is a POD (plain 'ol data structure) so we depend on zero
129 // initialization here to null these out. Do not add a constructor or
130 // destructor to this class.
131
132 void* (*_underlyingMallocFunc)(size_t);
133 void* (*_underlyingReallocFunc)(void*, size_t);
134 void* (*_underlyingMemalignFunc)(size_t, size_t);
135 void (*_underlyingFreeFunc)(void*);
136};
137
138ARCH_NAMESPACE_CLOSE_SCOPE
139
140#endif // PXR_ARCH_MALLOC_HOOK_H
Override default malloc() functionality.
Definition mallocHook.h:55
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.
void * Memalign(size_t alignment, size_t nBytes)
Call the original system memalign() function.
Definition mallocHook.h:112
void * Malloc(size_t nBytes)
Call the original system malloc() function.
Definition mallocHook.h:90
void Free(void *ptr)
Call the original system free() function.
Definition mallocHook.h:123
bool IsInitialized()
Return true if *this has been (successfully) initialized.
void * Realloc(void *ptr, size_t nBytes)
Call the original system realloc() function.
Definition mallocHook.h:101
bool ArchIsStlAllocatorOff()
Return true if the C++ STL allocator was requested to be turned off.
bool ArchIsPtmallocActive()
Return true if ptmalloc is being used as the memory allocator.