Coverage Report

Created: 2025-08-26 06:37

/src/spotify-json/include/spotify/json/detail/bitset.hpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2016-2019 Spotify AB
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
 * use this file except in compliance with the License. You may obtain a copy of
6
 * 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, WITHOUT
12
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
 * License for the specific language governing permissions and limitations under
14
 * the License.
15
 */
16
17
#pragma once
18
19
#include <cstddef>
20
#include <memory>
21
#include <vector>
22
23
#include <spotify/json/detail/macros.hpp>
24
25
namespace spotify {
26
namespace json {
27
namespace detail {
28
29
struct bitset_base {
30
  ~bitset_base();
31
32
911
  json_force_inline uint8_t test_and_set(const std::size_t index) {
33
911
    const auto byte = (index / 8);
34
911
    const auto bidx = (index & 7);
35
911
    const auto mask = (1 << bidx);
36
911
    const auto byte_before = _base[byte];
37
911
    _base[byte] = (byte_before | mask);
38
911
    return (byte_before & mask) >> bidx;
39
911
  }
40
41
 protected:
42
  bitset_base(const std::size_t size, uint8_t *inline_base);
43
44
 private:
45
  std::unique_ptr<std::vector<uint8_t>> _vector;
46
  uint8_t *_base;
47
};
48
49
template <std::size_t inline_size>
50
struct bitset final : public bitset_base {
51
  bitset(const std::size_t size)
52
2.34k
      : bitset_base(size, size <= inline_size ? _array : nullptr) {
53
2.34k
    if (json_likely(size <= inline_size)) {
54
2.34k
      memset(_array, 0, sizeof(_array));
55
2.34k
    }
56
2.34k
  }
57
58
 private:
59
  static constexpr auto _num_inline_bytes = ((inline_size + 7) / 8);
60
  uint8_t _array[_num_inline_bytes];
61
};
62
63
}  // namespace detail
64
}  // namespace json
65
}  // namespace spotify