Skip to content

Commit

Permalink
Make Address Sanitizer happy
Browse files Browse the repository at this point in the history
  • Loading branch information
mm304321141 committed Mar 31, 2021
1 parent 65f859b commit 2139145
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
15 changes: 4 additions & 11 deletions db/version_set.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,6 @@ class FilePicker {
};
} // anonymous namespace

VersionStorageInfo::~VersionStorageInfo() { delete[](files_ - 1); }

Version::~Version() {
assert(refs_ == 0);

Expand Down Expand Up @@ -1174,7 +1172,7 @@ VersionStorageInfo::VersionStorageInfo(
num_non_empty_levels_(0),
file_indexer_(user_comparator),
compaction_style_(compaction_style),
files_(new std::vector<FileMetaData*>[num_levels_ + 1]),
files_(num_levels_),
base_level_(num_levels_ == 1 ? -1 : 1),
level_multiplier_(0.0),
files_by_compaction_pri_(num_levels_),
Expand All @@ -1196,9 +1194,7 @@ VersionStorageInfo::VersionStorageInfo(
is_pick_compaction_fail(false),
is_pick_garbage_collection_fail(false),
force_consistency_checks_(_force_consistency_checks),
blob_marked_for_compaction_(false) {
++files_; // level -1 used for dependence files
}
blob_marked_for_compaction_(false) {}

Version::Version(ColumnFamilyData* column_family_data, VersionSet* vset,
const EnvOptions& env_opt,
Expand Down Expand Up @@ -4046,9 +4042,7 @@ Status VersionSet::ReduceNumberOfLevels(const std::string& dbname,
// we need to allocate an array with the old number of levels size to
// avoid SIGSEGV in WriteSnapshot()
// however, all levels bigger or equal to new_levels will be empty
std::vector<FileMetaData*>* new_files_list =
new std::vector<FileMetaData*>[current_levels + 1];
++new_files_list;
VersionStorageInfo::Files new_files_list(current_levels);
for (int i = -1; i < new_levels - 1; i++) {
new_files_list[i] = vstorage->LevelFiles(i);
}
Expand All @@ -4057,8 +4051,7 @@ Status VersionSet::ReduceNumberOfLevels(const std::string& dbname,
new_files_list[new_levels - 1] = vstorage->LevelFiles(first_nonempty_level);
}

delete[](vstorage->files_ - 1);
vstorage->files_ = new_files_list;
vstorage->files_ = std::move(new_files_list);
vstorage->num_levels_ = new_levels;

MutableCFOptions mutable_cf_options(*options);
Expand Down
53 changes: 52 additions & 1 deletion db/version_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,58 @@ class VersionStorageInfo {

// List of files per level, files in each level are arranged
// in increasing order of keys
std::vector<FileMetaData*>* files_;
struct Files {
Files(const Files&) = delete;
Files& operator=(const Files&) = delete;

#if __SANITIZE_ADDRESS__
std::vector<std::vector<FileMetaData*>> files;

Files(size_t num_levels) { files.resize(num_levels + 1); }
Files(Files&&) = default;
Files& operator=(Files&&) = default;

std::vector<FileMetaData*>& operator[](int l) { return files[l + 1]; }
const std::vector<FileMetaData*>& operator[](int l) const {
return files[l + 1];
}

operator std::vector<FileMetaData*> const *() const {
return files.data() + 1;
}
#else
std::vector<FileMetaData*>* files;
Files(size_t num_levels)
: files(new std::vector<FileMetaData*>[num_levels + 1]) {}
Files(Files&& other) {
files = other.files;
other.files = nullptr;
}
Files& operator=(Files&& other) {
if (this != &other) {
if (files != nullptr) {
delete[](files - 1);
}
files = other.files;
other.files = nullptr;
}
return *this
}
~Files() {
if (files != nullptr) {
delete[](files - 1);
}
}

std::vector<FileMetaData*>& operator[](int l) { return files[l]; }
const std::vector<FileMetaData*>& operator[](int l) const {
return files[l];
}

operator std::vector<FileMetaData*> const *() const { return files; }
#endif
};
Files files_;

// Dependence files both in files[-1] and dependence_map
DependenceMap dependence_map_;
Expand Down

0 comments on commit 2139145

Please sign in to comment.