Threadsafe bitmap class

This commit is contained in:
Dr. Chat 2015-12-27 13:13:07 -06:00 committed by Ben Vanik
parent bb5fd73b9e
commit c5ac4185ac
2 changed files with 152 additions and 0 deletions

96
src/xenia/base/bit_map.cc Normal file
View File

@ -0,0 +1,96 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2015 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "xenia/base/bit_map.h"
#include "xenia/base/assert.h"
#include "xenia/base/atomic.h"
#include "xenia/base/math.h"
namespace xe {
BitMap::BitMap() = default;
BitMap::BitMap(size_t size) { Resize(size); }
BitMap::BitMap(uint32_t* data, size_t size) {
assert_true(size % 4 == 0);
data_.resize(size / 4);
std::memcpy(data_.data(), data, size);
}
size_t BitMap::Acquire() {
for (size_t i = 0; i < data_.size(); i++) {
uint32_t entry = 0;
uint32_t new_entry = 0;
int acquired_idx = -1;
do {
entry = data_[i];
uint8_t index = lzcnt(entry);
if (index == kDataSizeBits) {
// None free.
acquired_idx = -1;
break;
}
// Entry has a free bit. Acquire it.
uint32_t bit = 1 << (kDataSizeBits - index - 1);
new_entry = entry & ~bit;
assert_not_zero(entry & bit);
acquired_idx = index;
} while (!atomic_cas(entry, new_entry, &data_[i]));
if (acquired_idx != -1) {
// Acquired.
return (i * kDataSizeBits) + acquired_idx;
}
}
return -1;
}
void BitMap::Release(size_t index) {
auto slot = index / kDataSizeBits;
index -= slot * kDataSizeBits;
uint32_t bit = 1 << (kDataSizeBits - index - 1);
uint32_t entry = 0;
uint32_t new_entry = 0;
do {
entry = data_[slot];
assert_zero(entry & bit);
new_entry = entry | bit;
} while (!atomic_cas(entry, new_entry, &data_[slot]));
}
void BitMap::Resize(size_t new_size) {
auto old_size = data_.size();
assert_true(new_size % 4 == 0);
data_.resize(new_size / 4);
// Initialize new entries.
if (data_.size() > old_size) {
for (size_t i = old_size; i < data_.size(); i++) {
data_[i] = -1;
}
}
}
void BitMap::Reset() {
for (size_t i = 0; i < data_.size(); i++) {
data_[i] = -1;
}
}
} // namespace xe

56
src/xenia/base/bit_map.h Normal file
View File

@ -0,0 +1,56 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2015 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#ifndef XENIA_BASE_BIT_MAP_H_
#define XENIA_BASE_BIT_MAP_H_
#include <cstdint>
#include <vector>
namespace xe {
// Bit Map: Efficient lookup of free/used entries.
class BitMap {
public:
BitMap();
// Size is the number of 8-bit entries, must be a multiple of 4.
BitMap(size_t size);
// Data does not have to be aligned to a 4-byte boundary, but it is
// preferable.
// Size is the number of 8-bit entries, must be a multiple of 4.
BitMap(uint32_t* data, size_t size);
// (threadsafe) Acquires an entry and returns its index. Returns -1 if there
// are no more free entries.
size_t Acquire();
// (threadsafe) Releases an entry by an index.
void Release(size_t index);
// Resize the bitmap. Size is the number of 8-bit entries, must be a multiple
// of 4.
void Resize(size_t new_size);
// Sets all entries to free.
void Reset();
const std::vector<uint32_t> data() const { return data_; }
std::vector<uint32_t>& data() { return data_; }
private:
const static uint32_t kDataSize = 4;
const static uint32_t kDataSizeBits = kDataSize * 8;
std::vector<uint32_t> data_;
};
} // namespace xe
#endif // XENIA_BASE_BIT_MAP_H_