Samchon Framework for CPP
1.0.0
|
An allocator ensuring concurrency. More...
#include <CriticalAllocator.hpp>
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... | |
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.
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
_Ty | Type of the elements allocated by the object (aliased as member type value_type). |
Definition at line 50 of file CriticalAllocator.hpp.
|
inline |
Get rw_mutex.
Definition at line 70 of file CriticalAllocator.hpp.
References samchon::library::CriticalAllocator< T >::rw_mutex.
|
inline |
Lock when constructed.
[Inherited] Construct an object.
.
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).
_U | |
_Args |
ptr | Pointer to a location with enough storage space to contain an element of type _U. |
args | Arguments forwarded to the constructor. Args is a list of zero or more types. |
Definition at line 104 of file CriticalAllocator.hpp.
|
inline |
Lock when destroyed
[Inherited] Destory an object.
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).
_U |
ptr | Pointer to the object to be destroyed. |
Definition at line 129 of file CriticalAllocator.hpp.
|
inline |
Lock when allocated
[Inherited] Allocate block of storage.
Locks a mutex for concurrency when children elements are deallocated.
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.
n | Number 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. |
hint | Either 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. |
Definition at line 168 of file CriticalAllocator.hpp.
|
inline |
Lock when deallocated
[Inherited] Release block of storage.
Locks a mutex for concurrency when children elements are deallocated.
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).
ptr | Pointer 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>). |
size | Number 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.
|
private |
A rw_mutex for handling concurrency.
Definition at line 59 of file CriticalAllocator.hpp.
Referenced by samchon::library::CriticalAllocator< T >::getMutex().