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