xref: /aosp_15_r20/external/cronet/net/disk_cache/blockfile/bitmap.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NET_DISK_CACHE_BLOCKFILE_BITMAP_H_
6 #define NET_DISK_CACHE_BLOCKFILE_BITMAP_H_
7 
8 #include <stdint.h>
9 #include <string.h>
10 
11 #include <memory>
12 
13 #include "base/containers/span.h"
14 #include "base/memory/raw_ptr.h"
15 #include "net/base/net_export.h"
16 
17 namespace disk_cache {
18 
19 // This class provides support for simple maps of bits.
20 class NET_EXPORT_PRIVATE Bitmap {
21  public:
22   Bitmap();
23 
24   // This constructor will allocate on a uint32_t boundary. If |clear_bits| is
25   // false, the bitmap bits will not be initialized.
26   Bitmap(int num_bits, bool clear_bits);
27 
28   // Constructs a Bitmap with the actual storage provided by the caller. |map|
29   // has to be valid until this object destruction. |num_bits| is the number of
30   // bits in the bitmap, and |num_words| is the size of |map| in 32-bit words.
31   Bitmap(uint32_t* map, int num_bits, int num_words);
32 
33   Bitmap(const Bitmap&) = delete;
34   Bitmap& operator=(const Bitmap&) = delete;
35 
36   ~Bitmap();
37 
38   // Resizes the bitmap.
39   // If |num_bits| < Size(), the extra bits will be discarded.
40   // If |num_bits| > Size(), the extra bits will be filled with zeros if
41   // |clear_bits| is true.
42   // This object cannot be using memory provided during construction.
43   void Resize(int num_bits, bool clear_bits);
44 
45   // Returns the number of bits in the bitmap.
Size()46   int Size() const { return num_bits_; }
47 
48   // Returns the number of 32-bit words in the bitmap.
ArraySize()49   int ArraySize() const { return array_size_; }
50 
51   // Sets all the bits to true or false.
SetAll(bool value)52   void SetAll(bool value) {
53     memset(map_, (value ? 0xFF : 0x00), array_size_ * sizeof(*map_));
54   }
55 
56   // Clears all bits in the bitmap
Clear()57   void Clear() { SetAll(false); }
58 
59   // Sets the value, gets the value or toggles the value of a given bit.
60   void Set(int index, bool value);
61   bool Get(int index) const;
62   void Toggle(int index);
63 
64   // Directly sets an element of the internal map. Requires |array_index| <
65   // ArraySize();
66   void SetMapElement(int array_index, uint32_t value);
67 
68   // Gets an entry of the internal map. Requires array_index <
69   // ArraySize()
70   uint32_t GetMapElement(int array_index) const;
71 
72   // Directly sets the whole internal map. |size| is the number of 32-bit words
73   // to set from |map|. If  |size| > array_size(), it ignores the end of |map|.
74   void SetMap(const uint32_t* map, int size);
75 
76   // Gets a pointer to the internal map.
GetMap()77   const uint32_t* GetMap() const { return map_; }
78 
79   // Gets a span describing the internal map.
GetSpan()80   base::span<const uint32_t> GetSpan() const {
81     return base::make_span(GetMap(), static_cast<size_t>(ArraySize()));
82   }
83 
84   // Sets a range of bits to |value|.
85   void SetRange(int begin, int end, bool value);
86 
87   // Returns true if any bit between begin inclusive and end exclusive is set.
88   // 0 <= |begin| <= |end| <= Size() is required.
89   bool TestRange(int begin, int end, bool value) const;
90 
91   // Scans bits starting at bit *|index|, looking for a bit set to |value|. If
92   // it finds that bit before reaching bit index |limit|, sets *|index| to the
93   // bit index and returns true. Otherwise returns false.
94   // Requires |limit| <= Size().
95   //
96   // Note that to use these methods in a loop you must increment the index
97   // after each use, as in:
98   //
99   //  for (int index = 0 ; map.FindNextBit(&index, limit, value) ; ++index) {
100   //    DoSomethingWith(index);
101   //  }
102   bool FindNextBit(int* index, int limit, bool value) const;
103 
104   // Finds the first offset >= *|index| and < |limit| that has its bit set.
105   // See FindNextBit() for more info.
FindNextSetBitBeforeLimit(int * index,int limit)106   bool FindNextSetBitBeforeLimit(int* index, int limit) const {
107     return FindNextBit(index, limit, true);
108   }
109 
110   // Finds the first offset >= *|index| that has its bit set.
111   // See FindNextBit() for more info.
FindNextSetBit(int * index)112   bool FindNextSetBit(int *index) const {
113     return FindNextSetBitBeforeLimit(index, num_bits_);
114   }
115 
116   // Scans bits starting at bit *|index|, looking for a bit set to |value|. If
117   // it finds that bit before reaching bit index |limit|, sets *|index| to the
118   // bit index and then counts the number of consecutive bits set to |value|
119   // (before reaching |limit|), and returns that count. If no bit is found
120   // returns 0. Requires |limit| <= Size().
121   int FindBits(int* index, int limit, bool value) const;
122 
123   // Returns number of allocated words required for a bitmap of size |num_bits|.
RequiredArraySize(int num_bits)124   static int RequiredArraySize(int num_bits) {
125     // Force at least one allocated word.
126     if (num_bits <= kIntBits)
127       return 1;
128 
129     return (num_bits + kIntBits - 1) >> kLogIntBits;
130   }
131 
132  private:
133   static const int kIntBits = sizeof(uint32_t) * 8;
134   static const int kLogIntBits = 5;  // 2^5 == 32 bits per word.
135 
136   // Sets |len| bits from |start| to |value|. All the bits to be set should be
137   // stored in the same word, and len < kIntBits.
138   void SetWordBits(int start, int len, bool value);
139 
140   int num_bits_ = 0;    // The upper bound of the bitmap.
141   int array_size_ = 0;  // The physical size (in uint32s) of the bitmap.
142   std::unique_ptr<uint32_t[]> allocated_map_;  // The allocated data.
143   raw_ptr<uint32_t, AllowPtrArithmetic> map_ = nullptr;  // The bitmap.
144 };
145 
146 }  // namespace disk_cache
147 
148 #endif  // NET_DISK_CACHE_BLOCKFILE_BITMAP_H_
149