Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.8 KiB

  1. #if !defined(_FUSION_INC_STRINGPOOL_H_INCLUDED_)
  2. #define _FUSION_INC_STRINGPOOL_H_INCLUDED_
  3. #pragma once
  4. #include <windows.h>
  5. #include "debmacro.h"
  6. #include "fusiondeque.h"
  7. #include "fusionbuffer.h"
  8. #include "util.h"
  9. #include "sxsapi.h"
  10. //
  11. // Design notes:
  12. //
  13. // This string pool class is intended to manage an un-reference-counted
  14. // pool of strings. Adding something to the pool leaves it there until
  15. // the pool is destroyed; this makes it useful for things like syntax
  16. // trees where many of the elements will have duplicate strings, but
  17. // you would never want to actually keep one of these around for the
  18. // lifetime of the process or DLL.
  19. //
  20. class CStringPool;
  21. class CStringPoolEntry;
  22. class CStringPoolEntryClump;
  23. class CStringPoolHeap;
  24. class CStringPoolHeapSegment;
  25. class CStringPoolHeapSegment
  26. {
  27. public:
  28. CStringPoolHeapSegment() : m_cchUsed(0) { }
  29. ~CStringPoolHeapSegment() { }
  30. BOOL Initialize();
  31. BOOL Initialize(const WCHAR *StringIn, ULONG Cch, const WCHAR *&rpStringOut);
  32. BOOL TryAllocString(const WCHAR *StringIn, ULONG Cch, const WCHAR *&rpStringOut);
  33. inline static ULONG CchMax() { return 4096; }
  34. CDequeLinkage m_leLinks;
  35. WCHAR m_rgchBuffer[4096];
  36. ULONG m_cchUsed;
  37. };
  38. class CStringPoolSingletonString
  39. {
  40. public:
  41. CStringPoolSingletonString() : m_prgwch(NULL) { }
  42. ~CStringPoolSingletonString() { if (m_prgwch != NULL) { FUSION_DELETE_ARRAY(m_prgwch); m_prgwch = NULL; } }
  43. BOOL Initialize(const WCHAR *StringIn, ULONG Cch, const WCHAR *&rpStringOut);
  44. WCHAR *m_prgwch;
  45. CDequeLinkage m_leLinks;
  46. };
  47. class CStringPoolHeap
  48. {
  49. public:
  50. CStringPoolHeap() { }
  51. ~CStringPoolHeap()
  52. {
  53. CSxsPreserveLastError ple;
  54. m_dequeSegments.Clear<CStringPoolHeap>(this, CStringPoolHeap::ClearSegment);
  55. m_dequeSingletons.Clear<CStringPoolHeap>(this, CStringPoolHeap::ClearSingleton);
  56. ple.Restore();
  57. }
  58. BOOL Initialize();
  59. BOOL DupString(const WCHAR *StringIn, ULONG Cch, const WCHAR *&rpStringOut);
  60. VOID ClearSegment(CStringPoolHeapSegment *p) const { FUSION_DELETE_SINGLETON(p); }
  61. VOID ClearSingleton(CStringPoolSingletonString *p) const { FUSION_DELETE_SINGLETON(p); }
  62. CDeque<CStringPoolHeapSegment, FIELD_OFFSET(CStringPoolHeapSegment, m_leLinks)> m_dequeSegments;
  63. CDeque<CStringPoolSingletonString, FIELD_OFFSET(CStringPoolSingletonString, m_leLinks)> m_dequeSingletons;
  64. private:
  65. CStringPoolHeap(const CStringPoolHeap &r); // intentionally not implemented
  66. void operator =(const CStringPoolHeap &r); // intentionally not implemented
  67. };
  68. class CStringPoolEntry : public _SXS_XML_STRING
  69. {
  70. friend CStringPoolEntryClump;
  71. public:
  72. CStringPoolEntry()
  73. {
  74. // Initialize members in base type...
  75. this->Flags = 0;
  76. this->PseudoKey = 0;
  77. this->Length = 0;
  78. this->Buffer = NULL;
  79. }
  80. ~CStringPoolEntry() { }
  81. BOOL Initialize(const WCHAR *StringIn, ULONG CchIn, ULONG ulPsuedoKey, CStringPoolHeap &rStringPoolHeap);
  82. };
  83. class CStringPoolEntryClump
  84. {
  85. friend CStringPool;
  86. public:
  87. CStringPoolEntryClump() : m_prgEntries(NULL), m_cEntriesAllocated(0), m_cEntriesUsed(0) { }
  88. ~CStringPoolEntryClump();
  89. BOOL Initialize(ULONG cEntriesToAllocate = 32);
  90. enum FindOrAddDisposition
  91. {
  92. eInvalid,
  93. eFound,
  94. eAdded,
  95. eNoRoom
  96. };
  97. BOOL FindOrAddEntry(
  98. const WCHAR *StringIn,
  99. ULONG CchIn,
  100. ULONG ulPseudoKey,
  101. CStringPoolHeap &rStringHeap,
  102. ULONG &rulPosition,
  103. FindOrAddDisposition &rdisposition,
  104. const CStringPoolEntry *&rpEntry
  105. );
  106. ULONG EntriesAllocate() const { return m_cEntriesAllocated; }
  107. ULONG EntriesUsed() const { return m_cEntriesUsed; }
  108. BOOL FillInStringArray(ULONG nArraySize, SXS_XML_STRING *prgStrings, ULONG iCurrent, ULONG &rcWritten);
  109. //protected:
  110. CStringPoolEntry *m_prgEntries;
  111. ULONG m_cEntriesAllocated;
  112. ULONG m_cEntriesUsed;
  113. CDequeLinkage m_leEntryChain;
  114. };
  115. class CStringPool
  116. {
  117. public:
  118. CStringPool() : m_fInitialized(false), m_cEntries(0) { }
  119. ~CStringPool();
  120. BOOL Initialize();
  121. BOOL Canonicalize(
  122. const WCHAR *StringIn,
  123. ULONG CchIn,
  124. ULONG ulPseudoKey,
  125. ULONG &rulPosition,
  126. const WCHAR *&rStringOut
  127. );
  128. BOOL GetEntryCount() const { return m_cEntries; }
  129. BOOL FillInStringArray(ULONG nArraySize, SXS_XML_STRING *prgStrings, ULONG &rcWritten);
  130. protected:
  131. bool m_fInitialized;
  132. ULONG m_cEntries;
  133. VOID ClearDequeEntry(CStringPoolEntryClump *p) const { FUSION_DELETE_SINGLETON(p); }
  134. CStringPoolHeap m_StringHeap;
  135. CDeque<CStringPoolEntryClump, FIELD_OFFSET(CStringPoolEntryClump, m_leEntryChain)> m_dequeEntryClumps;
  136. private:
  137. CStringPool(const CStringPool &); // intentionally not implemented
  138. void operator =(const CStringPool &r); // intentionally not implemented
  139. };
  140. #endif