GetSnapshot() and ReleaseSnapshot() to move new and free out of DB mutex

Summary: We currently issue malloc and free inside DB mutex in GetSnapshot() and ReleaseSnapshot(). Move them out.

Test Plan:
Go through all tests
make valgrind_check

Reviewers: yhchiang, rven, IslamAbdelRahman, anthony, igor

Reviewed By: igor

Subscribers: maykov, hermanlee4, MarkCallaghan, yoshinorim, leveldb, dhruba

Differential Revision: https://reviews.facebook.net/D39753
main
sdong 10 years ago
parent 643bbbf081
commit d8c8f08c12
  1. 16
      db/db_impl.cc
  2. 6
      db/snapshot.h

@ -3212,16 +3212,24 @@ Status DBImpl::NewIterators(
const Snapshot* DBImpl::GetSnapshot() { const Snapshot* DBImpl::GetSnapshot() {
int64_t unix_time = 0; int64_t unix_time = 0;
env_->GetCurrentTime(&unix_time); // Ignore error env_->GetCurrentTime(&unix_time); // Ignore error
SnapshotImpl* s = new SnapshotImpl;
InstrumentedMutexLock l(&mutex_); InstrumentedMutexLock l(&mutex_);
// returns null if the underlying memtable does not support snapshot. // returns null if the underlying memtable does not support snapshot.
if (!is_snapshot_supported_) return nullptr; if (!is_snapshot_supported_) {
return snapshots_.New(versions_->LastSequence(), unix_time); delete s;
return nullptr;
}
return snapshots_.New(s, versions_->LastSequence(), unix_time);
} }
void DBImpl::ReleaseSnapshot(const Snapshot* s) { void DBImpl::ReleaseSnapshot(const Snapshot* s) {
InstrumentedMutexLock l(&mutex_); const SnapshotImpl* casted_s = reinterpret_cast<const SnapshotImpl*>(s);
snapshots_.Delete(reinterpret_cast<const SnapshotImpl*>(s)); {
InstrumentedMutexLock l(&mutex_);
snapshots_.Delete(casted_s);
}
delete casted_s;
} }
// Convenience methods // Convenience methods

@ -49,8 +49,8 @@ class SnapshotList {
SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; } SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; }
SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; } SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; }
const SnapshotImpl* New(SequenceNumber seq, uint64_t unix_time) { const SnapshotImpl* New(SnapshotImpl* s, SequenceNumber seq,
SnapshotImpl* s = new SnapshotImpl; uint64_t unix_time) {
s->number_ = seq; s->number_ = seq;
s->unix_time_ = unix_time; s->unix_time_ = unix_time;
s->list_ = this; s->list_ = this;
@ -62,12 +62,12 @@ class SnapshotList {
return s; return s;
} }
// Do not responsible to free the object.
void Delete(const SnapshotImpl* s) { void Delete(const SnapshotImpl* s) {
assert(s->list_ == this); assert(s->list_ == this);
s->prev_->next_ = s->next_; s->prev_->next_ = s->next_;
s->next_->prev_ = s->prev_; s->next_->prev_ = s->prev_;
count_--; count_--;
delete s;
} }
// retrieve all snapshot numbers. They are sorted in ascending order. // retrieve all snapshot numbers. They are sorted in ascending order.

Loading…
Cancel
Save