Embedded Multicore Building Blocks V1.0.0
Static Public Member Functions | List of all members
embb::base::Allocation Class Reference

Common (static) functionality for unaligned and aligned memory allocation. More...

#include <memory_allocation.h>

Static Public Member Functions

template<typename Type >
static Type * New ()
 Allocates memory for an instance of type Type and default-initializes it. More...
 
template<typename Type , typename Arg1 , ... >
static Type * New (Arg1 argument1,...)
 Allocates memory unaligned for an instance of type Type and initializes it with the specified arguments. More...
 
template<typename Type >
static void Delete (Type *to_delete)
 Destructs an instance of type Type and frees the allocated memory. More...
 
static size_t AllocatedBytes ()
 Returns the total number of bytes currently allocated. More...
 
static void * Allocate (size_t size)
 Allocates size bytes of memory (unaligned). More...
 
static void Free (void *ptr)
 Frees memory that has been allocated by Allocation::Allocate() for some pointer ptr. More...
 
static void * AllocateAligned (size_t alignment, size_t size)
 Allocates size bytes of memory with alignment alignment. More...
 
static void FreeAligned (void *ptr)
 Frees memory that has been allocated by Allocation::AllocateAligned() or Allocation::AllocateCacheAligned() for some pointer ptr. More...
 
static void * AllocateCacheAligned (size_t size)
 Allocates size bytes of cache-aligned memory. More...
 

Detailed Description

Common (static) functionality for unaligned and aligned memory allocation.

This class is a wrapper for the functions in embb/base/c/memory_allocation.h

Member Function Documentation

template<typename Type >
static Type* embb::base::Allocation::New ( )
static

Allocates memory for an instance of type Type and default-initializes it.

Keeps track of allocated memory in debug mode.

Returns
Pointer to new instance of type Type
Exceptions
embb::base::NoMemoryExceptionif not enough memory is available for the given type.
See also
Delete()
Dynamic memory allocation
size+3*sizeof(size_t) bytes in debug mode, otherwise size bytes
Concurrency
Thread-safe
Template Parameters
TypeType of the object to be allocated
template<typename Type , typename Arg1 , ... >
static Type* embb::base::Allocation::New ( Arg1  argument1,
  ... 
)
static

Allocates memory unaligned for an instance of type Type and initializes it with the specified arguments.

Keeps track of allocated memory in debug mode.

Returns
Pointer to new instance of type Type
Exceptions
embb::base::NoMemoryExceptionif not enough memory is available for the given type.
See also
Delete()
Dynamic memory allocation
size+3*sizeof(size_t) bytes in debug mode, otherwise size bytes
Concurrency
Thread-safe
Template Parameters
TypeType of the instance to be allocated
Arg1Type of (first) constructor argument
Parameters
[in]argument1(First) argument for constructor of Type
template<typename Type >
static void embb::base::Allocation::Delete ( Type *  to_delete)
static

Destructs an instance of type Type and frees the allocated memory.

Template Parameters
TypeType of instance to be deleted
Parameters
[in,out]to_deleteInstance to be deleted
static size_t embb::base::Allocation::AllocatedBytes ( )
static

Returns the total number of bytes currently allocated.

Wrapper for C function embb_get_bytes_allocated().

Returns
Number of currently allocated bytes in debug mode, otherwise 0.
Concurrency
Thread-safe and wait-free
static void* embb::base::Allocation::Allocate ( size_t  size)
static

Allocates size bytes of memory (unaligned).

Wrapper for C function embb_allocate().

Keeps track of allocated memory in debug mode.

Returns
NULL in case of failure, otherwise address of allocated memory block.
Exceptions
embb::base::NoMemoryExceptionif not enough memory is available.
Dynamic memory allocation
size+3*sizeof(size_t) bytes in debug mode, otherwise size bytes
Concurrency
Thread-safe
Note
Memory allocated using this function must be freed using Allocation::Free().
See also
AllocateAligned(), AllocateCacheAligned(), Free()
Parameters
[in]sizeSize of memory block to be allocated in bytes
static void embb::base::Allocation::Free ( void *  ptr)
static

Frees memory that has been allocated by Allocation::Allocate() for some pointer ptr.

Wrapper for C function embb_free().

Keeps track of freed memory in debug mode.

Concurrency
Thread-safe
See also
Allocate()
Parameters
[in,out]ptrPointer to memory block to be freed
static void* embb::base::Allocation::AllocateAligned ( size_t  alignment,
size_t  size 
)
static

Allocates size bytes of memory with alignment alignment.

Wrapper for C function embb_alloc_aligned().

This function can be used to align objects to certain boundaries such as cache lines, memory pages, etc.

Keeps track of allocated memory in debug mode.

It is not required that size is a multiple of alignment as, e.g., for the aligned_alloc function of the C11 Standard.

Precondition
The alignment has to be power of 2 and a multiple of size(void*).
Postcondition
The returned pointer is a multiple of alignment.
Returns
NULL in case of failure, otherwise address of allocated memory block.
Exceptions
embb::base::NoMemoryExceptionif not enough memory is available.
Dynamic memory allocation
Debug mode: Let n be the number of aligned cells necessary to fit the payload. Then, (n+1)*alignment+3*size_of(size_t)-1 bytes are allocated.
Release mode: size bytes are requested using the functions provided by the operating systems.
Concurrency
Thread-safe
Note
Memory allocated using this function must be freed using Allocation::FreeAligned().
See also
Allocate(), AllocateCacheAligned(), FreeAligned()
Parameters
[in]alignmentAlignment in bytes
[in]sizeSize of memory block to be allocated in bytes
static void embb::base::Allocation::FreeAligned ( void *  ptr)
static

Frees memory that has been allocated by Allocation::AllocateAligned() or Allocation::AllocateCacheAligned() for some pointer ptr.

Wrapper for C function embb_free_aligned().

Keeps track of freed memory in debug mode.

Concurrency
Thread-safe
See also
AllocateAligned(), AllocateCacheAligned()
Parameters
[in,out]ptrPointer to memory block to be freed
static void* embb::base::Allocation::AllocateCacheAligned ( size_t  size)
static

Allocates size bytes of cache-aligned memory.

Wrapper for C function embb_alloc_cache_aligned().

Specialized version of Allocation::AllocateAligned(). The alignment is chosen automatically (usually 64 bytes).

Keeps track of allocated memory in debug mode.

Postcondition
The returned pointer is a multiple of the cache line size.
Returns
NULL in case of failure, otherwise address of allocated memory block.
Exceptions
embb::base::NoMemoryExceptionif not enough memory is available.
Dynamic memory allocation
See Allocation::AllocateAligned()
Concurrency
Thread-safe
Note
Memory allocated using this function must be freed using Allocation::FreeAligned().
See also
Allocate(), AllocateAligned(), FreeAligned()
Parameters
[in]sizeSize of memory block to be allocated in bytes