Fix a number of object lifetime/ownership issues

Summary:
Replace manual memory management with std::unique_ptr in a
number of places; not exhaustive, but this fixes a few leaks with file
handles as well as clarifies semantics of the ownership of file handles
with log classes.

Test Plan: db_stress, make check

Reviewers: dhruba

Reviewed By: dhruba

CC: zshao, leveldb, heyongqiang

Differential Revision: https://reviews.facebook.net/D8043
This commit is contained in:
Chip Turner
2013-01-20 02:07:13 -08:00
parent 88b79b24f3
commit 2fdf91a4f8
39 changed files with 362 additions and 355 deletions

View File

@@ -27,7 +27,7 @@ struct Table::Rep {
Options options;
Status status;
RandomAccessFile* file;
unique_ptr<RandomAccessFile> file;
uint64_t cache_id;
FilterBlockReader* filter;
const char* filter_data;
@@ -37,10 +37,10 @@ struct Table::Rep {
};
Status Table::Open(const Options& options,
RandomAccessFile* file,
unique_ptr<RandomAccessFile>&& file,
uint64_t size,
Table** table) {
*table = NULL;
unique_ptr<Table>* table) {
table->reset();
if (size < Footer::kEncodedLength) {
return Status::InvalidArgument("file is too short to be an sstable");
}
@@ -66,7 +66,7 @@ Status Table::Open(const Options& options,
BlockContents contents;
Block* index_block = NULL;
if (s.ok()) {
s = ReadBlock(file, ReadOptions(), footer.index_handle(), &contents);
s = ReadBlock(file.get(), ReadOptions(), footer.index_handle(), &contents);
if (s.ok()) {
index_block = new Block(contents);
}
@@ -77,13 +77,13 @@ Status Table::Open(const Options& options,
// ready to serve requests.
Rep* rep = new Table::Rep;
rep->options = options;
rep->file = file;
rep->file = std::move(file);
rep->metaindex_handle = footer.metaindex_handle();
rep->index_block = index_block;
rep->cache_id = (options.block_cache ? options.block_cache->NewId() : 0);
rep->filter_data = NULL;
rep->filter = NULL;
*table = new Table(rep);
table->reset(new Table(rep));
(*table)->ReadMeta(footer);
} else {
if (index_block) delete index_block;
@@ -101,7 +101,8 @@ void Table::ReadMeta(const Footer& footer) {
// it is an empty block.
ReadOptions opt;
BlockContents contents;
if (!ReadBlock(rep_->file, opt, footer.metaindex_handle(), &contents).ok()) {
if (!ReadBlock(rep_->file.get(), opt, footer.metaindex_handle(),
&contents).ok()) {
// Do not propagate errors since meta info is not needed for operation
return;
}
@@ -129,7 +130,7 @@ void Table::ReadFilter(const Slice& filter_handle_value) {
// requiring checksum verification in Table::Open.
ReadOptions opt;
BlockContents block;
if (!ReadBlock(rep_->file, opt, filter_handle, &block).ok()) {
if (!ReadBlock(rep_->file.get(), opt, filter_handle, &block).ok()) {
return;
}
if (block.heap_allocated) {
@@ -164,7 +165,7 @@ Iterator* Table::BlockReader(void* arg,
const Slice& index_value,
bool* didIO) {
Table* table = reinterpret_cast<Table*>(arg);
Cache* block_cache = table->rep_->options.block_cache;
Cache* block_cache = table->rep_->options.block_cache.get();
Statistics* const statistics = table->rep_->options.statistics;
Block* block = NULL;
Cache::Handle* cache_handle = NULL;
@@ -188,7 +189,7 @@ Iterator* Table::BlockReader(void* arg,
RecordTick(statistics, BLOCK_CACHE_HIT);
} else {
s = ReadBlock(table->rep_->file, options, handle, &contents);
s = ReadBlock(table->rep_->file.get(), options, handle, &contents);
if (s.ok()) {
block = new Block(contents);
if (contents.cachable && options.fill_cache) {
@@ -203,7 +204,7 @@ Iterator* Table::BlockReader(void* arg,
RecordTick(statistics, BLOCK_CACHE_MISS);
}
} else {
s = ReadBlock(table->rep_->file, options, handle, &contents);
s = ReadBlock(table->rep_->file.get(), options, handle, &contents);
if (s.ok()) {
block = new Block(contents);
}

View File

@@ -221,8 +221,7 @@ class BlockConstructor: public Constructor {
class TableConstructor: public Constructor {
public:
TableConstructor(const Comparator* cmp)
: Constructor(cmp),
source_(NULL), table_(NULL) {
: Constructor(cmp) {
}
~TableConstructor() {
Reset();
@@ -244,11 +243,12 @@ class TableConstructor: public Constructor {
ASSERT_EQ(sink.contents().size(), builder.FileSize());
// Open the table
source_ = new StringSource(sink.contents());
source_.reset(new StringSource(sink.contents()));
Options table_options;
table_options.comparator = options.comparator;
table_options.compression_opts = options.compression_opts;
return Table::Open(table_options, source_, sink.contents().size(), &table_);
return Table::Open(table_options, std::move(source_),
sink.contents().size(), &table_);
}
virtual Iterator* NewIterator() const {
@@ -261,14 +261,12 @@ class TableConstructor: public Constructor {
private:
void Reset() {
delete table_;
delete source_;
table_ = NULL;
source_ = NULL;
table_.reset();
source_.reset();
}
StringSource* source_;
Table* table_;
unique_ptr<StringSource> source_;
unique_ptr<Table> table_;
TableConstructor();
};