BFGraph: src/CompressedCoverage.hpp Source File

BFGraph

CompressedCoverage.hpp
1 #ifndef BFG_COMPRESSED_COVERAGE_HPP
2 #define BFG_COMPRESSED_COVERAGE_HPP
3
4 #include <cstring>
5 #include <stdint.h>
6 #include <vector>
7
8 using std::vector;
9 using std::pair;
10
11 /* Short description:
12 * - Tagged pointer union that is either
13 * - a pointer to a char array that stores 2-bit integers
14 * - a local 2-bit integer array
15 * - The bits are stored either as
16 * pppppppp|pppppppp|pppppppp|pppppppp|pppppppp|pppppppp|pppppppp|ppppppF0
17 * dddddddd|dddddddd|dddddddd|dddddddd|dddddddd|dddddddd|dddddddd|ssssssF1
18 *
19 * - Last bit is 0 for pointer and 1 for local array
20 * - Second last bit is 1 for a full coverage and 0 for non-full
21 * - For local array last 8 except last 2 bits store size of the array
22 * - For the pointer version first 62 bits encode the pointer (last two bits
23 * are zeroed out before dereferencing)
24 * - The pointer points to an array of bytes, where the first 8 encode the
25 * size of the array used in uint32_t and the number of full positions.
26 * - The remainder of bytes are 2-bit encoded integers.
27 * - If the full bit is set then the pointer must be 0 and the memory released
28 * */
29 class CompressedCoverage {
30
31 public:
32
33 CompressedCoverage(size_t sz=0, bool full=false);
34 ~CompressedCoverage();
35
36 // Copy constructors
37 CompressedCoverage(const CompressedCoverage& o);
38 CompressedCoverage& operator=(const CompressedCoverage& o);
39
40 // move constructors
41 CompressedCoverage(CompressedCoverage&& o);
42 CompressedCoverage& operator=(CompressedCoverage&& o);
43
44 void initialize(const size_t sz, const bool full);
45
46 void cover(size_t start, size_t end);
47 uint8_t covAt(const size_t index) const;
48
49 bool isFull() const;
50 void setFull();
51
52 vector<pair<int, int>> splittingVector() const;
53 pair<size_t, size_t> lowCoverageInfo() const;
54
55 size_t size() const;
56 std::string toString() const; // for debugging
57
58 static const size_t size_limit = 28; // 56 bit array, 28 2-bit integers
59 static const size_t cov_full = 2;
60
61 private:
62
63 static const uintptr_t tagMask = 1; // local array bit
64 static const uintptr_t fullMask = 2; // full bit
65 static const uintptr_t sizeMask = 0xFC; // 0b11111100
66 static const uintptr_t localCoverageMask = 0xAAAAAAAAAAAAAA; // 0b10101010101010101010101010101010101010101010101010101010
67 static const uintptr_t pointerMask = ~(tagMask | fullMask); // rest of bits
68
69 inline size_t round_to_bytes(const size_t len) const { return (len + 3) / 4; }
70
71 inline uint8_t *get8Pointer() const { return reinterpret_cast<uint8_t *>(asBits & pointerMask); }
72 inline uint32_t *get32Pointer() const { return reinterpret_cast<uint32_t *>(asBits & pointerMask); }
73 inline const uint32_t *getConst32Pointer() const { return reinterpret_cast<const uint32_t *>(asBits & pointerMask); }
74
75 void releasePointer();
76
77 union {
78
79 uint8_t *asPointer;
80 uintptr_t asBits;
81 };
82 };
83
84 template<typename T> struct CompressedCoverage_t {
85
86 CompressedCoverage_t(size_t sz = 0, bool full = false){ ccov = CompressedCoverage(sz, full); }
87
88 inline const T* getData() const { return &data; }
89 inline T* getData() { return &data; }
90 inline void setData(const T* const data_){ data = *data_; }
91 //inline void releaseData(T* const data){ delete data; }
92
93 CompressedCoverage ccov;
94 T data;
95 };
96
97 template<> struct CompressedCoverage_t<void> {
98
99 CompressedCoverage_t(size_t sz = 0, bool full = false){ ccov = CompressedCoverage(sz, full); }
100
101 inline const void* getData() const { return nullptr; }
102 inline void* getData() { return nullptr; }
103 inline void setData(const void* const data_){ return; }
104 //inline void releaseData(void* const data){ return; }
105
106 CompressedCoverage ccov;
107 };
108
109 #endif // BFG_COMPRESSED_COVERAGE_HPP
CompressedCoverage
Definition: CompressedCoverage.hpp:29
CompressedCoverage_t
Definition: CompressedCoverage.hpp:84