MemViz v1.0.0
C++ Memory Layout Inspector
Loading...
Searching...
No Matches
memviz Namespace Reference

Namespaces

namespace  detail
 

Classes

struct  AllocationInfo
 
struct  MemberInfo
 

Functions

template<typename To , typename From >
void castAndPrint (From from)
 Bitwise casts an object to another type and prints the result.
 
template<typename T >
constexpr auto getMembers ()
 Retrieves a compile-time tuple of member information for type T.
 
auto & table ()
 Access the singleton allocation table.
 
std::mutex & lock ()
 Access the global mutex used to protect the table.
 
void on_alloc (const void *ptr, std::size_t sz)
 Register a new allocation.
 
void on_free (const void *ptr)
 Unregister a freed allocation.
 
bool isLive (const void *ptr)
 Check if a pointer is currently tracked as allocated.
 
void reportLeaks (std::ostream &os=std::cout)
 Print a summary of all currently live allocations (potential leaks).
 
void printAllocationLog (std::ostream &os=std::cout)
 Print all currently tracked allocations.
 

Function Documentation

◆ castAndPrint()

template<typename To , typename From >
void memviz::castAndPrint ( From  from)

Bitwise casts an object to another type and prints the result.

This function uses std::bitcast (C++20) to reinterpret the bits of a value of type From as a value of type To. It then outputs the cast result to std::cout.

Template Parameters
ToThe destination type to cast to. Must be trivially copyable and have the same size as From. Must be printable via operator<<.
FromThe source type to cast from. Must be trivially copyable and have the same size as To.
Parameters
fromThe value to be cast.
Note
Both To and From must be trivially copyable and of equal size.
Requires C++20 or later.
Endianness affects how the bits are interpreted between different platforms.
Warning
If To does not have an operator<< overload, this function will fail to compile.
See also
std::bit_cast, std::is_trivially_copyable
struct Test { int x; float y; };
std::ostream& operator<<(std::ostream& os, const Test& t) {
return os << "{ x: " << t.x << ", y: " << t.y << " }";
}
Test t{42, 3.14f};
memviz::castAndPrint<unsigned long long>(t); // prints integer bit pattern
unsigned long long bits = std::bitcast<unsigned long long>(t);
memviz::castAndPrint<Test>(bits); // prints "{ x: 42, y: 3.14 }"
void castAndPrint(From from)
Bitwise casts an object to another type and prints the result.
Definition BitCaster.hpp:42

◆ getMembers()

template<typename T >
constexpr auto memviz::getMembers ( )
constexpr

Retrieves a compile-time tuple of member information for type T.

This function returns the result of memviz::MemberInfo<T>::get(), which should be defined via the MEMVIZ_REGISTER macro. It is used internally by dumpLayout<T>() to inspect object layout at runtime.

Template Parameters
TThe user-defined type being introspected.
Returns
A std::tuple of std::pair<const char*, T member_ptr> representing member names and pointers.
Note
If MemberInfo<T> is not specialized, this function will cause a compile-time error. Use if constexpr (requires { MemberInfo<T>::get(); }) to guard calls safely.
See also
MEMVIZ_REGISTER
dumpLayout

◆ isLive()

bool memviz::isLive ( const void ptr)
inline

Check if a pointer is currently tracked as allocated.

Parameters
ptrPointer to check.
Returns
true if the pointer is in the allocation table, false otherwise.

◆ lock()

std::mutex & memviz::lock ( )
inline

Access the global mutex used to protect the table.

Returns
Reference to a process-wide mutex.

◆ on_alloc()

void memviz::on_alloc ( const void ptr,
std::size_t  sz 
)
inline

Register a new allocation.

Parameters
ptrPointer to the allocated memory block.
sizeSize of the allocated block in bytes.

If ptr is null, this call is ignored. If tracking is already active (e.g., recursive initialization), the call is skipped.

◆ on_free()

void memviz::on_free ( const void ptr)
inline

Unregister a freed allocation.

Parameters
ptrPointer to the memory block being freed.

If ptr is null, this call is ignored. If tracking is disabled (re-entrancy), the call is skipped.

◆ printAllocationLog()

void memviz::printAllocationLog ( std::ostream &  os = std::cout)
inline

Print all currently tracked allocations.

Parameters
osOutput stream to print to (default = std::cout).

This is similar to reportLeaks, but reports each tracked block as "LIVE" (no summary of total leaks). Useful for debugging allocation state.

◆ reportLeaks()

void memviz::reportLeaks ( std::ostream &  os = std::cout)
inline

Print a summary of all currently live allocations (potential leaks).

Parameters
osOutput stream to print to (default = std::cout).

Prints each live allocation pointer and its size, followed by a summary line with the total number of leaks and total leaked bytes.

◆ table()

auto & memviz::table ( )
inline

Access the singleton allocation table.

Returns
Reference to the global allocation table.

The table maps pointer addresses to allocation information. It is allocated on the heap to avoid static destruction order issues.