GME  13
XMLBufferMgr.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  *
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00023 // ---------------------------------------------------------------------------
00024 //  Includes
00025 // ---------------------------------------------------------------------------
00026 //#include <string.h>
00027 #include <xercesc/framework/XMLBufferMgr.hpp>
00028 #include <xercesc/util/RuntimeException.hpp>
00029 
00030 XERCES_CPP_NAMESPACE_BEGIN
00031 
00032 // ---------------------------------------------------------------------------
00033 //  Constructors and Destructor
00034 // ---------------------------------------------------------------------------
00035 XMLBufferMgr::XMLBufferMgr(MemoryManager* const manager) :
00036 
00037     fBufCount(32)
00038     , fMemoryManager(manager)
00039     , fBufList(0)
00040 {
00041     // Allocate the buffer list and zero it out
00042     fBufList = (XMLBuffer**) fMemoryManager->allocate(fBufCount * sizeof(XMLBuffer*)); // new XMLBuffer*[fBufCount];
00043     for (XMLSize_t index = 0; index < fBufCount; index++)
00044         fBufList[index] = 0;
00045 }
00046 
00047 XMLBufferMgr::~XMLBufferMgr()
00048 {
00049     // Delete any buffers that got allocated
00050     for (XMLSize_t index = 0; index < fBufCount; index++)
00051         delete fBufList[index];
00052 
00053     // And then the buffer list
00054     fMemoryManager->deallocate(fBufList); //delete [] fBufList;
00055 }
00056 
00057 
00058 // ---------------------------------------------------------------------------
00059 //  Buffer management
00060 // ---------------------------------------------------------------------------
00061 XMLBuffer& XMLBufferMgr::bidOnBuffer()
00062 {
00063     //
00064     //  Look for a buffer that is not in use. If we hit a null entry, then
00065     //  we have to add one.
00066     //
00067     for (XMLSize_t index = 0; index < fBufCount; index++)
00068     {
00069         // No more buffers available, so create one and take it
00070         if (!fBufList[index])
00071         {
00072             fBufList[index] = new (fMemoryManager) XMLBuffer(1023, fMemoryManager);
00073             fBufList[index]->setInUse(true);
00074             return *fBufList[index];
00075         }
00076 
00077         //
00078         //  There's one here, so see if its use. If not, mark it, reset it,
00079         //  and take it
00080         //
00081         if (!fBufList[index]->getInUse())
00082         {
00083             fBufList[index]->reset();
00084             fBufList[index]->setInUse(true);
00085             return *(fBufList[index]);
00086         }
00087     }
00088 
00089     // We did not find one, so freak out
00090     ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::BufMgr_NoMoreBuffers, fMemoryManager);
00091 
00092     // NOTE: Dummy return to make some compilers happy. Never really gets called!
00093     return *fBufList[0];
00094 }
00095 
00096 
00097 void XMLBufferMgr::releaseBuffer(XMLBuffer& toRelease)
00098 {
00099     // Look for this buffer in the list
00100     for (XMLSize_t index = 0; index < fBufCount; index++)
00101     {
00102         if (fBufList[index] == &toRelease)
00103         {
00104             // Unmark it
00105             toRelease.setInUse(false);
00106             return;
00107         }
00108     }
00109 
00110     // It was not a legal buffer
00111     ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::BufMgr_BufferNotInPool, fMemoryManager);
00112 }
00113 
00114 XERCES_CPP_NAMESPACE_END