-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpool.h
1578 lines (1200 loc) · 45.1 KB
/
pool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* version 1.0
*
*
* BSD 3-Clause License
*
* Copyright (c) 2021, Koynov Stas - [email protected]
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef POOL_H
#define POOL_H
#include <array>
#include <bitset>
#include <memory>
#include <cstdint>
#include <cstddef>
#include <iterator>
#include <type_traits>
namespace pool_impl {
//helper with shorter name
template <typename T, typename... Args>
inline constexpr bool is_nothrow_create = std::is_nothrow_constructible_v<T, Args...>;
template <class Impl,
class Value,
class Category = std::bidirectional_iterator_tag,
class Difference = std::ptrdiff_t>
class Iterator_facade
{
public:
using value_type = std::remove_cv_t<Value>;
using reference = Value&;
using pointer = Value*;
using difference_type = Difference;
using iterator_category = Category;
reference operator*() const noexcept { return impl().dereference(); };
pointer operator->() const noexcept { return &impl().dereference(); };
Impl& operator++() noexcept
{
impl().increment();
return impl();
}
Impl operator++(int) noexcept
{
Impl result(impl());
impl().increment();
return result;
}
Impl& operator--() noexcept
{
impl().decrement();
return impl();
}
Impl operator--(int) noexcept
{
Impl result(impl());
impl().decrement();
return result;
}
friend bool operator== (const Impl &lhs, const Impl &rhs) noexcept
{
return lhs.equal_to(rhs);
}
friend bool operator!= (const Impl &lhs, const Impl &rhs) noexcept
{
return !(lhs == rhs);
}
protected:
constexpr Impl& impl() { return *static_cast< Impl*>(this); }
constexpr const Impl& impl() const { return *static_cast<const Impl*>(this); }
constexpr bool equal_to(const Impl &other) const noexcept { return impl().equal(other); }
};
using Pool_flags_t = uint32_t;
enum Pool_flags: Pool_flags_t
{
POOL_DTOR_OFF = (1u << 0), //Don't generate destructor for pool
POOL_FIXED_CAPACITY = (1u << 1), //Add new Node only from reserve() method (only for dynamic pool)
POOL_SELF_MOVE_GUARD = (1u << 2), //C++ Core Guidelines c65-make-move-assignment-safe-for-self-assignment
POOL_CREATE_EXCEPTION = (1u << 3), //Throw std::bad_alloc exception if no memory
POOL_RESERVE_EXCEPTION= (1u << 4), //Throw std::bad_alloc exception if no memory
};
template <class Impl, Pool_flags_t Flags, typename Enable = void>
class Pool_dtor
{
public:
~Pool_dtor() noexcept { static_cast<Impl*>(this)->dtor(); };
};
template <class Impl, Pool_flags_t Flags>
class Pool_dtor<Impl, Flags, std::enable_if_t< (Flags & POOL_DTOR_OFF) > >
{
};
template <typename T,
std::size_t N,
std::size_t Align,
Pool_flags_t Flags,
class Impl>
class Pool_base
{
static_assert(Align > 0, "Align == 0 is not support");
static_assert(Align >= alignof(T), "Align can't be less than the requirements of the type");
public:
using value_type = T;
using reference = value_type&;
using pointer = value_type*;
using const_reference = const value_type&;
using const_pointer = const value_type*;
using size_type = std::size_t;
Pool_base() = default;
static constexpr std::size_t ALIGN = Align;
static constexpr Pool_flags_t FLAGS = Flags;
static constexpr std::size_t N_VALUE = N;
constexpr std::size_t size() const noexcept { return m_size; }
constexpr bool empty() const noexcept { return size() == 0; }
constexpr bool full() const noexcept { return size() == impl().capacity(); }
template <typename... Args>
T* create(Args&&... args) noexcept(is_nothrow_create<T, Args...>)
{
return impl().create_obj(std::forward<Args>(args)...);
}
protected:
std::size_t m_size = 0;
// Curiously Recurring Template interface
constexpr Impl& impl() { return *static_cast< Impl*>(this); }
constexpr const Impl& impl() const { return *static_cast<const Impl*>(this); }
};
template <typename T>
constexpr Pool_flags_t SPool_base_flags(Pool_flags_t Flags)
{
return std::is_trivially_destructible_v<T> ? (Flags | POOL_DTOR_OFF) : Flags;
}
template <typename T,
std::size_t N,
std::size_t Align,
Pool_flags_t Flags,
class Impl>
class SPool_base: public Pool_base<T, N, Align, Flags, Impl>,
public Pool_dtor<Impl, SPool_base_flags<T>(Flags)>
{
public:
SPool_base() = default;
static constexpr std::size_t capacity() noexcept { return N; }
// disable copy/move semantics
SPool_base(const SPool_base&) = delete;
SPool_base(SPool_base&&) = delete;
SPool_base& operator=(const SPool_base&) = delete;
SPool_base& operator=(SPool_base&&) = delete;
protected:
void dtor() noexcept { this->impl().destroy_all(); }
friend Pool_dtor<Impl, SPool_base_flags<T>(Flags)>;
};
template <typename T,
std::size_t N,
std::size_t Align,
Pool_flags_t Flags,
class Impl>
class DPool_base: public Pool_base<T, N, Align, Flags, Impl>,
public Pool_dtor<Impl, Flags>
{
public:
DPool_base() = default;
constexpr std::size_t capacity() const noexcept { return m_capacity; }
template <typename... Args>
T* create(Args&&... args) noexcept(is_nothrow_create<T, Args...>)
{
if constexpr ( !(Flags & POOL_FIXED_CAPACITY) )
{
if(this->full())
this->impl().add_node();
}
return this->impl().create_obj(std::forward<Args>(args)...);
}
void reserve(std::size_t new_cap) noexcept( !(Flags & POOL_RESERVE_EXCEPTION) )
{
for(auto i = capacity(); (capacity() < new_cap) && (i < new_cap); i++)
{
this->impl().add_node();
}
if constexpr(Flags & POOL_RESERVE_EXCEPTION)
{
if(capacity() < new_cap)
throw std::bad_alloc();
}
}
// disable copy semantics
DPool_base(const DPool_base&) = delete;
DPool_base& operator=(const DPool_base&) = delete;
protected:
std::size_t m_capacity = 0;
Impl& move_assign_operator(Impl&& other) noexcept
{
auto& self = this->impl();
if constexpr (Flags & POOL_SELF_MOVE_GUARD)
{
if(&self == &other)
return self;
}
self.dtor(); // Free the existing resource
self.move_from(std::move(other));
return self;
}
};
template <class AlgBase, class Impl>
class Pool_node_allocator
{
public:
void shrink_to_fit(std::size_t new_cap = 0) noexcept
{
new_cap = std::max(impl().size(), new_cap);
for(auto i = impl().capacity(); (impl().capacity() > new_cap) && (i > new_cap); i--)
{
del_node();
}
}
protected:
using Node = typename AlgBase::Node;
void add_node() noexcept
{
auto new_node = new(std::nothrow) Node();
if(new_node)
{
impl().add_to_free_nodes(new_node);
impl().m_capacity++;
}
}
void del_node() noexcept
{
impl().m_capacity--;
auto top_node = impl().top_free_node();
impl().pop_free_node();
delete top_node;
}
void dtor() noexcept
{
while(impl().top_free_node())
del_node();
}
void move_from(Impl&&) noexcept {}
private:
constexpr Impl& impl() { return *static_cast<Impl*>(this); }
};
template <std::size_t N, class AlgBase, class Impl>
class Pool_block_allocator
{
public:
void shrink_to_fit(std::size_t new_cap = 0) noexcept
{
if(!impl().empty())
return;
for(auto i = impl().capacity(); (impl().capacity() > new_cap) && (i > new_cap); i--)
{
del_node();
}
readd_blocks();
}
protected:
using Node = typename AlgBase::Node;
struct Block {
Block *next;
std::array<Node, N> nodes;
};
Block *m_blocks{nullptr};
void add_node() noexcept
{
auto new_block = new(std::nothrow) Block();
if(!new_block)
return;
impl().add_to_free_nodes(new_block->nodes);
new_block->next = m_blocks;
m_blocks = new_block;
impl().m_capacity += N;
}
void del_node() noexcept
{
auto block = m_blocks;
m_blocks = block->next;
impl().m_capacity -= N;
delete block;
}
void readd_blocks() noexcept
{
impl().reset_free_nodes();
auto block = m_blocks;
while(block)
{
impl().add_to_free_nodes(block->nodes);
block = block->next;
}
}
void dtor() noexcept
{
while(m_blocks)
del_node();
}
void move_from(Impl&& other) noexcept
{
m_blocks = other.m_blocks;
other.m_blocks = nullptr;
}
private:
constexpr Impl& impl() { return *static_cast<Impl*>(this); }
};
/*
* This is a minimal implementation of a circular doubly linked list.
* Has limited methods and is not intended to be used as a complete class.
*/
struct dlist_head
{
public:
dlist_head *next;
dlist_head *prev;
//There is no constructor. We're gonna call init method when it's really necessary!
constexpr void init() noexcept { prev = next = this; }
constexpr bool empty() const noexcept { return next == this; }
void push_back(dlist_head *node) noexcept { insert(node, prev, this); }
/*
* move_to_back - deletes from current list and add to new_head
*
* new_head: the head that will follow our node
*/
void move_to_back(dlist_head *new_head) noexcept
{
del_node(this);
new_head->push_back(this);
}
/*
* remove self from list.
* Note: empty() on node does not return true after this,
* the node is in an undefined state.
*/
void remove() noexcept { del_node(this); }
/*
* splice_front - transfers all the elements of src
* into the this head (copy to front)
*/
void splice_front(dlist_head *src) noexcept
{
if(!src->empty())
{
splice(src, this, next);
src->init();
}
}
private:
/*
* Insert a new node between two known consecutive nodes.
*
* This is only for internal list manipulation when
* we've already known both the prev/next nodes!
*
* before: [prev] <-> [next]
* after: [prev] <-> [node] <-> [next]
*/
static void insert(dlist_head *node,
dlist_head *prev,
dlist_head *next) noexcept
{
next->prev = node;
node->next = next;
node->prev = prev;
prev->next = node;
}
/*
* Delete a list node by making the prev/next nodes
* are pointing to each other
*
* This is only for internal list manipulation when
* we've already known both the prev/next nodes!
*/
static void del(dlist_head *prev, dlist_head *next) noexcept
{
next->prev = prev;
prev->next = next;
}
/*
* Delete node from list.
*
* node: the element to delete from the list.
*
* Note: empty() on node does not return true after this,
* the node is in an undefined state.
*
* before: [prev] <-> [node] <-> [next]
* after: [prev] <-> [next]; old_val <- [node] -> old_val
*/
static void del_node(dlist_head *node) noexcept
{
del(node->prev, node->next);
}
static void splice(dlist_head *list,
dlist_head *prev,
dlist_head *next) noexcept
{
dlist_head *first = list->next;
dlist_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
};
/*
* A mixin class that implements a node management algorithm
* based on a circular doubly-linked list
*
* Technical details:
*
* Each Node is a struct of a dlist_head and a type T.
* Creation and Destroying is very fast - as it is a simple replacement
* of pointers in a circular doubly-linked list.
*/
template <typename T,
std::size_t N,
std::size_t Align,
Pool_flags_t Flags,
class Impl>
class Pool_dlist_base
{
public:
Pool_dlist_base() noexcept
{
m_used_nodes.init();
}
template <typename UnaryFunction>
void for_each(UnaryFunction f)
{
auto iter = m_used_nodes.next;
while(iter != &m_used_nodes)
{
auto next_iter = iter->next; //guard if user will delete obj(for current iter) from pool
f((T *)get_data(iter));
iter = next_iter;
}
}
void destroy(const T* obj) noexcept
{
if(obj)
destroy_obj(obj);
}
void destroy_all() noexcept
{
for_each([this](T* obj){ this->impl().destroy_obj(obj); });
}
template <class Value>
class Iterator_t: public Iterator_facade<Iterator_t<Value>, Value>
{
public:
explicit Iterator_t(const dlist_head *node = nullptr) noexcept:
m_node(const_cast<dlist_head *>(node)) {}
template <class OtherValue>
Iterator_t(const Iterator_t<OtherValue> &other) noexcept:
m_node(other.m_node) {}
private:
dlist_head *m_node;
template <class OtherValue>
bool equal(const Iterator_t<OtherValue> &other) const noexcept
{
return m_node == other.m_node;
}
void increment() noexcept { m_node = m_node->next; }
void decrement() noexcept { m_node = m_node->prev; }
Value& dereference() const noexcept
{
auto data = (Value *)Pool_dlist_base::get_data(m_node);
return *data;
}
template <class> friend class Iterator_t;
friend class Iterator_facade<Iterator_t, Value>;
friend class Pool_dlist_base;
};
using iterator = Iterator_t<T>;
using const_iterator = Iterator_t<const T>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr auto begin() noexcept { return iterator(m_used_nodes.next); }
constexpr auto end() noexcept { return iterator(&m_used_nodes ); }
constexpr auto begin() const noexcept { return const_iterator(m_used_nodes.next); }
constexpr auto end() const noexcept { return const_iterator(&m_used_nodes ); }
constexpr auto cbegin()const noexcept { return const_iterator(m_used_nodes.next); }
constexpr auto cend() const noexcept { return const_iterator(&m_used_nodes ); }
constexpr auto rbegin() noexcept { return reverse_iterator(end()); }
constexpr auto rend() noexcept { return reverse_iterator(begin()); }
constexpr auto rbegin() const noexcept { return const_reverse_iterator(end()); }
constexpr auto rend() const noexcept { return const_reverse_iterator(begin()); }
constexpr auto crbegin()const noexcept { return const_reverse_iterator(cend()); }
constexpr auto crend() const noexcept { return const_reverse_iterator(cbegin());}
iterator destroy(const_iterator pos) noexcept
{
iterator ret(pos.m_node->next);
destroy_obj(&(*pos));
return ret;
}
iterator destroy(const_iterator first, const_iterator last) noexcept
{
while (first != last)
first = destroy(first);
return last;
}
protected:
using Data = struct { alignas(Align) std::byte data[sizeof(T)]; };
struct Node {
union {
Node *next;
dlist_head head;
};
Data data;
};
Node *m_free_nodes{nullptr};
dlist_head m_used_nodes;
template <typename... Args>
T* create_obj(Args&&... args) noexcept(is_nothrow_create<T, Args...>)
{
if(!m_free_nodes)
return nullptr;
auto free_node = m_free_nodes;
auto obj = ::new (&free_node->data) T(std::forward<Args>(args)...);
//---- Kalb line ----
m_free_nodes = free_node->next;
m_used_nodes.push_back(&free_node->head);
impl().m_size++;
return obj;
}
void destroy_obj(const T* obj) noexcept
{
impl().m_size--;
std::destroy_at(obj);
auto node = get_node(obj);
node->head.remove(); //remove node from m_used_nodes
add_to_free_nodes(node);
}
static constexpr Node* get_node(const T* obj) noexcept {
return (Node *)((char *)obj - offsetof(Node, data));
}
static constexpr void* get_data(const dlist_head* head) noexcept {
return ((char *)head + offsetof(Node, data));
}
constexpr Node* top_free_node() noexcept { return m_free_nodes; }
constexpr void reset_free_nodes() noexcept { m_free_nodes = nullptr;}
//If m_free_nodes == nullptr calling pop_free_node is undefined
constexpr void pop_free_node() noexcept
{
auto free_node = m_free_nodes;
m_free_nodes = free_node->next;
}
constexpr void add_to_free_nodes(Node* node) noexcept
{
node->next = m_free_nodes;
m_free_nodes = node;
}
constexpr void add_to_free_nodes(std::array<Node, N> &nodes) noexcept
{
for(auto &node: nodes)
{
add_to_free_nodes(&node);
}
}
void move_from(Impl&& other) noexcept //only for dynamic
{
impl().m_size = other.m_size;
impl().m_capacity = other.m_capacity;
impl().m_free_nodes = other.m_free_nodes;
impl().m_used_nodes.splice_front(&other.m_used_nodes);
other.m_size = 0;
other.m_capacity = 0;
other.m_free_nodes = nullptr;
}
private:
constexpr Impl& impl() { return *static_cast<Impl*>(this); }
friend Pool_node_allocator <Pool_dlist_base, Impl>;
friend Pool_block_allocator<N, Pool_dlist_base, Impl>;
};
/*
* A mixin class that implements a node management algorithm
* based on a singly-linked list
*
* Technical details:
*
* Each Node is a union of a pointer and a type T.
* Free nodes contain a pointer to the next free node.
* Creation is quick as all that is necessary
* is to return the address of the next free node.
* Destroying also very quick as the node's content is simply
* replaced with the address of the current next free node.
*/
template <typename T,
std::size_t N,
std::size_t Align,
Pool_flags_t Flags,
class Impl>
class Pool_list_base
{
public:
void destroy(const T* obj) noexcept
{
if(obj)
destroy_obj(obj);
}
protected:
using Data = struct { alignas(Align) std::byte data[sizeof(T)]; };
union Node {
Node* next;
Data data;
};
Node* m_free_nodes{nullptr};
template<class S>
class state_saver
{
private:
S stored_value;
S& obj_ref;
public:
state_saver(S& object) noexcept:
stored_value(object),
obj_ref(object)
{}
~state_saver() noexcept { if (enable) obj_ref = stored_value; }
bool enable{true};
};
template <typename... Args>
T* create_obj(Args&&... args) noexcept(is_nothrow_create<T, Args...>)
{
if(!m_free_nodes)
return nullptr;
auto next_node = m_free_nodes->next;
//Saver is a RAII for restore of state(linked list)
//in case ctor of object will thrown an exception.
//It is needed for basic exception guarantee.
//INFO: Some MSVC and mingw32 gcc version 7.3.0
//get internal compiler error:(for Auto Type Deduction for state_saver)
//The solution is to set the type manually
state_saver<Node*> saver(m_free_nodes->next);
auto obj = ::new (m_free_nodes) T(std::forward<Args>(args)...);
saver.enable = false; //ctor of object did not throw an exception, all ok
//---- Kalb line ----
impl().m_size++;
m_free_nodes = next_node;
return obj;
}
void destroy_obj(const T* obj) noexcept
{
impl().m_size--;
std::destroy_at(obj);
add_to_free_nodes((Node*)obj);
}
//This algorithm does not support the for_each method and,
//as a consequence, the destroy_all method. But we declare it empty
//so that we can call it in the derived (if necessary).
//We can use SFINAE test to check if a method destroy_all() exists
//but this is more code than just declaring a non-public empty method
//that will be removed by the compiler.
void destroy_all() noexcept {}
constexpr void add_to_free_nodes(Node* node) noexcept
{
node->next = m_free_nodes;
m_free_nodes = node;
}
constexpr void add_to_free_nodes(std::array<Node, N> &nodes) noexcept
{
for(auto &node: nodes)
{
add_to_free_nodes(&node);
}
}
constexpr Node* top_free_node() noexcept { return m_free_nodes; }
constexpr void reset_free_nodes() noexcept { m_free_nodes = nullptr;}
//If m_free_nodes == nullptr calling pop_free_node is undefined
constexpr void pop_free_node() noexcept
{
auto free_node = m_free_nodes;
m_free_nodes = free_node->next;
}
void move_from(Impl&& other) noexcept //only for dynamic
{
impl().m_size = other.m_size;
impl().m_capacity = other.m_capacity;
impl().m_free_nodes = other.m_free_nodes;
other.m_size = 0;
other.m_capacity = 0;
other.m_free_nodes = nullptr;
}
private:
constexpr Impl& impl() { return *static_cast<Impl*>(this); }
friend Pool_node_allocator <Pool_list_base, Impl>;
friend Pool_block_allocator<N, Pool_list_base, Impl>;
};
/*
* Static object pool is implemented on a singly-linked list
*
* Technical details:
*
* We don't store information(list) of the used nodes.
* This gives a lot of complexity == O(N^2) for the for_each algorithm.
*/
template <typename T,
std::size_t N,
std::size_t Align = alignof(T),
Pool_flags_t Flags = 0>
class SPool_list: public SPool_base<T, N, Align, Flags,
SPool_list<T, N, Align, Flags> >,
public Pool_list_base<T, N, Align, Flags,
SPool_list<T, N, Align, Flags> >
{
public:
SPool_list() noexcept
{
this->add_to_free_nodes(m_pool);
}
template <typename UnaryFunction>
void for_each(UnaryFunction f)
{
if(this->empty())
return;
if(this->full()) //complexity == O(N)
{
for(auto &node : m_pool)
f((T *)&node);
return;
}
//The pool is not empty and not full, we have no information
//about the nodes used. But there is a list of free ones,
//this check leads to the complexity == O(N^2)
for(auto &node : m_pool)
{
if(node_is_used(&node))
f((T *)&node);
}
}
void destroy_all() noexcept
{
for_each([this](T* obj){ this->destroy_obj(obj); });
}
private:
using Node = typename Pool_list_base<T, N, Align, Flags, SPool_list>::Node;