tc_bitmap.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #include "util/tc_bitmap.h"
  17. #include "util/tc_common.h"
  18. #include <cassert>
  19. #include <string.h>
  20. #include <iostream>
  21. namespace tars
  22. {
  23. const int TC_BitMap::BitMap::_magic_bits[8]={0x80,0x40,0x20,0x10,0x8,0x4,0x2,0x1};
  24. size_t TC_BitMap::BitMap::calcMemSize(size_t iElementCount)
  25. {
  26. assert(iElementCount > 0);
  27. iElementCount--;
  28. size_t iMemSize = iElementCount/8+1;
  29. iMemSize += sizeof(tagBitMapHead);
  30. return iMemSize;
  31. }
  32. void TC_BitMap::BitMap::create(void *pAddr, size_t iSize)
  33. {
  34. memset((char*)pAddr, 0, iSize);
  35. _pHead = static_cast<tagBitMapHead*>(pAddr);
  36. _pHead->_cVersion = BM_VERSION;
  37. _pHead->_iMemSize = iSize;
  38. _pData = (unsigned char*)pAddr + sizeof(tagBitMapHead);
  39. }
  40. int TC_BitMap::BitMap::connect(void *pAddr, size_t iSize)
  41. {
  42. _pHead = static_cast<tagBitMapHead*>(pAddr);
  43. if(_pHead->_cVersion != BM_VERSION)
  44. {
  45. return -1;
  46. }
  47. if(iSize != _pHead->_iMemSize)
  48. {
  49. return -2;
  50. }
  51. _pData = (unsigned char*)pAddr + sizeof(tagBitMapHead);
  52. return 0;
  53. }
  54. int TC_BitMap::BitMap::get(size_t i)
  55. {
  56. if(i/8 >= (_pHead->_iMemSize-sizeof(tagBitMapHead)))
  57. {
  58. return -1;
  59. }
  60. unsigned char* p =_pData + i/8;
  61. return _get_bit(*p, i%8)>0?1:0;
  62. }
  63. int TC_BitMap::BitMap::set(size_t i)
  64. {
  65. if(i/8 >= (_pHead->_iMemSize-sizeof(tagBitMapHead)))
  66. {
  67. return -1;
  68. }
  69. unsigned char* p=(unsigned char*)_pData + i/8;
  70. *p = _set_bit(*p, i%8);
  71. return (int)(*p)>0?1:0;
  72. }
  73. int TC_BitMap::BitMap::clear(size_t i)
  74. {
  75. if(i/8 >= (_pHead->_iMemSize-sizeof(tagBitMapHead)))
  76. {
  77. return -1;
  78. }
  79. unsigned char* p = (unsigned char*)_pData + i/8;
  80. *p = _clear_bit(*p, i%8);
  81. return (int)(*p)>0?1:0;
  82. }
  83. int TC_BitMap::BitMap::clear4all()
  84. {
  85. memset(_pData, 0, _pHead->_iMemSize-sizeof(tagBitMapHead));
  86. return 0;
  87. }
  88. int TC_BitMap::BitMap::dump2file(const string &sFile)
  89. {
  90. FILE *fp = fopen(sFile.c_str(), "wb");
  91. if(fp == NULL)
  92. {
  93. return -1;
  94. }
  95. size_t ret = fwrite((void*)_pHead, 1, _pHead->_iMemSize, fp);
  96. fclose(fp);
  97. if(ret == _pHead->_iMemSize)
  98. {
  99. return 0;
  100. }
  101. return -1;
  102. }
  103. int TC_BitMap::BitMap::load5file(const string &sFile)
  104. {
  105. FILE *fp = fopen(sFile.c_str(), "rb");
  106. if(fp == NULL)
  107. {
  108. return -1;
  109. }
  110. fseek(fp, 0L, SEEK_END);
  111. size_t fs = ftell(fp);
  112. if(fs != _pHead->_iMemSize)
  113. {
  114. fclose(fp);
  115. return -2;
  116. }
  117. fseek(fp, 0L, SEEK_SET);
  118. size_t iSize = 1024*1024*10;
  119. size_t iLen = 0;
  120. char *pBuffer = new char[iSize];
  121. while(true)
  122. {
  123. size_t ret = fread(pBuffer, 1, iSize, fp);
  124. if(ret == 0)
  125. {
  126. break;
  127. }
  128. //检查版本
  129. if(iLen == 0)
  130. {
  131. tagBitMapHead *tmp = (tagBitMapHead*)pBuffer;
  132. if(tmp->_cVersion != BM_VERSION)
  133. {
  134. fclose(fp);
  135. delete[] pBuffer;
  136. return -3;
  137. }
  138. if(tmp->_iMemSize != _pHead->_iMemSize)
  139. {
  140. fclose(fp);
  141. delete[] pBuffer;
  142. return -2;
  143. }
  144. }
  145. memcpy((char*)_pHead + iLen, pBuffer, ret);
  146. iLen += ret;
  147. }
  148. fclose(fp);
  149. delete[] pBuffer;
  150. if(iLen != _pHead->_iMemSize)
  151. {
  152. return -2;
  153. }
  154. return 0;
  155. }
  156. ////////////////////////////////////////////////////////////////////////////
  157. size_t TC_BitMap::calcMemSize(size_t iElementCount, unsigned iBitCount)
  158. {
  159. size_t n = BitMap::calcMemSize(iElementCount);
  160. if(n * iBitCount < n)
  161. {
  162. throw TC_BitMap_Exception("[TC_BitMap::calcMemSize] memory to much error");
  163. }
  164. return n * iBitCount;
  165. }
  166. void TC_BitMap::create(void *pAddr, size_t iSize, unsigned iBitCount)
  167. {
  168. assert(iBitCount != 0);
  169. assert(iSize % iBitCount == 0);
  170. BitMap bitmap;
  171. size_t n = iSize/iBitCount;
  172. for(unsigned i = 0; i < iBitCount; i++)
  173. {
  174. bitmap.create((char*)pAddr + i*n, n);
  175. _bitmaps.push_back(bitmap);
  176. }
  177. }
  178. int TC_BitMap::connect(void *pAddr, size_t iSize, unsigned iBitCount)
  179. {
  180. assert(iBitCount != 0);
  181. assert(iSize % iBitCount == 0);
  182. BitMap bitmap;
  183. size_t n = iSize/iBitCount;
  184. for(unsigned i = 0; i < iBitCount; i++)
  185. {
  186. int ret = bitmap.connect((char*)pAddr + i*n, n);
  187. if(ret != 0)
  188. {
  189. return ret;
  190. }
  191. _bitmaps.push_back(bitmap);
  192. }
  193. return 0;
  194. }
  195. int TC_BitMap::get(size_t i, unsigned iBit)
  196. {
  197. assert(iBit != 0);
  198. if(iBit > _bitmaps.size())
  199. {
  200. throw TC_BitMap_Exception("[TC_BitMap::get] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
  201. }
  202. return _bitmaps[iBit-1].get(i);
  203. }
  204. int TC_BitMap::set(size_t i, unsigned iBit)
  205. {
  206. assert(iBit != 0);
  207. if(iBit > _bitmaps.size())
  208. {
  209. throw TC_BitMap_Exception("[TC_BitMap::set] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
  210. }
  211. return _bitmaps[iBit-1].set(i);
  212. }
  213. int TC_BitMap::clear(size_t i, unsigned iBit)
  214. {
  215. assert(iBit != 0);
  216. if(iBit > _bitmaps.size())
  217. {
  218. throw TC_BitMap_Exception("[TC_BitMap::clear] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
  219. }
  220. return _bitmaps[iBit-1].clear(i);
  221. }
  222. int TC_BitMap::clear4all(unsigned iBit)
  223. {
  224. assert(iBit != 0);
  225. if (iBit != (unsigned)(-1) && iBit > _bitmaps.size())
  226. {
  227. throw TC_BitMap_Exception("[TC_BitMap::clear4all] bit beyond range:"+TC_Common::tostr(iBit)+">"+TC_Common::tostr(_bitmaps.size()));
  228. }
  229. for (vector<BitMap>::size_type i = 0; i < _bitmaps.size(); i++)
  230. {
  231. if (iBit == (unsigned)(-1) || iBit == i + 1)
  232. {
  233. _bitmaps[i].clear4all();
  234. }
  235. }
  236. return 0;
  237. }
  238. int TC_BitMap::dump2file(const string &sFile)
  239. {
  240. FILE *fp = fopen(sFile.c_str(), "wb");
  241. if(fp == NULL)
  242. {
  243. return -1;
  244. }
  245. for(unsigned i = 0; i < _bitmaps.size(); i++)
  246. {
  247. size_t ret = fwrite((void*)_bitmaps[i].getAddr(), 1, _bitmaps[i].getMemSize(), fp);
  248. if(ret != _bitmaps[i].getMemSize())
  249. {
  250. fclose(fp);
  251. return -1;
  252. }
  253. }
  254. fclose(fp);
  255. return 0;
  256. }
  257. int TC_BitMap::load5file(const string &sFile)
  258. {
  259. FILE *fp = fopen(sFile.c_str(), "rb");
  260. if(fp == NULL)
  261. {
  262. return -1;
  263. }
  264. //总内存大小
  265. size_t iAllSize = 0;
  266. for(unsigned i = 0; i < _bitmaps.size(); i++)
  267. {
  268. iAllSize += _bitmaps[i].getMemSize();
  269. }
  270. fseek(fp, 0L, SEEK_END);
  271. size_t fs = ftell(fp);
  272. if(fs != iAllSize)
  273. {
  274. fclose(fp);
  275. return -2;
  276. }
  277. fseek(fp, 0L, SEEK_SET);
  278. size_t iSize = 1024*1024*10;
  279. size_t iLen = 0;
  280. char *pBuffer = new char[iSize];
  281. while(true)
  282. {
  283. int ret = fread(pBuffer, 1, iSize, fp);
  284. if(ret == 0)
  285. {
  286. break;
  287. }
  288. //检查版本
  289. if(iLen == 0)
  290. {
  291. BitMap::tagBitMapHead *tmp = (BitMap::tagBitMapHead*)pBuffer;
  292. if(tmp->_cVersion != BM_VERSION)
  293. {
  294. fclose(fp);
  295. delete[] pBuffer;
  296. return -3;
  297. }
  298. if(tmp->_iMemSize != _bitmaps[0].getMemSize())
  299. {
  300. fclose(fp);
  301. delete[] pBuffer;
  302. return -2;
  303. }
  304. }
  305. memcpy((char*)_bitmaps[0].getAddr() + iLen, pBuffer, ret);
  306. iLen += ret;
  307. }
  308. fclose(fp);
  309. delete[] pBuffer;
  310. if(iLen != iAllSize)
  311. {
  312. return -2;
  313. }
  314. return 0;
  315. }
  316. }