cds  2.3.2
cds::memory::vyukov_queue_pool< T, Traits > Class Template Reference

Free-list based on bounded lock-free queue cds::intrusive::VyukovMPMCCycleQueue. More...

#include <cds/memory/vyukov_queue_pool.h>

Public Types

typedef cds::intrusive::VyukovMPMCCycleQueue< T, Traits > queue_type
 Queue type.
 
typedef T value_type
 Value type.
 
typedef Traits traits
 Traits type.
 
typedef traits::allocator::template rebind< value_type >::other allocator_type
 allocator type
 
typedef traits::back_off back_off
 back-off strategy
 

Public Member Functions

 vyukov_queue_pool (size_t nCapacity=0)
 Preallocates the pool of object. More...
 
 ~vyukov_queue_pool ()
 Deallocates the pool.
 
value_typeallocate (size_t n)
 Allocates an object from pool. More...
 
void deallocate (value_type *p, size_t n)
 Deallocated the object p. More...
 

Detailed Description

template<typename T, typename Traits = vyukov_queue_pool_traits>
class cds::memory::vyukov_queue_pool< T, Traits >

Free-list based on bounded lock-free queue cds::intrusive::VyukovMPMCCycleQueue.

Template parameters:

Internals

This free-list is very simple. At construction time, the free-list allocates the array of N items and stores them into queue, where N is the queue capacity. When allocating the free-list tries to pop an object from internal queue i.e. from preallocated pool. If success the popped object is returned. Otherwise a new one is allocated. When deallocating, the free-list checks whether the object is from preallocated pool. If so, the object is pushed into queue, otherwise it is deallocated by using the allocator provided. The pool can manage more than N items but only N items is contained in the free-list.

Usage

vyukov_queue_pool should be used together with pool_allocator. You should declare an static object of type vyukov_queue_pool, provide an accessor to that object and use pool_allocator as an allocator:

#include <cds/memory/vyukov_queue_pool.h>
#include <cds/memory/pool_allocator.h>
// Pool of Foo object of size 1024.
struct pool_traits: public cds::memory::vyukov_queue_pool_traits
{
};
static pool_type thePool;
struct pool_accessor {
typedef typename pool_type::value_type value_type;
pool_type& operator()() const
{
return thePool;
}
};
// Declare pool allocator
// Use pool_allocator
// Allocate an object
Foo * p = pool_allocator().allocate( 1 );
// construct object
new(p) Foo;
//...
// Destruct object
p->~Foo();
// Deallocate object
pool_allocator().deallocate( p , 1 );

Constructor & Destructor Documentation

◆ vyukov_queue_pool()

template<typename T , typename Traits = vyukov_queue_pool_traits>
cds::memory::vyukov_queue_pool< T, Traits >::vyukov_queue_pool ( size_t  nCapacity = 0)
inline

Preallocates the pool of object.

nCapacity argument is the queue capacity. It should be passed if the queue is based on dynamically-allocated buffer. See cds::intrusive::VyukovMPMCCycleQueue for explanation.

Member Function Documentation

◆ allocate()

template<typename T , typename Traits = vyukov_queue_pool_traits>
value_type* cds::memory::vyukov_queue_pool< T, Traits >::allocate ( size_t  n)
inline

Allocates an object from pool.

The pool supports allocation only single object (n = 1). If n > 1 the behavior is undefined.

If the queue is not empty, the popped value is returned. Otherwise, a new value allocated.

◆ deallocate()

template<typename T , typename Traits = vyukov_queue_pool_traits>
void cds::memory::vyukov_queue_pool< T, Traits >::deallocate ( value_type p,
size_t  n 
)
inline

Deallocated the object p.

The pool supports allocation only single object (n = 1). If n > 1 the behavior is undefined.

If p is from preallocated pool, it pushes into the queue. Otherwise, p is deallocated by allocator provided.


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

cds 2.3.2 Developed by Maxim Khizhinsky aka khizmax and other contributors 2007 - 2017
Autogenerated Sun Dec 31 2017 12:10:49 by Doxygen 1.8.13