1.. _memory_pool_cls:
2
3memory_pool
4===========
5
6.. note::
7   To enable this feature, set the ``TBB_PREVIEW_MEMORY_POOL`` macro to 1.
8
9A class template for scalable memory allocation from memory blocks provided by an underlying allocator.
10
11.. contents::
12    :local:
13    :depth: 1
14
15Description
16***********
17
18A ``memory_pool`` allocates and frees memory in a way that scales with the number of processors.
19The memory is obtained as big chunks from an underlying allocator specified by the template
20argument. The latter must satisfy the subset of the allocator requirements from the [allocator.requirements]
21ISO C++ Standard section. A ``memory_pool`` meet the :doc:`Memory Pool named requirement<../scalable_memory_pools>`.
22
23.. caution::
24
25    If the underlying allocator refers to another scalable memory pool, the inner pool (or pools)
26    must be destroyed before the outer pool is destroyed or recycled.
27
28API
29***
30
31Header
32------
33
34.. code:: cpp
35
36    #include "oneapi/tbb/memory_pool.h"
37
38Synopsis
39--------
40
41.. code:: cpp
42
43    namespace oneapi {
44        namespace tbb {
45            template <typename Alloc>
46            class memory_pool {
47            public:
48                explicit memory_pool(const Alloc &src = Alloc());
49                memory_pool(const memory_pool& other) = delete;
50                memory_pool& operator=(const memory_pool& other) = delete;
51                ~memory_pool();
52                void recycle();
53                void *malloc(size_t size);
54                void free(void* ptr);
55               void *realloc(void* ptr, size_t size);
56            };
57        }
58    }
59
60Member Functions
61----------------
62
63.. cpp:function:: explicit memory_pool(const Alloc &src = Alloc())
64
65    **Effects**: Constructs a memory pool with an instance of underlying memory allocator of type ``Alloc`` copied from ``src``.
66    Throws the ``bad_alloc`` exception if runtime fails to construct an instance of the class.
67
68Examples
69********
70
71The code below provides a simple example of allocation from an extensible memory pool.
72
73.. code:: cpp
74
75    #define TBB_PREVIEW_MEMORY_POOL 1
76    #include "oneapi/tbb/memory_pool.h"
77    ...
78    oneapi::tbb::memory_pool<std::allocator<char> > my_pool;
79    void* my_ptr = my_pool.malloc(10);
80    my_pool.free(my_ptr);
81