LCOV - code coverage report
Current view: top level - include/o3tl - vector_pool.hxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 26 26 100.0 %
Date: 2015-06-13 12:38:46 Functions: 20 20 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #ifndef INCLUDED_O3TL_VECTOR_POOL_HXX
      21             : #define INCLUDED_O3TL_VECTOR_POOL_HXX
      22             : 
      23             : #include <sal/types.h>
      24             : #include <vector>
      25             : 
      26             : namespace o3tl
      27             : {
      28             :     namespace detail
      29             :     {
      30         231 :         template<typename ValueType, class Container> class simple_pool_impl :
      31             :             public Container
      32             :         {
      33             :             typedef typename Container::value_type value_type;
      34             :             std::ptrdiff_t mnFirstFreeIndex;
      35             : 
      36             :         public:
      37         231 :             simple_pool_impl() :
      38         231 :                 mnFirstFreeIndex(-1)
      39         231 :             {}
      40             : 
      41         493 :             std::ptrdiff_t alloc()
      42             :             {
      43         493 :                 return store(ValueType());
      44             :             }
      45             : 
      46         498 :             std::ptrdiff_t store(const ValueType& rCopy)
      47             :             {
      48         498 :                 if( mnFirstFreeIndex != -1 )
      49             :                 {
      50          45 :                     std::ptrdiff_t nIdx=mnFirstFreeIndex;
      51          45 :                     mnFirstFreeIndex = this->at(mnFirstFreeIndex).nextFree;
      52          45 :                     this->at(nIdx).value = rCopy;
      53          45 :                     this->at(nIdx).nextFree = -1;
      54             : 
      55          45 :                     return nIdx;
      56             :                 }
      57             :                 else
      58             :                 {
      59         453 :                     this->push_back(value_type(rCopy));
      60         453 :                     return this->size()-1;
      61             :                 }
      62             :             }
      63             : 
      64          71 :             void free( std::ptrdiff_t nIdx )
      65             :             {
      66          71 :                 this->at(nIdx).nextFree = mnFirstFreeIndex;
      67          71 :                 mnFirstFreeIndex = nIdx;
      68          71 :             }
      69             : 
      70             :             const ValueType& get( std::ptrdiff_t nIdx ) const
      71             :             {
      72             :                 return this->operator[](nIdx).value;
      73             :             }
      74        3024 :             ValueType& get( std::ptrdiff_t nIdx )
      75             :             {
      76        3024 :                 return this->operator[](nIdx).value;
      77             :             }
      78             :         };
      79             : 
      80             :         template< typename ValueType > struct struct_from_value
      81             :         {
      82        1679 :             struct type
      83             :             {
      84             :                 type() :
      85             :                     value(),
      86             :                     nextFree(-1)
      87             :                 {}
      88         453 :                 explicit type( const ValueType& val ) :
      89             :                     value(val),
      90         453 :                     nextFree(-1)
      91         453 :                 {}
      92             : 
      93             :                 ValueType      value;
      94             :                 std::ptrdiff_t nextFree;
      95             :             };
      96             :         };
      97             :     }
      98             : 
      99             :     /** Simple vector-based memory pool allocator
     100             : 
     101             :         This template can be used to provide simple pooled memory
     102             :         allocation from a container class that adheres to the stl
     103             :         random access container concept. Note that alloc/free works
     104             :         with _indices_ into the container!
     105             : 
     106             :         @example
     107             :         <pre>
     108             : vector_pool<type> myPool;
     109             : int nIdx=myPool.alloc();
     110             : myPool[nIdx] = myVal;
     111             :  ... do stuff ...
     112             : myPool.free(nIdx);
     113             :         </pre>
     114             :      */
     115         462 :     template<typename ValueType> struct vector_pool :
     116             :         public detail::simple_pool_impl<ValueType,
     117             :                                        std::vector<typename detail::struct_from_value<ValueType>::type > >
     118             :     {};
     119             : }
     120             : 
     121             : #endif /* INCLUDED_O3TL_VECTOR_POOL_HXX */
     122             : 
     123             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11