2011-06-23 19:17:08 +05:30
|
|
|
#ifndef CORE_MAP_H
|
|
|
|
#define CORE_MAP_H
|
|
|
|
|
|
|
|
#include "space_map.h"
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace persistent_data {
|
|
|
|
class core_map : public space_map {
|
|
|
|
public:
|
|
|
|
core_map(block_address nr_blocks)
|
2011-07-13 21:29:12 +05:30
|
|
|
: counts_(nr_blocks, 0),
|
|
|
|
nr_free_(nr_blocks) {
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
block_address get_nr_blocks() const {
|
2011-07-13 21:29:12 +05:30
|
|
|
return counts_.size();
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
block_address get_nr_free() const {
|
2011-07-13 21:29:12 +05:30
|
|
|
return nr_free_;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ref_t get_count(block_address b) const {
|
|
|
|
return counts_[b];
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_count(block_address b, ref_t c) {
|
2011-07-13 21:29:12 +05:30
|
|
|
if (counts_[b] == 0 && c > 0)
|
|
|
|
nr_free_--;
|
|
|
|
|
|
|
|
else if (counts_[b] > 0 && c == 0)
|
|
|
|
nr_free_++;
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
counts_[b] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void commit() {
|
|
|
|
}
|
|
|
|
|
2011-07-13 21:29:12 +05:30
|
|
|
void inc(block_address b) {
|
|
|
|
if (counts_[b] == 0)
|
|
|
|
nr_free_--;
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
counts_[b]++;
|
|
|
|
}
|
|
|
|
|
2011-07-13 21:29:12 +05:30
|
|
|
void dec(block_address b) {
|
2011-06-23 19:17:08 +05:30
|
|
|
counts_[b]--;
|
2011-07-13 21:29:12 +05:30
|
|
|
|
|
|
|
if (counts_[b] == 0)
|
|
|
|
nr_free_++;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
block_address new_block() {
|
|
|
|
for (block_address i = 0; i < counts_.size(); i++)
|
|
|
|
if (counts_[i] == 0) {
|
|
|
|
counts_[i] = 1;
|
2011-07-13 21:29:12 +05:30
|
|
|
nr_free_--;
|
2011-06-23 19:17:08 +05:30
|
|
|
return i;
|
|
|
|
}
|
2011-07-13 21:29:12 +05:30
|
|
|
|
|
|
|
throw std::runtime_error("no space");
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool count_possibly_greater_than_one(block_address b) const {
|
2011-07-13 21:29:12 +05:30
|
|
|
return counts_[b] > 1;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<ref_t> counts_;
|
2011-07-13 21:29:12 +05:30
|
|
|
unsigned nr_free_;
|
2011-06-23 19:17:08 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|