libStatGen Software 1
Loading...
Searching...
No Matches
SamRecordPool.cpp
1/*
2 * Copyright (C) 2011 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <stdexcept>
19#include "SamRecordPool.h"
20
22 : myFreeSamRecords(),
23 myMaxAllowedRecs(-1),
24 myAllocatedRecs(0)
25{
26}
27
28
30 : myFreeSamRecords(),
31 myMaxAllowedRecs(maxNumRecs),
32 myAllocatedRecs(0)
33{
34}
35
36
38{
39 // Loop through the stack deleting the free records.
40 while (!myFreeSamRecords.empty())
41 {
42 delete(myFreeSamRecords.front());
43 myFreeSamRecords.pop();
44 }
45}
46
47
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}
79
80
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}
92
93
95{
96 myMaxAllowedRecs = maxNumRecs;
97}
void setMaxAllocatedRecs(int maxNumRecs)
Set the maximum number of records allowed to be allocated.
void releaseRecord(SamRecord *record)
If record is not NULL, adds it back to the free list.
~SamRecordPool()
Destructor.
SamRecordPool()
Constructor that sets there to be no max number of allocated records.
SamRecord * getRecord()
Get a SamRecord.
Class providing an easy to use interface to get/set/operate on the fields in a SAM/BAM record.
Definition SamRecord.h:52