PrivateHeap.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Author : Bruce Liang
  5. * Website : https://github.com/ldcsaa
  6. * Project : https://github.com/ldcsaa/HP-Socket
  7. * Blog : http://www.cnblogs.com/ldcsaa
  8. * Wiki : http://www.oschina.net/p/hp-socket
  9. * QQ Group : 44636872, 75375912
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. #pragma once
  24. #include "hpsocket/GlobalDef.h"
  25. #include "Singleton.h"
  26. #include <malloc.h>
  27. #define HEAP_ZERO_MEMORY 0x08
  28. class CGlobalHeapImpl
  29. {
  30. public:
  31. PVOID Alloc(SIZE_T dwSize, DWORD dwFlags = 0)
  32. {
  33. PVOID pv = malloc(dwSize);
  34. if(!pv)
  35. throw std::bad_alloc();
  36. if(dwFlags & HEAP_ZERO_MEMORY)
  37. ZeroMemory(pv, dwSize);
  38. return pv;
  39. }
  40. PVOID ReAlloc(PVOID pvMemory, SIZE_T dwSize, DWORD dwFlags = 0)
  41. {
  42. PVOID pv = realloc(pvMemory, dwSize);
  43. if(!pv)
  44. {
  45. if(pvMemory)
  46. free(pvMemory);
  47. throw std::bad_alloc();
  48. }
  49. if(dwFlags & HEAP_ZERO_MEMORY)
  50. ZeroMemory(pv, dwSize);
  51. return pv;
  52. }
  53. BOOL Free(PVOID pvMemory, DWORD dwFlags = 0)
  54. {
  55. if(pvMemory)
  56. {
  57. free(pvMemory);
  58. return TRUE;
  59. }
  60. return FALSE;
  61. }
  62. SIZE_T Compact (DWORD dwFlags = 0) {return -1;}
  63. SIZE_T Size (PVOID pvMemory, DWORD dwFlags = 0) {return _msize(pvMemory);}
  64. BOOL IsValid() {return TRUE;}
  65. BOOL Reset() {return TRUE;}
  66. public:
  67. CGlobalHeapImpl (DWORD dwOptions = 0, SIZE_T dwInitSize = 0, SIZE_T dwMaxSize = 0) {}
  68. ~CGlobalHeapImpl() {}
  69. DECLARE_NO_COPY_CLASS(CGlobalHeapImpl)
  70. };
  71. #if !defined (_USE_CUSTOM_PRIVATE_HEAP)
  72. using CPrivateHeap = CGlobalHeapImpl;
  73. #endif
  74. template<class T> class CPrivateHeapBuffer
  75. {
  76. public:
  77. CPrivateHeapBuffer(CPrivateHeap& hpPrivate, SIZE_T dwSize = 0)
  78. : m_hpPrivate (hpPrivate)
  79. , m_pvMemory (nullptr)
  80. {
  81. ASSERT(m_hpPrivate.IsValid());
  82. Alloc(dwSize);
  83. }
  84. ~CPrivateHeapBuffer() {Free();}
  85. public:
  86. T* Alloc(SIZE_T dwSize, DWORD dwFlags = 0)
  87. {
  88. if(IsValid())
  89. Free();
  90. if(dwSize > 0)
  91. m_pvMemory = (T*)m_hpPrivate.Alloc(dwSize * sizeof(T), dwFlags);
  92. return m_pvMemory;
  93. }
  94. T* ReAlloc(SIZE_T dwSize, DWORD dwFlags = 0)
  95. {return m_pvMemory = (T*)m_hpPrivate.ReAlloc(m_pvMemory, dwSize * sizeof(T), dwFlags);}
  96. SIZE_T Size(DWORD dwFlags = 0)
  97. {return m_hpPrivate.Size(m_pvMemory, dwFlags) / sizeof(T);}
  98. BOOL Free(DWORD dwFlags = 0)
  99. {
  100. BOOL isOK = TRUE;
  101. if(IsValid())
  102. {
  103. isOK = m_hpPrivate.Free(m_pvMemory, dwFlags);
  104. m_pvMemory = nullptr;
  105. }
  106. return isOK;
  107. }
  108. BOOL IsValid() {return m_pvMemory != nullptr;}
  109. operator T* () const {return m_pvMemory;}
  110. T& operator [] (int i) const {return *(m_pvMemory + i);}
  111. private:
  112. CPrivateHeap& m_hpPrivate;
  113. T* m_pvMemory;
  114. DECLARE_NO_COPY_CLASS(CPrivateHeapBuffer)
  115. };
  116. using CPrivateHeapByteBuffer = CPrivateHeapBuffer<BYTE>;
  117. using CPrivateHeapStrBuffer = CPrivateHeapBuffer<TCHAR>;