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 (unsigned int objectsPerPage=255)
 
 BatchAllocator (const BatchAllocator &)=delete
 
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.

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

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  }
assert(be >=bs)
static BatchInfo * allocateBatch(unsigned int objectsPerPage)
AllocatedType * freeHead_
Head of a single linked list within the allocated space of freeed object.

◆ ~BatchAllocator()

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

Definition at line 37 of file json_batchallocator.h.

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

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

◆ 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.

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_.

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  }
static BatchInfo * allocateBatch(unsigned int objectsPerPage)
AllocatedType * freeHead_
Head of a single linked list within the allocated space of freeed object.

◆ 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.

References photonOfflineClient_cfi::batch.

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

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  }

◆ 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.

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

73  {
74  assert(object != 0);
75  *(AllocatedType **)object = freeHead_;
76  freeHead_ = object;
77  }
assert(be >=bs)
AllocatedType * freeHead_
Head of a single linked list within the allocated space of freeed 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