1.. _memory_pool_allocator_cls:
2
3memory_pool_allocator
4=====================
5
6.. note::
7   To enable this feature, set the ``TBB_PREVIEW_MEMORY_POOL`` macro to 1.
8
9A class template that provides a memory pool with a C++ allocator interface.
10
11.. contents::
12    :local:
13    :depth: 1
14
15Description
16***********
17
18``memory_pool_allocator`` meets the allocator requirements from the [allocator.requirements] ISO C++ Standard section
19It also provides a constructor to allocate and deallocate memory.
20This constructor is linked with an instance of either the ``memory_pool`` or the ``fixed_pool`` class.
21The class is mainly intended for enabling memory pools within STL containers.
22
23API
24***
25
26Header
27------
28
29.. code:: cpp
30
31    #include "oneapi/tbb/memory_pool.h"
32
33Synopsis
34--------
35
36.. code:: cpp
37
38    namespace oneapi {
39        namespace tbb {
40            template<typename T>
41            class memory_pool_allocator {
42            public:
43                using value_type = T;
44                using pointer = value_type*;
45                using const_pointer = const value_type*;
46                using reference = value_type&;
47                using const_reference = const value_type&;
48                using size_type = size_t;
49                using difference_type = ptrdiff_t;
50                template<typename U> struct rebind {
51                    using other = memory_pool_allocator<U>;
52                };
53                explicit memory_pool_allocator(memory_pool &pool) throw();
54                explicit memory_pool_allocator(fixed_pool &pool) throw();
55                memory_pool_allocator(const memory_pool_allocator& src) throw();
56                template<typename U>
57                memory_pool_allocator(const memory_pool_allocator<U,P>& src) throw();
58                pointer address(reference x) const;
59                const_pointer address(const_reference x) const;
60                pointer allocate(size_type n, const void* hint=0);
61                void deallocate(pointer p, size_type);
62                size_type max_size() const throw();
63                void construct(pointer p, const T& value);
64                void destroy(pointer p);
65            };
66
67            template<>
68            class memory_pool_allocator<void> {
69            public:
70                using pointer = void*;
71                using const_pointer = const void*;
72                using value_type = void;
73                template<typename U> struct rebind {
74                    using other = memory_pool_allocator<U>;
75                };
76                memory_pool_allocator(memory_pool &pool) throw();
77                memory_pool_allocator(fixed_pool &pool) throw();
78                memory_pool_allocator(const memory_pool_allocator& src) throw();
79                template<typename U>
80                memory_pool_allocator(const memory_pool_allocator<U>& src) throw();
81            };
82        } // namespace tbb
83    } // namespace oneapi
84
85    template<typename T, typename U>
86    inline bool operator==( const memory_pool_allocator<T>& a,
87                           const memory_pool_allocator<U>& b);
88    template<typename T, typename U>
89    inline bool operator!=( const memory_pool_allocator<T>& a,
90                            const memory_pool_allocator<U>& b);
91
92Member Functions
93----------------
94
95.. cpp:function:: explicit memory_pool_allocator(memory_pool &pool)
96
97    **Effects**: Constructs a memory pool allocator serviced by ``memory_pool`` instance pool.
98
99-------------------------------------------------------
100
101.. cpp:function:: explicit memory_pool_allocator(fixed_pool &pool)
102
103    **Effects**: Constructs a memory pool allocator serviced by ``fixed_pool`` instance pool.
104
105Examples
106********
107
108The code below provides a simple example of container construction with the use of a memory pool.
109
110.. code:: cpp
111
112    #define TBB_PREVIEW_MEMORY_POOL 1
113    #include "oneapi/tbb/memory_pool.h"
114    ...
115    typedef oneapi::tbb::memory_pool_allocator<int>
116    pool_allocator_t;
117    std::list<int, pool_allocator_t> my_list(pool_allocator_t( my_pool ));
118