/monitor/lib/tce/src/tce_shm_bitmap.h

https://github.com/Tencent/MSEC · C Header · 655 lines · 478 code · 147 blank · 30 comment · 35 complexity · f34470e25745a2b2debab3363bffdeab MD5 · raw file

  1. /**
  2. * Tencent is pleased to support the open source community by making MSEC available.
  3. *
  4. * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the GNU General Public License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License. You may
  8. * obtain a copy of the License at
  9. *
  10. * https://opensource.org/licenses/GPL-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software distributed under the
  13. * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  14. * either express or implied. See the License for the specific language governing permissions
  15. * and limitations under the License.
  16. */
  17. #ifndef __TCE_SHM_BITMAP_H__
  18. #define __TCE_SHM_BITMAP_H__
  19. #include "tce_def.h"
  20. #include "tce_shm.h"
  21. namespace tce{
  22. namespace shm{
  23. template<size_t _bit>
  24. class CBitmap{};
  25. //0-1
  26. __TCE_TEMPLATE_NULL class CBitmap<1>
  27. {
  28. typedef size_t size_type;
  29. struct SHead{
  30. char szReserve[1024];
  31. };
  32. typedef SHead head_type;
  33. public:
  34. CBitmap()
  35. :m_bInit(false)
  36. {}
  37. ~CBitmap(){}
  38. bool init(const int iShmKey, const size_t nBitmapSize, const bool bReadOnly=false){
  39. if ( m_bInit )
  40. return true;
  41. m_nShmSize = (nBitmapSize+7)/8 + sizeof(SHead);
  42. m_nMaxIndex = nBitmapSize;
  43. if ( !m_oShm.Init( iShmKey, m_nShmSize ) )
  44. {
  45. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  46. return false;
  47. }
  48. //¸ù¾ÝÊÇ·ñÖ»¶Á·½Ê½°ó¶¨¹²ÏíÄÚ´æ
  49. if ( !m_oShm.Attach(bReadOnly) )
  50. {
  51. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  52. return false;
  53. }
  54. m_pHead = (head_type*)reinterpret_cast<char*>(m_oShm.GetShmBuf());
  55. m_pDataPtr = (char*)(reinterpret_cast<char*>(m_oShm.GetShmBuf())+sizeof(head_type));
  56. m_bInit = true;
  57. return true;
  58. }
  59. bool get(const size_t nIndex) const {
  60. if ( nIndex > m_nMaxIndex )
  61. {
  62. return false;
  63. }
  64. char cFlag;
  65. cFlag = *(m_pDataPtr+nIndex/ 8) >> (nIndex % 8);
  66. cFlag = cFlag & 0x1;
  67. return (cFlag ==1 ? true:false);
  68. }
  69. bool set(const size_t nIndex, const bool bVal){
  70. int iIndexBitPos;
  71. static unsigned char caUinFlag[] = {0xfe, 0xfd, 0xfb, 0xf7,0xef,0xdf,0xbf,0x7f};
  72. char cFlag = bVal ? 1 : 0;
  73. if ( nIndex > m_nMaxIndex )
  74. {
  75. return false;
  76. }
  77. iIndexBitPos = nIndex % 8;
  78. cFlag = cFlag << (iIndexBitPos);
  79. *(m_pDataPtr+nIndex/8) &= caUinFlag[iIndexBitPos]; //ÇåλΪ0
  80. *(m_pDataPtr+nIndex/8) |= cFlag;//
  81. return true;
  82. }
  83. void clear() { memset(m_pDataPtr, 0, m_nShmSize-sizeof(SHead)); }
  84. const char* data() const { return (char*)m_pDataPtr; }
  85. size_t size() const { return m_nMaxIndex; }
  86. protected:
  87. private:
  88. SHead* m_pHead;
  89. char* m_pDataPtr;
  90. size_t m_nMaxIndex;
  91. tce::CShm m_oShm;
  92. char m_szErrMsg[1024];
  93. bool m_bInit;
  94. size_t m_nShmSize;
  95. };
  96. //0-3
  97. __TCE_TEMPLATE_NULL class CBitmap<2>
  98. {
  99. typedef size_t size_type;
  100. struct SHead{
  101. char szReserve[1024];
  102. };
  103. typedef SHead head_type;
  104. public:
  105. CBitmap(){}
  106. ~CBitmap(){}
  107. bool init(const int iShmKey, const size_t nBitmapSize, const bool bReadOnly=false){
  108. if ( m_bInit )
  109. return true;
  110. m_nShmSize = ((nBitmapSize+7)/8)*2 + sizeof(SHead);
  111. m_nMaxIndex = nBitmapSize;
  112. if ( !m_oShm.Init( iShmKey, m_nShmSize ) )
  113. {
  114. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  115. return false;
  116. }
  117. //¸ù¾ÝÊÇ·ñÖ»¶Á·½Ê½°ó¶¨¹²ÏíÄÚ´æ
  118. if ( !m_oShm.Attach(bReadOnly) )
  119. {
  120. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  121. return false;
  122. }
  123. m_pHead = (head_type*)reinterpret_cast<char*>(m_oShm.GetShmBuf());
  124. m_pDataPtr = (char*)(reinterpret_cast<char*>(m_oShm.GetShmBuf())+sizeof(head_type));
  125. m_bInit = true;
  126. return true;
  127. }
  128. int get(const size_t nIndex) const {
  129. if ( nIndex > m_nMaxIndex )
  130. {
  131. return false;
  132. }
  133. unsigned char ucFlag;
  134. ucFlag = *(m_pDataPtr+nIndex/ 4) >> (nIndex%4*2);
  135. ucFlag = ucFlag & 0x3;
  136. return ucFlag;
  137. }
  138. bool set(const size_t nIndex, const unsigned char ucVal){
  139. int iIndexBitPos;
  140. static unsigned char caUinFlag[] = {0xfc, 0xf3, 0xcf, 0x3f};
  141. uint32_t ucFlag = ucVal;
  142. if ( nIndex > m_nMaxIndex )
  143. {
  144. return false;
  145. }
  146. iIndexBitPos = nIndex%4;
  147. ucFlag = ucFlag << (iIndexBitPos*2);
  148. *(m_pDataPtr+nIndex/4) &= caUinFlag[iIndexBitPos]; //ÇåλΪ0
  149. *(m_pDataPtr+nIndex/4) |= ucFlag;//
  150. return true;
  151. }
  152. void clear() { memset(m_pDataPtr, 0, m_nShmSize-sizeof(SHead)); }
  153. const char* data() const { return (char*)m_pDataPtr; }
  154. size_t size() const { return m_nMaxIndex; }
  155. protected:
  156. private:
  157. SHead* m_pHead;
  158. char* m_pDataPtr;
  159. size_t m_nMaxIndex;
  160. tce::CShm m_oShm;
  161. char m_szErrMsg[1024];
  162. bool m_bInit;
  163. size_t m_nShmSize;
  164. };
  165. //0-7
  166. __TCE_TEMPLATE_NULL class CBitmap<3>
  167. {
  168. typedef size_t size_type;
  169. struct SHead{
  170. char szReserve[1024];
  171. };
  172. typedef SHead head_type;
  173. public:
  174. CBitmap(){}
  175. ~CBitmap(){}
  176. bool init(const int iShmKey, const size_t nBitmapSize, const bool bReadOnly=false){
  177. if ( m_bInit )
  178. return true;
  179. m_nShmSize = ((nBitmapSize+7)/8)*3 + sizeof(SHead);
  180. m_nMaxIndex = nBitmapSize;
  181. if ( !m_oShm.Init( iShmKey, m_nShmSize ) )
  182. {
  183. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  184. return false;
  185. }
  186. //¸ù¾ÝÊÇ·ñÖ»¶Á·½Ê½°ó¶¨¹²ÏíÄÚ´æ
  187. if ( !m_oShm.Attach(bReadOnly) )
  188. {
  189. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  190. return false;
  191. }
  192. m_pHead = (head_type*)reinterpret_cast<char*>(m_oShm.GetShmBuf());
  193. m_pDataPtr = (char*)(reinterpret_cast<char*>(m_oShm.GetShmBuf())+sizeof(head_type));
  194. m_bInit = true;
  195. return true;
  196. }
  197. int get(const size_t nIndex) const {
  198. if ( nIndex > m_nMaxIndex )
  199. {
  200. return false;
  201. }
  202. uint32_t dwTmpValue = *(uint32_t*)(m_pDataPtr+(nIndex/8)*3) >> (nIndex%8*3 );
  203. dwTmpValue = dwTmpValue & 0x7;
  204. return dwTmpValue;
  205. }
  206. bool set(const size_t nIndex, const unsigned char ucVal){
  207. int iIndexBitPos;
  208. static uint32_t caUinFlag[] = {0xFFFFFFF8, 0xFFFFFFC7, 0xFFFFFE3F, 0xFFFFF1FF, 0xFFFF8FFF, 0xFFFC7FFF, 0xFFE3FFFF, 0xFF1FFFFF};
  209. uint32_t dwFlag = ucVal;
  210. if ( nIndex > m_nMaxIndex )
  211. {
  212. return false;
  213. }
  214. iIndexBitPos = nIndex%8;
  215. dwFlag = dwFlag << (iIndexBitPos*3);
  216. *(uint32_t*)(m_pDataPtr+(nIndex/8)*3) &= caUinFlag[iIndexBitPos]; //ÇåλΪ0
  217. *(uint32_t*)(m_pDataPtr+(nIndex/8)*3) |= dwFlag;//
  218. return true;
  219. }
  220. void clear() { memset(m_pDataPtr, 0, m_nShmSize-sizeof(SHead)); }
  221. const char* data() const { return (char*)m_pDataPtr; }
  222. size_t size() const { return m_nMaxIndex; }
  223. protected:
  224. private:
  225. SHead* m_pHead;
  226. char* m_pDataPtr;
  227. size_t m_nMaxIndex;
  228. tce::CShm m_oShm;
  229. char m_szErrMsg[1024];
  230. bool m_bInit;
  231. size_t m_nShmSize;
  232. };
  233. //0-15
  234. __TCE_TEMPLATE_NULL class CBitmap<4>
  235. {
  236. typedef size_t size_type;
  237. struct SHead{
  238. char szReserve[1024];
  239. };
  240. typedef SHead head_type;
  241. public:
  242. CBitmap(){}
  243. ~CBitmap(){}
  244. bool init(const int iShmKey, const size_t nBitmapSize, const bool bReadOnly=false){
  245. if ( m_bInit )
  246. return true;
  247. m_nShmSize = ((nBitmapSize+7)/8)*4 + sizeof(SHead);
  248. m_nMaxIndex = nBitmapSize;
  249. if ( !m_oShm.Init( iShmKey, m_nShmSize ) )
  250. {
  251. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  252. return false;
  253. }
  254. //¸ù¾ÝÊÇ·ñÖ»¶Á·½Ê½°ó¶¨¹²ÏíÄÚ´æ
  255. if ( !m_oShm.Attach(bReadOnly) )
  256. {
  257. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  258. return false;
  259. }
  260. m_pHead = (head_type*)reinterpret_cast<char*>(m_oShm.GetShmBuf());
  261. m_pDataPtr = (char*)(reinterpret_cast<char*>(m_oShm.GetShmBuf())+sizeof(head_type));
  262. m_bInit = true;
  263. return true;
  264. }
  265. int get(const uint32_t nIndex) const {
  266. if ( nIndex > m_nMaxIndex )
  267. {
  268. return false;
  269. }
  270. unsigned char ucFlag;
  271. ucFlag = *(m_pDataPtr+nIndex/ 2) >> (nIndex%2*4);
  272. ucFlag = ucFlag & 0x0F;
  273. return ucFlag;
  274. }
  275. bool set(const uint32_t nIndex, const unsigned char ucVal){
  276. int iIndexBitPos;
  277. static unsigned char caUinFlag[] = {0xf0, 0x0f};
  278. unsigned char ucFlag = ucVal;
  279. if ( nIndex > m_nMaxIndex )
  280. {
  281. return false;
  282. }
  283. iIndexBitPos = nIndex%2;
  284. ucFlag = ucFlag << (iIndexBitPos*4);
  285. *(m_pDataPtr+nIndex/2) &= caUinFlag[iIndexBitPos]; //ÇåλΪ0
  286. *(m_pDataPtr+nIndex/2) |= ucFlag;//
  287. return true;
  288. }
  289. void clear() { memset(m_pDataPtr, 0, m_nShmSize-sizeof(SHead)); }
  290. const char* data() const { return (char*)m_pDataPtr; }
  291. size_t size() const { return m_nMaxIndex; }
  292. protected:
  293. private:
  294. SHead* m_pHead;
  295. char* m_pDataPtr;
  296. size_t m_nMaxIndex;
  297. tce::CShm m_oShm;
  298. char m_szErrMsg[1024];
  299. bool m_bInit;
  300. size_t m_nShmSize;
  301. };
  302. //0-31
  303. __TCE_TEMPLATE_NULL class CBitmap<5>
  304. {
  305. typedef size_t size_type;
  306. struct SHead{
  307. char szReserve[1024];
  308. };
  309. typedef SHead head_type;
  310. public:
  311. CBitmap(){}
  312. ~CBitmap(){}
  313. bool init(const int iShmKey, const size_t nBitmapSize, const bool bReadOnly=false){
  314. if ( m_bInit )
  315. return true;
  316. m_nShmSize = ((nBitmapSize+7)/8)*5 + sizeof(SHead);
  317. m_nMaxIndex = nBitmapSize;
  318. if ( !m_oShm.Init( iShmKey, m_nShmSize ) )
  319. {
  320. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  321. return false;
  322. }
  323. //¸ù¾ÝÊÇ·ñÖ»¶Á·½Ê½°ó¶¨¹²ÏíÄÚ´æ
  324. if ( !m_oShm.Attach(bReadOnly) )
  325. {
  326. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  327. return false;
  328. }
  329. m_pHead = (head_type*)reinterpret_cast<char*>(m_oShm.GetShmBuf());
  330. m_pDataPtr = (char*)(reinterpret_cast<char*>(m_oShm.GetShmBuf())+sizeof(head_type));
  331. m_bInit = true;
  332. return true;
  333. }
  334. int get(const size_t nIndex) const {
  335. if ( nIndex > m_nMaxIndex )
  336. {
  337. return false;
  338. }
  339. uint64_t ui64TmpValue= *(int64_t*)(m_pDataPtr+(nIndex/8)*5) >> (nIndex%8*5 );
  340. ui64TmpValue = ui64TmpValue & 0x1F;
  341. return (int)ui64TmpValue;
  342. }
  343. bool set(const size_t nIndex, const unsigned char ucVal){
  344. int iIndexBitPos;
  345. static uint64_t caUinFlag[] = {0xFFFFFFFFFFFFFFE0LL, 0xFFFFFFFFFFFFFC1FLL, 0xFFFFFFFFFFFF83FFLL, 0xFFFFFFFFFFF07FFFLL, 0xFFFFFFFFFE0FFFFFLL, 0xFFFFFFFFC1FFFFFFLL, 0xFFFFFFF83FFFFFFFLL, 0xFFFFFF07FFFFFFFFLL};
  346. uint64_t ui64Flag = ucVal;
  347. if ( nIndex > m_nMaxIndex )
  348. {
  349. return false;
  350. }
  351. iIndexBitPos = nIndex%8;
  352. ui64Flag = ui64Flag << (iIndexBitPos*5);
  353. *(uint64_t*)(m_pDataPtr+(nIndex/8)*5) &= caUinFlag[iIndexBitPos]; //ÇåλΪ0
  354. *(uint64_t*)(m_pDataPtr+(nIndex/8)*5) |= ui64Flag;//
  355. return true;
  356. }
  357. void clear() { memset(m_pDataPtr, 0, m_nShmSize-sizeof(SHead)); }
  358. const char* data() const { return (char*)m_pDataPtr; }
  359. size_t size() const { return m_nMaxIndex; }
  360. protected:
  361. private:
  362. SHead* m_pHead;
  363. char* m_pDataPtr;
  364. size_t m_nMaxIndex;
  365. tce::CShm m_oShm;
  366. char m_szErrMsg[1024];
  367. bool m_bInit;
  368. size_t m_nShmSize;
  369. };
  370. //0-63
  371. __TCE_TEMPLATE_NULL class CBitmap<6>
  372. {
  373. typedef size_t size_type;
  374. struct SHead{
  375. char szReserve[1024];
  376. };
  377. typedef SHead head_type;
  378. public:
  379. CBitmap(){}
  380. ~CBitmap(){}
  381. bool init(const int iShmKey, const size_t nBitmapSize, const bool bReadOnly=false){
  382. if ( m_bInit )
  383. return true;
  384. m_nShmSize = ((nBitmapSize+7)/8)*6 + sizeof(SHead);
  385. m_nMaxIndex = nBitmapSize;
  386. if ( !m_oShm.Init( iShmKey, m_nShmSize ) )
  387. {
  388. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  389. return false;
  390. }
  391. //¸ù¾ÝÊÇ·ñÖ»¶Á·½Ê½°ó¶¨¹²ÏíÄÚ´æ
  392. if ( !m_oShm.Attach(bReadOnly) )
  393. {
  394. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  395. return false;
  396. }
  397. m_pHead = (head_type*)reinterpret_cast<char*>(m_oShm.GetShmBuf());
  398. m_pDataPtr = (char*)(reinterpret_cast<char*>(m_oShm.GetShmBuf())+sizeof(head_type));
  399. m_bInit = true;
  400. return true;
  401. }
  402. int get(const size_t nIndex) const {
  403. if ( nIndex > m_nMaxIndex )
  404. {
  405. return false;
  406. }
  407. uint32_t dwTmpValue = *(uint32_t*)(m_pDataPtr+(nIndex/4)*3) >> (nIndex%4*6 );
  408. dwTmpValue = dwTmpValue & 0x3F;
  409. return dwTmpValue;
  410. }
  411. bool set(const size_t nIndex, const unsigned char ucVal){
  412. int iIndexBitPos;
  413. static uint32_t caUinFlag[] = {0xFFFFFFC0, 0xFFFFF03F, 0xFFFC0FFF, 0xFF03FFFF};
  414. uint32_t dwFlag = ucVal;
  415. if ( nIndex > m_nMaxIndex )
  416. {
  417. return false;
  418. }
  419. iIndexBitPos = nIndex%4;
  420. dwFlag = dwFlag << (iIndexBitPos*6);
  421. *(uint32_t*)(m_pDataPtr+(nIndex/4)*3) &= caUinFlag[iIndexBitPos]; //ÇåλΪ0
  422. *(uint32_t*)(m_pDataPtr+(nIndex/4)*3) |= dwFlag;//
  423. return true;
  424. }
  425. void clear() { memset(m_pDataPtr, 0, m_nShmSize-sizeof(SHead)); }
  426. const char* data() const { return (char*)m_pDataPtr; }
  427. size_t size() const { return m_nMaxIndex; }
  428. protected:
  429. private:
  430. SHead* m_pHead;
  431. char* m_pDataPtr;
  432. size_t m_nMaxIndex;
  433. tce::CShm m_oShm;
  434. char m_szErrMsg[1024];
  435. bool m_bInit;
  436. size_t m_nShmSize;
  437. };
  438. __TCE_TEMPLATE_NULL class CBitmap<8>
  439. {
  440. typedef size_t size_type;
  441. struct SHead{
  442. char szReserve[1024];
  443. };
  444. typedef SHead head_type;
  445. public:
  446. CBitmap(){}
  447. ~CBitmap(){}
  448. bool init(const int iShmKey, const size_t nBitmapSize, const bool bReadOnly=false){
  449. if ( m_bInit )
  450. return true;
  451. m_nShmSize = ((nBitmapSize+7)/8)*8 + sizeof(SHead);
  452. m_nMaxIndex = nBitmapSize;
  453. if ( !m_oShm.Init( iShmKey, m_nShmSize ) )
  454. {
  455. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  456. return false;
  457. }
  458. //¸ù¾ÝÊÇ·ñÖ»¶Á·½Ê½°ó¶¨¹²ÏíÄÚ´æ
  459. if ( !m_oShm.Attach(bReadOnly) )
  460. {
  461. xsnprintf(m_szErrMsg,sizeof(m_szErrMsg),"Array Init Failed:%s", m_oShm.GetErrMsg());
  462. return false;
  463. }
  464. m_pHead = (head_type*)reinterpret_cast<char*>(m_oShm.GetShmBuf());
  465. m_pDataPtr = (unsigned char*)(reinterpret_cast<char*>(m_oShm.GetShmBuf())+sizeof(head_type));
  466. m_bInit = true;
  467. return true;
  468. }
  469. int get(const size_t nIndex) const {
  470. if ( nIndex > m_nMaxIndex )
  471. {
  472. return false;
  473. }
  474. return *(m_pDataPtr+nIndex);
  475. }
  476. bool set(const size_t nIndex, const unsigned char ucVal){
  477. if ( nIndex > m_nMaxIndex )
  478. {
  479. return false;
  480. }
  481. *(m_pDataPtr+nIndex) = ucVal;
  482. return true;
  483. }
  484. void clear() { memset(m_pDataPtr, 0, m_nShmSize-sizeof(SHead)); }
  485. const char* data() const { return (char*)m_pDataPtr; }
  486. size_t size() const { return m_nMaxIndex; }
  487. protected:
  488. private:
  489. SHead* m_pHead;
  490. unsigned char* m_pDataPtr;
  491. size_t m_nMaxIndex;
  492. tce::CShm m_oShm;
  493. char m_szErrMsg[1024];
  494. bool m_bInit;
  495. size_t m_nShmSize;
  496. };
  497. //1bitµÄbitmap
  498. typedef CBitmap<1> BITMAP; //1bitµÄbitmap
  499. typedef CBitmap<1> BITMAP1;
  500. typedef CBitmap<2> BITMAP2; //2bitµÄbitmap
  501. typedef CBitmap<3> BITMAP3; //3bitµÄbitmap
  502. typedef CBitmap<4> BITMAP4; //4bitµÄbitmap
  503. typedef CBitmap<5> BITMAP5; //5bitµÄbitmap
  504. typedef CBitmap<6> BITMAP6; //6bitµÄbitmap
  505. typedef CBitmap<8> BITMAP8; //8bitµÄbitmap
  506. };
  507. };
  508. #endif