From Jason Turner

[mem.res.pool.overview]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmpupaynkr3/{from.md → to.md} +98 -0
tmp/tmpupaynkr3/{from.md → to.md} RENAMED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #### Classes `synchronized_pool_resource` and `unsynchronized_pool_resource` <a id="mem.res.pool.overview">[[mem.res.pool.overview]]</a>
2
+
3
+ The `synchronized_pool_resource` and `unsynchronized_pool_resource`
4
+ classes (collectively called *pool resource classes*) are
5
+ general-purpose memory resources having the following qualities:
6
+
7
+ - Each resource frees its allocated memory on destruction, even if
8
+ `deallocate` has not been called for some of the allocated blocks.
9
+ - A pool resource consists of a collection of *pools*, serving requests
10
+ for different block sizes. Each individual pool manages a collection
11
+ of *chunks* that are in turn divided into blocks of uniform size,
12
+ returned via calls to `do_allocate`. Each call to
13
+ `do_allocate(size, alignment)` is dispatched to the pool serving the
14
+ smallest blocks accommodating at least `size` bytes.
15
+ - When a particular pool is exhausted, allocating a block from that pool
16
+ results in the allocation of an additional chunk of memory from the
17
+ *upstream allocator* (supplied at construction), thus replenishing the
18
+ pool. With each successive replenishment, the chunk size obtained
19
+ increases geometrically. \[*Note 1*: By allocating memory in chunks,
20
+ the pooling strategy increases the chance that consecutive allocations
21
+ will be close together in memory. — *end note*]
22
+ - Allocation requests that exceed the largest block size of any pool are
23
+ fulfilled directly from the upstream allocator.
24
+ - A `pool_options` struct may be passed to the pool resource
25
+ constructors to tune the largest block size and the maximum chunk
26
+ size.
27
+
28
+ A `synchronized_pool_resource` may be accessed from multiple threads
29
+ without external synchronization and may have thread-specific pools to
30
+ reduce synchronization costs. An `unsynchronized_pool_resource` class
31
+ may not be accessed from multiple threads simultaneously and thus avoids
32
+ the cost of synchronization entirely in single-threaded applications.
33
+
34
+ ``` cpp
35
+ struct pool_options {
36
+ size_t max_blocks_per_chunk = 0;
37
+ size_t largest_required_pool_block = 0;
38
+ };
39
+
40
+ class synchronized_pool_resource : public memory_resource {
41
+ public:
42
+ synchronized_pool_resource(const pool_options& opts,
43
+ memory_resource* upstream);
44
+
45
+ synchronized_pool_resource()
46
+ : synchronized_pool_resource(pool_options(), get_default_resource()) {}
47
+ explicit synchronized_pool_resource(memory_resource* upstream)
48
+ : synchronized_pool_resource(pool_options(), upstream) {}
49
+ explicit synchronized_pool_resource(const pool_options& opts)
50
+ : synchronized_pool_resource(opts, get_default_resource()) {}
51
+
52
+ synchronized_pool_resource(const synchronized_pool_resource&) = delete;
53
+ virtual ~synchronized_pool_resource();
54
+
55
+ synchronized_pool_resource&
56
+ operator=(const synchronized_pool_resource&) = delete;
57
+
58
+ void release();
59
+ memory_resource* upstream_resource() const;
60
+ pool_options options() const;
61
+
62
+ protected:
63
+ void *do_allocate(size_t bytes, size_t alignment) override;
64
+ void do_deallocate(void *p, size_t bytes, size_t alignment) override;
65
+
66
+ bool do_is_equal(const memory_resource& other) const noexcept override;
67
+ };
68
+
69
+ class unsynchronized_pool_resource : public memory_resource {
70
+ public:
71
+ unsynchronized_pool_resource(const pool_options& opts,
72
+ memory_resource* upstream);
73
+
74
+ unsynchronized_pool_resource()
75
+ : unsynchronized_pool_resource(pool_options(), get_default_resource()) {}
76
+ explicit unsynchronized_pool_resource(memory_resource* upstream)
77
+ : unsynchronized_pool_resource(pool_options(), upstream) {}
78
+ explicit unsynchronized_pool_resource(const pool_options& opts)
79
+ : unsynchronized_pool_resource(opts, get_default_resource()) {}
80
+
81
+ unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete;
82
+ virtual ~unsynchronized_pool_resource();
83
+
84
+ unsynchronized_pool_resource&
85
+ operator=(const unsynchronized_pool_resource&) = delete;
86
+
87
+ void release();
88
+ memory_resource *upstream_resource() const;
89
+ pool_options options() const;
90
+
91
+ protected:
92
+ void* do_allocate(size_t bytes, size_t alignment) override;
93
+ void do_deallocate(void* p, size_t bytes, size_t alignment) override;
94
+
95
+ bool do_is_equal(const memory_resource& other) const noexcept override;
96
+ };
97
+ ```
98
+