From d482a76bdaf66fd76330fd8b948dbf1e66efdf5c Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Mon, 28 Jul 2014 14:32:33 +0100 Subject: [PATCH] Use placement new to initialise the blocks --- block-cache/block_cache.cc | 4 ++-- block-cache/block_cache.h | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/block-cache/block_cache.cc b/block-cache/block_cache.cc index 7aae48b..10145a4 100644 --- a/block-cache/block_cache.cc +++ b/block-cache/block_cache.cc @@ -81,8 +81,7 @@ namespace bcache { blocks_data_.reset(reinterpret_cast(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); diff --git a/block-cache/block_cache.h b/block-cache/block_cache.h index cb991ab..6d5b439 100644 --- a/block-cache/block_cache.h +++ b/block-cache/block_cache.h @@ -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 blocks_memory_; // FIXME: change to a vector + std::auto_ptr blocks_memory_; std::auto_ptr blocks_data_; io_context_t aio_context_;