siplasplas
A library for C++ reflection and introspection
Memory-manip

Tools related to raw memory manipulation. More...

Typedefs

using cpp::detail::AlignedMallocAlingOffset = std::uint8_t
 Type used to store the offset between the aligned pointer returned by aligned_malloc() and the beginning of the allocated block. More...
 

Functions

SIPLASPLAS_UTILITY_EXPORT char * cpp::detail::aligned_ptr (char *pointer, std::size_t alignment)
 Returns an address aligned to an specific boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT void * cpp::detail::aligned_ptr (void *pointer, std::size_t alignment)
 Returns an address aligned to an specific boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT const char * cpp::detail::aligned_ptr (const char *pointer, std::size_t alignment)
 Returns an address aligned to an specific boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT const void * cpp::detail::aligned_ptr (const void *pointer, std::size_t alignment)
 Returns an address aligned to an specific boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT bool cpp::detail::is_aligned (char *pointer, std::size_t alignment)
 Checks if an address is aligned to a given boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT bool cpp::detail::is_aligned (void *pointer, std::size_t alignment)
 Checks if an address is aligned to a given boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT bool cpp::detail::is_aligned (const char *pointer, std::size_t alignment)
 Checks if an address is aligned to a given boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT bool cpp::detail::is_aligned (const void *pointer, std::size_t alignment)
 Checks if an address is aligned to a given boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT std::uintptr_t cpp::detail::missalignment (const char *address, std::size_t alignment)
 Returns the distance between a memory address and the next address aligned to the given boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT std::uintptr_t cpp::detail::missalignment (const void *address, std::size_t alignment)
 Returns the distance between a memory address and the next address aligned to the given boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT std::uintptr_t cpp::detail::missalignment (char *address, std::size_t alignment)
 Returns the distance between a memory address and the next address aligned to the given boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT std::uintptr_t cpp::detail::missalignment (void *address, std::size_t alignment)
 Returns the distance between a memory address and the next address aligned to the given boundary. More...
 
template<typename T , typename U >
T * cpp::detail::tagPointer (T *pointer, U data)
 Tags a pointer with the specified data. More...
 
template<typename R , typename U , typename... Args>
auto cpp::detail::tagPointer (R(*pointer)(Args...), U data) -> decltype(pointer)
 Tags a pointer with the specified data. More...
 
template<typename T >
T * cpp::detail::untagPointer (T *pointer)
 Untags a pointer. More...
 
template<typename R , typename... Args>
auto cpp::detail::untagPointer (R(*pointer)(Args...)) -> decltype(pointer)
 Untags a pointer. More...
 
template<typename T >
std::uint16_t cpp::detail::readTaggedPointer (T *pointer)
 Reads the data stored in a tagged pointer. More...
 
template<typename R , typename... Args>
std::uint16_t cpp::detail::readTaggedPointer (R(*pointer)(Args...))
 Reads the data stored in a tagged pointer. More...
 
SIPLASPLAS_UTILITY_EXPORT void * cpp::detail::aligned_malloc (std::size_t size, std::size_t alignment, std::size_t offset=0)
 Allocates a block of memory of memory aligned to an specific boundary. More...
 
SIPLASPLAS_UTILITY_EXPORT void * cpp::detail::aligned_malloc_block (void *pointer, std::size_t offset=0)
 Returns a pointer to the full block allocated by cpp::aligned_malloc() More...
 
SIPLASPLAS_UTILITY_EXPORT void cpp::detail::aligned_free (void *pointer, std::size_t offset=0)
 Deallocates a block allocated by cpp::aligned_malloc() More...
 

Detailed Description

Tools related to raw memory manipulation.

Typedef Documentation

using cpp::detail::AlignedMallocAlingOffset = typedef std::uint8_t

Type used to store the offset between the aligned pointer returned by aligned_malloc() and the beginning of the allocated block.

This type limits the maximum alignment requirement that can be passed to aligned_malloc() stores the distance to the start of the allocated block so it can be deallocated in aligned_free(). To use as little extra memory as possible, a 8 bit unsigned integer is used by default, which means up to 256 byte alignment boundary is supported by default. Users can change that limit by defining SIPLASPLAS_UTILITY_ALIGNEDMALLOC_ALIGNOFFSET_BITS to the with in bits of the unsigned integer used for offset storage (8, 16, 32, and 64 are supported)

#define SIPLASPLAS_UTILITY_ALIGNEDMALLOC_ALIGNEDOFFSET_BITS 32
#include <siplasplas/utility/memory_manip.hpp>
void* ptr =cpp::aligned_malloc(1024, 1024); // Allocate 1024 bytes in the 1024 boundary

Function Documentation

SIPLASPLAS_UTILITY_EXPORT void cpp::detail::aligned_free ( void *  pointer,
std::size_t  offset = 0 
)

Deallocates a block allocated by cpp::aligned_malloc()

Parameters
pointerPointer to the allocated block
offsetUser offset. The behavior is undefined if it's different to the argument given to cpp::aligned_malloc() when allocating the block
SIPLASPLAS_UTILITY_EXPORT void* cpp::detail::aligned_malloc ( std::size_t  size,
std::size_t  alignment,
std::size_t  offset = 0 
)

Allocates a block of memory of memory aligned to an specific boundary.

This function allocates a memory block starting at a specific alignment boundary. Users can also set some extra bytes for bookeeping data before the returned block. To deallocate blocks allocated with aligned_malloc(), use aligned_free(), never std::free()

Parameters
sizeRequested block size in bytes
alignmentRequired alignment boundary. Must be a power of two
offsetExtra space reserved for the user right brefore the returned block. 0 by default.
Returns
A pointer to a memory block of size bytes, multiple of alignment. nullptr if fails
SIPLASPLAS_UTILITY_EXPORT void* cpp::detail::aligned_malloc_block ( void *  pointer,
std::size_t  offset = 0 
)

Returns a pointer to the full block allocated by cpp::aligned_malloc()

aligned_malloc() allocates an oversized block in order to acomplish the alignment requirements While aligned_malloc() returns the expected aligned block, this function can be used to get the complete allocated block.

Parameters
pointerPointer to a block allocated by cpp::aligned_malloc()
offsetUser offset. The behavior is undefined if it's different to the argument given to cpp::aligned_malloc() when allocating the block
Returns
A pointer to the beginning of the complete allocated block. This pointer can be deallocated by std::free().
SIPLASPLAS_UTILITY_EXPORT char* cpp::detail::aligned_ptr ( char *  pointer,
std::size_t  alignment 
)

Returns an address aligned to an specific boundary.

Parameters
pointerStarting address
alignmentAlignment boundary. Must be a power of two
Returns
The next (upper) address from the given address that's aligned to the required boundary
SIPLASPLAS_UTILITY_EXPORT void* cpp::detail::aligned_ptr ( void *  pointer,
std::size_t  alignment 
)

Returns an address aligned to an specific boundary.

Parameters
pointerStarting address
alignmentAlignment boundary. Must be a power of two
Returns
The next (upper) address from the given address that's aligned to the required boundary
SIPLASPLAS_UTILITY_EXPORT const char* cpp::detail::aligned_ptr ( const char *  pointer,
std::size_t  alignment 
)

Returns an address aligned to an specific boundary.

Parameters
pointerStarting address
alignmentAlignment boundary. Must be a power of two
Returns
The next (upper) address from the given address that's aligned to the required boundary
SIPLASPLAS_UTILITY_EXPORT const void* cpp::detail::aligned_ptr ( const void *  pointer,
std::size_t  alignment 
)

Returns an address aligned to an specific boundary.

Parameters
pointerStarting address
alignmentAlignment boundary. Must be a power of two
Returns
The next (upper) address from the given address that's aligned to the required boundary
SIPLASPLAS_UTILITY_EXPORT bool cpp::detail::is_aligned ( char *  pointer,
std::size_t  alignment 
)

Checks if an address is aligned to a given boundary.

Parameters
pointerAddress to check
alignmentRequired alignment. Must be a power of two
Returns
true if pointer is aligned to alignment boundary. False otherwise.
SIPLASPLAS_UTILITY_EXPORT bool cpp::detail::is_aligned ( void *  pointer,
std::size_t  alignment 
)

Checks if an address is aligned to a given boundary.

Parameters
pointerAddress to check
alignmentRequired alignment. Must be a power of two
Returns
true if pointer is aligned to alignment boundary. False otherwise.
SIPLASPLAS_UTILITY_EXPORT bool cpp::detail::is_aligned ( const char *  pointer,
std::size_t  alignment 
)

Checks if an address is aligned to a given boundary.

Parameters
pointerAddress to check
alignmentRequired alignment. Must be a power of two
Returns
true if pointer is aligned to alignment boundary. False otherwise.
SIPLASPLAS_UTILITY_EXPORT bool cpp::detail::is_aligned ( const void *  pointer,
std::size_t  alignment 
)

Checks if an address is aligned to a given boundary.

Parameters
pointerAddress to check
alignmentRequired alignment. Must be a power of two
Returns
true if pointer is aligned to alignment boundary. False otherwise.
SIPLASPLAS_UTILITY_EXPORT std::uintptr_t cpp::detail::missalignment ( const char *  address,
std::size_t  alignment 
)

Returns the distance between a memory address and the next address aligned to the given boundary.

Parameters
addressMemory address
alignmentRequired alignment. Must be a power of two
Returns
The distance in bytes to the next aligned address
SIPLASPLAS_UTILITY_EXPORT std::uintptr_t cpp::detail::missalignment ( const void *  address,
std::size_t  alignment 
)

Returns the distance between a memory address and the next address aligned to the given boundary.

Parameters
addressMemory address
alignmentRequired alignment. Must be a power of two
Returns
The distance in bytes to the next aligned address
SIPLASPLAS_UTILITY_EXPORT std::uintptr_t cpp::detail::missalignment ( char *  address,
std::size_t  alignment 
)

Returns the distance between a memory address and the next address aligned to the given boundary.

Parameters
addressMemory address
alignmentRequired alignment. Must be a power of two
Returns
The distance in bytes to the next aligned address
SIPLASPLAS_UTILITY_EXPORT std::uintptr_t cpp::detail::missalignment ( void *  address,
std::size_t  alignment 
)

Returns the distance between a memory address and the next address aligned to the given boundary.

Parameters
addressMemory address
alignmentRequired alignment. Must be a power of two
Returns
The distance in bytes to the next aligned address
template<typename T >
std::uint16_t cpp::detail::readTaggedPointer ( T *  pointer)

Reads the data stored in a tagged pointer.

Assuming the pointer is a tagged pointer, this function reads the data tagged in the 16 more significative bits of the pointer. Compilation fails if this function is used in non 64 bit architectures.

Parameters
pointerTagged pointer to read
Returns
The 16 bit unsigned integer value tagged in the pointer
template<typename R , typename... Args>
std::uint16_t cpp::detail::readTaggedPointer ( R(*)(Args...)  pointer)

Reads the data stored in a tagged pointer.

Assuming the pointer is a tagged pointer, this function reads the data tagged in the 16 more significative bits of the pointer. Compilation fails if this function is used in non 64 bit architectures.

Parameters
pointerTagged pointer to read
Returns
The 16 bit unsigned integer value tagged in the pointer
template<typename T , typename U >
T* cpp::detail::tagPointer ( T *  pointer,
data 
)

Tags a pointer with the specified data.

This function uses the tagged pointer technique to store data in a 64 bit virtual memory address. Passing data of more than 16 bits wide has undefined behavior. Compilation fails if this function is used in non 64 bit architectures. Note accessing a tagged pointer directly may cause a segmentation fault. See cpp::untagPointer().

Template Parameters
TPointed type
UMust be an integral type of max sizeof(U) = 16 bit
Parameters
pointerpointer where to store data
datadata to store
Returns
A pointer of type T* with the data and the same address
template<typename R , typename U , typename... Args>
auto cpp::detail::tagPointer ( R(*)(Args...)  pointer,
data 
) -> decltype(pointer)

Tags a pointer with the specified data.

This function uses the tagged pointer technique to store data in a 64 bit virtual memory address. Passing data of more than 16 bits wide has undefined behavior. Compilation fails if this function is used in non 64 bit architectures. Note accessing a tagged pointer directly may cause a segmentation fault. See cpp::untagPointer().

Template Parameters
UMust be an integral type of max sizeof(U) = 16 bit
Parameters
pointerpointer where to store data
datadata to store
Returns
A pointer of type T* with the data and the same address
template<typename T >
T* cpp::detail::untagPointer ( T *  pointer)

Untags a pointer.

Assuming the pointer is a tagged pointer, this function removes the tagged data and returns the memory address ready to be referenced. Compilation fails if this function is used in non 64 bit architectures.

Parameters
pointerTagged pointer to clear
Returns
The given pointer with the 16 upper bits cleared
template<typename R , typename... Args>
auto cpp::detail::untagPointer ( R(*)(Args...)  pointer) -> decltype(pointer)

Untags a pointer.

Assuming the pointer is a tagged pointer, this function removes the tagged data and returns the memory address ready to be referenced. Compilation fails if this function is used in non 64 bit architectures.

Parameters
pointerTagged pointer to clear
Returns
The given pointer with the 16 upper bits cleared