ezEngine  Milestone 7
ezMemoryUtils Class Reference

This class provides functions to work on raw memory. More...

#include <MemoryUtils.h>

Public Types

typedef void(* ConstructorFunction )(void *pDestination)
 
typedef void(* CopyConstructorFunction )(void *pDestination, const void *pSource)
 
typedef void(* DestructorFunction )(void *pDestination)
 

Public Member Functions

template<typename T >
EZ_FORCE_INLINE void Construct (T *pDestination, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE
ezMemoryUtils::ConstructorFunction 
MakeConstructorFunction ()
 
template<typename T >
EZ_FORCE_INLINE void DefaultConstruct (T *pDestination, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE
ezMemoryUtils::ConstructorFunction 
MakeDefaultConstructorFunction ()
 
template<typename T >
EZ_FORCE_INLINE void CopyConstruct (T *pDestination, const T &copy, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE void CopyConstruct (T *pDestination, const T *pSource, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE
ezMemoryUtils::CopyConstructorFunction 
MakeCopyConstructorFunction ()
 
template<typename T >
EZ_FORCE_INLINE void RelocateConstruct (T *pDestination, T *pSource, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE void MoveConstruct (T *pDestination, T &&source)
 
template<typename T >
EZ_FORCE_INLINE void Destruct (T *pDestination, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE
ezMemoryUtils::DestructorFunction 
MakeDestructorFunction ()
 
template<typename T >
EZ_FORCE_INLINE void Copy (T *pDestination, const T *pSource, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE void CopyOverlapped (T *pDestination, const T *pSource, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE void Relocate (T *pDestination, T *pSource, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE void RelocateOverlapped (T *pDestination, T *pSource, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE void Prepend (T *pDestination, const T &source, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE void Prepend (T *pDestination, T &&source, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE bool IsEqual (const T *a, const T *b, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE void ZeroFill (T *pDestination, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE ezInt32 ByteCompare (const T *a, const T *b, size_t uiCount)
 
template<typename T >
EZ_FORCE_INLINE T * AddByteOffset (T *ptr, ptrdiff_t iOffset)
 
template<typename T >
EZ_FORCE_INLINE const T * AddByteOffsetConst (const T *ptr, ptrdiff_t iOffset)
 
template<typename T >
EZ_FORCE_INLINE T * Align (T *ptr, size_t uiAlignment)
 
template<typename T >
EZ_FORCE_INLINE T AlignSize (T uiSize, T uiAlignment)
 
template<typename T >
EZ_FORCE_INLINE bool IsAligned (const T *ptr, size_t uiAlignment)
 
template<typename T >
EZ_FORCE_INLINE void Construct (T *pDestination, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void Construct (T *pDestination, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE
ezMemoryUtils::ConstructorFunction 
MakeConstructorFunction (ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE
ezMemoryUtils::ConstructorFunction 
MakeConstructorFunction (ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void CopyConstruct (T *pDestination, const T &copy, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void CopyConstruct (T *pDestination, const T &copy, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void CopyConstruct (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void CopyConstruct (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void RelocateConstruct (T *pDestination, T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void RelocateConstruct (T *pDestination, T *pSource, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
EZ_FORCE_INLINE void RelocateConstruct (T *pDestination, T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void Destruct (T *pDestination, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void Destruct (T *pDestination, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE
ezMemoryUtils::DestructorFunction 
MakeDestructorFunction (ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE
ezMemoryUtils::DestructorFunction 
MakeDestructorFunction (ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void Copy (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void Copy (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void CopyOverlapped (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
void CopyOverlapped (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void Relocate (T *pDestination, T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void Relocate (T *pDestination, T *pSource, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
EZ_FORCE_INLINE void Relocate (T *pDestination, T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void RelocateOverlapped (T *pDestination, T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void RelocateOverlapped (T *pDestination, T *pSource, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
EZ_FORCE_INLINE void RelocateOverlapped (T *pDestination, T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void Prepend (T *pDestination, const T &source, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void Prepend (T *pDestination, const T &source, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
EZ_FORCE_INLINE void Prepend (T *pDestination, const T &source, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE void Prepend (T *pDestination, T &&source, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE void Prepend (T *pDestination, T &&source, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
EZ_FORCE_INLINE void Prepend (T *pDestination, T &&source, size_t uiCount, ezTypeIsClass)
 
template<typename T >
EZ_FORCE_INLINE bool IsEqual (const T *a, const T *b, size_t uiCount, ezTypeIsPod)
 
template<typename T >
EZ_FORCE_INLINE bool IsEqual (const T *a, const T *b, size_t uiCount, ezTypeIsClass)
 

Static Public Member Functions

template<typename T >
static void Construct (T *pDestination, size_t uiCount)
 Constructs uiCount objects of type T in a raw buffer at pDestination. More...
 
template<typename T >
static ConstructorFunction MakeConstructorFunction ()
 Returns a function pointer to construct an instance of T. Returns nullptr for trivial types.
 
template<typename T >
static void DefaultConstruct (T *pDestination, size_t uiCount)
 Default constructs uiCount objects of type T in a raw buffer at pDestination regardless of T being a class, POD or trivial.
 
template<typename T >
static ConstructorFunction MakeDefaultConstructorFunction ()
 Returns a function pointer to construct an instance of T. Always returns a constructor function regardless of T being a class, POD or trivial.
 
template<typename T >
static void CopyConstruct (T *pDestination, const T &copy, size_t uiCount)
 Constructs uiCount objects of type T in a raw buffer at pDestination, by creating uiCount copies of copy.
 
template<typename T >
static void CopyConstruct (T *pDestination, const T *pSource, size_t uiCount)
 Constructs uiCount objects of type T in a raw buffer at pDestination from an existing array of objects at pSource by using copy construction.
 
template<typename T >
static CopyConstructorFunction MakeCopyConstructorFunction ()
 Returns a function pointer to copy construct an instance of T.
 
template<typename T >
static void RelocateConstruct (T *pDestination, T *pSource, size_t uiCount)
 Constructs uiCount objects of type T in a raw buffer at pDestionation from an existing array of objects at pSource by using move construction.
 
template<typename T >
static void MoveConstruct (T *pDestination, T &&source)
 Constructs an object of type T in a raw buffer at pDestionation, by using move construction from source.
 
template<typename T >
static void Destruct (T *pDestination, size_t uiCount)
 Destructs uiCount objects of type T at pDestination.
 
template<typename T >
static DestructorFunction MakeDestructorFunction ()
 Returns a function pointer to destruct an instance of T. Returns nullptr for POD-types.
 
template<typename T >
static void Copy (T *pDestination, const T *pSource, size_t uiCount)
 Copies objects of type T from pSource to pDestination. More...
 
template<typename T >
static void CopyOverlapped (T *pDestination, const T *pSource, size_t uiCount)
 Copies objects of type T from pSource to pDestination. More...
 
template<typename T >
static void Relocate (T *pDestination, T *pSource, size_t uiCount)
 Moves objects of type T from pSource to pDestination.
 
template<typename T >
static void RelocateOverlapped (T *pDestination, T *pSource, size_t uiCount)
 Moves objects of type T from pSource to pDestination. More...
 
template<typename T >
static void Prepend (T *pDestination, const T &source, size_t uiCount)
 Moves uiCount objects in pDestination by one object and copies source to the free space.
 
template<typename T >
static void Prepend (T *pDestination, T &&source, size_t uiCount)
 Moves uiCount objects in pDestination by one object and moves source to the free space.
 
template<typename T >
static bool IsEqual (const T *a, const T *b, size_t uiCount=1)
 Tests if objects of type T from pSource and pDestination are equal.
 
template<typename T >
static void ZeroFill (T *pDestination, size_t uiCount=1)
 Zeros out buffer of a raw memory.
 
template<typename T >
static ezInt32 ByteCompare (const T *a, const T *b, size_t uiCount=1)
 Compares two buffers of raw memory byte wise.
 
template<typename T >
static T * AddByteOffset (T *ptr, ptrdiff_t iOffset)
 Returns the address stored in ptr plus the given byte offset iOffset, cast to type T. More...
 
template<typename T >
static const T * AddByteOffsetConst (const T *ptr, ptrdiff_t iOffset)
 Returns the address stored in ptr plus the given byte offset iOffset, cast to type const T. More...
 
template<typename T >
static T * Align (T *ptr, size_t uiAlignment)
 Aligns the pointer ptr by moving its address backwards to the previous multiple of uiAlignment.
 
template<typename T >
static T AlignSize (T uiSize, T uiAlignment)
 Aligns the given size uiSize by rounding up to the next multiple of the size.
 
template<typename T >
static bool IsAligned (const T *ptr, size_t uiAlignment)
 Checks whether ptr is aligned to a memory address that is a multiple of uiAlignment.
 
static void ReserveLower4GBAddressSpace ()
 Reserves the lower 4GB of address space in 64-bit builds to ensure all allocations start above 4GB. More...
 

Static Private Member Functions

template<typename T >
static void Construct (T *pDestination, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void Construct (T *pDestination, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static ConstructorFunction MakeConstructorFunction (ezTypeIsPod)
 
template<typename T >
static ConstructorFunction MakeConstructorFunction (ezTypeIsClass)
 
template<typename T >
static void CopyConstruct (T *pDestination, const T &copy, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void CopyConstruct (T *pDestination, const T &copy, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static void CopyConstruct (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void CopyConstruct (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static void RelocateConstruct (T *pDestination, T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void RelocateConstruct (T *pDestination, T *pSource, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
static void RelocateConstruct (T *pDestination, T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static void Destruct (T *pDestination, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void Destruct (T *pDestination, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static DestructorFunction MakeDestructorFunction (ezTypeIsPod)
 
template<typename T >
static DestructorFunction MakeDestructorFunction (ezTypeIsClass)
 
template<typename T >
static void Copy (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void Copy (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static void CopyOverlapped (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void CopyOverlapped (T *pDestination, const T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static void Relocate (T *pDestination, T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void Relocate (T *pDestination, T *pSource, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
static void Relocate (T *pDestination, T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static void RelocateOverlapped (T *pDestination, T *pSource, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void RelocateOverlapped (T *pDestination, T *pSource, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
static void RelocateOverlapped (T *pDestination, T *pSource, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static void Prepend (T *pDestination, const T &source, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void Prepend (T *pDestination, const T &source, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
static void Prepend (T *pDestination, const T &source, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static void Prepend (T *pDestination, T &&source, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static void Prepend (T *pDestination, T &&source, size_t uiCount, ezTypeIsMemRelocatable)
 
template<typename T >
static void Prepend (T *pDestination, T &&source, size_t uiCount, ezTypeIsClass)
 
template<typename T >
static bool IsEqual (const T *a, const T *b, size_t uiCount, ezTypeIsPod)
 
template<typename T >
static bool IsEqual (const T *a, const T *b, size_t uiCount, ezTypeIsClass)
 

Detailed Description

This class provides functions to work on raw memory.

The following concepts are realized: Copy: Copying a object from a to b means that two equivalent objects will exists in both a and b. Relocate: Relocating an object from a to b means that the object will exist in b afterwards but will no longer exist in a, which means a will be destructed. Move: Moving an object from a to b menas that the object will exist in b afterwards but a will be empty afterwards, but not destructed. Construct: Constructing assumes that the destination does not contain a valid object. Overlapped: The source and destination range may overlap for the operation to be performed. The above mentioned concepts can be combined, e.g. RelocateConstruct for relocating to an uninitialized buffer.

Member Function Documentation

template<typename T >
static T* ezMemoryUtils::AddByteOffset ( T *  ptr,
ptrdiff_t  iOffset 
)
static

Returns the address stored in ptr plus the given byte offset iOffset, cast to type T.

This is useful when working with raw memory, to safely modify a pointer without having to take care of the details of pointer arithmetic.

template<typename T >
static const T* ezMemoryUtils::AddByteOffsetConst ( const T *  ptr,
ptrdiff_t  iOffset 
)
static

Returns the address stored in ptr plus the given byte offset iOffset, cast to type const T.

This is useful when working with raw memory, to safely modify a pointer without having to take care of the details of pointer arithmetic. Also it allows you to modify the pointer without having to do a const_cast.

template<typename T >
static void ezMemoryUtils::Construct ( T *  pDestination,
size_t  uiCount 
)
static

Constructs uiCount objects of type T in a raw buffer at pDestination.

You should use 'DefaultConstruct' instead if default construction is needed for trivial types as well.

template<typename T >
static void ezMemoryUtils::Copy ( T *  pDestination,
const T *  pSource,
size_t  uiCount 
)
static

Copies objects of type T from pSource to pDestination.

If the two buffers overlap use CopyOverlapped instead.

template<typename T >
static void ezMemoryUtils::CopyOverlapped ( T *  pDestination,
const T *  pSource,
size_t  uiCount 
)
static

Copies objects of type T from pSource to pDestination.

The two buffers may overlap when using this method.

template<typename T >
static void ezMemoryUtils::RelocateOverlapped ( T *  pDestination,
T *  pSource,
size_t  uiCount 
)
static

Moves objects of type T from pSource to pDestination.

The two buffers may overlap when using this method.

void ezMemoryUtils::ReserveLower4GBAddressSpace ( )
static

Reserves the lower 4GB of address space in 64-bit builds to ensure all allocations start above 4GB.

Note
Note that this does NOT reserve 4GB of RAM, only address space. This can help to detect pointer truncation. In 32-bit builds it does nothing.

Currently only implemented on Windows.


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