TfMallocTag::Auto Class Reference

Scoped (i.e. More...

Public Member Functions

 Auto (const Auto &)=delete
Autooperator= (const Auto &)=delete
 Auto (Auto &&)=delete
Autooperator= (Auto &&)=delete
template<class Str , class... Strs>
 Auto (Str &&name1, Strs &&... nameN)
 Push one or more memory tags onto the local-call stack with names name1 ... More...
void Release ()
 Pop the tag from the stack before it is destructed. More...
 ~Auto ()
 Pop a memory tag from the local-call stack. More...


class TfMallocTag

Detailed Description

Scoped (i.e.

local) object for creating/destroying memory tags.

Note: TfAutoMallocTag is a typedef to TfMallocTag::Auto; the convention is to use TfAutoMallocTag to make it clear that the local object exists only because its constructor and destructor modify program state.

A TfAutoMallocTag object is used to push memory tags onto the current call stack; destruction of the object pops the tags. Note that each thread has its own tag-stack.

There is very little cost to creating or destroying memory tags if TfMallocTag::Initialize() has not been called: an inline read of a global variable and a branch. If tagging has been initialized, then there is a small cost associated with pushing and popping memory tags on the local stack. Most of the cost is taking a shared/read lock on a mutex and looking up the tag data structures in hash tables. Pushing or popping the call stack does not actually cause any memory allocation unless this is the first time that the given named tag has been encountered.

Definition at line 255 of file mallocTag.h.

Constructor & Destructor Documentation

◆ Auto()

Auto ( Str &&  name1,
Strs &&...  nameN 

Push one or more memory tags onto the local-call stack with names name1 ...

nameN. The passed names should be either string literals, const char pointers, or std::strings.

If TfMallocTag::Initialize() has not been called, this constructor does essentially no work, assuming the names are string literals or a pointer to an existing c-string. However if any of the names are expressions that evaluate to std::string objects, the work done constructing those strings will still be incurred. If this is an issue, you can query TfMallocTag::IsInitialized() to avoid unneeded work when tagging is inactive.

Objects of this class should only be created as local variables; never as member variables, global variables, or via new. If you can't create your object as a local variable, you can make manual calls to TfMallocTag::Push() and TfMallocTag::Pop(), though you should do this only as a last resort.

Definition at line 281 of file mallocTag.h.

◆ ~Auto()

~Auto ( )

Pop a memory tag from the local-call stack.

If TfMallocTag::Initialize() was not called when this tag was pushed onto the stack, popping the tag from the stack does essentially no (measurable) work.

Definition at line 309 of file mallocTag.h.

Member Function Documentation

◆ Release()

void Release ( )

Pop the tag from the stack before it is destructed.

Normally you should not use this. The normal destructor is preferable because it ensures proper release order. If you call Release(), make sure all tags are released in the opposite order they were declared in. It is better to use sub-scopes to control the life span of tags, but if that won't work, Release() is still preferable to TfMallocTag::Push() and TfMallocTag::Pop() because it isn't vulnerable to early returns or exceptions.

Definition at line 297 of file mallocTag.h.

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