Line data Source code
1 : // Author(s): Maurice Laveaux 2 : // Copyright: see the accompanying file COPYING or copy at 3 : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING 4 : // 5 : // Distributed under the Boost Software License, Version 1.0. 6 : // (See accompanying file LICENSE_1_0.txt or copy at 7 : // http://www.boost.org/LICENSE_1_0.txt) 8 : // 9 : 10 : #ifndef MCRL2_UTILITIES_BLOCK_ALLOCATOR_H_ 11 : #define MCRL2_UTILITIES_BLOCK_ALLOCATOR_H_ 12 : 13 : #include "memory_pool.h" 14 : 15 : #include <memory> 16 : #include <stddef.h> 17 : 18 : namespace mcrl2 19 : { 20 : namespace utilities 21 : { 22 : 23 : /// \brief The block allocator provides the allocator interface for the memory pool class. 24 : /// As such is can be used as an allocator for allocator aware storages. 25 : /// \details Also provides several non-standard allocate functions specifically for the term pool. 26 : template <class T, 27 : std::size_t ElementsPerBlock = 1024, 28 : bool ThreadSafe = false> 29 : class block_allocator : public memory_pool<T, ElementsPerBlock, ThreadSafe> 30 : { 31 : private: 32 : using super = memory_pool<T, ElementsPerBlock, ThreadSafe>; 33 : 34 : public: 35 : using value_type = T; 36 : using size_type = std::size_t; 37 : using difference_type = std::ptrdiff_t; 38 : 39 : template <class U> 40 : struct rebind 41 : { 42 : typedef block_allocator<U, ElementsPerBlock, ThreadSafe> other; 43 : }; 44 : 45 1623 : block_allocator() = default; 46 : 47 : /// \details The unused parameter is to make the interface equivalent 48 : /// to the allocator. 49 1172339 : T* allocate(size_type n, const void* hint = nullptr) 50 : { 51 1172339 : if (n != 1 || hint) 52 : { 53 0 : throw std::bad_alloc(); 54 : } 55 : 56 1172339 : return super::allocate(); 57 : } 58 : 59 : /// \details The unused parameter is to make the interface equivalent 60 : /// to the allocator. 61 240725 : void deallocate(T* p, size_type) 62 : { 63 240725 : super::deallocate(p); 64 240725 : } 65 : 66 : // Move assignment and construction is possible. 67 : block_allocator(block_allocator&&) = default; 68 : block_allocator& operator=(block_allocator&&) = default; 69 : }; 70 : 71 : } // namespace utilities 72 : } // namespace mcrl2 73 : 74 : #endif