Samchon Framework for CPP  1.0.0
samchon::library::CriticalAllocator< T > Class Template Reference

An allocator ensuring concurrency. More...

#include <CriticalAllocator.hpp>

Collaboration diagram for samchon::library::CriticalAllocator< T >:

Public Member Functions

auto getMutex () -> RWMutex &
 Get rw_mutex. More...
 
template<class _U , class... _Args>
void construct (_U *ptr, _Args &&...args)
 Lock when constructed.
[Inherited] Construct an object.
. More...
 
template<class _U >
void destroy (_U *ptr)
 Lock when destroyed
[Inherited] Destory an object. More...
 
auto allocate (size_type n, std::allocator< void >::const_pointer hint=NULL) -> pointer
 Lock when allocated
[Inherited] Allocate block of storage. More...
 
void deallocate (pointer ptr, size_type size)
 Lock when deallocated
[Inherited] Release block of storage. More...
 

Private Attributes

RWMutex rw_mutex
 A rw_mutex for handling concurrency. More...
 

Detailed Description

template<class T>
class samchon::library::CriticalAllocator< T >

An allocator ensuring concurrency.

CriticalAllocator is a std::allocator ensuring concurrency.

CriticalAllocator keeps safety in multi-threading environment automatically. But it ensures only safety. If a logic needs a mutual exclusion not only level of the container, it'd better to avoid using the CriticalAllocator and use RWMutex by yourself.

[Inherited]

Allocators are classes that define memory models to be used by some parts of the Standard Library, and most specifically, by STL containers.

This section describes the default allocator template allocator (lowercase). This is the allocator that all standard containers will use if their last (and optional) template parameter is not specified, and is the only predefined allocator in the standard library. </p<

Other allocators may be defined. Any class Alloc for which allocator_traits<Alloc> produces a valid instantiation with the appropriate members defined can be used as an allocator on standard containers (Alloc may or may not implement the functionality through member functions). </p<

Except for its destructor, no member of the standard default allocator class template shall introduce data races. Calls to member functions that allocate or deallocate storage shall occur in a single total order, and each such deallocation shall happen before the next allocation (if any) in this order.

Technically, a memory model described by allocators might be specialized for each type of object to be allocated and even may store local data for each container they work with. Although this does not happen with the default allocator.

Referenced comments of std::allocator

Class Diagram
Template Parameters
_TyType of the elements allocated by the object (aliased as member type value_type).

Library - Critical Section

Author
Jeongho Nam http://samchon.org

Definition at line 50 of file CriticalAllocator.hpp.

Member Function Documentation

template<class T >
auto samchon::library::CriticalAllocator< T >::getMutex ( ) -> RWMutex&
inline

Get rw_mutex.

Definition at line 70 of file CriticalAllocator.hpp.

References samchon::library::CriticalAllocator< T >::rw_mutex.

template<class T >
template<class _U , class... _Args>
void samchon::library::CriticalAllocator< T >::construct ( _U *  ptr,
_Args &&...  args 
)
inline

Lock when constructed.
[Inherited] Construct an object.
.

Locks a mutex for concurrency when an elmented is constructed.
[Inherited]

Constructs an element object on the location pointed by p.

Notice that this does not allocate space for the element. It should already be available at p (see member allocate to allocate space).

Template Parameters
_U
_Args
Parameters
ptrPointer to a location with enough storage space to contain an element of type _U.
argsArguments forwarded to the constructor.
Args is a list of zero or more types.

Definition at line 104 of file CriticalAllocator.hpp.

template<class T >
template<class _U >
void samchon::library::CriticalAllocator< T >::destroy ( _U *  ptr)
inline

Lock when destroyed
[Inherited] Destory an object.

Locks a mutex for concurrency when an child is destroyed
[Inherited]

Destroys in-place the object pointed by p.

Notice that this does not deallocate the storage for the element (see member deallocate to release storage space).

Template Parameters
_U
Parameters
ptrPointer to the object to be destroyed.

Definition at line 129 of file CriticalAllocator.hpp.

template<class T >
auto samchon::library::CriticalAllocator< T >::allocate ( size_type  n,
std::allocator< void >::const_pointer  hint = NULL 
) -> pointer
inline

Lock when allocated
[Inherited] Allocate block of storage.

Locks a mutex for concurrency when children elements are deallocated.

[Inherited]

Attempts to allocate a block of storage with a size large enough to contain n elements of member type value_type (an alias of the allocator's template parameter), and returns a pointer to the first element.

The storage is aligned appropriately for objects of type value_type, but they are not constructed.

In the standard default allocator, the block of storage is allocated using ::operator new one or more times, and throws bad_alloc if it cannot allocate the total amount of storage requested.

Parameters
nNumber of elements (each of size sizeof(value_type)) to be allocated.
The member type size_type is an alias of size_t (in the standard default allocator) size_t is an unsigned integral type.
hintEither 0 or a value previously obtained by another call to allocate and not yet freed with deallocate. When it is not 0, this value may be used as a hint to improve performance by allocating the new block near the one specified. The address of an adjacent element is often a good choice.
Returns
A pointer to the initial element in the block of storage.
pointer and const_pointer are member types (defined as aliases of T* and const T* respectively in std::allocator).
The standard default allocator throws bad_alloc if it cannot allocate the requested amount of storage.

Definition at line 168 of file CriticalAllocator.hpp.

template<class T >
void samchon::library::CriticalAllocator< T >::deallocate ( pointer  ptr,
size_type  size 
)
inline

Lock when deallocated
[Inherited] Release block of storage.

Locks a mutex for concurrency when children elements are deallocated.

[Inherited]

Releases a block of storage previously allocated with member allocate and not yet released.

The elements in the array are not destroyed by a call to this member function.

In the default allocator, the block of storage is at some point deallocated using ::operator delete (either during the function call, or later).

Parameters
ptrPointer to a block of storage previously allocated with allocator::allocate.
pointer is a member type (defined as an alias of T* in std::allocator<_Ty>).
sizeNumber of elements allocated on the call to allocator::allocate for this block of storage. The member type size_type is an alias of size_t (in the standard default allocator). size_t is an unsigned integral type.

Definition at line 196 of file CriticalAllocator.hpp.

Member Data Documentation

template<class T >
RWMutex samchon::library::CriticalAllocator< T >::rw_mutex
private

A rw_mutex for handling concurrency.

Definition at line 59 of file CriticalAllocator.hpp.

Referenced by samchon::library::CriticalAllocator< T >::getMutex().


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