CMS 3D CMS Logo

List of all members | Classes | Public Types | Public Member Functions | Static Private Member Functions | Private Attributes
Json::BatchAllocator< AllocatedType, objectPerAllocation > Class Template Reference

#include <json_batchallocator.h>

Classes

struct  BatchInfo
 

Public Types

typedef AllocatedType Type
 

Public Member Functions

AllocatedType * allocate ()
 
 BatchAllocator (const BatchAllocator &)=delete
 
 BatchAllocator (unsigned int objectsPerPage=255)
 
void operator= (const BatchAllocator &)=delete
 
void release (AllocatedType *object)
 
 ~BatchAllocator ()
 

Static Private Member Functions

static BatchInfoallocateBatch (unsigned int objectsPerPage)
 

Private Attributes

BatchInfobatches_
 
BatchInfocurrentBatch_
 
AllocatedType * freeHead_
 Head of a single linked list within the allocated space of freeed object. More...
 
unsigned int objectsPerPage_
 

Detailed Description

template<typename AllocatedType, const unsigned int objectPerAllocation>
class Json::BatchAllocator< AllocatedType, objectPerAllocation >

Definition at line 24 of file json_batchallocator.h.

Member Typedef Documentation

◆ Type

template<typename AllocatedType , const unsigned int objectPerAllocation>
typedef AllocatedType Json::BatchAllocator< AllocatedType, objectPerAllocation >::Type

Definition at line 26 of file json_batchallocator.h.

Constructor & Destructor Documentation

◆ BatchAllocator() [1/2]

template<typename AllocatedType , const unsigned int objectPerAllocation>
Json::BatchAllocator< AllocatedType, objectPerAllocation >::BatchAllocator ( unsigned int  objectsPerPage = 255)
inline

Definition at line 28 of file json_batchallocator.h.

28  : freeHead_(0), objectsPerPage_(objectsPerPage) {
29  // printf( "Size: %d => %s\n", sizeof(AllocatedType), typeid(AllocatedType).name() );
30  assert(sizeof(AllocatedType) * objectPerAllocation >=
31  sizeof(AllocatedType *)); // We must be able to store a slist in the object free space.
32  assert(objectsPerPage >= 16);
33  batches_ = allocateBatch(0); // allocated a dummy page
35  }

References Json::BatchAllocator< AllocatedType, objectPerAllocation >::allocateBatch(), cms::cuda::assert(), Json::BatchAllocator< AllocatedType, objectPerAllocation >::batches_, and Json::BatchAllocator< AllocatedType, objectPerAllocation >::currentBatch_.

◆ ~BatchAllocator()

template<typename AllocatedType , const unsigned int objectPerAllocation>
Json::BatchAllocator< AllocatedType, objectPerAllocation >::~BatchAllocator ( )
inline

Definition at line 37 of file json_batchallocator.h.

37  {
38  for (BatchInfo *batch = batches_; batch;) {
39  BatchInfo *nextBatch = batch->next_;
40  free(batch);
41  batch = nextBatch;
42  }
43  }

References photonOfflineClient_cfi::batch, and Json::BatchAllocator< AllocatedType, objectPerAllocation >::batches_.

◆ BatchAllocator() [2/2]

template<typename AllocatedType , const unsigned int objectPerAllocation>
Json::BatchAllocator< AllocatedType, objectPerAllocation >::BatchAllocator ( const BatchAllocator< AllocatedType, objectPerAllocation > &  )
delete

Member Function Documentation

◆ allocate()

template<typename AllocatedType , const unsigned int objectPerAllocation>
AllocatedType* Json::BatchAllocator< AllocatedType, objectPerAllocation >::allocate ( )
inline

allocate space for an array of objectPerAllocation object.

Warning
it is the responsability of the caller to call objects constructors.

Definition at line 47 of file json_batchallocator.h.

47  {
48  if (freeHead_) // returns node from free list.
49  {
50  AllocatedType *object = freeHead_;
51  freeHead_ = *(AllocatedType **)object;
52  return object;
53  }
58 
59  if (!currentBatch_) // no free batch found, allocate a new one
60  {
62  currentBatch_->next_ = batches_; // insert at the head of the list
64  }
65  }
66  AllocatedType *allocated = currentBatch_->used_;
67  currentBatch_->used_ += objectPerAllocation;
68  return allocated;
69  }

References Json::BatchAllocator< AllocatedType, objectPerAllocation >::allocateBatch(), Json::BatchAllocator< AllocatedType, objectPerAllocation >::batches_, Json::BatchAllocator< AllocatedType, objectPerAllocation >::currentBatch_, Json::BatchAllocator< AllocatedType, objectPerAllocation >::BatchInfo::end_, Json::BatchAllocator< AllocatedType, objectPerAllocation >::freeHead_, Json::BatchAllocator< AllocatedType, objectPerAllocation >::BatchInfo::next_, resolutioncreator_cfi::object, Json::BatchAllocator< AllocatedType, objectPerAllocation >::objectsPerPage_, and Json::BatchAllocator< AllocatedType, objectPerAllocation >::BatchInfo::used_.

◆ allocateBatch()

template<typename AllocatedType , const unsigned int objectPerAllocation>
static BatchInfo* Json::BatchAllocator< AllocatedType, objectPerAllocation >::allocateBatch ( unsigned int  objectsPerPage)
inlinestaticprivate

Definition at line 91 of file json_batchallocator.h.

91  {
92  const unsigned int mallocSize = sizeof(BatchInfo) - sizeof(AllocatedType) * objectPerAllocation +
93  sizeof(AllocatedType) * objectPerAllocation * objectsPerPage;
94  BatchInfo *batch = static_cast<BatchInfo *>(malloc(mallocSize));
95  batch->next_ = 0;
96  batch->used_ = batch->buffer_;
97  batch->end_ = batch->buffer_ + objectsPerPage;
98  return batch;
99  }

References photonOfflineClient_cfi::batch.

Referenced by Json::BatchAllocator< AllocatedType, objectPerAllocation >::allocate(), and Json::BatchAllocator< AllocatedType, objectPerAllocation >::BatchAllocator().

◆ operator=()

template<typename AllocatedType , const unsigned int objectPerAllocation>
void Json::BatchAllocator< AllocatedType, objectPerAllocation >::operator= ( const BatchAllocator< AllocatedType, objectPerAllocation > &  )
delete

◆ release()

template<typename AllocatedType , const unsigned int objectPerAllocation>
void Json::BatchAllocator< AllocatedType, objectPerAllocation >::release ( AllocatedType *  object)
inline

Release the object.

Warning
it is the responsability of the caller to actually destruct the object.

Definition at line 73 of file json_batchallocator.h.

73  {
74  assert(object != 0);
75  *(AllocatedType **)object = freeHead_;
76  freeHead_ = object;
77  }

References cms::cuda::assert(), Json::BatchAllocator< AllocatedType, objectPerAllocation >::freeHead_, and resolutioncreator_cfi::object.

Member Data Documentation

◆ batches_

template<typename AllocatedType , const unsigned int objectPerAllocation>
BatchInfo* Json::BatchAllocator< AllocatedType, objectPerAllocation >::batches_
private

◆ currentBatch_

template<typename AllocatedType , const unsigned int objectPerAllocation>
BatchInfo* Json::BatchAllocator< AllocatedType, objectPerAllocation >::currentBatch_
private

◆ freeHead_

template<typename AllocatedType , const unsigned int objectPerAllocation>
AllocatedType* Json::BatchAllocator< AllocatedType, objectPerAllocation >::freeHead_
private

Head of a single linked list within the allocated space of freeed object.

Definition at line 104 of file json_batchallocator.h.

Referenced by Json::BatchAllocator< AllocatedType, objectPerAllocation >::allocate(), and Json::BatchAllocator< AllocatedType, objectPerAllocation >::release().

◆ objectsPerPage_

template<typename AllocatedType , const unsigned int objectPerAllocation>
unsigned int Json::BatchAllocator< AllocatedType, objectPerAllocation >::objectsPerPage_
private
Json::BatchAllocator::currentBatch_
BatchInfo * currentBatch_
Definition: json_batchallocator.h:102
Json::BatchAllocator::objectsPerPage_
unsigned int objectsPerPage_
Definition: json_batchallocator.h:105
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
Json::BatchAllocator::allocateBatch
static BatchInfo * allocateBatch(unsigned int objectsPerPage)
Definition: json_batchallocator.h:91
cms::cuda::assert
assert(be >=bs)
Json::BatchAllocator::batches_
BatchInfo * batches_
Definition: json_batchallocator.h:101
Json::BatchAllocator::BatchInfo::used_
AllocatedType * used_
Definition: json_batchallocator.h:86
Json::BatchAllocator::BatchInfo::end_
AllocatedType * end_
Definition: json_batchallocator.h:87
photonOfflineClient_cfi.batch
batch
Definition: photonOfflineClient_cfi.py:12
Json::BatchAllocator::freeHead_
AllocatedType * freeHead_
Head of a single linked list within the allocated space of freeed object.
Definition: json_batchallocator.h:104
Json::BatchAllocator::BatchInfo::next_
BatchInfo * next_
Definition: json_batchallocator.h:85