Pixar Arch
Loading...
Searching...
No Matches
pxr::ArchMallocHook Class Reference

Override default malloc() functionality. More...

#include <mallocHook.h>

Public Member Functions

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 bool IsInitialized ()
 Return true if *this has been (successfully) initialized.
 
ARCH_API void * Malloc (size_t nBytes)
 Call the original system malloc() function.
 
ARCH_API void * Realloc (void *ptr, size_t nBytes)
 Call the original system realloc() function.
 
ARCH_API void * Memalign (size_t alignment, size_t nBytes)
 Call the original system memalign() function.
 
ARCH_API void Free (void *ptr)
 Call the original system free() function.
 

Detailed Description

Override default malloc() functionality.

The ArchMallocHook class is used on supported systems to install a call-back function in place of the standard malloc/realloc/free/memalign function calls. Supported systems are currently restricted to 64-bit linux systems.

The call-back function can access the original allocation function by calling, for example, ArchMallocHook::Malloc, or it is free to perform its own allocation.

The ArchMallocHook is a POD (plain old datastructure) which means that to use it properly, it should be declared at global scope, ensuring zero-initialization.

Member Function Documentation

◆ Initialize()

ARCH_API bool pxr::ArchMallocHook::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.

Calling Initialize() installs the supplied functions as call-back in place of the standard system memory allocation routines. Note that the callbacks take an extra const void* parameter; on supported systems, the called function should simply ignore the extra parameter.

If initialization fails, false is returned and *errMsg is set accordingly. If *this has already been initialized, calling Initialize() a second time will fail.

◆ IsInitialized()

ARCH_API bool pxr::ArchMallocHook::IsInitialized ( )

Return true if *this has been (successfully) initialized.

In order for this function to work properly, this cannot be a local or dynamically initialized variable; rather, this must be a global variable, to ensure zero-initialization.

◆ Malloc()

ARCH_API void * pxr::ArchMallocHook::Malloc ( size_t nBytes)
inline

Call the original system malloc() function.

This function allows user-supplied callbacks to access the original system-supplied malloc() call. For speed reasons, no safety checks are performed; in particular, calling this function without having successfully initialized *this will likely crash your program.

◆ Realloc()

ARCH_API void * pxr::ArchMallocHook::Realloc ( void * ptr,
size_t nBytes )
inline

Call the original system realloc() function.

This function allows user-supplied callbacks to access the original system-supplied realloc() call. For speed reasons, no safety checks are performed; in particular, calling this function without having successfully initialized *this will likely crash your program.

◆ Memalign()

ARCH_API void * pxr::ArchMallocHook::Memalign ( size_t alignment,
size_t nBytes )
inline

Call the original system memalign() function.

This function allows user-supplied callbacks to access the original system-supplied memalign() call. For speed reasons, no safety checks are performed; in particular, calling this function without having successfully initialized *this will likely crash your program.

◆ Free()

ARCH_API void pxr::ArchMallocHook::Free ( void * ptr)
inline

Call the original system free() function.

This function allows user-supplied callbacks to access the original system-supplied free() call. For speed reasons, no safety checks are performed; in particular, calling this function without having successfully initialized *this will likely crash your program.


The documentation for this class was generated from the following file: