empty_filter.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <limits.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include "pt_malloc.h"
  20. #include "empty_filter.h"
  21. #include "algorithm/bitsop.h"
  22. EmptyNodeFilter::EmptyNodeFilter() : _enf(0)
  23. {
  24. memset(errmsg_, 0x0, sizeof(errmsg_));
  25. }
  26. EmptyNodeFilter::~EmptyNodeFilter()
  27. {
  28. }
  29. int EmptyNodeFilter::ISSET(uint32_t key)
  30. {
  31. uint32_t bitoff = get_offset(key);
  32. uint32_t tableid = get_index(key);
  33. if (_enf->enf_tables[tableid].t_size < bitoff / CHAR_BIT + 1)
  34. return 0;
  35. return ISSET_B(bitoff,
  36. M_POINTER(void, _enf->enf_tables[tableid].t_handle));
  37. }
  38. void EmptyNodeFilter::SET(uint32_t key)
  39. {
  40. uint32_t bitoff = get_offset(key);
  41. uint32_t tableid = get_index(key);
  42. if (_enf->enf_tables[tableid].t_size < bitoff / CHAR_BIT + 1) {
  43. /* 按step的整数倍来increase table*/
  44. int incbyte = bitoff / CHAR_BIT + 1 -
  45. _enf->enf_tables[tableid].t_size;
  46. int how = (incbyte + _enf->enf_step - 1) / _enf->enf_step;
  47. size_t size =
  48. _enf->enf_tables[tableid].t_size + how * _enf->enf_step;
  49. _enf->enf_tables[tableid].t_handle =
  50. M_REALLOC(_enf->enf_tables[tableid].t_handle, size);
  51. if (_enf->enf_tables[tableid].t_handle == INVALID_HANDLE) {
  52. /* realloc 失败后,不会重试*/
  53. return;
  54. }
  55. _enf->enf_tables[tableid].t_size = size;
  56. }
  57. return SET_B(bitoff,
  58. M_POINTER(void, _enf->enf_tables[tableid].t_handle));
  59. }
  60. void EmptyNodeFilter::CLR(uint32_t key)
  61. {
  62. uint32_t bitoff = get_offset(key);
  63. uint32_t tableid = get_index(key);
  64. if (_enf->enf_tables[tableid].t_size < bitoff / CHAR_BIT + 1)
  65. /* 超出表范围,return*/
  66. return;
  67. return CLR_B(bitoff,
  68. M_POINTER(void, _enf->enf_tables[tableid].t_handle));
  69. }
  70. int EmptyNodeFilter::do_init(uint32_t total, uint32_t step, uint32_t mod)
  71. {
  72. mod = mod ? mod : DF_ENF_MOD;
  73. step = step ? step : DF_ENF_STEP;
  74. total = total ? total : DF_ENF_TOTAL;
  75. /* allocate header */
  76. uint32_t size = sizeof(ENF_T);
  77. size += sizeof(ENF_TABLE_T) * mod;
  78. MEM_HANDLE_T v = M_CALLOC(size);
  79. if (INVALID_HANDLE == v) {
  80. snprintf(errmsg_, sizeof(errmsg_),
  81. "calloc %u bytes mem failed, %s", size, M_ERROR());
  82. return -1;
  83. }
  84. _enf = M_POINTER(ENF_T, v);
  85. _enf->enf_total = total;
  86. _enf->enf_step = step;
  87. _enf->enf_mod = mod;
  88. return 0;
  89. }
  90. int EmptyNodeFilter::do_attach(MEM_HANDLE_T v)
  91. {
  92. if (INVALID_HANDLE == v) {
  93. snprintf(errmsg_, sizeof(errmsg_),
  94. "attach Empty-Node Filter failed, memory handle = 0");
  95. return -1;
  96. }
  97. _enf = M_POINTER(ENF_T, v);
  98. return 0;
  99. }
  100. int EmptyNodeFilter::do_detach(void)
  101. {
  102. _enf = 0;
  103. errmsg_[0] = 0;
  104. return 0;
  105. }