]> git.ipfire.org Git - thirdparty/squid.git/blob - src/FileMap.h
Source Format Enforcement (#532)
[thirdparty/squid.git] / src / FileMap.h
1 /*
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 08 Swap File Bitmap */
10
11 #ifndef FILEMAP_H_
12 #define FILEMAP_H_
13
14 #include "store/forward.h"
15
16 /** A bitmap used for managing UFS StoreEntry "file numbers".
17 *
18 * Nth bit represents whether file number N is used.
19 * The map automatically grows to hold up to 2^24 bits.
20 * New bit is "off" or zero by default, representing unused fileno.
21 * TODO: consider using std::bitset instead.
22 */
23 class FileMap
24 {
25 public:
26 FileMap();
27 ~FileMap();
28
29 /** Set the num-th bit in the FileMap
30 *
31 * \warning FileMap's backing storage will be extended as needed to
32 * hold the representation, but if the bit is already set
33 * it will break the file number accounting, so the caller must
34 * ensure that setBit is only called if the bit is not already set,
35 * by using testBit on it before.
36 */
37 bool setBit(sfileno num);
38
39 /// Test whether the num-th bit in the FileMap is set
40 bool testBit(sfileno num) const;
41
42 /** Clear the num-th bit in the FileMap
43 *
44 * \warning that clearBit doesn't do any bounds checking, nor it
45 * checks that the bit is set before clearing. The caller will have
46 * to ensure that both are true using testBit before clearing.
47 */
48 void clearBit(sfileno num);
49
50 /** locate an unused slot in the FileMap, possibly at or after position suggestion
51 *
52 * Obtain the location of an unused slot in the FileMap,
53 * growing it if needed.
54 * The suggestion is only an advice; there is no guarantee
55 * that it will be followed.
56 */
57 sfileno allocate(sfileno suggestion);
58
59 /// return the max number of slots in the FileMap
60 int capacity() const {return capacity_;}
61
62 /// return the number of used slots in the FileMap
63 int numFilesInMap() const {return usedSlots_;}
64 private:
65 /// grow the FileMap (size is doubled each time, up to 2^24 bits)
66 void grow();
67 FileMap(const FileMap &); //no copying
68 FileMap& operator=(const FileMap &); //no assignments
69
70 /// max number of files which can be tracked in the current store
71 sfileno capacity_;
72 /// used slots in the map
73 unsigned int usedSlots_;
74 /// number of "long ints" making up the filemap
75 unsigned int nwords;
76 unsigned long *bitmap;
77 };
78
79 #endif /* FILEMAP_H_ */
80