libStatGen Software 1
Loading...
Searching...
No Matches
SamRecordPool Class Reference

Public Member Functions

 SamRecordPool ()
 Constructor that sets there to be no max number of allocated records.
 
 SamRecordPool (int maxNumRecs)
 Constructor that sets the maximum number of allocated records.
 
 ~SamRecordPool ()
 Destructor.
 
SamRecordgetRecord ()
 Get a SamRecord.
 
void releaseRecord (SamRecord *record)
 If record is not NULL, adds it back to the free list.
 
void setMaxAllocatedRecs (int maxNumRecs)
 Set the maximum number of records allowed to be allocated.
 

Detailed Description

Definition at line 25 of file SamRecordPool.h.

Constructor & Destructor Documentation

◆ SamRecordPool() [1/2]

SamRecordPool::SamRecordPool ( )

Constructor that sets there to be no max number of allocated records.

Definition at line 21 of file SamRecordPool.cpp.

22 : myFreeSamRecords(),
23 myMaxAllowedRecs(-1),
24 myAllocatedRecs(0)
25{
26}

◆ SamRecordPool() [2/2]

SamRecordPool::SamRecordPool ( int  maxNumRecs)

Constructor that sets the maximum number of allocated records.

Parameters
maxNumRecsmaximum number of allocated records (-1 means no max)

Definition at line 29 of file SamRecordPool.cpp.

30 : myFreeSamRecords(),
31 myMaxAllowedRecs(maxNumRecs),
32 myAllocatedRecs(0)
33{
34}

◆ ~SamRecordPool()

SamRecordPool::~SamRecordPool ( )

Destructor.

Any records that were allocated without calling "releaseRecord" will not get cleaned up and the user will need to delete them.

Definition at line 37 of file SamRecordPool.cpp.

38{
39 // Loop through the stack deleting the free records.
40 while (!myFreeSamRecords.empty())
41 {
42 delete(myFreeSamRecords.front());
43 myFreeSamRecords.pop();
44 }
45}

Member Function Documentation

◆ getRecord()

SamRecord * SamRecordPool::getRecord ( )

Get a SamRecord.

If records are already allocated and free use those, if not and there are still more that are allowed to be allocated, allocate a new one. If no more records are allowed to be allocated, NULL is returned. NOTE: The user should call releaseRecord when done using the record. If the user deletes the record instead, it still counts as allocated when comparing against the maxNumRecs but cannot be reused.

Returns
pointer to a SamRecord available for use, or NULL if no more records are allowed to be allocated.

Definition at line 48 of file SamRecordPool.cpp.

49{
50 // Get new samRecord.
51 SamRecord* returnSam = NULL;
52 if(!myFreeSamRecords.empty())
53 {
54 // have free already allocated records, so get one of those.
55 returnSam = myFreeSamRecords.front();
56 myFreeSamRecords.pop();
57 }
58 else if((myMaxAllowedRecs == -1) || (myAllocatedRecs < myMaxAllowedRecs))
59 {
60 // There were no free records, but either there is no max or
61 // there is still room to allocate more.
62 returnSam = new SamRecord();
63 ++myAllocatedRecs;
64 if(returnSam == NULL)
65 {
66 // Failed allocation.
67 throw(std::runtime_error("Failed to allocate SamRecord"));
68 }
69 }
70 else
71 {
72 // There are no more free ones and we have already hit the
73 // max number allowed to be allocated, so return NULL.
74 // The user will have to release some or update the max.
75 returnSam = NULL;
76 }
77 return(returnSam);
78}
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
Definition SamRecord.h:52

◆ releaseRecord()

void SamRecordPool::releaseRecord ( SamRecord record)

If record is not NULL, adds it back to the free list.


If record is NULL, nothing is done.

Parameters
recordpointer to a record that is no longer being used and is available for reuse.

Definition at line 81 of file SamRecordPool.cpp.

82{
83 if(record == NULL)
84 {
85 // Nothing to release, so just return.
86 return;
87 }
88
89 // Release the samRecord to be reused.
90 myFreeSamRecords.push(record);
91}

Referenced by SamCoordOutput::flush().

◆ setMaxAllocatedRecs()

void SamRecordPool::setMaxAllocatedRecs ( int  maxNumRecs)

Set the maximum number of records allowed to be allocated.

If more than the new value have already been allocated, it does not deallocate any, and will continue to reuse the already allocated records, but it will not allocate any additional records.

Definition at line 94 of file SamRecordPool.cpp.

95{
96 myMaxAllowedRecs = maxNumRecs;
97}

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