Skip to content

Commit

Permalink
make bitmap class template-specialized and remove bitmap_local class
Browse files Browse the repository at this point in the history
  • Loading branch information
wbenny committed Aug 1, 2019
1 parent d30f38b commit cd23e08
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
46 changes: 23 additions & 23 deletions src/hvpp/hvpp/lib/bitmap.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#include "bitmap.h"

void* bitmap::buffer() noexcept
void* basic_bitmap::buffer() noexcept
{
return buffer_;
}

const void* bitmap::buffer() const noexcept
const void* basic_bitmap::buffer() const noexcept
{
return buffer_;
}

int bitmap::size_in_bits() const noexcept
int basic_bitmap::size_in_bits() const noexcept
{
return size_in_bits_;
}

int bitmap::size_in_bytes() const noexcept
int basic_bitmap::size_in_bytes() const noexcept
{
return size_in_bits_ / 8;
}

void bitmap::set(int index, int count) noexcept
void basic_bitmap::set(int index, int count) noexcept
{
//
// Stolen from ReactOS - RtlSetbits.
Expand Down Expand Up @@ -55,17 +55,17 @@ void bitmap::set(int index, int count) noexcept
}
}

void bitmap::set(int bit) noexcept
void basic_bitmap::set(int bit) noexcept
{
buffer_[word(bit)] |= mask(bit);
}

void bitmap::set() noexcept
void basic_bitmap::set() noexcept
{
memset(buffer_, ~0, size_in_bits_ / 8);
}

void bitmap::clear(int index, int count) noexcept
void basic_bitmap::clear(int index, int count) noexcept
{
//
// Stolen from ReactOS - RtlClearBits.
Expand Down Expand Up @@ -100,27 +100,27 @@ void bitmap::clear(int index, int count) noexcept
}
}

void bitmap::clear(int bit) noexcept
void basic_bitmap::clear(int bit) noexcept
{
buffer_[word(bit)] &= ~mask(bit);
}

void bitmap::clear() noexcept
void basic_bitmap::clear() noexcept
{
memset(buffer_, 0, size_in_bits_ / 8);
}

bool bitmap::test(int bit) const noexcept
bool basic_bitmap::test(int bit) const noexcept
{
return !!ia32_asm_bt(buffer_, bit);
}

int bitmap::find_first_set(int count) const noexcept
int basic_bitmap::find_first_set(int count) const noexcept
{
return find_first_set(0, count);
}

int bitmap::find_first_set(int index, int count) const noexcept
int basic_bitmap::find_first_set(int index, int count) const noexcept
{
if (count > size_in_bits_)
{
Expand Down Expand Up @@ -157,7 +157,7 @@ int bitmap::find_first_set(int index, int count) const noexcept
return -1;
}

int bitmap::find_first_set() const noexcept
int basic_bitmap::find_first_set() const noexcept
{
for (int i = 0; i * 8 < size_in_bits_; ++i)
{
Expand All @@ -172,7 +172,7 @@ int bitmap::find_first_set() const noexcept
return size_in_bits_;
}

int bitmap::find_first_clear(int index, int count) const noexcept
int basic_bitmap::find_first_clear(int index, int count) const noexcept
{
if (count > size_in_bits_)
{
Expand Down Expand Up @@ -209,12 +209,12 @@ int bitmap::find_first_clear(int index, int count) const noexcept
return -1;
}

int bitmap::find_first_clear(int count) const noexcept
int basic_bitmap::find_first_clear(int count) const noexcept
{
return find_first_clear(0, count);
}

int bitmap::find_first_clear() const noexcept
int basic_bitmap::find_first_clear() const noexcept
{
for (int i = 0; i * 8 < size_in_bits_; ++i)
{
Expand All @@ -229,7 +229,7 @@ int bitmap::find_first_clear() const noexcept
return size_in_bits_;
}

bool bitmap::are_bits_set(int index, int count) const noexcept
bool basic_bitmap::are_bits_set(int index, int count) const noexcept
{
if (index + count > size_in_bits_ ||
index + count <= index)
Expand All @@ -240,7 +240,7 @@ bool bitmap::are_bits_set(int index, int count) const noexcept
return get_length_of_set(index, count) >= count;
}

bool bitmap::are_bits_clear(int index, int count) const noexcept
bool basic_bitmap::are_bits_clear(int index, int count) const noexcept
{
if (index + count > size_in_bits_ ||
index + count <= index)
Expand All @@ -251,17 +251,17 @@ bool bitmap::are_bits_clear(int index, int count) const noexcept
return get_length_of_clear(index, count) >= count;
}

bool bitmap::all_set() const noexcept
bool basic_bitmap::all_set() const noexcept
{
return are_bits_set(0, size_in_bits_);
}

bool bitmap::all_clear() const noexcept
bool basic_bitmap::all_clear() const noexcept
{
return are_bits_clear(0, size_in_bits_);
}

int bitmap::get_length_of_set(int index, int count) const noexcept
int basic_bitmap::get_length_of_set(int index, int count) const noexcept
{
//
// Stolen from ReactOS - RtlpGetLengthOfRunSet.
Expand Down Expand Up @@ -303,7 +303,7 @@ int bitmap::get_length_of_set(int index, int count) const noexcept
return length;
}

int bitmap::get_length_of_clear(int index, int count) const noexcept
int basic_bitmap::get_length_of_clear(int index, int count) const noexcept
{
//
// Stolen from ReactOS - RtlpGetLengthOfRunClear.
Expand Down
48 changes: 31 additions & 17 deletions src/hvpp/hvpp/lib/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
# undef max
#endif

class bitmap
class basic_bitmap
{
public:
bitmap() noexcept : buffer_{}, size_in_bits_{} { };
bitmap(const bitmap& other) noexcept = delete;
bitmap(bitmap&& other) noexcept = default;
bitmap& operator=(const bitmap& other) = delete;
bitmap& operator=(bitmap&& other) = default;
basic_bitmap() noexcept : buffer_{ nullptr }, size_in_bits_{ 0 } { };
basic_bitmap(const basic_bitmap& other) noexcept = delete;
basic_bitmap(basic_bitmap&& other) noexcept = default;
basic_bitmap& operator=(const basic_bitmap& other) = delete;
basic_bitmap& operator=(basic_bitmap&& other) = default;

bitmap(void* buffer, int size_in_bits) noexcept
basic_bitmap(void* buffer, int size_in_bits) noexcept
: buffer_{ reinterpret_cast<word_t*>(buffer) }
, size_in_bits_{ size_in_bits } { }

template <typename T, int SIZE>
bitmap(T(&buffer)[SIZE], int size_in_bits = SIZE * sizeof(T)) noexcept
basic_bitmap(T(&buffer)[SIZE], int size_in_bits = SIZE * sizeof(T)) noexcept
: buffer_{ reinterpret_cast<word_t*>(buffer) }
, size_in_bits_{ size_in_bits } { }

~bitmap() noexcept = default;
~basic_bitmap() noexcept = default;

const void* buffer() const noexcept;
void* buffer() noexcept;
Expand Down Expand Up @@ -83,20 +83,34 @@ class bitmap
int size_in_bits_;
};

template <
size_t SIZE_IN_BITS = 0
>
class bitmap;

template <>
class bitmap<>
: public basic_bitmap
{
public:
using basic_bitmap::basic_bitmap;
};

template <
size_t SIZE_IN_BITS
>
class bitmap_local
: public bitmap
class bitmap
: public bitmap<>
{
public:
bitmap_local() : bitmap{ buffer_, SIZE_IN_BITS }, buffer_{} { }
bitmap_local(const bitmap_local& other) noexcept = delete;
bitmap_local(bitmap_local&& other) noexcept = default;
bitmap_local& operator=(const bitmap_local& other) noexcept = delete;
bitmap_local& operator=(bitmap_local&& other) = default;
bitmap() : bitmap<>{ buffer_, SIZE_IN_BITS }, buffer_{} { }
bitmap(const bitmap& other) noexcept = delete;
bitmap(bitmap&& other) noexcept = default;
bitmap& operator=(const bitmap& other) noexcept = delete;
bitmap& operator=(bitmap&& other) = default;

~bitmap() noexcept = default;

~bitmap_local() noexcept = default;
private:
word_t buffer_[
word (SIZE_IN_BITS) +
Expand Down
2 changes: 1 addition & 1 deletion src/hvpp/hvpp/lib/mm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

namespace mm
{
using pgbmp_t = object_t<bitmap>;
using pgbmp_t = object_t<bitmap<>>;
using pgmap_t = uint16_t;

struct global_t
Expand Down
4 changes: 2 additions & 2 deletions src/hvpp/hvpp/vmexit/vmexit_stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class vmexit_stats_handler

void handle(vcpu_t& vp) noexcept override;

bitmap& trace_bitmap() noexcept
bitmap<>& trace_bitmap() noexcept
{ return vmexit_trace_bitmap_; }

vmexit_stats_storage_t* storage() const noexcept
Expand Down Expand Up @@ -62,7 +62,7 @@ class vmexit_stats_handler
// Bitmap of traced VM-exit reasons.
// There are currently defined 65 VM-exit reasons.
//
bitmap_local<65> vmexit_trace_bitmap_;
bitmap<65> vmexit_trace_bitmap_;

//
// Count of terminated VCPUs.
Expand Down
6 changes: 3 additions & 3 deletions src/hvppdrv/vmexit_custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void vmexit_custom_handler::setup(vcpu_t& vp) noexcept
vp.processor_based_controls(procbased_ctls);

vmx::io_bitmap_t io_bitmap{};
bitmap(io_bitmap.a).set(0x64);
bitmap<>(io_bitmap.a).set(0x64);

vp.io_bitmap(io_bitmap);
#else
Expand Down Expand Up @@ -74,8 +74,8 @@ void vmexit_custom_handler::setup(vcpu_t& vp) noexcept
//
// Disable VMWare backdoor.
//
bitmap(io_bitmap.a).clear(0x5658);
bitmap(io_bitmap.a).clear(0x5659);
bitmap<>(io_bitmap.a).clear(0x5658);
bitmap<>(io_bitmap.a).clear(0x5659);
vp.io_bitmap(io_bitmap);
#endif
Expand Down

0 comments on commit cd23e08

Please sign in to comment.