assert() is traditionally disabled in release builds, but not in
scylladb. This hasn't caused problems so far, but the latest abseil
release includes a commit [1] that causes a 1000 insn/op regression when
NDEBUG is not defined.
Clearly, we must move towards a build system where NDEBUG is defined in
release builds. But we can't just define it blindly without vetting
all the assert() calls, as some were written with the expectation that
they are enabled in release mode.
To solve the conundrum, change all assert() calls to a new SCYLLA_ASSERT()
macro in utils/assert.hh. This macro is always defined and is not conditional
on NDEBUG, so we can later (after vetting Seastar) enable NDEBUG in release
mode.
[1] 66ef711d68
Closes scylladb/scylladb#20006
100 lines
2.6 KiB
C++
100 lines
2.6 KiB
C++
/*
|
|
* Copyright (C) 2020 ScyllaDB
|
|
*/
|
|
|
|
/*
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include "utils/assert.hh"
|
|
#include <boost/intrusive/parent_from_member.hpp>
|
|
#include <assert.h>
|
|
|
|
// A movable pointer-like object paired with exactly one other object of the same type.
|
|
// The two objects which are paired with each other point at each other.
|
|
// The relationship is symmetrical.
|
|
//
|
|
// A pair of such objects can be used for implementing bi-directional traversal in data structures.
|
|
//
|
|
// Moving this object automatically updates the other reference, so the references remain
|
|
// consistent when the containing objects are managed by LSA.
|
|
//
|
|
// get()
|
|
// ------------------>
|
|
// ----------- -----------
|
|
// | entangled |~~~~~~~~~~~| entangled |
|
|
// ----------- -----------
|
|
// <------------------
|
|
// get()
|
|
//
|
|
class entangled final {
|
|
entangled* _ref = nullptr;
|
|
private:
|
|
struct init_tag {};
|
|
entangled(init_tag, entangled& other) {
|
|
SCYLLA_ASSERT(!other._ref);
|
|
_ref = &other;
|
|
other._ref = this;
|
|
}
|
|
public:
|
|
// Creates a new object which is paired with a given "other".
|
|
// The other is also paired with this object afterwards.
|
|
// The other must not be paired before the call.
|
|
static entangled make_paired_with(entangled& other) {
|
|
return entangled(init_tag(), other);
|
|
}
|
|
|
|
// Creates an unpaired object.
|
|
entangled() = default;
|
|
entangled(const entangled&) = delete;
|
|
|
|
entangled(entangled&& other) noexcept
|
|
: _ref(other._ref)
|
|
{
|
|
if (_ref) {
|
|
_ref->_ref = this;
|
|
}
|
|
other._ref = nullptr;
|
|
}
|
|
|
|
~entangled() {
|
|
if (_ref) {
|
|
_ref->_ref = nullptr;
|
|
}
|
|
}
|
|
|
|
entangled& operator=(entangled&& other) noexcept {
|
|
if (_ref) {
|
|
_ref->_ref = nullptr;
|
|
}
|
|
_ref = other._ref;
|
|
if (_ref) {
|
|
_ref->_ref = this;
|
|
}
|
|
other._ref = nullptr;
|
|
return *this;
|
|
}
|
|
|
|
entangled* get() { return _ref; }
|
|
const entangled* get() const { return _ref; }
|
|
explicit operator bool() const { return _ref != nullptr; }
|
|
|
|
template<typename T>
|
|
T* get(entangled T::* paired_member) {
|
|
if (!_ref) {
|
|
return nullptr;
|
|
}
|
|
return boost::intrusive::get_parent_from_member(get(), paired_member);
|
|
}
|
|
|
|
template<typename T>
|
|
const T* get(entangled T::* paired_member) const {
|
|
if (!_ref) {
|
|
return nullptr;
|
|
}
|
|
return boost::intrusive::get_parent_from_member(get(), paired_member);
|
|
}
|
|
};
|