/src/xerces-c/src/xercesc/framework/XMLBufferMgr.cpp
Line | Count | Source (jump to first uncovered 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 <xercesc/framework/XMLBufferMgr.hpp> |
28 | | #include <xercesc/util/RuntimeException.hpp> |
29 | | |
30 | | XERCES_CPP_NAMESPACE_BEGIN |
31 | | |
32 | | // --------------------------------------------------------------------------- |
33 | | // Constructors and Destructor |
34 | | // --------------------------------------------------------------------------- |
35 | | XMLBufferMgr::XMLBufferMgr(MemoryManager* const manager) : |
36 | | |
37 | 15.3k | fBufCount(32) |
38 | 15.3k | , fMemoryManager(manager) |
39 | 15.3k | , fBufList(0) |
40 | 15.3k | { |
41 | | // Allocate the buffer list and zero it out |
42 | 15.3k | fBufList = (XMLBuffer**) fMemoryManager->allocate(fBufCount * sizeof(XMLBuffer*)); // new XMLBuffer*[fBufCount]; |
43 | 507k | for (XMLSize_t index = 0; index < fBufCount; index++) |
44 | 492k | fBufList[index] = 0; |
45 | 15.3k | } |
46 | | |
47 | | XMLBufferMgr::~XMLBufferMgr() |
48 | 15.3k | { |
49 | | // Delete any buffers that got allocated |
50 | 507k | for (XMLSize_t index = 0; index < fBufCount; index++) |
51 | 492k | delete fBufList[index]; |
52 | | |
53 | | // And then the buffer list |
54 | 15.3k | fMemoryManager->deallocate(fBufList); //delete [] fBufList; |
55 | 15.3k | } |
56 | | |
57 | | |
58 | | // --------------------------------------------------------------------------- |
59 | | // Buffer management |
60 | | // --------------------------------------------------------------------------- |
61 | | XMLBuffer& XMLBufferMgr::bidOnBuffer() |
62 | 3.74M | { |
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 | 4.92M | for (XMLSize_t index = 0; index < fBufCount; index++) |
68 | 4.92M | { |
69 | | // No more buffers available, so create one and take it |
70 | 4.92M | if (!fBufList[index]) |
71 | 86.0k | { |
72 | 86.0k | fBufList[index] = new (fMemoryManager) XMLBuffer(1023, fMemoryManager); |
73 | 86.0k | fBufList[index]->setInUse(true); |
74 | 86.0k | return *fBufList[index]; |
75 | 86.0k | } |
76 | | |
77 | | // |
78 | | // There's one here, so see if its use. If not, mark it, reset it, |
79 | | // and take it |
80 | | // |
81 | 4.83M | if (!fBufList[index]->getInUse()) |
82 | 3.66M | { |
83 | 3.66M | fBufList[index]->reset(); |
84 | 3.66M | fBufList[index]->setInUse(true); |
85 | 3.66M | return *(fBufList[index]); |
86 | 3.66M | } |
87 | 4.83M | } |
88 | | |
89 | | // We did not find one, so freak out |
90 | 3.74M | ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::BufMgr_NoMoreBuffers, fMemoryManager); |
91 | | |
92 | | // NOTE: Dummy return to make some compilers happy. Never really gets called! |
93 | 0 | return *fBufList[0]; |
94 | 3.74M | } |
95 | | |
96 | | |
97 | | void XMLBufferMgr::releaseBuffer(XMLBuffer& toRelease) |
98 | 3.74M | { |
99 | | // Look for this buffer in the list |
100 | 4.92M | for (XMLSize_t index = 0; index < fBufCount; index++) |
101 | 4.92M | { |
102 | 4.92M | if (fBufList[index] == &toRelease) |
103 | 3.74M | { |
104 | | // Unmark it |
105 | 3.74M | toRelease.setInUse(false); |
106 | 3.74M | return; |
107 | 3.74M | } |
108 | 4.92M | } |
109 | | |
110 | | // It was not a legal buffer |
111 | 0 | ThrowXMLwithMemMgr(RuntimeException, XMLExcepts::BufMgr_BufferNotInPool, fMemoryManager); |
112 | 3.74M | } |
113 | | |
114 | | XERCES_CPP_NAMESPACE_END |