Skip to content

Commit

Permalink
Add SharedPtr, an efficient non-intrusive shared pointer.
Browse files Browse the repository at this point in the history
Rename `RefCountedPtr` to `IntrusiveSharedPtr` to highlight a relationship to
`SharedPtr`.

Migrate most usages of `IntrusiveSharedPtr` to `SharedPtr`.

Deprecate `RefCountedBase`. It has no more usages in Riegeli. Almost all usages
of `RefCountedPtr` which rely on `RefCountedBase` for intrusive reference
counting can be migrated to `SharedPtr` instead of `IntrusiveSharedPtr`.

Split `intrusive_ref_count.h` to `ref_count.h`, `shared_ptr.h`, and
`intrusive_shared_ptr.h`.

Rename member functions from std style to Google style:
* `reset()` → `Reset()`
* `release()` → `Release()`
* `has_unique_owner()` → `HasUniqueOwner()`

Deprecate `MakeRefCounted()`. Construct the object held by
`{,Intrusive}SharedPtr` using a constructor from `Initializer`. As a bonus,
`Reset()` from `Initializer` can assign to an existing object if possible.

Minor changes to the API:
* Add a direct definition of `IntrusiveSharedPtr::operator=(std::nullptr_t)` to
  complement the implicit conversion.
* Add `IntrusiveSharedPtr::IsUnique()` for consistency with `SharedPtr`.
* Remove `IntrusiveSharedPtr::exchange()`. `std::exchange()` is equally
  efficient.
* Support heterogeneous equality.
* Rename `{,Sized}SharedBuffer::has_unique_owner()` to `IsUnique()`.

In compression dictionaries and `CsvHeader`, simplify code by using
`Initializer<std::string>::AllowingExplicit` instead of pairs of overloads for
`absl::string_view` and `std::string&&` (the latter as a template to avoid
ambiguity).

PiperOrigin-RevId: 629405077
  • Loading branch information
QrczakMK committed Apr 30, 2024
1 parent 2b784fe commit 9597fd0
Show file tree
Hide file tree
Showing 32 changed files with 1,087 additions and 666 deletions.
28 changes: 24 additions & 4 deletions riegeli/base/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,30 @@ cc_library(
)

cc_library(
name = "intrusive_ref_count",
hdrs = ["intrusive_ref_count.h"],
name = "shared_ptr",
hdrs = [
"intrusive_shared_ptr.h",
"ref_count.h",
"shared_ptr.h",
],
deps = [
":arithmetic",
":assert",
":compare",
":initializer",
":memory_estimator",
":new_aligned",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/meta:type_traits",
],
)

cc_library(
name = "intrusive_ref_count",
hdrs = ["intrusive_ref_count.h"],
deps = [
":initializer",
":shared_ptr",
"@com_google_absl//absl/base:core_headers",
],
)
Expand Down Expand Up @@ -368,7 +387,8 @@ cc_library(
":buffer",
":buffering",
":cord_utils",
":intrusive_ref_count",
":initializer",
":shared_ptr",
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
Expand Down Expand Up @@ -402,11 +422,11 @@ cc_library(
":compare",
":cord_utils",
":initializer",
":intrusive_ref_count",
":memory_estimator",
":new_aligned",
":no_destructor",
":shared_buffer",
":shared_ptr",
":sized_shared_buffer",
":string_utils",
":zeros",
Expand Down
6 changes: 3 additions & 3 deletions riegeli/base/chain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "riegeli/base/buffering.h"
#include "riegeli/base/compare.h"
#include "riegeli/base/cord_utils.h"
#include "riegeli/base/intrusive_ref_count.h"
#include "riegeli/base/intrusive_shared_ptr.h"
#include "riegeli/base/maker.h"
#include "riegeli/base/memory_estimator.h"
#include "riegeli/base/new_aligned.h"
Expand Down Expand Up @@ -302,7 +302,7 @@ class Chain::BlockRef {
}

private:
RefCountedPtr<RawBlock> block_;
IntrusiveSharedPtr<RawBlock> block_;
};

template <Chain::Ownership ownership>
Expand All @@ -319,7 +319,7 @@ inline Chain::BlockRef::BlockRef(RawBlock* block,
block = target;
}
if (ownership == Ownership::kShare) block->Ref();
block_.reset(block);
block_.Reset(block);
}

void Chain::BlockRef::DumpStructure(absl::string_view data,
Expand Down
13 changes: 8 additions & 5 deletions riegeli/base/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@
#include "riegeli/base/buffering.h"
#include "riegeli/base/compare.h"
#include "riegeli/base/initializer.h"
#include "riegeli/base/intrusive_ref_count.h"
#include "riegeli/base/intrusive_shared_ptr.h"
#include "riegeli/base/memory_estimator.h"
#include "riegeli/base/new_aligned.h"
#include "riegeli/base/ref_count.h"
#include "riegeli/base/sized_shared_buffer.h"

namespace riegeli {
Expand Down Expand Up @@ -828,8 +829,8 @@ class Chain::PinnedBlock {

private:
friend class BlockIterator;
explicit PinnedBlock(RawBlock* block) : block_(block) {}
RefCountedPtr<RawBlock> block_;
explicit PinnedBlock(RawBlock* block);
IntrusiveSharedPtr<RawBlock> block_;
};

class Chain::Blocks {
Expand Down Expand Up @@ -1334,7 +1335,7 @@ void Chain::RawBlock::Unref() {
}

inline bool Chain::RawBlock::has_unique_owner() const {
return ref_count_.has_unique_owner();
return ref_count_.HasUniqueOwner();
}

inline size_t Chain::RawBlock::capacity() const {
Expand Down Expand Up @@ -1558,6 +1559,8 @@ inline Chain::PinnedBlock Chain::BlockIterator::Pin() {
return PinnedBlock(PinImpl());
}

inline Chain::PinnedBlock::PinnedBlock(RawBlock* block) : block_(block) {}

inline absl::string_view Chain::PinnedBlock::operator*() const {
RIEGELI_ASSERT(block_ != nullptr)
<< "Failed precondition of Chain::PinnedBlock::operator*: null pointer";
Expand All @@ -1579,7 +1582,7 @@ inline void* Chain::PinnedBlock::Share() const& {
inline void* Chain::PinnedBlock::Share() && {
RIEGELI_ASSERT(block_ != nullptr)
<< "Failed precondition of Chain::PinnedBlock::Share(): null pointer";
return block_.release();
return block_.Release();
}

inline void Chain::PinnedBlock::DeleteShared(void* ptr) {
Expand Down
Loading

0 comments on commit 9597fd0

Please sign in to comment.