bufferpool.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Copyright: JessMA Open Source (ldcsaa@gmail.com)
  3. *
  4. * Version : 2.3.15
  5. * Author : Bruce Liang
  6. * Website : http://www.jessma.org
  7. * Project : https://github.com/ldcsaa
  8. * Blog : http://www.cnblogs.com/ldcsaa
  9. * Wiki : http://www.oschina.net/p/hp-socket
  10. * QQ Group : 75375912
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License");
  13. * you may not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS,
  20. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. /******************************************************************************
  25. Module: BufferPool.h
  26. Notices: Copyright (c) 2013 Bruce Liang
  27. Purpose: ¼òµ¥Äڴ滺³å³Ø
  28. Desc:
  29. ******************************************************************************/
  30. #pragma once
  31. #include "Singleton.h"
  32. #include "STLHelper.h"
  33. #include "RingBuffer.h"
  34. #include "PrivateHeap.h"
  35. struct TItem
  36. {
  37. template<typename T> friend struct TSimpleList;
  38. template<typename T> friend class CNodePoolT;
  39. friend struct TItemList;
  40. friend struct TBuffer;
  41. public:
  42. inline int Cat (const BYTE* pData, int length);
  43. inline int Cat (const TItem& other);
  44. inline int Fetch (BYTE* pData, int length);
  45. inline int Peek (BYTE* pData, int length);
  46. inline int Reduce (int length);
  47. inline void Reset (int first = 0, int last = 0);
  48. BYTE* Ptr () {return begin;}
  49. const BYTE* Ptr () const {return begin;}
  50. int Size () const {return (int)(end - begin);}
  51. int Remain () const {return capacity - (int)(end - head);}
  52. int Capacity() const {return capacity;}
  53. bool IsEmpty () const {return Size() == 0;}
  54. bool IsFull () const {return Remain() == 0;}
  55. public:
  56. operator BYTE* () {return Ptr();}
  57. operator const BYTE* () const {return Ptr();}
  58. public:
  59. static TItem* Construct(CPrivateHeap& heap,
  60. int capacity = DEFAULT_ITEM_CAPACITY,
  61. BYTE* pData = nullptr,
  62. int length = 0);
  63. static void Destruct(TItem* pItem);
  64. private:
  65. TItem(CPrivateHeap& hp, int cap = DEFAULT_ITEM_CAPACITY, BYTE* pData = nullptr, int length = 0)
  66. : heap(hp), capacity(cap), begin(head), end(head), next(nullptr), last(nullptr)
  67. {
  68. if(pData != nullptr && length != 0)
  69. Cat(pData, length);
  70. }
  71. ~TItem() {}
  72. DECLARE_NO_COPY_CLASS(TItem)
  73. public:
  74. static const DWORD DEFAULT_ITEM_CAPACITY;
  75. private:
  76. CPrivateHeap& heap;
  77. private:
  78. TItem* next;
  79. TItem* last;
  80. int capacity;
  81. BYTE* head;
  82. BYTE* begin;
  83. BYTE* end;
  84. };
  85. template<class T> struct TSimpleList
  86. {
  87. public:
  88. T* PushFront(T* pItem)
  89. {
  90. if(pFront != nullptr)
  91. {
  92. pFront->last = pItem;
  93. pItem->next = pFront;
  94. }
  95. else
  96. {
  97. pItem->last = nullptr;
  98. pItem->next = nullptr;
  99. pBack = pItem;
  100. }
  101. pFront = pItem;
  102. ++size;
  103. return pItem;
  104. }
  105. T* PushBack(T* pItem)
  106. {
  107. if(pBack != nullptr)
  108. {
  109. pBack->next = pItem;
  110. pItem->last = pBack;
  111. }
  112. else
  113. {
  114. pItem->last = nullptr;
  115. pItem->next = nullptr;
  116. pFront = pItem;
  117. }
  118. pBack = pItem;
  119. ++size;
  120. return pItem;
  121. }
  122. T* PopFront()
  123. {
  124. T* pItem = pFront;
  125. if(pFront != pBack)
  126. {
  127. pFront = pFront->next;
  128. pFront->last = nullptr;
  129. }
  130. else if(pFront != nullptr)
  131. {
  132. pFront = nullptr;
  133. pBack = nullptr;
  134. }
  135. if(pItem != nullptr)
  136. {
  137. pItem->next = nullptr;
  138. pItem->last = nullptr;
  139. --size;
  140. }
  141. return pItem;
  142. }
  143. T* PopBack()
  144. {
  145. T* pItem = pBack;
  146. if(pFront != pBack)
  147. {
  148. pBack = pBack->last;
  149. pBack->next = nullptr;
  150. }
  151. else if(pBack != nullptr)
  152. {
  153. pFront = nullptr;
  154. pBack = nullptr;
  155. }
  156. if(pItem != nullptr)
  157. {
  158. pItem->next = nullptr;
  159. pItem->last = nullptr;
  160. --size;
  161. }
  162. return pItem;
  163. }
  164. TSimpleList<T>& Shift(TSimpleList<T>& other)
  165. {
  166. if(&other != this && other.size > 0)
  167. {
  168. if(size > 0)
  169. {
  170. pBack->next = other.pFront;
  171. other.pFront->last = pBack;
  172. }
  173. else
  174. {
  175. pFront = other.pFront;
  176. }
  177. pBack = other.pBack;
  178. size += other.size;
  179. other.Reset();
  180. }
  181. return *this;
  182. }
  183. void Clear()
  184. {
  185. if(size > 0)
  186. {
  187. T* pItem;
  188. while((pItem = PopFront()) != nullptr)
  189. T::Destruct(pItem);
  190. }
  191. }
  192. T* Front () const {return pFront;}
  193. T* Back () const {return pBack;}
  194. int Size () const {return size;}
  195. bool IsEmpty () const {return size == 0;}
  196. public:
  197. TSimpleList() {Reset();}
  198. ~TSimpleList() {Clear();}
  199. DECLARE_NO_COPY_CLASS(TSimpleList<T>)
  200. private:
  201. void Reset()
  202. {
  203. pFront = nullptr;
  204. pBack = nullptr;
  205. size = 0;
  206. }
  207. private:
  208. int size;
  209. T* pFront;
  210. T* pBack;
  211. };
  212. template<class T> class CNodePoolT
  213. {
  214. public:
  215. void PutFreeItem(T* pItem)
  216. {
  217. ASSERT(pItem != nullptr);
  218. if(!m_lsFreeItem.TryPut(pItem))
  219. T::Destruct(pItem);
  220. }
  221. void PutFreeItem(TSimpleList<T>& lsItem)
  222. {
  223. if(lsItem.IsEmpty())
  224. return;
  225. T* pItem;
  226. while((pItem = lsItem.PopFront()) != nullptr)
  227. PutFreeItem(pItem);
  228. }
  229. T* PickFreeItem()
  230. {
  231. T* pItem = nullptr;
  232. if(m_lsFreeItem.TryGet(&pItem))
  233. pItem->Reset();
  234. else
  235. pItem = T::Construct(m_heap, m_dwItemCapacity);
  236. return pItem;
  237. }
  238. inline void Prepare()
  239. {
  240. m_lsFreeItem.Reset(m_dwPoolHold);
  241. }
  242. inline void Clear()
  243. {
  244. T* pItem = nullptr;
  245. while(m_lsFreeItem.TryGet(&pItem))
  246. T::Destruct(pItem);
  247. VERIFY(m_lsFreeItem.IsEmpty());
  248. m_lsFreeItem.Reset();
  249. m_heap.Reset();
  250. }
  251. public:
  252. void SetItemCapacity(DWORD dwItemCapacity) {m_dwItemCapacity = dwItemCapacity;}
  253. void SetPoolSize (DWORD dwPoolSize) {m_dwPoolSize = dwPoolSize;}
  254. void SetPoolHold (DWORD dwPoolHold) {m_dwPoolHold = dwPoolHold;}
  255. DWORD GetItemCapacity () {return m_dwItemCapacity;}
  256. DWORD GetPoolSize () {return m_dwPoolSize;}
  257. DWORD GetPoolHold () {return m_dwPoolHold;}
  258. public:
  259. CNodePoolT( DWORD dwPoolSize = DEFAULT_POOL_SIZE,
  260. DWORD dwPoolHold = DEFAULT_POOL_HOLD,
  261. DWORD dwItemCapacity = DEFAULT_ITEM_CAPACITY)
  262. : m_dwPoolSize(dwPoolSize)
  263. , m_dwPoolHold(dwPoolHold)
  264. , m_dwItemCapacity(dwItemCapacity)
  265. {
  266. }
  267. ~CNodePoolT() {Clear();}
  268. DECLARE_NO_COPY_CLASS(CNodePoolT)
  269. public:
  270. static const DWORD DEFAULT_ITEM_CAPACITY;
  271. static const DWORD DEFAULT_POOL_SIZE;
  272. static const DWORD DEFAULT_POOL_HOLD;
  273. private:
  274. CPrivateHeap m_heap;
  275. DWORD m_dwItemCapacity;
  276. DWORD m_dwPoolSize;
  277. DWORD m_dwPoolHold;
  278. CRingPool<T> m_lsFreeItem;
  279. };
  280. template<class T> const DWORD CNodePoolT<T>::DEFAULT_ITEM_CAPACITY = TItem::DEFAULT_ITEM_CAPACITY;
  281. template<class T> const DWORD CNodePoolT<T>::DEFAULT_POOL_SIZE = 300;
  282. template<class T> const DWORD CNodePoolT<T>::DEFAULT_POOL_HOLD = 1200;
  283. typedef CNodePoolT<TItem> CItemPool;
  284. struct TItemList : public TSimpleList<TItem>
  285. {
  286. public:
  287. int Cat (const BYTE* pData, int length);
  288. int Cat (const TItem* pItem);
  289. int Cat (const TItemList& other);
  290. int Fetch (BYTE* pData, int length);
  291. int Peek (BYTE* pData, int length);
  292. int Reduce (int length);
  293. void Release();
  294. public:
  295. TItemList(CItemPool& pool) : itPool(pool)
  296. {
  297. }
  298. private:
  299. CItemPool& itPool;
  300. };
  301. struct TItemListEx : public TItemList
  302. {
  303. public:
  304. TItem* PushFront(TItem* pItem)
  305. {
  306. length += pItem->Size();
  307. return __super::PushFront(pItem);
  308. }
  309. TItem* PushBack(TItem* pItem)
  310. {
  311. length += pItem->Size();
  312. return __super::PushBack(pItem);
  313. }
  314. TItem* PopFront()
  315. {
  316. TItem* pItem = __super::PopFront();
  317. if(pItem != nullptr)
  318. length -= pItem->Size();
  319. return pItem;
  320. }
  321. TItem* PopBack()
  322. {
  323. TItem* pItem = __super::PopBack();
  324. if(pItem != nullptr)
  325. length -= pItem->Size();
  326. return pItem;
  327. }
  328. TItemListEx& Shift(TItemListEx& other)
  329. {
  330. if(&other != this && other.length > 0)
  331. {
  332. length += other.length;
  333. other.length = 0;
  334. __super::Shift(other);
  335. }
  336. return *this;
  337. }
  338. void Clear()
  339. {
  340. __super::Clear();
  341. length = 0;
  342. }
  343. void Release()
  344. {
  345. __super::Release();
  346. length = 0;
  347. }
  348. public:
  349. int Cat(const BYTE* pData, int length)
  350. {
  351. int cat = __super::Cat(pData, length);
  352. this->length += cat;
  353. return cat;
  354. }
  355. int Cat(const TItem* pItem)
  356. {
  357. int cat = __super::Cat(pItem->Ptr(), pItem->Size());
  358. this->length += cat;
  359. return cat;
  360. }
  361. int Cat(const TItemList& other)
  362. {
  363. int cat = __super::Cat(other);
  364. this->length += cat;
  365. return cat;
  366. }
  367. int Fetch(BYTE* pData, int length)
  368. {
  369. int fetch = __super::Fetch(pData, length);
  370. this->length -= fetch;
  371. return fetch;
  372. }
  373. int Reduce(int length)
  374. {
  375. int reduce = __super::Reduce(length);
  376. this->length -= reduce;
  377. return reduce;
  378. }
  379. int Length() const {return length;}
  380. public:
  381. TItemListEx(CItemPool& pool) : TItemList(pool), length(0)
  382. {
  383. }
  384. ~TItemListEx()
  385. {
  386. ASSERT(length >= 0);
  387. }
  388. DECLARE_NO_COPY_CLASS(TItemListEx)
  389. private:
  390. int length;
  391. };
  392. struct TItemPtr
  393. {
  394. public:
  395. TItem* Reset(TItem* pItem = nullptr)
  396. {
  397. if(m_pItem != nullptr)
  398. itPool.PutFreeItem(m_pItem);
  399. m_pItem = pItem;
  400. return m_pItem;
  401. }
  402. TItem* Attach(TItem* pItem)
  403. {
  404. return Reset(pItem);
  405. }
  406. TItem* Detach()
  407. {
  408. TItem* pItem = m_pItem;
  409. m_pItem = nullptr;
  410. return pItem;
  411. }
  412. bool IsValid () {return m_pItem != nullptr;}
  413. TItem* operator -> () {return m_pItem;}
  414. TItem* operator = (TItem* pItem) {return Reset(pItem);}
  415. operator TItem* () {return m_pItem;}
  416. TItem* Ptr () {return m_pItem;}
  417. const TItem* Ptr () const {return m_pItem;}
  418. operator const TItem* () const {return m_pItem;}
  419. public:
  420. TItemPtr(CItemPool& pool, TItem* pItem = nullptr)
  421. : itPool(pool), m_pItem(pItem)
  422. {
  423. }
  424. ~TItemPtr()
  425. {
  426. Reset();
  427. }
  428. DECLARE_NO_COPY_CLASS(TItemPtr)
  429. private:
  430. CItemPool& itPool;
  431. TItem* m_pItem;
  432. };
  433. struct TBuffer
  434. {
  435. template<typename T> friend struct TSimpleList;
  436. friend class CBufferPool;
  437. public:
  438. static TBuffer* Construct(CBufferPool& pool, ULONG_PTR dwID);
  439. static void Destruct(TBuffer* pBuffer);
  440. public:
  441. int Cat (const BYTE* pData, int len);
  442. int Cat (const TItem* pItem);
  443. int Cat (const TItemList& other);
  444. int Fetch (BYTE* pData, int length);
  445. int Peek (BYTE* pData, int length);
  446. int Reduce (int len);
  447. public:
  448. CCriSec& CriSec () {return cs;}
  449. TItemList& ItemList() {return items;}
  450. ULONG_PTR ID () const {return id;}
  451. int Length () const {return length;}
  452. bool IsValid () const {return id != 0;}
  453. private:
  454. int IncreaseLength (int len) {return (length += len);}
  455. int DecreaseLength (int len) {return (length -= len);}
  456. inline void Reset ();
  457. private:
  458. TBuffer(CPrivateHeap& hp, CItemPool& itPool, ULONG_PTR dwID = 0)
  459. : heap(hp), items(itPool), id(dwID), length(0)
  460. {
  461. }
  462. ~TBuffer() {}
  463. DECLARE_NO_COPY_CLASS(TBuffer)
  464. private:
  465. CPrivateHeap& heap;
  466. private:
  467. ULONG_PTR id;
  468. int length;
  469. DWORD freeTime;
  470. private:
  471. TBuffer* next;
  472. TBuffer* last;
  473. CCriSec cs;
  474. TItemList items;
  475. };
  476. class CBufferPool
  477. {
  478. typedef CRingPool<TBuffer> TBufferList;
  479. typedef CCASQueue<TBuffer> TBufferQueue;
  480. typedef CRingCache<TBuffer, ULONG_PTR, true> TBufferCache;
  481. public:
  482. void PutFreeBuffer (ULONG_PTR dwID);
  483. TBuffer* PutCacheBuffer (ULONG_PTR dwID);
  484. TBuffer* FindCacheBuffer (ULONG_PTR dwID);
  485. TBuffer* PickFreeBuffer (ULONG_PTR dwID);
  486. void PutFreeBuffer (TBuffer* pBuffer);
  487. void Prepare ();
  488. void Clear ();
  489. private:
  490. void ReleaseGCBuffer (BOOL bForce = FALSE);
  491. public:
  492. void SetItemCapacity (DWORD dwItemCapacity) {m_itPool.SetItemCapacity(dwItemCapacity);}
  493. void SetItemPoolSize (DWORD dwItemPoolSize) {m_itPool.SetPoolSize(dwItemPoolSize);}
  494. void SetItemPoolHold (DWORD dwItemPoolHold) {m_itPool.SetPoolHold(dwItemPoolHold);}
  495. void SetMaxCacheSize (DWORD dwMaxCacheSize) {m_dwMaxCacheSize = dwMaxCacheSize;}
  496. void SetBufferLockTime (DWORD dwBufferLockTime) {m_dwBufferLockTime = dwBufferLockTime;}
  497. void SetBufferPoolSize (DWORD dwBufferPoolSize) {m_dwBufferPoolSize = dwBufferPoolSize;}
  498. void SetBufferPoolHold (DWORD dwBufferPoolHold) {m_dwBufferPoolHold = dwBufferPoolHold;}
  499. DWORD GetItemCapacity () {return m_itPool.GetItemCapacity();}
  500. DWORD GetItemPoolSize () {return m_itPool.GetPoolSize();}
  501. DWORD GetItemPoolHold () {return m_itPool.GetPoolHold();}
  502. DWORD GetMaxCacheSize () {return m_dwMaxCacheSize;}
  503. DWORD GetBufferLockTime () {return m_dwBufferLockTime;}
  504. DWORD GetBufferPoolSize () {return m_dwBufferPoolSize;}
  505. DWORD GetBufferPoolHold () {return m_dwBufferPoolHold;}
  506. TBuffer* operator [] (ULONG_PTR dwID) {return FindCacheBuffer(dwID);}
  507. public:
  508. CBufferPool(DWORD dwPoolSize = DEFAULT_BUFFER_POOL_SIZE,
  509. DWORD dwPoolHold = DEFAULT_BUFFER_POOL_HOLD,
  510. DWORD dwLockTime = DEFAULT_BUFFER_LOCK_TIME,
  511. DWORD dwMaxCacheSize = DEFAULT_MAX_CACHE_SIZE)
  512. : m_dwBufferPoolSize(dwPoolSize)
  513. , m_dwBufferPoolHold(dwPoolHold)
  514. , m_dwBufferLockTime(dwLockTime)
  515. , m_dwMaxCacheSize(dwMaxCacheSize)
  516. {
  517. }
  518. ~CBufferPool() {Clear();}
  519. DECLARE_NO_COPY_CLASS(CBufferPool)
  520. public:
  521. CPrivateHeap& GetPrivateHeap() {return m_heap;}
  522. CItemPool& GetItemPool() {return m_itPool;}
  523. public:
  524. static const DWORD DEFAULT_MAX_CACHE_SIZE;
  525. static const DWORD DEFAULT_ITEM_CAPACITY;
  526. static const DWORD DEFAULT_ITEM_POOL_SIZE;
  527. static const DWORD DEFAULT_ITEM_POOL_HOLD;
  528. static const DWORD DEFAULT_BUFFER_LOCK_TIME;
  529. static const DWORD DEFAULT_BUFFER_POOL_SIZE;
  530. static const DWORD DEFAULT_BUFFER_POOL_HOLD;
  531. private:
  532. DWORD m_dwMaxCacheSize;
  533. DWORD m_dwBufferLockTime;
  534. DWORD m_dwBufferPoolSize;
  535. DWORD m_dwBufferPoolHold;
  536. CPrivateHeap m_heap;
  537. CItemPool m_itPool;
  538. TBufferCache m_bfCache;
  539. TBufferList m_lsFreeBuffer;
  540. TBufferQueue m_lsGCBuffer;
  541. };