tc_bitmap.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #ifndef __TC_BIT_MAP_H__
  17. #define __TC_BIT_MAP_H__
  18. #include <iostream>
  19. #include <string>
  20. #include <vector>
  21. #include "util/tc_ex.h"
  22. using namespace std;
  23. namespace tars
  24. {
  25. /////////////////////////////////////////////////
  26. /**
  27. * @file tc_bitmap.h
  28. * @brief 多位bitmap类.
  29. */
  30. /////////////////////////////////////////////////
  31. /**
  32. * @brief 异常
  33. */
  34. struct TC_BitMap_Exception : public TC_Exception
  35. {
  36. TC_BitMap_Exception(const string &buffer) : TC_Exception(buffer){};
  37. ~TC_BitMap_Exception() throw(){};
  38. };
  39. /**
  40. * @brief 内存bitmap,每个整数1位,可以支持多位,即几个整数多位.
  41. *
  42. * 操作过程不加锁,如果有需要在外面调用的时候加,通常采用群锁策略.
  43. *
  44. * 注意群锁策略应该/8,然后按照尾号分群锁
  45. */
  46. class TC_BitMap
  47. {
  48. public:
  49. /**
  50. * @brief 内存的bitmap,每个整数保持1位
  51. *
  52. */
  53. class BitMap
  54. {
  55. public:
  56. static const int _magic_bits[8];
  57. #define _set_bit(n,m) (n|_magic_bits[m])
  58. #define _clear_bit(n,m) (n&(~_magic_bits[m]))
  59. #define _get_bit(n,m) (n&_magic_bits[m])
  60. /**共享内存版本*/
  61. #define BM_VERSION 1
  62. /**
  63. * @brief 根据元素个数计算需要内存的大小
  64. * @param iElementCount, 需要保存的元素个数(元素从0开始记)
  65. *
  66. * @return size_t
  67. */
  68. static size_t calcMemSize(size_t iElementCount);
  69. /**
  70. * @brief 初始化
  71. * @param pAddr 绝对地址
  72. * @param iSize 大小, 采用(calcMemSize)计算出来
  73. * @return 0: 成功, -1:内存不够
  74. */
  75. void create(void *pAddr, size_t iSize);
  76. /**
  77. * @brief 链接到内存块
  78. * @param pAddr 地址, 采用(calcMemSize)计算出来
  79. * @return 0, 成功, -1,版本不对, -2:大小不对
  80. */
  81. int connect(void *pAddr, size_t iSize);
  82. /**
  83. * @brief 是否有标识
  84. * @param i
  85. * @return int, >0:有标识, =0:无标识, <0:超过范围
  86. */
  87. int get(size_t i);
  88. /**
  89. * @brief 设置标识
  90. * @param i
  91. * @return int, >0:有标识, =0:无标识, <0:超过范围
  92. */
  93. int set(size_t i);
  94. /**
  95. * @brief 清除标识
  96. * @param i
  97. *
  98. * @return int, >0:有标识, =0:无标识, <0:超过范围
  99. */
  100. int clear(size_t i);
  101. /**
  102. * @brief 清除所有的数据
  103. *
  104. * @return int
  105. */
  106. int clear4all();
  107. /**
  108. * @brief dump到文件
  109. * @param sFile
  110. *
  111. * @return int
  112. */
  113. int dump2file(const string &sFile);
  114. /**
  115. * @brief 从文件load
  116. * @param sFile
  117. *
  118. * @return int
  119. */
  120. int load5file(const string &sFile);
  121. /**共享内存头部*/
  122. #pragma pack(1)
  123. struct tagBitMapHead
  124. {
  125. char _cVersion; /**版本, 当前版本为1*/
  126. size_t _iMemSize; /**共享内存大小*/
  127. };
  128. #pragma pack()
  129. /**
  130. * @brief 获取头部地址
  131. * @return tagBitMapHead* 共享内存头部
  132. */
  133. BitMap::tagBitMapHead *getAddr() const { return _pHead; }
  134. /**
  135. * @brief 获取内存大小
  136. * @return 内存大小
  137. */
  138. size_t getMemSize() const { return _pHead->_iMemSize; }
  139. protected:
  140. /**
  141. * 共享内存头部
  142. */
  143. tagBitMapHead *_pHead;
  144. /**
  145. * 数据块指针
  146. */
  147. unsigned char * _pData;
  148. };
  149. /**
  150. * @brief 根据元素个数计算需要内存的大小
  151. * @param iElementCount 需要保存的元素个数(元素从0开始记)
  152. * @param iBitCount 每个元素支持几位(默认1位) (位数>=1)
  153. * @return 所需内存的大小
  154. */
  155. static size_t calcMemSize(size_t iElementCount, unsigned iBitCount = 1);
  156. /**
  157. * @brief 初始化
  158. * @param pAddr 绝对地址
  159. * @param iSize 大小, 采用(calcMemSize)计算出来
  160. * @return 0: 成功, -1:内存不够
  161. */
  162. void create(void *pAddr, size_t iSize, unsigned iBitCount = 1);
  163. /**
  164. * @brief 链接到内存块
  165. * @param pAddr 地址,采用(calcMemSize)计算出来
  166. * @return 0:成功, -1:版本不对, -2:大小不对
  167. */
  168. int connect(void *pAddr, size_t iSize, unsigned iBitCount = 1);
  169. /**
  170. * @brief 是否有标识
  171. * @param i 元素值
  172. * @param iBit 第几位
  173. * @return int, >0:有标识, =0:无标识, <0:超过范围
  174. */
  175. int get(size_t i, unsigned iBit = 1);
  176. /**
  177. * @brief 设置标识
  178. * @param i 元素值
  179. * @param iBit 第几位
  180. * @return int, >0:有标识, =0:无标识, <0:超过范围
  181. */
  182. int set(size_t i, unsigned iBit = 1);
  183. /**
  184. * @brief 清除标识
  185. * @param i 元素值
  186. * @param iBit 第几位
  187. * @return int, >0:有标识, =0:无标识, <0:超过范围
  188. */
  189. int clear(size_t i, unsigned iBit = 1);
  190. /**
  191. * @brief 清除所有的标识
  192. *
  193. * @param iBit 第几位
  194. * @return int
  195. */
  196. int clear4all(unsigned iBit = (unsigned)(-1));
  197. /**
  198. * @brief dump到文件
  199. * @param sFile
  200. *
  201. * @return int
  202. */
  203. int dump2file(const string &sFile);
  204. /**
  205. * @brief 从文件load
  206. * @param sFile
  207. *
  208. * @return int
  209. */
  210. int load5file(const string &sFile);
  211. protected:
  212. vector<BitMap> _bitmaps;
  213. };
  214. }
  215. #endif