LSSTApplications  8.0.0.0+107,8.0.0.1+13,9.1+18,9.2,master-g084aeec0a4,master-g0aced2eed8+6,master-g15627eb03c,master-g28afc54ef9,master-g3391ba5ea0,master-g3d0fb8ae5f,master-g4432ae2e89+36,master-g5c3c32f3ec+17,master-g60f1e072bb+1,master-g6a3ac32d1b,master-g76a88a4307+1,master-g7bce1f4e06+57,master-g8ff4092549+31,master-g98e65bf68e,master-ga6b77976b1+53,master-gae20e2b580+3,master-gb584cd3397+53,master-gc5448b162b+1,master-gc54cf9771d,master-gc69578ece6+1,master-gcbf758c456+22,master-gcec1da163f+63,master-gcf15f11bcc,master-gd167108223,master-gf44c96c709
LSSTDataManagementBasePackage
Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT > Class Template Reference

A thread-safe memory block allocator that uses a Bitset to track which blocks (out of a fixed size pool of blocks) are in-use/free. More...

#include <ChunkManagerImpl.h>

Inheritance diagram for lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >:

Public Member Functions

 BlockAllocator (unsigned char const *const ref, std::size_t const offset)
 
std::size_t allocate ()
 
void allocate (std::size_t *const blockOffsets, int const n)
 
void free (std::size_t const *const blockOffsets, int const n)
 

Private Types

typedef Bitset
< boost::uint64_t,
TraitsT::NUM_BLOCKS > 
Allocator
 

Private Member Functions

 BOOST_STATIC_ASSERT (TraitsT::ENTRIES_PER_BLOCK_LOG2 >=9)
 

Private Attributes

MutexT _mutex
 
Allocator _allocator
 
std::size_t const _offset
 

Static Private Attributes

static std::size_t const BLOCK_SIZE
 

Detailed Description

template<typename MutexT, typename DataT, typename TraitsT = DataTraits<DataT>>
class lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >

A thread-safe memory block allocator that uses a Bitset to track which blocks (out of a fixed size pool of blocks) are in-use/free.

The allocator never returns raw pointers - instead, blocks are identified by an offset in bytes relative to the address of the allocator instance.

This scheme allows an allocator instance, the memory blocks it manages, and offsets referencing them to be stored in shared memory. Clients then map these offsets to an actual block address simply by adding the offsets to the (process-specific) block allocator address.

Definition at line 149 of file ChunkManagerImpl.h.

Member Typedef Documentation

template<typename MutexT , typename DataT , typename TraitsT = DataTraits<DataT>>
typedef Bitset<boost::uint64_t, TraitsT::NUM_BLOCKS> lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::Allocator
private

Definition at line 158 of file ChunkManagerImpl.h.

Constructor & Destructor Documentation

template<typename MutexT , typename DataT , typename TraitsT >
lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::BlockAllocator ( unsigned char const *const  reference,
std::size_t const  offset 
)

Creates a new BlockAllocator instance. The memory blocks to be tracked by the allocator are located in contiguous memory, starting offset bytes after the given reference address.

Parameters
[in]referenceThe address relative to which offset is specified.
[in]offsetThe location of the first memory block in the pool of contiguous blocks to be managed by this allocator instance, specified as an offset in bytes relative to the reference address.

Definition at line 252 of file ChunkManagerImpl.cc.

255  :
256  _mutex(),
257  _allocator(),
258  _offset(static_cast<std::size_t>((reference + offset) - reinterpret_cast<unsigned char * >(this)))
259 {
260  _allocator.reset();
261 }
void reset()
Definition: Bitset.h:123

Member Function Documentation

template<typename MutexT , typename DataT , typename TraitsT >
std::size_t lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::allocate ( )

Allocates a single memory block.

Returns
The offset (in bytes relative to the address of this allocator instance) of the newly allocated block.
Exceptions
lsst::pex::exceptions:::MemoryErrorThrown if there was no free block available.

Definition at line 274 of file ChunkManagerImpl.cc.

274  {
275  int i[1];
276  ScopedLock<MutexT> lock(_mutex);
277  if (!_allocator.set(i, 1)) {
278  throw LSST_EXCEPT(lsst::pex::exceptions::MemoryError, "no free blocks remain");
279  }
280  return _offset + i[0]*BLOCK_SIZE;
281 }
static std::size_t const BLOCK_SIZE
void set()
Definition: Bitset.h:128
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
template<typename MutexT , typename DataT , typename TraitsT >
void lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::allocate ( std::size_t *const  blockOffsets,
int const  n 
)

Allocates n memory blocks, storing their offsets in the given array.

Parameters
[out]blockOffsetsThe array in which the offsets (relative to this allocator instance) of allocated memory blocks are stored. Assumed to be of length at least n.
[in]nThe number of memory blocks to allocate.
Exceptions
lsst::pex::exceptions:::MemoryErrorThrown if there were less than n free blocks available.
lsst::pex::exceptions:::RangeErrorThrown if the number of blocks to allocate is negative or too large.

Definition at line 297 of file ChunkManagerImpl.cc.

300  {
301  if (n < 0 || n > TraitsT::MAX_BLOCKS_PER_CHUNK) {
302  throw LSST_EXCEPT(lsst::pex::exceptions::RangeError,
303  "invalid number of memory blocks in allocation request");
304  }
305  int i[TraitsT::MAX_BLOCKS_PER_CHUNK];
306 
307  ScopedLock<MutexT> lock(_mutex);
308  if (!_allocator.set(i, n)) {
309  throw LSST_EXCEPT(lsst::pex::exceptions::MemoryError,
310  "number of free blocks too small to satisfy allocation request");
311  }
312  for (int j = 0; j < n; ++j) {
313  blockOffsets[j] = _offset + i[j]*BLOCK_SIZE;
314  }
315 }
static std::size_t const BLOCK_SIZE
void set()
Definition: Bitset.h:128
#define LSST_EXCEPT(type,...)
Definition: Exception.h:46
template<typename MutexT , typename DataT , typename TraitsT = DataTraits<DataT>>
lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::BOOST_STATIC_ASSERT ( TraitsT::ENTRIES_PER_BLOCK_LOG2 >=  9)
private
template<typename MutexT , typename DataT , typename TraitsT >
void lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::free ( std::size_t const *const  blockOffsets,
int const  n 
)

Frees n memory blocks, identified by offsets stored in the given array. Never throws.

Parameters
[in]blockOffsetsThe array in which the offsets (relative to this allocator instance) of the memory blocks to free are stored. Assumed to be of length at least n.
[in]nThe number of memory blocks to free.

Definition at line 327 of file ChunkManagerImpl.cc.

330  {
331  assert(n >= 0 && n <= TraitsT::MAX_BLOCKS_PER_CHUNK &&
332  "invalid number of memory blocks in free request");
333 
334  // translate block offsets to block indexes
335  int i[TraitsT::MAX_BLOCKS_PER_CHUNK];
336  for (int j = 0; j < n; ++j) {
337  std::size_t off = blockOffsets[j] - _offset;
338  assert(off < TraitsT::NUM_BLOCKS*BLOCK_SIZE &&
339  "block was not allocated by this allocator");
340  assert(off % BLOCK_SIZE == 0 && "invalid block address");
341  i[j] = static_cast<int>(off/BLOCK_SIZE);
342  }
343 
344  // clear bit corresponding to each block to free
345  ScopedLock<MutexT> lock(_mutex);
346  _allocator.reset(i, n);
347 }
static std::size_t const BLOCK_SIZE
void reset()
Definition: Bitset.h:123

Member Data Documentation

template<typename MutexT , typename DataT , typename TraitsT = DataTraits<DataT>>
Allocator lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::_allocator
private

Definition at line 166 of file ChunkManagerImpl.h.

template<typename MutexT , typename DataT , typename TraitsT = DataTraits<DataT>>
MutexT lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::_mutex
private

Definition at line 165 of file ChunkManagerImpl.h.

template<typename MutexT , typename DataT , typename TraitsT = DataTraits<DataT>>
std::size_t const lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::_offset
private

Definition at line 167 of file ChunkManagerImpl.h.

template<typename MutexT , typename DataT , typename TraitsT = DataTraits<DataT>>
std::size_t const lsst::ap::detail::BlockAllocator< MutexT, DataT, TraitsT >::BLOCK_SIZE
staticprivate
Initial value:
=
(sizeof(DataT) + sizeof(ChunkEntryFlag)) << TraitsT::ENTRIES_PER_BLOCK_LOG2

Definition at line 162 of file ChunkManagerImpl.h.


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