From ca8353e94b58d575ff60bac07986d4c73a0d52bc Mon Sep 17 00:00:00 2001 From: Lenard Szolnoki Date: Tue, 16 Aug 2022 16:28:21 +0100 Subject: [PATCH] Use std::allocator_traits instead of direct members of std::allocator Usage of direct members of std::allocator got deprecated in C++17 and removed in C++20. The preferred usage is through std::allocator_traits. --- src/include/fst/cache.h | 3 +- src/include/fst/memory.h | 76 ++++++++++++++++++++---------------- src/include/fst/vector-fst.h | 3 +- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/include/fst/cache.h b/src/include/fst/cache.h index 13b7cf8..0df5526 100644 --- a/src/include/fst/cache.h +++ b/src/include/fst/cache.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -70,7 +71,7 @@ class CacheState { using ArcAllocator = M; using StateAllocator = - typename ArcAllocator::template rebind>::other; + typename std::allocator_traits::template rebind_alloc>; // Provides STL allocator for arcs. explicit CacheState(const ArcAllocator &alloc) diff --git a/src/include/fst/memory.h b/src/include/fst/memory.h index c1f0bdd..05d9840 100644 --- a/src/include/fst/memory.h +++ b/src/include/fst/memory.h @@ -230,13 +230,13 @@ template class BlockAllocator { public: using Allocator = std::allocator; - using size_type = typename Allocator::size_type; - using difference_type = typename Allocator::difference_type; - using pointer = typename Allocator::pointer; - using const_pointer = typename Allocator::const_pointer; - using reference = typename Allocator::reference; - using const_reference = typename Allocator::const_reference; - using value_type = typename Allocator::value_type; + using size_type = typename std::allocator_traits::size_type; + using difference_type = typename std::allocator_traits::difference_type; + using pointer = typename std::allocator_traits::pointer; + using const_pointer = typename std::allocator_traits::const_pointer; + using value_type = typename std::allocator_traits::value_type; + using reference = value_type&; + using const_reference = const value_type&; template struct rebind { @@ -261,31 +261,36 @@ class BlockAllocator { if (Arenas()->DecrRefCount() == 0) delete Arenas(); } - pointer address(reference ref) const { return Allocator().address(ref); } - - const_pointer address(const_reference ref) const { - return Allocator().address(ref); + size_type max_size() const { + auto alloc = Allocator(); + return std::allocator_traits::max_size(alloc); } - size_type max_size() const { return Allocator().max_size(); } - template void construct(U *p, Args &&... args) { - Allocator().construct(p, std::forward(args)...); + auto alloc = Allocator(); + return std::allocator_traits::construct(alloc, p, std::forward(args)...); } - void destroy(pointer p) { Allocator().destroy(p); } + void destroy(pointer p) { + auto alloc = Allocator(); + return std::allocator_traits::destroy(alloc, p); + } pointer allocate(size_type n, const void *hint = nullptr) { if (n * kAllocFit <= kAllocSize) { return static_cast(Arena()->Allocate(n)); } else { - return Allocator().allocate(n, hint); + auto alloc = Allocator(); + return std::allocator_traits::allocate(alloc, n, hint); } } void deallocate(pointer p, size_type n) { - if (n * kAllocFit > kAllocSize) Allocator().deallocate(p, n); + if (n * kAllocFit > kAllocSize){ + auto alloc = Allocator(); + std::allocator_traits::deallocate(alloc, p, n); + } } MemoryArenaCollection *Arenas() const { return arenas_; } @@ -322,13 +327,13 @@ template class PoolAllocator { public: using Allocator = std::allocator; - using size_type = typename Allocator::size_type; - using difference_type = typename Allocator::difference_type; - using pointer = typename Allocator::pointer; - using const_pointer = typename Allocator::const_pointer; - using reference = typename Allocator::reference; - using const_reference = typename Allocator::const_reference; - using value_type = typename Allocator::value_type; + using size_type = typename std::allocator_traits::size_type; + using difference_type = typename std::allocator_traits::difference_type; + using pointer = typename std::allocator_traits::pointer; + using const_pointer = typename std::allocator_traits::const_pointer; + using value_type = typename std::allocator_traits::value_type; + using reference = value_type&; + using const_reference = const value_type&; template struct rebind { @@ -353,20 +358,21 @@ class PoolAllocator { if (Pools()->DecrRefCount() == 0) delete Pools(); } - pointer address(reference ref) const { return Allocator().address(ref); } - - const_pointer address(const_reference ref) const { - return Allocator().address(ref); + size_type max_size() const { + auto alloc = Allocator(); + return std::allocator_traits::max_size(alloc); } - size_type max_size() const { return Allocator().max_size(); } - template void construct(U *p, Args &&... args) { - Allocator().construct(p, std::forward(args)...); + auto alloc = Allocator(); + std::allocator_traits::construct(alloc, p, std::forward(args)...); } - void destroy(pointer p) { Allocator().destroy(p); } + void destroy(pointer p) { + auto alloc = Allocator(); + std::allocator_traits::destroy(alloc, p); + } pointer allocate(size_type n, const void *hint = nullptr) { if (n == 1) { @@ -384,7 +390,8 @@ class PoolAllocator { } else if (n <= 64) { return static_cast(Pool<64>()->Allocate()); } else { - return Allocator().allocate(n, hint); + auto alloc = Allocator(); + return std::allocator_traits::allocate(alloc, n, hint); } } @@ -404,7 +411,8 @@ class PoolAllocator { } else if (n <= 64) { Pool<64>()->Free(p); } else { - Allocator().deallocate(p, n); + auto alloc = Allocator(); + return std::allocator_traits::deallocate(alloc, p, n); } } diff --git a/src/include/fst/vector-fst.h b/src/include/fst/vector-fst.h index 7514bc5..388da27 100644 --- a/src/include/fst/vector-fst.h +++ b/src/include/fst/vector-fst.h @@ -6,6 +6,7 @@ #ifndef FST_VECTOR_FST_H_ #define FST_VECTOR_FST_H_ +#include #include #include #include @@ -35,7 +36,7 @@ class VectorState { using Weight = typename Arc::Weight; using ArcAllocator = M; using StateAllocator = - typename ArcAllocator::template rebind>::other; + typename std::allocator_traits::template rebind_alloc>; // Provide STL allocator for arcs. explicit VectorState(const ArcAllocator &alloc)