AutoReleaseBlock.hpp 1.86 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17
18
19
20

#pragma once 
#include <memory>
#include "castor/tape/tapeserver/daemon/MemBlock.hpp"
21

22
23
24
25
26
namespace castor {
namespace tape {
namespace tapeserver {
namespace daemon {

27
28
/*
 * Use RAII to make sure the memory block is released  
29
 *(ie pushed back to the memory manager) in any case (exception or not)
30
31
32
33
34
35
 * Example of use 
 * {
 *   MemBlock* block = getItFromSomewhere()
 *   {Recall/Migration}MemoryManager mm;
 *   AutoReleaseBlock releaser(block,mm);
 * }
36
37
 */
 template <class MemManagerT> class AutoReleaseBlock {
38
39
40
41
42
43
44
45
   /**
    * The block to release
    */
   MemBlock* const m_block;
   
   /**
    * To whom it should be given back
    */
46
47
   MemManagerT& memManager;
  public:
48
49
50
51
52
53
    /**
     * 
     * @param mb he block to release
     * @param mm To whom it should be given back
     */
    AutoReleaseBlock(MemBlock* const mb,MemManagerT& mm):
54
55
    m_block(mb),memManager(mm){}
        
56
    //let the magic begin 
57
58
59
60
61
    ~AutoReleaseBlock(){
      memManager.releaseBlock(m_block);
    } 
  };
  
62
63
}}}}