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

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

Original Xerces files with import mods for icxercesc

File size: 6.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: 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 <assert.h>
28
29XERCES_CPP_NAMESPACE_BEGIN
30
31// ---------------------------------------------------------------------------
32//  XMLStringPool: Constructors and Destructor
33// ---------------------------------------------------------------------------
34XMLStringPool::XMLStringPool(const  unsigned int  modulus,
35                             MemoryManager* const manager) :
36
37    fMemoryManager(manager)
38    , fIdMap(0)
39    , fHashTable(0)
40    , fMapCapacity(64)
41    , fCurId(1)
42{
43    // Create the hash table, passing it the modulus
44    fHashTable = new (fMemoryManager) RefHashTableOf<PoolElem>(modulus, false, fMemoryManager);
45
46    // Do an initial allocation of the id map and zero it all out
47    fIdMap = (PoolElem**) fMemoryManager->allocate
48    (
49        fMapCapacity * sizeof(PoolElem*)
50    ); //new PoolElem*[fMapCapacity];
51    memset(fIdMap, 0, sizeof(PoolElem*) * fMapCapacity);
52}
53
54XMLStringPool::~XMLStringPool()
55{
56    // delete all buckelements, since the hashtable doesn't adopt the elements anymore
57    for (unsigned int index = 1; index < fCurId; index++)
58    {
59        //fIdMap[index]->~PoolElem();                                // we have no destructor
60        fMemoryManager->deallocate((void*) fIdMap[index]->fString);  // deallocate memory
61        fMemoryManager->deallocate(fIdMap[index]);                   // deallocate memory
62    }
63    delete fHashTable;
64    fMemoryManager->deallocate(fIdMap); //delete [] fIdMap;
65}
66
67// ---------------------------------------------------------------------------
68//  XMLStringPool: Pool management methods
69// ---------------------------------------------------------------------------
70void XMLStringPool::flushAll()
71{
72    // delete all buckelements, since the hashtable doesn't adopt the elements anymore
73    for (unsigned int index = 1; index < fCurId; index++)
74    {
75        //fIdMap[index]->~PoolElem();                                // we have no destructor
76        fMemoryManager->deallocate((void*) fIdMap[index]->fString);  // deallocate memory
77        fMemoryManager->deallocate(fIdMap[index]);                   // deallocate memory
78    }
79    fCurId = 1;
80    fHashTable->removeAll();
81}
82
83// ---------------------------------------------------------------------------
84//  XMLStringPool: Private helper methods
85// ---------------------------------------------------------------------------
86unsigned int XMLStringPool::addNewEntry(const XMLCh* const newString)
87{
88    // See if we need to expand the id map
89    if (fCurId == fMapCapacity)
90    {
91        // Calculate the new capacity, create a temp new map, and zero it
92        const unsigned int newCap = (unsigned int)(fMapCapacity * 1.5);
93        PoolElem** newMap = (PoolElem**) fMemoryManager->allocate
94        (
95            newCap * sizeof(PoolElem*)
96        ); //new PoolElem*[newCap];
97        memset(newMap, 0, sizeof(PoolElem*) * newCap);
98
99        //
100        //  Copy over the old elements from the old map. They are just pointers
101        //  so we can do it all at once.
102        //
103        memcpy(newMap, fIdMap, sizeof(PoolElem*) * fMapCapacity);
104
105        // Clean up the old map and store the new info
106        fMemoryManager->deallocate(fIdMap); //delete [] fIdMap;
107        fIdMap = newMap;
108        fMapCapacity = newCap;
109    }
110
111    //
112    //  Ok, now create a new element and add it to the hash table. Then store
113    //  this new element in the id map at the current id index, then bump the
114    //  id index.
115    //
116    PoolElem* newElem = (PoolElem*) fMemoryManager->allocate(sizeof(PoolElem));
117    newElem->fId      = fCurId;
118    newElem->fString  = XMLString::replicate(newString, fMemoryManager);
119    fHashTable->put((void*)newElem->fString, newElem);
120    fIdMap[fCurId] = newElem;
121
122    // Bump the current id and return the id of the new elem we just added
123    fCurId++;
124    return newElem->fId;
125}
126
127/***
128 * Support for Serialization/De-serialization
129 ***/
130
131IMPL_XSERIALIZABLE_TOCREATE(XMLStringPool)
132
133void XMLStringPool::serialize(XSerializeEngine& serEng)
134{
135    /***
136     * Since we are pretty sure that fIdMap and fHashTable is
137     * not shared by any other object, therefore there is no owned/referenced
138     * issue. Thus we can serialize the raw data only, rather than serializing
139     * both fIdMap and fHashTable.
140     *
141     * And we can rebuild the fIdMap and fHashTable out of the raw data during
142     * deserialization.
143     *
144    ***/
145    if (serEng.isStoring())
146    {
147        serEng<<fCurId;
148        for (unsigned int index = 1; index < fCurId; index++)
149        {
150            const XMLCh* stringData = getValueForId(index);
151            serEng.writeString(stringData);
152        }
153    }
154    else
155    {
156        unsigned int mapSize;
157        serEng>>mapSize;
158        assert(1 == fCurId);  //make sure empty
159
160        for (unsigned int index = 1; index < mapSize; index++)
161        {
162            XMLCh* stringData;
163            serEng.readString(stringData);
164            addNewEntry(stringData);
165
166            //we got to deallocate this string
167            //since stringpool will duplicate this string in the PoolElem and own that copy
168            fMemoryManager->deallocate(stringData);
169        }
170    }
171}
172
173XMLStringPool::XMLStringPool(MemoryManager* const manager) :
174    fMemoryManager(manager)
175    , fIdMap(0)
176    , fHashTable(0)
177    , fMapCapacity(64)
178    , fCurId(1)
179{
180    // Create the hash table, passing it the modulus
181    fHashTable = new (fMemoryManager) RefHashTableOf<PoolElem>(109, false, fMemoryManager);
182
183    // Do an initial allocation of the id map and zero it all out
184    fIdMap = (PoolElem**) fMemoryManager->allocate
185    (
186        fMapCapacity * sizeof(PoolElem*)
187    ); //new PoolElem*[fMapCapacity];
188    memset(fIdMap, 0, sizeof(PoolElem*) * fMapCapacity);
189}
190
191XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.