Skip to content

Commit

Permalink
Clarify that `{{,Intrusive}SharedPtr,{,Sized}SharedBuffer}::IsUnique(…
Browse files Browse the repository at this point in the history
…)` return

`false` if there is no object.

Remove handling of `buffer_ == nullptr` in `SharedBuffer::mutable_data()`.
This is excluded by the precondition.

Cosmetics in `SharedBuffer` constructor: use initialization instead of
`Reset()`.

PiperOrigin-RevId: 643929600
  • Loading branch information
QrczakMK committed Jun 17, 2024
1 parent c2a7c7c commit f319a29
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions riegeli/base/intrusive_shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ class
// This can be used to check if the object may be modified (in contrast to
// `std::shared_ptr::unique()`).
//
// If `*this` is empty, returns `false`.
//
// Supported if `T` supports `HasUniqueOwner()`.
template <typename DependentT = T,
std::enable_if_t<intrusive_shared_ptr_internal::HasHasUniqueOwner<
Expand Down
22 changes: 12 additions & 10 deletions riegeli/base/shared_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ class SharedBuffer {
SharedBuffer(SharedBuffer&& that) = default;
SharedBuffer& operator=(SharedBuffer&& that) = default;

// Ensures at least `min_capacity` of space and unique ownership of the data.
// Existing contents are lost.
// Ensures at least `min_capacity` of space, and unique ownership of the data
// if `min_capacity > 0`. Existing contents are lost.
//
// Drops the allocation if the resulting capacity would be wasteful for
// `min_capacity`.
ABSL_ATTRIBUTE_REINITIALIZES void Reset(size_t min_capacity = 0);

// Returns `true` if `*this` is the only owner of the data.
//
// If `capacity() == 0`, returns `false`.
bool IsUnique() const;

// Returns the mutable data pointer.
Expand Down Expand Up @@ -108,20 +110,21 @@ class SharedBuffer {
}

private:
SharedPtr<Buffer> buffer_;
SharedPtr<Buffer> buffer_; // `nullptr` means `capacity() == 0`.
};

// Implementation details follow.

inline SharedBuffer::SharedBuffer(size_t min_capacity) {
if (min_capacity > 0) buffer_.Reset(riegeli::Maker(min_capacity));
}
inline SharedBuffer::SharedBuffer(size_t min_capacity)
: buffer_(min_capacity == 0
? nullptr
: SharedPtr<Buffer>(riegeli::Maker(min_capacity))) {}

inline void SharedBuffer::Reset(size_t min_capacity) {
if (min_capacity > 0) {
buffer_.Reset(riegeli::Maker(min_capacity));
} else {
if (min_capacity == 0) {
if (!buffer_.IsUnique()) buffer_.Reset();
} else {
buffer_.Reset(riegeli::Maker(min_capacity));
}
}

Expand All @@ -131,7 +134,6 @@ inline char* SharedBuffer::mutable_data() const {
RIEGELI_ASSERT(IsUnique())
<< "Failed precondition of SharedBuffer::mutable_data(): "
"ownership is shared";
if (buffer_ == nullptr) return nullptr;
return buffer_->data();
}

Expand Down
2 changes: 2 additions & 0 deletions riegeli/base/shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ class
//
// This can be used to check if the object may be modified (in contrast to
// `std::shared_ptr::unique()`).
//
// If `*this` is empty, returns `false`.
bool IsUnique() const {
return ptr_ != nullptr && ref_count(ptr_).HasUniqueOwner();
}
Expand Down
8 changes: 5 additions & 3 deletions riegeli/base/sized_shared_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class
ABSL_ATTRIBUTE_REINITIALIZES void Clear();

// Returns `true` if `*this` is the only owner of the data.
//
// If `capacity() == 0`, returns `false`.
bool IsUnique() const { return buffer_.IsUnique(); }

explicit operator absl::string_view() const {
Expand Down Expand Up @@ -181,9 +183,9 @@ class

SharedBuffer buffer_;
// Invariant:
// `(data_ >= buffer_.data() &&
// data_ + size_ <= buffer_.data() + buffer_.capacity()) ||
// (data_ == nullptr && size_ == 0)`
// `(data_ == nullptr && size_ == 0) ||
// (data_ >= buffer_.data() &&
// data_ + size_ <= buffer_.data() + buffer_.capacity())`
char* data_ = nullptr;
size_t size_ = 0;
};
Expand Down

0 comments on commit f319a29

Please sign in to comment.