source: icXML/icXML-devel/src/xercesc/framework/XMLBufferMgr.cpp @ 3157

Last change on this file since 3157 was 3157, checked in by cameron, 6 years ago

Fixes for icXML 0.9

File size: 3.7 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**
19 * $Id: XMLBufferMgr.cpp 673679 2008-07-03 13:50:10Z borisk $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26//#include <string.h>
27#include <icxercesc/framework/XMLBufferMgr.hpp>
28#include <xercesc/util/RuntimeException.hpp>
29
30XERCES_CPP_NAMESPACE_BEGIN
31
32// ---------------------------------------------------------------------------
33//  Constructors and Destructor
34// ---------------------------------------------------------------------------
35XMLBufferMgr::XMLBufferMgr(MemoryManager* const manager) :
36
37    fBufCount(32)
38    , fMemoryManager(manager)
39    , fBufList(0)
40{
41    // Allocate the buffer list and zero it out
42    fBufList = (XMLBuffer**) fMemoryManager->allocate(fBufCount * sizeof(XMLBuffer*)); // new XMLBuffer*[fBufCount];
43    for (XMLSize_t index = 0; index < fBufCount; index++)
44        fBufList[index] = 0;
45}
46
47XMLBufferMgr::~XMLBufferMgr()
48{
49    // Delete any buffers that got allocated
50    for (XMLSize_t index = 0; index < fBufCount; index++)
51        delete fBufList[index];
52
53    // And then the buffer list
54    fMemoryManager->deallocate(fBufList); //delete [] fBufList;
55}
56
57
58// ---------------------------------------------------------------------------
59//  Buffer management
60// ---------------------------------------------------------------------------
61XMLBuffer& XMLBufferMgr::bidOnBuffer()
62{
63    //
64    //  Look for a buffer that is not in use. If we hit a null entry, then
65    //  we have to add one.
66    //
67    for (XMLSize_t index = 0; index < fBufCount; index++)
68    {
69        // No more buffers available, so create one and take it
70        if (!fBufList[index])
71        {
72            fBufList[index] = new (fMemoryManager) XMLBuffer(1023, fMemoryManager);
73            fBufList[index]->setInUse(true);
74            return *fBufList[index];
75        }
76
77        //
78        //  There's one here, so see if its use. If not, mark it, reset it,
79        //  and take it
80        //
81        if (!fBufList[index]->getInUse())
82        {
83            fBufList[index]->reset();
84            fBufList[index]->setInUse(true);
85            return *(fBufList[index]);
86        }
87    }
88
89    // We did not find one, so freak out
90    ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::BufMgr_NoMoreBuffers, fMemoryManager);
91
92    // NOTE: Dummy return to make some compilers happy. Never really gets called!
93    return *fBufList[0];
94}
95
96
97void XMLBufferMgr::releaseBuffer(XMLBuffer& toRelease)
98{
99    // Look for this buffer in the list
100    for (XMLSize_t index = 0; index < fBufCount; index++)
101    {
102        if (fBufList[index] == &toRelease)
103        {
104            // Unmark it
105            toRelease.setInUse(false);
106            return;
107        }
108    }
109
110    // It was not a legal buffer
111    ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::BufMgr_BufferNotInPool, fMemoryManager);
112}
113
114XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.