BufferPtr.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 <memory.h>
  26. #include <malloc.h>
  27. template<class T, size_t MAX_CACHE_SIZE = 0>
  28. class CBufferPtrT
  29. {
  30. public:
  31. explicit CBufferPtrT(size_t size = 0, bool zero = false) {Reset(); Malloc(size, zero);}
  32. explicit CBufferPtrT(const T* pch, size_t size) {Reset(); Copy(pch, size);}
  33. CBufferPtrT(const CBufferPtrT& other) {Reset(); Copy(other);}
  34. template<size_t S> CBufferPtrT(const CBufferPtrT<T, S>& other) {Reset(); Copy(other);}
  35. ~CBufferPtrT() {Free();}
  36. T* Malloc(size_t size = 1, bool zero = false)
  37. {
  38. Free();
  39. return Alloc(size, zero, false);
  40. }
  41. T* Realloc(size_t size, bool zero = false)
  42. {
  43. return Alloc(size, zero, true);
  44. }
  45. void Free()
  46. {
  47. if(m_pch)
  48. {
  49. free(m_pch);
  50. Reset();
  51. }
  52. }
  53. template<size_t S> CBufferPtrT& Copy(const CBufferPtrT<T, S>& other)
  54. {
  55. if((void*)&other != (void*)this)
  56. Copy(other.Ptr(), other.Size());
  57. return *this;
  58. }
  59. CBufferPtrT& Copy(const T* pch, size_t size)
  60. {
  61. Malloc(size);
  62. if(m_pch)
  63. memcpy(m_pch, pch, size * sizeof(T));
  64. return *this;
  65. }
  66. template<size_t S> CBufferPtrT& Cat(const CBufferPtrT<T, S>& other)
  67. {
  68. if((void*)&other != (void*)this)
  69. Cat(other.Ptr(), other.Size());
  70. return *this;
  71. }
  72. CBufferPtrT& Cat(const T* pch, size_t size = 1)
  73. {
  74. size_t pre_size = m_size;
  75. Realloc(m_size + size);
  76. if(m_pch)
  77. memcpy(m_pch + pre_size, pch, size * sizeof(T));
  78. return *this;
  79. }
  80. template<size_t S> bool Equal(const CBufferPtrT<T, S>& other) const
  81. {
  82. if((void*)&other == (void*)this)
  83. return true;
  84. else if(m_size != other.Size())
  85. return false;
  86. else if(m_size == 0)
  87. return true;
  88. else
  89. return (memcmp(m_pch, other.Ptr(), m_size * sizeof(T)) == 0);
  90. }
  91. bool Equal(T* pch) const
  92. {
  93. if(m_pch == pch)
  94. return true;
  95. else if(!m_pch || !pch)
  96. return false;
  97. else
  98. return (memcmp(m_pch, pch, m_size * sizeof(T)) == 0);
  99. }
  100. size_t SetSize(size_t size)
  101. {
  102. if(size < 0 || size > m_capacity)
  103. size = m_capacity;
  104. return (m_size = size);
  105. }
  106. T* Ptr() {return m_pch;}
  107. const T* Ptr() const {return m_pch;}
  108. T& Get(int i) {return *(m_pch + i);}
  109. const T& Get(int i) const {return *(m_pch + i);}
  110. size_t Size() const {return m_size;}
  111. size_t Capacity() const {return m_capacity;}
  112. bool IsValid() const {return m_pch != 0;}
  113. operator T* () {return Ptr();}
  114. operator const T* () const {return Ptr();}
  115. T& operator [] (int i) {return Get(i);}
  116. const T& operator [] (int i) const {return Get(i);}
  117. bool operator == (T* pv) const {return Equal(pv);}
  118. template<size_t S> bool operator == (const CBufferPtrT<T, S>& other) {return Equal(other);}
  119. CBufferPtrT& operator = (const CBufferPtrT& other) {return Copy(other);}
  120. template<size_t S> CBufferPtrT& operator = (const CBufferPtrT<T, S>& other) {return Copy(other);}
  121. private:
  122. void Reset() {m_pch = 0; m_size = 0; m_capacity = 0;}
  123. size_t GetAllocSize(size_t size) {return MAX(size, MIN(size * 2, m_size + MAX_CACHE_SIZE));}
  124. T* Alloc(size_t size, bool zero = false, bool is_realloc = false)
  125. {
  126. if(size != m_size)
  127. {
  128. size_t rsize = GetAllocSize(size);
  129. if(size > m_capacity || rsize < m_size)
  130. {
  131. T* pch = is_realloc ?
  132. (T*)realloc(m_pch, rsize * sizeof(T)) :
  133. (T*)malloc(rsize * sizeof(T)) ;
  134. if(pch || rsize == 0)
  135. {
  136. m_pch = pch;
  137. m_size = size;
  138. m_capacity = rsize;
  139. }
  140. else
  141. {
  142. Free();
  143. throw std::bad_alloc();
  144. }
  145. }
  146. else
  147. m_size = size;
  148. }
  149. if(zero && m_pch)
  150. memset(m_pch, 0, m_size * sizeof(T));
  151. return m_pch;
  152. }
  153. private:
  154. T* m_pch;
  155. size_t m_size;
  156. size_t m_capacity;
  157. };
  158. typedef CBufferPtrT<char> CCharBufferPtr;
  159. typedef CBufferPtrT<wchar_t> CWCharBufferPtr;
  160. typedef CBufferPtrT<unsigned char> CByteBufferPtr;
  161. typedef CByteBufferPtr CBufferPtr;
  162. #ifdef _UNICODE
  163. typedef CWCharBufferPtr CTCharBufferPtr;
  164. #else
  165. typedef CCharBufferPtr CTCharBufferPtr;
  166. #endif