SourceForge.net Logo
StringPool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001, 2008,
3  * DecisionSoft Limited. All rights reserved.
4  * Copyright (c) 2004, 2018 Oracle and/or its affiliates. All rights reserved.
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef __STRINGPOOL_HPP
21 #define __STRINGPOOL_HPP
22 
23 #include <xqilla/framework/XQillaExport.hpp>
25 #include <memory>
26 #include <cstring>
27 //Added so xqilla will compile under CC on Solaris
28 #include <string>
29 // Added so xqilla will compile on SunOS 10 using STLPort
30 #include <memory.h>
31 
32 class XQILLA_API StringPool
33 {
34 public:
35  StringPool(XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *mm);
36  ~StringPool();
37 
39  const XMLCh* getPooledString(const XMLCh *src);
41  const XMLCh* getPooledString(const XMLCh *src, unsigned int length);
43  const XMLCh* getPooledString(const char *src);
44 
45  unsigned int getCount() const { return _count; }
46  unsigned int getHits() const { return _hits; }
47  unsigned int getMisses() const { return _misses; }
48  unsigned int getTooBig() const { return _toobig; }
49  void dumpStatistics() const;
50 
51 private:
52  StringPool(const StringPool&);
53  StringPool &operator=(const StringPool&);
54 
55  static unsigned int hash(const XMLCh *v, unsigned int length);
56  const XMLCh *replicate(const XMLCh *v, unsigned int length) const;
57  void resize();
58 
59  class Bucket
60  {
61  public:
62  Bucket(const XMLCh *v, unsigned int l, unsigned int h, Bucket *n)
63  : value(v), length(l), hashValue(h), next(n) {}
64 
65  const XMLCh *value;
66  unsigned int length;
67  unsigned int hashValue;
68  Bucket *next;
69  };
70 
71  XERCES_CPP_NAMESPACE_QUALIFIER MemoryManager *_mm;
72  Bucket **_bucketList;
73  unsigned int _modulus;
74  unsigned int _count;
75  unsigned int _hits;
76  unsigned int _misses;
77  unsigned int _toobig;
78 };
79 
80 inline unsigned int StringPool::hash(const XMLCh *v, unsigned int length)
81 {
82  unsigned int hashVal = 0;
83  while(length) {
84  hashVal += (hashVal * 37) + (hashVal >> 24) + (unsigned int)(*v);
85  ++v;
86  --length;
87  }
88  return hashVal;
89 }
90 
91 inline const XMLCh *StringPool::replicate(const XMLCh *v, unsigned int length) const
92 {
93  unsigned int size = length * sizeof(XMLCh);
94  XMLCh *ret = (XMLCh*)_mm->allocate(size + sizeof(XMLCh));
95  memcpy(ret, v, size);
96  ret[length] = 0;
97  return ret;
98 }
99 
100 #endif
unsigned int getTooBig() const
Definition: StringPool.hpp:48
unsigned int getMisses() const
Definition: StringPool.hpp:47
virtual void * allocate(XMLSize_t size)=0
This method allocates requested memory.
Definition: StringPool.hpp:32
unsigned int getHits() const
Definition: StringPool.hpp:46
unsigned int getCount() const
Definition: StringPool.hpp:45