DStringPool.cpp
上传用户:zhuqijet
上传日期:2013-06-25
资源大小:10074k
文件大小:7k
源码类别:

词法分析

开发平台:

Visual C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999-2002 The Apache Software Foundation.  All rights
  5.  * reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  *
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in
  16.  *    the documentation and/or other materials provided with the
  17.  *    distribution.
  18.  *
  19.  * 3. The end-user documentation included with the redistribution,
  20.  *    if any, must include the following acknowledgment:
  21.  *       "This product includes software developed by the
  22.  *        Apache Software Foundation (http://www.apache.org/)."
  23.  *    Alternately, this acknowledgment may appear in the software itself,
  24.  *    if and wherever such third-party acknowledgments normally appear.
  25.  *
  26.  * 4. The names "Xerces" and "Apache Software Foundation" must
  27.  *    not be used to endorse or promote products derived from this
  28.  *    software without prior written permission. For written
  29.  *    permission, please contact apache@apache.org.
  30.  *
  31.  * 5. Products derived from this software may not be called "Apache",
  32.  *    nor may "Apache" appear in their name, without prior written
  33.  *    permission of the Apache Software Foundation.
  34.  *
  35.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38.  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39.  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42.  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44.  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45.  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46.  * SUCH DAMAGE.
  47.  * ====================================================================
  48.  *
  49.  * This software consists of voluntary contributions made by many
  50.  * individuals on behalf of the Apache Software Foundation, and was
  51.  * originally based on software copyright (c) 1999, International
  52.  * Business Machines, Inc., http://www.ibm.com .  For more information
  53.  * on the Apache Software Foundation, please see
  54.  * <http://www.apache.org/>.
  55.  */
  56. /*
  57.  * $Id: DStringPool.cpp,v 1.4 2003/05/22 02:26:50 knoaman Exp $
  58.  */
  59. //
  60. // file DStringPool.cpp
  61. //
  62. #include "DStringPool.hpp"
  63. #include <xercesc/util/XMLRegisterCleanup.hpp>
  64. #include <xercesc/util/XMLString.hpp>
  65. #include <xercesc/util/PlatformUtils.hpp>
  66. XERCES_CPP_NAMESPACE_BEGIN
  67. //
  68. //  DStringPoolEntry - one of these structs is allocated for each
  69. //                      DOMString in the pool.  Each slot in the
  70. //                      hash table array itself is a pointer to the head
  71. //                      of a singly-linked list of these structs.
  72. //
  73. struct DStringPoolEntry : public XMemory
  74. {
  75.     DStringPoolEntry    *fNext;
  76.     DOMString           fString;
  77. };
  78. DStringPool::DStringPool(int hashTableSize,
  79.                          MemoryManager* const manager)
  80. {
  81.     fHashTableSize = hashTableSize;
  82.     fHashTable = (DStringPoolEntry**) manager->allocate
  83.     (
  84.         hashTableSize * sizeof(DStringPoolEntry*)
  85.     );//new DStringPoolEntry *[hashTableSize];
  86.     fMemoryManager = manager;
  87.     for (int i=0; i<fHashTableSize; i++)
  88.         fHashTable[i] = 0;
  89. };
  90. //  Destructor.   Iterate through the pool, deleting each of the
  91. //                DSTringPoolEntry structs, then delete the hash
  92. //                array itself.
  93. //
  94. DStringPool::~DStringPool()
  95. {
  96.     for (int slot=0; slot<fHashTableSize; slot++)
  97.     {
  98.         DStringPoolEntry    *spe;
  99.         DStringPoolEntry    *nextSPE;
  100.         for (spe=fHashTable[slot]; spe != 0; spe = nextSPE )
  101.         {
  102.             // spe->string = 0;
  103.             nextSPE = spe->fNext;
  104.             delete spe;    // Note that this will invoke the destructor
  105.                            //   on spe->fString.
  106.         }
  107.     }
  108.     fMemoryManager->deallocate(fHashTable);//delete [] fHashTable;
  109.     fHashTable = 0;
  110. };
  111. const DOMString &DStringPool::getPooledString(const XMLCh *in)
  112. {
  113.     DStringPoolEntry    **pspe;
  114.     DStringPoolEntry    *spe;
  115.     int    inHash     = XMLString::hash(in, fHashTableSize);
  116.     pspe = &fHashTable[inHash];
  117.     while (*pspe != 0)
  118.     {
  119.         if ((*pspe)->fString.equals(in))
  120.             return (*pspe)->fString;
  121.         pspe = &((*pspe)->fNext);
  122.     }
  123.     *pspe = spe = new (fMemoryManager) DStringPoolEntry;
  124.     spe->fNext = 0;
  125.     spe->fString = DOMString(in);
  126.     return spe->fString;
  127. };
  128. const DOMString &DStringPool::getPooledString(const DOMString &in)
  129. {
  130.     DStringPoolEntry    **pspe;
  131.     DStringPoolEntry    *spe;
  132.     const XMLCh *inCharData = in.rawBuffer();
  133.     int          inLength   = in.length();
  134.     int          inHash     = XMLString::hashN(inCharData, inLength, fHashTableSize);
  135.     pspe = &fHashTable[inHash];
  136.     while (*pspe != 0)
  137.     {
  138.         if ((*pspe)->fString.equals(in))
  139.             return (*pspe)->fString;
  140.         pspe = &((*pspe)->fNext);
  141.     }
  142.     *pspe = spe = new (fMemoryManager) DStringPoolEntry;
  143.     spe->fNext = 0;
  144.     spe->fString = DOMString(in);
  145.     return spe->fString;
  146. };
  147. //
  148. //  getLiteralString
  149. //
  150. //     This is a static function that is somewhat separate from the rest
  151. //      of the string pool.  It is used to manage the one-time creation of
  152. //      static strings that are reused freqently within the DOM implementation.
  153. //      This is primarily things like the default names for the various
  154. //      node types ("#text" and the like).
  155. //
  156. const DOMString &DStringPool::getStaticString(const char *in
  157.                                             , DOMString **loc
  158.                                             , XMLRegisterCleanup::XMLCleanupFn fn
  159.                                             , XMLRegisterCleanup &clnObj)
  160. {
  161.     if (*loc == 0)
  162.     {
  163.         DOMString *t = new DOMString(in);   // This is one of the very few
  164.                                             //   places that a DOMString variable
  165.                                             //   is heap allocated.  Normal usage
  166.                                             //   is to create local instances and
  167.                                             //   pass them around by value.
  168.         if (XMLPlatformUtils::compareAndSwap((void **)loc, t, 0) != 0)
  169.             delete t;
  170.         else
  171.         {
  172.             // Register this string for deletion.  Doing each string individually
  173.             //   may be a little heavyweight, but will work for the time being
  174.             //   for arranging the deletion of eveything on Termination of XML.
  175.             clnObj.registerCleanup(fn);
  176.         }
  177.     }
  178.     return **loc;
  179. }
  180. XERCES_CPP_NAMESPACE_END