Use placement new to initialise the blocks

This commit is contained in:
Joe Thornber 2014-07-28 14:32:33 +01:00
parent 5c82d50204
commit d482a76bda
2 changed files with 9 additions and 5 deletions

View File

@ -81,8 +81,7 @@ namespace bcache {
blocks_data_.reset(reinterpret_cast<unsigned char *>(data));
for (i = 0; i < count; i++) {
block *b = blocks + i;
INIT_LIST_HEAD(&b->list_);
block *b = new (blocks + i) block();
b->data_ = data + block_size * i;
list_add(&b->list_, &free_);
@ -384,6 +383,7 @@ namespace bcache {
b->error_ = 0;
b->flags_ = 0;
b->v_ = validator::ptr(new noop_validator);
b->index_ = index;
setup_control_block(*b);

View File

@ -50,8 +50,12 @@ namespace bcache {
public:
block()
: v_() {
INIT_LIST_HEAD(&list_);
}
// Do not give this class a destructor, it wont get
// called because we manage allocation ourselves.
uint64_t get_index() const {
return index_;
}
@ -61,11 +65,11 @@ namespace bcache {
}
void mark_dirty() {
flags_ |= BF_DIRTY;
set_flags(BF_DIRTY);
}
void mark_flush() {
flags_ |= BF_FLUSH;
set_flags(BF_FLUSH);
}
void set_flags(unsigned flags) {
@ -172,7 +176,7 @@ namespace bcache {
uint64_t nr_data_blocks_;
uint64_t nr_cache_blocks_;
std::auto_ptr<unsigned char> blocks_memory_; // FIXME: change to a vector
std::auto_ptr<unsigned char> blocks_memory_;
std::auto_ptr<unsigned char> blocks_data_;
io_context_t aio_context_;