GGS(GenericGEANT4Simulation)Software  2.7.0
 All Data Structures Namespaces Files Functions Variables Typedefs Macros
Data Structures | Public Member Functions
GGSTClonesArrayService Class Reference

A service which manages a pool of reusable TClonesArray. More...

#include <GGSTClonesArrayService.h>

Public Member Functions

 GGSTClonesArrayService (const char *className)
 Constructor. More...
 
 ~GGSTClonesArrayService ()
 Destructor. More...
 
TClonesArray * ProvideArray (int sizeHint=0, bool clearElements=false)
 Method to obtain an array from the service. More...
 
void TakeBackArray (TClonesArray *&array)
 Gives an array back to the service. More...
 
void DeleteAll ()
 Delete all unassigned arrays in storage. More...
 
void DumpMemInfo ()
 

Detailed Description

A service which manages a pool of reusable TClonesArray.

This class takes care of managing a pool of TClonesArray. User routine can ask for TClonesArrays (possibly with a hint for desired size) and then release the array when finished. The released arrays can then be assigned to other routines which require them. The goal of this class is to prevent instantiating TClonesArrays when there are unused ones, and to assign available big arrays where they are most needed. The class owns the TClonesArray and will take care of deleting them. An array obtained with ProvideArray must never be explicitly deleted, it must instead be returned to the GGSTClonesArrayService by calling TakeBackArray.

Definition at line 30 of file GGSTClonesArrayService.h.

Constructor & Destructor Documentation

GGSTClonesArrayService::GGSTClonesArrayService ( const char *  className)

Constructor.

Parameters
classNameThe name of the class whose objects the TClonesArrays will contain.

Definition at line 15 of file GGSTClonesArrayService.cpp.

15 : _className(className) {}
GGSTClonesArrayService::~GGSTClonesArrayService ( )

Destructor.

When the service is destroyed it will destroy all the arrays it currently manages, including the assigned ones that may still be in use. It is user's responsibility to ensure that this does not happen.

TODO: Raise an exception when deleting assigned arrays?

Definition at line 19 of file GGSTClonesArrayService.cpp.

19  {
20  static const std::string routineName("GGSTClonesArrayService::~GGSTClonesArrayService");
21  if (_assignedArrays.size() != 0) {
22  COUT(WARNING) << "There are still " << _assignedArrays.size() << " currently assigned arrays for class "
23  << _className << "." << ENDL;
24  CCOUT(WARNING) << "These will not be deleted. The user routine must delete them." << ENDL;
25  for (ArrayList::iterator iArray = _assignedArrays.begin(); iArray != _assignedArrays.end(); iArray++) {
26  iArray->array = NULL; // Set the array pointer to NULL to avoid array deletion in ~ArrayDescriptor
27  }
28  }
29 }
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
#define CCOUT(level)
Smart log utility which prints no header at the beginning of the line.
Definition: GGSSmartLog.h:100

Member Function Documentation

void GGSTClonesArrayService::DeleteAll ( )

Delete all unassigned arrays in storage.

This method deletes all the arrays managed by the service which are currently not assigned.

Definition at line 111 of file GGSTClonesArrayService.cpp.

111  {
112  _availableArrays.resize(0); // The destructor of ArrayDescriptor will delete the array
113 }
TClonesArray * GGSTClonesArrayService::ProvideArray ( int  sizeHint = 0,
bool  clearElements = false 
)

Method to obtain an array from the service.

This method will return an array which can be used by the caller. The array will always have GetEntries = 0, but the service will try to provide an array whose actually allocated elements are as close as possible to sizeHint. The existing array elements will be cleared if the clear flag is set to true, i.e. the TClonesArray::Clear("C") method will be called before returning the array.

Parameters
sizeHintThe desired number of allocated elements.
clearElementsif true the elements in the TClonesArray are cleared.
Returns
pointer to the TClonesArray.

Definition at line 33 of file GGSTClonesArrayService.cpp.

33  {
34  static const std::string routineName("GGSTClonesArrayService::ProvideArray");
35 
36  // Step 1: search for available array whose size is fitting the request.
37  // Current "size fit" implementation: return the first array which has same or more elements than sizeHint
38  // (_availableArrays is ordered).
39  for (ArrayList::iterator iArray = _availableArrays.begin(); iArray != _availableArrays.end(); iArray++) {
40  if (iArray->allocSize >= sizeHint) {
41  _assignedArrays.splice(_assignedArrays.end(), _availableArrays, iArray);
42  if (clearElements) {
43  iArray->array->Clear("C");
44  } else {
45  iArray->array->Clear();
46  }
47  COUT(DEEPDEB) << "Assigned array at " << iArray->array << " for class " << _className << " with size " << sizeHint
48  << ENDL;
49  return iArray->array;
50  }
51  }
52  // Step 2: return the greatest available array if step 1 failed.
53  if (_availableArrays.size() > 0) {
54  ArrayList::iterator lastAvailable = _availableArrays.end();
55  lastAvailable--;
56  _assignedArrays.splice(_assignedArrays.end(), _availableArrays, lastAvailable);
57  if (clearElements) {
58  _assignedArrays.back().array->Clear("C");
59  } else {
60  _assignedArrays.back().array->Clear();
61  }
62  COUT(DEEPDEB) << "Assigned array at " << _assignedArrays.back().array << " for class " << _className
63  << " with size " << sizeHint << ENDL;
64  return _assignedArrays.back().array;
65  }
66  // Step 3: return a new array if step 2 failed.
67  _assignedArrays.push_back(ArrayDescriptor());
68  _assignedArrays.back().array = new TClonesArray(_className.c_str(), sizeHint);
69  _assignedArrays.back().allocSize = sizeHint;
70  COUT(DEEPDEB) << "Created array at " << _assignedArrays.back().array << " for class " << _className << " with size "
71  << sizeHint << ENDL;
72  return _assignedArrays.back().array;
73 }
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76
void GGSTClonesArrayService::TakeBackArray ( TClonesArray *&  array)

Gives an array back to the service.

This method should be called when an array obtained via ProvideArray is no longer needed. The service will clear and store it for future usage by some other applicant. If the array has not been retrieved by a call to ProvideArray, the service will adopt it and take care of its destruction.

Parameters
arrayThe array to be returned to the service (will be set to NULL to avoid accidental delete).

Definition at line 77 of file GGSTClonesArrayService.cpp.

77  {
78  static const std::string routineName("GGSTClonesArrayService::TakeBackArray");
79 
80  int arraySize = array->GetEntries();
81 
82  // Step 1: find insertion point in available arrays list
83  ArrayList::iterator iAvArray;
84  for (iAvArray = _availableArrays.begin(); iAvArray != _availableArrays.end(); iAvArray++) {
85  if (iAvArray->allocSize > arraySize) {
86  break;
87  }
88  }
89 
90  // Step 2: search the array between the currently assigned ones
91  for (ArrayList::iterator iArray = _assignedArrays.begin(); iArray != _assignedArrays.end(); iArray++) {
92  if (array == iArray->array) {
93  _availableArrays.splice(iAvArray, _assignedArrays, iArray);
94  array = NULL;
95  COUT(DEEPDEB) << "Took back array at " << iArray->array << " for class " << _className << " with size "
96  << arraySize << ENDL;
97  return;
98  }
99  }
100  // Step 2: new array, allocate a new element for it
101  ArrayList::iterator newArray = _availableArrays.insert(iAvArray, ArrayDescriptor());
102  newArray->array = array;
103  newArray->allocSize = arraySize;
104  array = NULL;
105  COUT(DEEPDEB) << "Adopted array at " << newArray->array << " for class " << _className << " with size " << arraySize
106  << ENDL;
107 }
#define ENDL
Definition: GGSSmartLog.h:105
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
Definition: GGSSmartLog.h:76

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