2016-04-26 19:41:07 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2016-04-26 19:41:07 +00:00
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
#include <algorithm>
|
|
|
|
#include <memory>
|
2016-08-11 18:54:17 +00:00
|
|
|
#include <utility>
|
2016-04-26 19:41:07 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "table/internal_iterator.h"
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2016-04-26 19:41:07 +00:00
|
|
|
|
|
|
|
// PinnedIteratorsManager will be notified whenever we need to pin an Iterator
|
|
|
|
// and it will be responsible for deleting pinned Iterators when they are
|
|
|
|
// not needed anymore.
|
2016-12-29 23:48:24 +00:00
|
|
|
class PinnedIteratorsManager : public Cleanable {
|
2016-04-26 19:41:07 +00:00
|
|
|
public:
|
2016-08-11 18:54:17 +00:00
|
|
|
PinnedIteratorsManager() : pinning_enabled(false) {}
|
|
|
|
~PinnedIteratorsManager() {
|
|
|
|
if (pinning_enabled) {
|
|
|
|
ReleasePinnedData();
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 19:41:07 +00:00
|
|
|
|
|
|
|
// Enable Iterators pinning
|
|
|
|
void StartPinning() {
|
2016-08-11 18:54:17 +00:00
|
|
|
assert(pinning_enabled == false);
|
|
|
|
pinning_enabled = true;
|
2016-04-26 19:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Is pinning enabled ?
|
|
|
|
bool PinningEnabled() { return pinning_enabled; }
|
|
|
|
|
2016-08-11 18:54:17 +00:00
|
|
|
// Take ownership of iter and delete it when ReleasePinnedData() is called
|
|
|
|
void PinIterator(InternalIterator* iter, bool arena = false) {
|
|
|
|
if (arena) {
|
|
|
|
PinPtr(iter, &PinnedIteratorsManager::ReleaseArenaInternalIterator);
|
|
|
|
} else {
|
|
|
|
PinPtr(iter, &PinnedIteratorsManager::ReleaseInternalIterator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:31:12 +00:00
|
|
|
using ReleaseFunction = void (*)(void* arg1);
|
2016-08-11 18:54:17 +00:00
|
|
|
void PinPtr(void* ptr, ReleaseFunction release_func) {
|
|
|
|
assert(pinning_enabled);
|
|
|
|
if (ptr == nullptr) {
|
2016-04-26 19:41:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-08-11 18:54:17 +00:00
|
|
|
pinned_ptrs_.emplace_back(ptr, release_func);
|
2016-04-26 19:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release pinned Iterators
|
2016-08-11 18:54:17 +00:00
|
|
|
inline void ReleasePinnedData() {
|
|
|
|
assert(pinning_enabled == true);
|
|
|
|
pinning_enabled = false;
|
|
|
|
|
|
|
|
// Remove duplicate pointers
|
|
|
|
std::sort(pinned_ptrs_.begin(), pinned_ptrs_.end());
|
2016-12-29 02:25:50 +00:00
|
|
|
auto unique_end = std::unique(pinned_ptrs_.begin(), pinned_ptrs_.end());
|
2016-04-26 19:41:07 +00:00
|
|
|
|
2016-12-29 02:25:50 +00:00
|
|
|
for (auto i = pinned_ptrs_.begin(); i != unique_end; ++i) {
|
|
|
|
void* ptr = i->first;
|
|
|
|
ReleaseFunction release_func = i->second;
|
2016-08-11 18:54:17 +00:00
|
|
|
release_func(ptr);
|
2016-04-26 19:41:07 +00:00
|
|
|
}
|
2016-08-11 18:54:17 +00:00
|
|
|
pinned_ptrs_.clear();
|
2016-12-29 23:48:24 +00:00
|
|
|
// Also do cleanups from the base Cleanable
|
|
|
|
Cleanable::Reset();
|
2016-04-26 19:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-08-11 18:54:17 +00:00
|
|
|
static void ReleaseInternalIterator(void* ptr) {
|
|
|
|
delete reinterpret_cast<InternalIterator*>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ReleaseArenaInternalIterator(void* ptr) {
|
|
|
|
reinterpret_cast<InternalIterator*>(ptr)->~InternalIterator();
|
|
|
|
}
|
|
|
|
|
2016-04-26 19:41:07 +00:00
|
|
|
bool pinning_enabled;
|
2016-08-11 18:54:17 +00:00
|
|
|
std::vector<std::pair<void*, ReleaseFunction>> pinned_ptrs_;
|
2016-04-26 19:41:07 +00:00
|
|
|
};
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|