Loading...
Searching...
No Matches
Memory Management

Functions having to do with memory allocation/handling. More...

Files

file  align.h
 Provide architecture-specific memory-alignment information.
 
file  mallocHook.h
 Routines for controlling malloc behavior.
 
file  prefetch.h
 Memory prefetch.
 

Classes

class  ArchMallocHook
 Override default malloc() functionality. More...
 

Macros

#define ARCH_MAX_ALIGNMENT_INCREASE
 Maximum extra space needed for alignment.
 
#define ARCH_CACHE_LINE_SIZE
 The size of a CPU cache line on the current processor architecture in bytes.
 

Enumerations

enum class  ArchPrefetchAccess { Read = 0 , Write = 1 }
 The kind of access a prefetch is preparing for. More...
 
enum class  ArchPrefetchLocality { None = 0 , L3 = 1 , L2 = 2 , L1 = 3 }
 How much temporal locality you expect on the address after the fetch. More...
 

Functions

size_t ArchAlignMemorySize (size_t nBytes)
 Return suitably aligned memory size.
 
void * ArchAlignMemory (void *base)
 Align memory to the next "best" alignment value.
 
ARCH_API void * ArchAlignedAlloc (size_t alignment, size_t size)
 Aligned memory allocation.
 
ARCH_API void ArchAlignedFree (void *ptr)
 Free memory allocated by ArchAlignedAlloc.
 
ARCH_API bool ArchIsPtmallocActive ()
 Return true if ptmalloc is being used as the memory allocator.
 
ARCH_API bool ArchIsStlAllocatorOff ()
 Return true if the C++ STL allocator was requested to be turned off.
 
template<size_t Size = 1, size_t Align = 1, ArchPrefetchAccess Access = ArchPrefetchAccess::Read, ArchPrefetchLocality Locality = ArchPrefetchLocality::L2>
void ArchPrefetch (void const *addr) noexcept
 Prefetch the cache lines that [addr, addr+Size) occupy.
 
template<ArchPrefetchAccess Access = ArchPrefetchAccess::Read, ArchPrefetchLocality Locality = ArchPrefetchLocality::L2>
void ArchPrefetchRange (void const *addr, size_t numBytes) noexcept
 Prefetch the cache lines that [addr, addr+numBytes) occupy, where numBytes is a runtime value.
 
template<ArchPrefetchAccess Access = ArchPrefetchAccess::Read, ArchPrefetchLocality Locality = ArchPrefetchLocality::L2, class T >
void ArchPrefetchRange (T const *addr, size_t count) noexcept
 Prefetch the cache lines that the count objects of type T starting at addr occupy, as in ArchPrefetchRange(vec.data(), vec.size()).
 
template<ArchPrefetchLocality Locality = ArchPrefetchLocality::L2, class T >
void ArchPrefetchRead (T const *addr) noexcept
 Prefetch for reading the object at addr with given locality.
 
template<ArchPrefetchLocality Locality = ArchPrefetchLocality::L2, class T >
void ArchPrefetchWrite (T const *addr) noexcept
 Prefetch for writing the object at addr with given locality.
 

Detailed Description

Functions having to do with memory allocation/handling.

Macro Definition Documentation

◆ ARCH_CACHE_LINE_SIZE

#define ARCH_CACHE_LINE_SIZE

The size of a CPU cache line on the current processor architecture in bytes.

Definition at line 67 of file align.h.

◆ ARCH_MAX_ALIGNMENT_INCREASE

#define ARCH_MAX_ALIGNMENT_INCREASE

Maximum extra space needed for alignment.

The ArchAlignMemorySize() can increase the required memory by no more than ARCH_MAX_ALIGNMENT_INCREASE.

Definition at line 47 of file align.h.

Enumeration Type Documentation

◆ ArchPrefetchAccess

enum class ArchPrefetchAccess
strong

The kind of access a prefetch is preparing for.

These follow gcc's __builtin_prefetch's 'rw' parameter. If you don't know what to use, just use Read. Most modern CPUs treat them the same regardless.

Definition at line 38 of file prefetch.h.

◆ ArchPrefetchLocality

enum class ArchPrefetchLocality
strong

How much temporal locality you expect on the address after the fetch.

For pure-streaming, single access and done, use None. For a cache where you expect multiple repeat hits, use higher levels like L2 or L1.

Definition at line 46 of file prefetch.h.

Function Documentation

◆ ArchAlignedAlloc()

ARCH_API void * ArchAlignedAlloc ( size_t alignment,
size_t size )

Aligned memory allocation.

◆ ArchAlignedFree()

ARCH_API void ArchAlignedFree ( void * ptr)

Free memory allocated by ArchAlignedAlloc.

◆ ArchAlignMemory()

void * ArchAlignMemory ( void * base)
inline

Align memory to the next "best" alignment value.

This will take a pointer and bump it to the next ideal alignment boundary that will work for all data types.

Definition at line 55 of file align.h.

◆ ArchAlignMemorySize()

size_t ArchAlignMemorySize ( size_t nBytes)
inline

Return suitably aligned memory size.

Requests to malloc() or ::new for a given size are often rounded upward. Given a request for nBytes bytes of storage, this function returns the amount that would actually be consumed by the system to satisfy it. This is needed for efficient user-defined memory management.

Definition at line 37 of file align.h.

◆ ArchIsPtmallocActive()

ARCH_API bool ArchIsPtmallocActive ( )

Return true if ptmalloc is being used as the memory allocator.

ptmalloc3 is an external shared library providing implementations of the standard memory allocation functions (e.g. malloc, free). Consumers with special behavior that depends on this library may use this function to determine if it is the active allocator.

◆ ArchIsStlAllocatorOff()

ARCH_API bool ArchIsStlAllocatorOff ( )

Return true if the C++ STL allocator was requested to be turned off.

Under gcc, this is done by setting the environment variable GLIBCXX_FORCE_NEW, but it might differ (or not even be possible) for other platforms.

◆ ArchPrefetch()

template<size_t Size = 1, size_t Align = 1, ArchPrefetchAccess Access = ArchPrefetchAccess::Read, ArchPrefetchLocality Locality = ArchPrefetchLocality::L2>
void ArchPrefetch ( void const * addr)
noexcept

Prefetch the cache lines that [addr, addr+Size) occupy.

The template argument Align (which must be a power of two) states the alignment the caller guarantees for addr. This can avoid a runtime check for ranges that could straddle cache lines when geometrically impossible due to alignment. A zero Size prefetches nothing.

Use ArchPrefetchRange() if the length is not a compile-time constant.

Definition at line 193 of file prefetch.h.

◆ ArchPrefetchRange() [1/2]

template<ArchPrefetchAccess Access = ArchPrefetchAccess::Read, ArchPrefetchLocality Locality = ArchPrefetchLocality::L2, class T >
void ArchPrefetchRange ( T const * addr,
size_t count )
noexcept

Prefetch the cache lines that the count objects of type T starting at addr occupy, as in ArchPrefetchRange(vec.data(), vec.size()).

A zero count prefetches nothing.

Note the unit of the second argument follows the pointer, matching the usual C++ conventions: a void const * takes a byte count, as memcpy() does, and a typed pointer takes an object count, as std::copy_n() does.

count objects must actually fit in memory – count * sizeof(T) is not checked for overflow.

Definition at line 231 of file prefetch.h.

◆ ArchPrefetchRange() [2/2]

template<ArchPrefetchAccess Access = ArchPrefetchAccess::Read, ArchPrefetchLocality Locality = ArchPrefetchLocality::L2>
void ArchPrefetchRange ( void const * addr,
size_t numBytes )
noexcept

Prefetch the cache lines that [addr, addr+numBytes) occupy, where numBytes is a runtime value.

A zero numBytes prefetches nothing.

There is no alignment parameter: unlike ArchPrefetch(), this cannot fold the line count at compile time, so knowing the alignment of addr would not save it any work. Prefer ArchPrefetch() when the length is a constant – it unrolls to a straight run of prefetch instructions.

Definition at line 210 of file prefetch.h.

◆ ArchPrefetchRead()

template<ArchPrefetchLocality Locality = ArchPrefetchLocality::L2, class T >
void ArchPrefetchRead ( T const * addr)
noexcept

Prefetch for reading the object at addr with given locality.

The type T must be complete. Prefetches the number of cache lines that T occupies at addr.

Definition at line 243 of file prefetch.h.

◆ ArchPrefetchWrite()

template<ArchPrefetchLocality Locality = ArchPrefetchLocality::L2, class T >
void ArchPrefetchWrite ( T const * addr)
noexcept

Prefetch for writing the object at addr with given locality.

The type T must be complete. Prefetches the number of cache lines that T occupies at addr.

Definition at line 254 of file prefetch.h.