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.

100 lines
2.8 KiB

  1. /******************************************************************************
  2. * SentItemMemory.h *
  3. *------------------*
  4. * This file defines and implements the CSentItemMemory class. This class was
  5. * written to simplify memory management in the sentence enumerator. The
  6. * const SPVSTATE member of the SPVSENTITEM struct needs to be modified in the
  7. * sentence enumerator, both during normalization and during lexicon lookup.
  8. * It was thus desireable to be able to free all of the memory which was
  9. * dynamically created in the sentence enumerator at once, without having to,
  10. * for example, figure out which pronunciations were const (specified in the
  11. * XML state) and which were dynamically created.
  12. *------------------------------------------------------------------------------
  13. * Copyright (C) 1999 Microsoft Corporation Date: 12/6/99
  14. * All Rights Reserved
  15. *
  16. *********************************************************************** AKH ***/
  17. struct MemoryChunk
  18. {
  19. BYTE* pMemory;
  20. MemoryChunk* pNext;
  21. };
  22. class CSentItemMemory
  23. {
  24. public:
  25. CSentItemMemory( )
  26. {
  27. m_pHead = NULL;
  28. m_pCurr = NULL;
  29. }
  30. ~CSentItemMemory()
  31. {
  32. MemoryChunk *pIterator = m_pHead, *pTemp = 0;
  33. while (pIterator)
  34. {
  35. pTemp = pIterator->pNext;
  36. delete [] pIterator->pMemory;
  37. delete pIterator;
  38. pIterator = pTemp;
  39. }
  40. }
  41. void* GetMemory( ULONG ulBytes, HRESULT *hr )
  42. {
  43. void *Memory = 0;
  44. if (!m_pHead)
  45. {
  46. m_pHead = new MemoryChunk;
  47. if (m_pHead)
  48. {
  49. m_pHead->pNext = NULL;
  50. m_pHead->pMemory = new BYTE[ulBytes];
  51. if (m_pHead->pMemory)
  52. {
  53. m_pCurr = m_pHead;
  54. Memory = (void*) m_pHead->pMemory;
  55. }
  56. else
  57. {
  58. *hr = E_OUTOFMEMORY;
  59. }
  60. }
  61. else
  62. {
  63. *hr = E_OUTOFMEMORY;
  64. }
  65. }
  66. else
  67. {
  68. m_pCurr->pNext = new MemoryChunk;
  69. if (m_pCurr->pNext)
  70. {
  71. m_pCurr = m_pCurr->pNext;
  72. m_pCurr->pNext = NULL;
  73. m_pCurr->pMemory = new BYTE[ulBytes];
  74. if (m_pCurr->pMemory)
  75. {
  76. Memory = (void*) m_pCurr->pMemory;
  77. }
  78. else
  79. {
  80. *hr = E_OUTOFMEMORY;
  81. }
  82. }
  83. else
  84. {
  85. *hr = E_OUTOFMEMORY;
  86. }
  87. }
  88. return Memory;
  89. }
  90. private:
  91. MemoryChunk* m_pHead;
  92. MemoryChunk* m_pCurr;
  93. };