Leaked source code of windows server 2003
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.

146 lines
3.1 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: strcache.cpp
  6. * Content: Class for caching strings
  7. *@@BEGIN_MSINTERNAL
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 02/04/2000 rmt Created
  12. * 02/17/2000 rmt Parameter validation work
  13. * 02/21/2000 rmt Updated to make core Unicode and remove ANSI calls
  14. *@@END_MSINTERNAL
  15. *
  16. ***************************************************************************/
  17. #include "dnaddri.h"
  18. // # of slots to grow the cache at each opportunity
  19. #define STRINGCACHE_GROW_SLOTS 10
  20. #undef DPF_MODNAME
  21. #define DPF_MODNAME "CStringCache::Initialize"
  22. void CStringCache::Initialize( void )
  23. {
  24. m_ppszStringCache = NULL;
  25. m_dwNumElements = 0;
  26. m_dwNumSlots = 0;
  27. }
  28. #undef DPF_MODNAME
  29. #define DPF_MODNAME "CStringCache::Deinitialize"
  30. void CStringCache::Deinitialize( void )
  31. {
  32. for( DWORD dwIndex = 0; dwIndex < m_dwNumElements; dwIndex++ )
  33. {
  34. DNFree(m_ppszStringCache[dwIndex]);
  35. }
  36. DNFree(m_ppszStringCache);
  37. }
  38. #undef DPF_MODNAME
  39. #define DPF_MODNAME "CStringCache::AddString"
  40. HRESULT CStringCache::AddString( const WCHAR *pszString, WCHAR * *ppszSlot )
  41. {
  42. HRESULT hr;
  43. PWSTR pszSlot;
  44. hr = GetString( pszString, &pszSlot );
  45. if( hr != DPN_OK )
  46. {
  47. DPFX(DPFPREP, 0, "Internal Error hr=0x%x", hr );
  48. return hr;
  49. }
  50. // Entry was found
  51. if( pszSlot != NULL )
  52. {
  53. *ppszSlot = pszSlot;
  54. return DPN_OK;
  55. }
  56. else
  57. {
  58. if( m_dwNumElements == m_dwNumSlots )
  59. {
  60. hr = GrowCache( m_dwNumSlots + STRINGCACHE_GROW_SLOTS );
  61. if( FAILED( hr ) )
  62. {
  63. DPFX(DPFPREP, 0, "Failed to grow string cache hr=0x%x", hr );
  64. return hr;
  65. }
  66. }
  67. m_ppszStringCache[m_dwNumElements] = (WCHAR*) DNMalloc((wcslen(pszString)+1)*sizeof(WCHAR));
  68. if( m_ppszStringCache[m_dwNumElements] == NULL )
  69. {
  70. DPFX(DPFPREP, 0, "Failed to alloc mem" );
  71. return DPNERR_OUTOFMEMORY;
  72. }
  73. wcscpy( m_ppszStringCache[m_dwNumElements], pszString );
  74. *ppszSlot = m_ppszStringCache[m_dwNumElements];
  75. m_dwNumElements++;
  76. return DPN_OK;
  77. }
  78. }
  79. #undef DPF_MODNAME
  80. #define DPF_MODNAME "CStringCache::GetString"
  81. HRESULT CStringCache::GetString( const WCHAR *pszString, WCHAR * *ppszSlot )
  82. {
  83. *ppszSlot = NULL;
  84. for( DWORD dwIndex = 0; dwIndex < m_dwNumElements; dwIndex++ )
  85. {
  86. if( wcscmp( m_ppszStringCache[dwIndex], pszString ) == 0 )
  87. {
  88. *ppszSlot = m_ppszStringCache[dwIndex];
  89. return DPN_OK;
  90. }
  91. }
  92. return DPN_OK;
  93. }
  94. #undef DPF_MODNAME
  95. #define DPF_MODNAME "CStringCache::GrowCache"
  96. HRESULT CStringCache::GrowCache( DWORD dwNewSize )
  97. {
  98. WCHAR **ppszNewCache;
  99. ppszNewCache = (WCHAR**) DNMalloc(dwNewSize * sizeof(WCHAR*));
  100. if( ppszNewCache == NULL )
  101. {
  102. DPFX(DPFPREP, 0, "Error allocating memory" );
  103. return DPNERR_OUTOFMEMORY;
  104. }
  105. memcpy( ppszNewCache, m_ppszStringCache, sizeof( WCHAR * ) * m_dwNumElements );
  106. m_dwNumSlots = dwNewSize;
  107. if( m_ppszStringCache != NULL )
  108. DNFree(m_ppszStringCache);
  109. m_ppszStringCache = ppszNewCache;
  110. return DPN_OK;
  111. }