2011-12-16 00:04:31 +05:30
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
2011-12-06 19:23:05 +05:30
|
|
|
//
|
2011-12-06 19:13:56 +05:30
|
|
|
// This file is part of the thin-provisioning-tools source.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is free software: you can redistribute it
|
|
|
|
// and/or modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation, either version 3 of
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is distributed in the hope that it will be
|
|
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with thin-provisioning-tools. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
#ifndef CORE_MAP_H
|
|
|
|
#define CORE_MAP_H
|
|
|
|
|
|
|
|
#include "space_map.h"
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace persistent_data {
|
2013-01-10 17:45:50 +05:30
|
|
|
class core_map : public checked_space_map {
|
2011-06-23 19:17:08 +05:30
|
|
|
public:
|
2013-01-10 17:45:50 +05:30
|
|
|
typedef boost::shared_ptr<core_map> ptr;
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-11 02:35:10 +05:30
|
|
|
maybe_block new_block(span_iterator &it) {
|
|
|
|
for (maybe_span ms = it.first(); ms; ms = it.next()) {
|
|
|
|
for (block_address b = ms->first; b < ms->second; b++) {
|
|
|
|
if (b >= counts_.size())
|
|
|
|
throw std::runtime_error("block out of bounds");
|
|
|
|
|
|
|
|
if (!counts_[b]) {
|
|
|
|
counts_[b] = 1;
|
|
|
|
nr_free_--;
|
|
|
|
return maybe_block(b);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
2013-01-11 02:35:10 +05:30
|
|
|
}
|
2011-07-13 21:29:12 +05:30
|
|
|
|
2011-11-16 18:23:37 +05:30
|
|
|
return maybe_block();
|
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
|
|
|
}
|
|
|
|
|
2011-08-15 16:21:49 +05:30
|
|
|
void extend(block_address extra_blocks) {
|
2013-01-11 02:35:10 +05:30
|
|
|
throw std::runtime_error("'extend' not implemented");
|
2011-08-15 16:21:49 +05:30
|
|
|
}
|
|
|
|
|
2013-01-10 17:45:50 +05:30
|
|
|
// FIXME: meaningless, but this class is only used for testing
|
|
|
|
size_t root_size() const {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: meaningless, but this class is only used for testing
|
|
|
|
virtual void copy_root(void *dest, size_t len) const {
|
2013-01-11 02:35:10 +05:30
|
|
|
throw std::runtime_error("'copy root' not implemented");
|
2013-01-10 17:45:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
checked_space_map::ptr clone() const {
|
|
|
|
return ptr(new core_map(*this));
|
|
|
|
}
|
|
|
|
|
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
|