Pixar Arch
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 "./api.h"
15
16#include <stdlib.h>
17#include <string>
18
19namespace pxr {
20
28ARCH_API bool ArchIsPtmallocActive();
29
36ARCH_API bool ArchIsStlAllocatorOff();
37
55public:
67 ARCH_API
68 bool Initialize(void* (*mallocWrapper)(size_t, const void*),
69 void* (*reallocWrapper)(void*, size_t, const void*),
70 void* (*memalignWrapper)(size_t, size_t, const void*),
71 void (*freeWrapper)(void*, const void*),
72 std::string* errMsg);
73
79 ARCH_API
81
88 ARCH_API
89 void* Malloc(size_t nBytes) {
90 return (*_underlyingMallocFunc)(nBytes);
91 }
92
99 ARCH_API
100 void* Realloc(void* ptr, size_t nBytes) {
101 return (*_underlyingReallocFunc)(ptr, nBytes);
102 }
103
110 ARCH_API
111 void* Memalign(size_t alignment, size_t nBytes) {
112 return (*_underlyingMemalignFunc)(alignment, nBytes);
113 }
114
121 ARCH_API
122 void Free(void* ptr) {
123 (*_underlyingFreeFunc)(ptr);
124 }
125
126private:
127 // Note: this is a POD (plain 'ol data structure) so we depend on zero
128 // initialization here to null these out. Do not add a constructor or
129 // destructor to this class.
130
131 void* (*_underlyingMallocFunc)(size_t);
132 void* (*_underlyingReallocFunc)(void*, size_t);
133 void* (*_underlyingMemalignFunc)(size_t, size_t);
134 void (*_underlyingFreeFunc)(void*);
135};
136
137} // namespace pxr
138
139#endif // PXR_ARCH_MALLOC_HOOK_H
Override default malloc() functionality.
Definition mallocHook.h:54
ARCH_API void Free(void *ptr)
Call the original system free() function.
Definition mallocHook.h:122
ARCH_API void * Malloc(size_t nBytes)
Call the original system malloc() function.
Definition mallocHook.h:89
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:100
ARCH_API void * Memalign(size_t alignment, size_t nBytes)
Call the original system memalign() function.
Definition mallocHook.h:111
ARCH_API bool IsInitialized()
Return true if *this has been (successfully) initialized.
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.