1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
/*
* Copyright (C) 2011 Regents of the University of Michigan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdexcept>
#include "SamRecordPool.h"
SamRecordPool::SamRecordPool()
: myFreeSamRecords(),
myMaxAllowedRecs(-1),
myAllocatedRecs(0)
{
}
SamRecordPool::SamRecordPool(int maxNumRecs)
: myFreeSamRecords(),
myMaxAllowedRecs(maxNumRecs),
myAllocatedRecs(0)
{
}
SamRecordPool::~SamRecordPool()
{
// Loop through the stack deleting the free records.
while (!myFreeSamRecords.empty())
{
delete(myFreeSamRecords.front());
myFreeSamRecords.pop();
}
}
SamRecord* SamRecordPool::getRecord()
{
// Get new samRecord.
SamRecord* returnSam = NULL;
if(!myFreeSamRecords.empty())
{
// have free already allocated records, so get one of those.
returnSam = myFreeSamRecords.front();
myFreeSamRecords.pop();
}
else if((myMaxAllowedRecs == -1) || (myAllocatedRecs < myMaxAllowedRecs))
{
// There were no free records, but either there is no max or
// there is still room to allocate more.
returnSam = new SamRecord();
++myAllocatedRecs;
if(returnSam == NULL)
{
// Failed allocation.
throw(std::runtime_error("Failed to allocate SamRecord"));
}
}
else
{
// There are no more free ones and we have already hit the
// max number allowed to be allocated, so return NULL.
// The user will have to release some or update the max.
returnSam = NULL;
}
return(returnSam);
}
void SamRecordPool::releaseRecord(SamRecord* record)
{
if(record == NULL)
{
// Nothing to release, so just return.
return;
}
// Release the samRecord to be reused.
myFreeSamRecords.push(record);
}
void SamRecordPool::setMaxAllocatedRecs(int maxNumRecs)
{
myMaxAllowedRecs = maxNumRecs;
}
|