source: icXML/icXML-devel/src/xercesc/util/StringPool.cpp @ 3565

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

Updates to xercesc sources

File size: 4.4 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: StringPool.cpp 471747 2006-11-06 14:31:56Z amassari $
20 */
21
22
23// ---------------------------------------------------------------------------
24//  Includes
25// ---------------------------------------------------------------------------
26#include <xercesc/util/StringPool.hpp>
27#include <icxmlc/Array.hpp>
28#include <assert.h>
29
30XERCES_CPP_NAMESPACE_BEGIN
31
32// ---------------------------------------------------------------------------
33//  XMLStringPool: Constructors and Destructor
34// ---------------------------------------------------------------------------
35XMLStringPool::XMLStringPool(const  unsigned int /* modulus */, MemoryManager* const manager)
36: fMemoryManager(manager)
37#ifndef XML_STRING_POOL_USES_INDIVIDUAL_HEAP_ALLOCATION
38, fMemoryPool(manager)
39#endif
40{
41    DEBUG_MESSAGE((size_t)(this) << ".XMLStringPool::XMLStringPool()")
42}
43
44XMLStringPool::XMLStringPool(MemoryManager* const manager)
45: fMemoryManager(manager)
46#ifndef XML_STRING_POOL_USES_INDIVIDUAL_HEAP_ALLOCATION
47, fMemoryPool(manager)
48#endif
49{
50    DEBUG_MESSAGE((size_t)(this) << ".XMLStringPool::XMLStringPool()")
51}
52
53XMLStringPool::~XMLStringPool()
54{
55    DEBUG_MESSAGE((size_t)(this) << ".XMLStringPool::~XMLStringPool()")
56    flushAll();
57}
58
59// ---------------------------------------------------------------------------
60//  XMLStringPool: Pool management methods
61// ---------------------------------------------------------------------------
62void XMLStringPool::flushAll()
63{
64    DEBUG_MESSAGE((size_t)(this) << ".XMLStringPool::flushAll()")
65    fHashMap.flush();
66    #ifndef XML_STRING_POOL_USES_INDIVIDUAL_HEAP_ALLOCATION
67    fMemoryPool.flushAll();
68    #endif
69}
70
71// ---------------------------------------------------------------------------
72//  XMLStringPool: Private helper methods
73// ---------------------------------------------------------------------------
74unsigned int XMLStringPool::addNewEntry(const XMLCh* const newString)
75{
76    const XMLSize_t length = XMLString::stringLen(newString);
77    PoolElem newElem;
78    #ifdef XML_STRING_POOL_USES_INDIVIDUAL_HEAP_ALLOCATION
79    newElem.fString = XMLString::replicate(newString, length, fMemoryManager);
80    #else
81    newElem.fString = const_cast<XMLCh*>(fMemoryPool.insert(newString, length));
82    #endif
83    newElem.fLength = length;
84    newElem.fId = fHashMap.count();
85    return fHashMap.add(newElem);
86}
87
88/***
89 * Support for Serialization/De-serialization
90 ***/
91
92IMPL_XSERIALIZABLE_TOCREATE(XMLStringPool)
93
94void XMLStringPool::serialize(XSerializeEngine& serEng)
95{
96    /***
97     * Since we are pretty sure that fIdMap and fHashTable is
98     * not shared by any other object, therefore there is no owned/referenced
99     * issue. Thus we can serialize the raw data only, rather than serializing
100     * both fIdMap and fHashTable.
101     *
102     * And we can rebuild the fIdMap and fHashTable out of the raw data during
103     * deserialization.
104     *
105    ***/
106    if (serEng.isStoring())
107    {
108        serEng << fHashMap.count();
109        for (unsigned int index = 0; index < fHashMap.count(); index++)
110        {
111            const XMLCh* stringData = getValueForId(index);
112            serEng.writeString(stringData);
113        }
114    }
115    else
116    {
117        unsigned int mapSize;
118        serEng>>mapSize;
119        assert(fHashMap.count() == 0);  //make sure empty
120
121        for (unsigned int index = 1; index < mapSize; index++)
122        {
123            XMLCh* stringData;
124            serEng.readString(stringData);
125            addNewEntry(stringData);
126
127            //we got to deallocate this string
128            //since stringpool will duplicate this string in the PoolElem and own that copy
129            fMemoryManager->deallocate(stringData);
130        }
131    }
132}
133
134XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.