2013-01-11 02:35:10 +05:30
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
2013-01-11 03:06:38 +05:30
|
|
|
#include "persistent-data/space-maps/careful_alloc.h"
|
2013-04-23 15:27:47 +05:30
|
|
|
#include "persistent-data/space-maps/subtracting_span_iterator.h"
|
2013-01-11 02:35:10 +05:30
|
|
|
|
2013-08-08 15:19:59 +05:30
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
2013-01-11 02:35:10 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace {
|
2013-05-28 18:18:10 +05:30
|
|
|
using namespace persistent_data;
|
|
|
|
|
2013-01-11 02:35:10 +05:30
|
|
|
class sm_careful_alloc : public checked_space_map {
|
|
|
|
public:
|
2013-08-08 15:19:59 +05:30
|
|
|
typedef boost::shared_ptr<sm_careful_alloc> ptr;
|
2013-01-11 02:35:10 +05:30
|
|
|
|
|
|
|
sm_careful_alloc(checked_space_map::ptr sm)
|
|
|
|
: sm_(sm) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual block_address get_nr_blocks() const {
|
|
|
|
return sm_->get_nr_blocks();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual block_address get_nr_free() const {
|
|
|
|
return sm_->get_nr_free();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual ref_t get_count(block_address b) const {
|
|
|
|
return sm_->get_count(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void set_count(block_address b, ref_t c) {
|
|
|
|
if (!c && sm_->get_count(b))
|
|
|
|
mark_freed(b);
|
|
|
|
|
|
|
|
sm_->set_count(b, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void commit() {
|
|
|
|
sm_->commit();
|
|
|
|
clear_freed();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void inc(block_address b) {
|
|
|
|
if (was_freed(b))
|
|
|
|
throw runtime_error("inc of block freed within current transaction");
|
|
|
|
|
|
|
|
sm_->inc(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void dec(block_address b) {
|
|
|
|
sm_->dec(b);
|
|
|
|
|
|
|
|
if (!sm_->get_count(b))
|
|
|
|
mark_freed(b);
|
|
|
|
}
|
|
|
|
|
2013-04-23 15:27:47 +05:30
|
|
|
virtual maybe_block find_free(span_iterator &it) {
|
2013-07-09 15:55:09 +05:30
|
|
|
subtracting_span_iterator filtered_it(get_nr_blocks(), it, freed_blocks_);
|
2013-04-23 15:27:47 +05:30
|
|
|
return sm_->find_free(filtered_it);
|
2013-01-11 02:35:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool count_possibly_greater_than_one(block_address b) const {
|
|
|
|
return sm_->count_possibly_greater_than_one(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void extend(block_address extra_blocks) {
|
|
|
|
return sm_->extend(extra_blocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void iterate(iterator &it) const {
|
|
|
|
sm_->iterate(it);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual size_t root_size() const {
|
|
|
|
return sm_->root_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void copy_root(void *dest, size_t len) const {
|
|
|
|
return sm_->copy_root(dest, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void check(block_counter &counter) const {
|
|
|
|
return sm_->check(counter);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual checked_space_map::ptr clone() const {
|
|
|
|
return checked_space_map::ptr(new sm_careful_alloc(sm_));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void clear_freed() {
|
|
|
|
freed_blocks_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void mark_freed(block_address b) {
|
2013-07-09 15:55:09 +05:30
|
|
|
freed_blocks_.add(b, b + 1);
|
2013-01-11 02:35:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool was_freed(block_address b) const {
|
2013-07-09 15:55:09 +05:30
|
|
|
return freed_blocks_.member(b) > 0;
|
2013-01-11 02:35:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
checked_space_map::ptr sm_;
|
2013-04-23 15:27:47 +05:30
|
|
|
subtracting_span_iterator::block_set freed_blocks_;
|
2013-01-11 02:35:10 +05:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
checked_space_map::ptr
|
|
|
|
persistent_data::create_careful_alloc_sm(checked_space_map::ptr sm)
|
|
|
|
{
|
|
|
|
return checked_space_map::ptr(new sm_careful_alloc(sm));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|