LCOV - code coverage report
Current view: top level - source/lib/allocators - STLAllocators.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 17 17 100.0 %
Date: 2023-01-19 00:18:29 Functions: 32 95 33.7 %

          Line data    Source code
       1             : /* Copyright (C) 2021 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : #ifndef INCLUDED_STL_ALLOCATORS
      24             : #define INCLUDED_STL_ALLOCATORS
      25             : 
      26             : #include <memory>
      27             : #include <type_traits>
      28             : 
      29             : /**
      30             :  * Adapt a 0 A.D.-style allocator for usage in STL containers.
      31             :  * Use 'Backend' as an underlying allocator.
      32             :  */
      33             : template<typename T, class Backend>
      34           7 : class STLAllocator
      35             : {
      36             :     template<typename A, class B>
      37             :     friend class STLAllocator;
      38             : public:
      39             :     using value_type = T;
      40             :     using pointer = T*;
      41             :     using is_always_equal = std::false_type;
      42             : 
      43           3 :     STLAllocator() : allocator(std::make_shared<Backend>())
      44             :     {
      45           3 :     }
      46             : 
      47             :     template<typename V>
      48           4 :     STLAllocator(const STLAllocator<V, Backend>& proxy) : allocator(proxy.allocator)
      49             :     {
      50           4 :     }
      51             : 
      52             :     template<typename V>
      53             :     struct rebind
      54             :     {
      55             :         using other = STLAllocator<V, Backend>;
      56             :     };
      57             : 
      58           4 :     T* allocate(size_t n)
      59             :     {
      60           4 :         return static_cast<T*>(allocator->allocate(n * sizeof(T), nullptr, alignof(T)));
      61             :     }
      62             : 
      63           4 :     void deallocate(T* ptr, const size_t n)
      64             :     {
      65           4 :         return allocator->deallocate(static_cast<void*>(ptr), n * sizeof(T));
      66             :     }
      67             : 
      68             : private:
      69             :     std::shared_ptr<Backend> allocator;
      70             : };
      71             : 
      72             : 
      73             : /**
      74             :  * Proxies allocation to another allocator.
      75             :  * This allows a single allocator to serve multiple STL containers.
      76             :  */
      77             : template<typename T, class Backend>
      78             : class ProxyAllocator
      79             : {
      80             :     template<typename A, class B>
      81             :     friend class ProxyAllocator;
      82             : public:
      83             :     using value_type = T;
      84             :     using pointer = T*;
      85             :     using is_always_equal = std::false_type;
      86             : 
      87           3 :     ProxyAllocator(Backend& alloc) : allocator(alloc)
      88             :     {
      89           3 :     }
      90             : 
      91             :     template<typename V>
      92           6 :     ProxyAllocator(const ProxyAllocator<V, Backend>& proxy) : allocator(proxy.allocator)
      93             :     {
      94           6 :     }
      95             : 
      96             :     template<typename V>
      97             :     struct rebind
      98             :     {
      99             :         using other = ProxyAllocator<V, Backend>;
     100             :     };
     101             : 
     102           4 :     T* allocate(size_t n)
     103             :     {
     104           4 :         return static_cast<T*>(allocator.allocate(n * sizeof(T), nullptr, alignof(T)));
     105             :     }
     106             : 
     107           4 :     void deallocate(T* ptr, const size_t n)
     108             :     {
     109           4 :         return allocator.deallocate(static_cast<void*>(ptr), n * sizeof(T));
     110             :     }
     111             : 
     112             : private:
     113             :     Backend& allocator;
     114             : };
     115             : 
     116             : #endif  // INCLUDED_STL_ALLOCATORS

Generated by: LCOV version 1.13