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.

95 lines
2.2 KiB

  1. /*******************************************************************************
  2. * SapiArray.h *
  3. *------------*
  4. * Description:
  5. * This is the header file for SAPI internal array stuff.
  6. *
  7. * Copyright 1998-2000 Microsoft Corporation All Rights Reserved.
  8. *
  9. *******************************************************************************/
  10. #ifndef SPDebug_h
  11. #include <SPDebug.h>
  12. #endif
  13. template <class T>
  14. HRESULT CopyAndExpandArray(T ** ppArraySrc, ULONG CurSize, T ** ppArrayDest, ULONG NewSize, BOOL fZeroMem = true)
  15. {
  16. T * pNew = new T[NewSize];
  17. if (pNew && ( CurSize < NewSize ))
  18. {
  19. if (CurSize)
  20. {
  21. memcpy(pNew, *ppArraySrc, sizeof(T) * CurSize);
  22. }
  23. if (fZeroMem)
  24. {
  25. memset(pNew + CurSize, 0, sizeof(T) * (NewSize - CurSize));
  26. }
  27. *ppArrayDest = pNew;
  28. return S_OK;
  29. }
  30. else if( pNew )
  31. {
  32. if( CurSize )
  33. {
  34. memcpy( pNew, *ppArraySrc, sizeof(T) * NewSize );
  35. }
  36. else if( fZeroMem )
  37. {
  38. memset( pNew , 0, sizeof(T) * NewSize );
  39. }
  40. *ppArrayDest = pNew;
  41. return S_OK;
  42. }
  43. else
  44. {
  45. return E_OUTOFMEMORY;
  46. }
  47. }
  48. template <class T>
  49. HRESULT ReallocateArray(T ** ppArray, ULONG CurSize, ULONG NewSize, BOOL fZeroMem = true)
  50. {
  51. T * pNew = new T[NewSize];
  52. SPDBG_ASSERT(CurSize <= NewSize);
  53. if (pNew)
  54. {
  55. if (CurSize)
  56. {
  57. memcpy(pNew, *ppArray, sizeof(T) * CurSize);
  58. }
  59. if (fZeroMem)
  60. {
  61. memset(pNew + CurSize, 0, sizeof(T) * (NewSize - CurSize));
  62. }
  63. delete[] *ppArray;
  64. *ppArray = pNew;
  65. return S_OK;
  66. }
  67. else
  68. {
  69. return E_OUTOFMEMORY;
  70. }
  71. }
  72. template <class T>
  73. HRESULT AllocateArray(T ** ppArray, ULONG Size, BOOL fZeroMem = true)
  74. {
  75. SPDBG_ASSERT(*ppArray == NULL);
  76. *ppArray = new T[Size];
  77. if (*ppArray)
  78. {
  79. if (fZeroMem)
  80. {
  81. memset(*ppArray, 0, sizeof(T) * Size);
  82. }
  83. return S_OK;
  84. }
  85. else
  86. {
  87. return E_OUTOFMEMORY;
  88. }
  89. }