Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
CachingAllocatorImplBase.cpp
1 /*
2  * The MIT License (MIT)
3  *
4  * Copyright (c) 2016 The SKA organisation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "cheetah/data/detail/CachingAllocatorImplBase.h"
25 #include "panda/Log.h"
26 #include "panda/Error.h"
27 
28 namespace ska {
29 namespace cheetah {
30 namespace data {
31 
32 template <typename DerivedType, typename T>
34 {
35 }
36 
37 template <typename DerivedType, typename T>
39 {
40  // free all allocations when CachingAllocatorImplBase goes out of scope
41  free_all();
42 }
43 
44 template <typename DerivedType, typename T>
46 {
47  std::lock_guard<std::mutex> lock(_mutex);
48  T* result = nullptr;
49 
50  // search the cache for a free block
51  typename FreeBlocksType::iterator free_block = _free_blocks.find(num_elements);
52 
53  if(free_block != _free_blocks.end())
54  {
55  // get the pointer
56  result = free_block->second;
57 
58  // erase from the _free_blocks map
59  _free_blocks.erase(free_block);
60  }
61  else
62  {
63  // no allocation of the right size exists
64  // create a new one with cuda::malloc
65  // throw if cuda::malloc can't satisfy the request
66  try
67  {
68  // allocate memory and convert cuda::pointer to raw pointer
69  result = DerivedType::_allocate(num_elements);
70  }
71  catch(std::runtime_error &e)
72  {
73  throw panda::Error(e);
74  }
75  }
76 
77  // insert the allocated pointer into the _allocated_blocks map
78  _allocated_blocks.insert(std::make_pair(result, num_elements));
79  return result;
80 }
81 
82 template <typename DerivedType, typename T>
84 {
85  std::lock_guard<std::mutex> lock(_mutex);
86  // erase the allocated block from the allocated blocks map
87  typename AllocatedBlocksType::iterator iter = _allocated_blocks.find(ptr);
88  std::size_t num_elements = iter->second;
89  _allocated_blocks.erase(iter);
90 
91  // insert the block into the free blocks map
92  _free_blocks.insert(std::make_pair(num_elements, ptr));
93 }
94 
95 template <typename DerivedType, typename T>
97 {
98  std::lock_guard<std::mutex> lock(_mutex);
99  // deallocate all outstanding blocks in both lists
100  for(typename FreeBlocksType::iterator i = _free_blocks.begin();
101  i != _free_blocks.end();
102  ++i)
103  {
104  // transform the pointer to cuda::pointer before calling cuda::free
105  DerivedType::_free(i->second);
106  }
107 
108  for(typename AllocatedBlocksType::iterator i = _allocated_blocks.begin();
109  i != _allocated_blocks.end();
110  ++i)
111  {
112  // transform the pointer to cuda::pointer before calling cuda::free
113  DerivedType::_free(i->first);
114  }
115 }
116 
117 } // namespace data
118 } // namespace cheetah
119 } // namespace ska
Some limits and constants for FLDO.
Definition: Brdz.h:35
T * allocate(std::size_t num_elements)
Allocate memory.
Base class for CachingAllocator implementations.
void deallocate(T *ptr, std::size_t)
Deallocate memory.