libStatGen Software  1
SamRecordPool.cpp
00001 /*
00002  *  Copyright (C) 2011  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #include <stdexcept>
00019 #include "SamRecordPool.h"
00020 
00021 SamRecordPool::SamRecordPool()
00022     : myFreeSamRecords(),
00023       myMaxAllowedRecs(-1),
00024       myAllocatedRecs(0)
00025 {
00026 }
00027 
00028 
00029 SamRecordPool::SamRecordPool(int maxNumRecs)
00030     : myFreeSamRecords(),
00031       myMaxAllowedRecs(maxNumRecs),
00032       myAllocatedRecs(0)
00033 {
00034 }
00035 
00036 
00037 SamRecordPool::~SamRecordPool()
00038 {
00039     // Loop through the stack deleting the free records.
00040     while (!myFreeSamRecords.empty())
00041     {
00042         delete(myFreeSamRecords.front());
00043         myFreeSamRecords.pop();
00044     }
00045 }
00046 
00047 
00048 SamRecord* SamRecordPool::getRecord()
00049 {
00050     // Get new samRecord.
00051     SamRecord* returnSam = NULL;
00052     if(!myFreeSamRecords.empty())
00053     {
00054         // have free already allocated records, so get one of those.
00055         returnSam = myFreeSamRecords.front();
00056         myFreeSamRecords.pop();
00057     }
00058     else if((myMaxAllowedRecs == -1) || (myAllocatedRecs < myMaxAllowedRecs))
00059     {
00060         // There were no free records, but either there is no max or
00061         // there is still room to allocate more.
00062         returnSam = new SamRecord();
00063         ++myAllocatedRecs;
00064         if(returnSam == NULL)
00065         {
00066             // Failed allocation.
00067             throw(std::runtime_error("Failed to allocate SamRecord"));
00068         }
00069     }
00070     else
00071     {
00072         // There are no more free ones and we have already hit the
00073         // max number allowed to be allocated, so return NULL.
00074         // The user will have to release some or update the max.
00075         returnSam = NULL;
00076     }
00077     return(returnSam);
00078 }
00079 
00080 
00081 void SamRecordPool::releaseRecord(SamRecord* record)
00082 {
00083     if(record == NULL)
00084     {
00085         // Nothing to release, so just return.
00086         return;
00087     }
00088 
00089     // Release the samRecord to be reused.
00090     myFreeSamRecords.push(record);
00091 }
00092 
00093 
00094 void SamRecordPool::setMaxAllocatedRecs(int maxNumRecs)
00095 {
00096     myMaxAllowedRecs = maxNumRecs;
00097 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends