Cheetah - SKA - PSS - Prototype Time Domain Search Pipeline
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
ska::cheetah::data::CachingAllocatorImplBase< DerivedType, T > Class Template Reference

Base class for CachingAllocator implementations. More...

#include <cheetah/data/detail/CachingAllocatorImplBase.h>

Inheritance diagram for ska::cheetah::data::CachingAllocatorImplBase< DerivedType, T >:
Inheritance graph
Collaboration diagram for ska::cheetah::data::CachingAllocatorImplBase< DerivedType, T >:
Collaboration graph

Public Member Functions

 CachingAllocatorImplBase ()
 Create a new instance.
 
T * allocate (std::size_t num_elements)
 Allocate memory. More...
 
void deallocate (T *ptr, std::size_t)
 Deallocate memory. More...
 

Protected Types

typedef std::multimap< std::size_t, T * > FreeBlocksType
 
typedef std::map< T *, std::size_t > AllocatedBlocksType
 

Protected Member Functions

void free_all ()
 

Protected Attributes

FreeBlocksType _free_blocks
 
AllocatedBlocksType _allocated_blocks
 
std::mutex _mutex
 

Detailed Description

template<typename DerivedType, typename T>
class ska::cheetah::data::CachingAllocatorImplBase< DerivedType, T >

Base class for CachingAllocator implementations.

Template Parameters
DerivedType{ description }
T{ description }

Definition at line 41 of file CachingAllocatorImplBase.h.

Member Function Documentation

◆ allocate()

template<typename DerivedType , typename T >
T * ska::cheetah::data::CachingAllocatorImplBase< DerivedType, T >::allocate ( std::size_t  num_elements)

Allocate memory.

Parameters
[in]num_elementsThe number elements to be allocate of type T
Returns
A pointer to the allocated memory

Definition at line 45 of file CachingAllocatorImplBase.cpp.

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 }

◆ deallocate()

template<typename DerivedType , typename T>
void ska::cheetah::data::CachingAllocatorImplBase< DerivedType, T >::deallocate ( T *  ptr,
std::size_t   
)

Deallocate memory.

Parameters
[in]ptrPointer to the memory to be deallocated
[in]nThe number of elements of type T to be deallocated

Definition at line 83 of file CachingAllocatorImplBase.cpp.

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 }

The documentation for this class was generated from the following files: