Skip to content

Commit 3b0afae

Browse files
committed
Add more comments
1 parent bdc8295 commit 3b0afae

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

paddle/framework/details/cow_ptr.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ class ThreadUnsafeOwnershipFlags {
2525
public:
2626
ThreadUnsafeOwnershipFlags(bool flag) : flag_(flag) {}
2727

28-
ThreadUnsafeOwnershipFlags(const ThreadUnsafeOwnershipFlags& o) = delete;
29-
ThreadUnsafeOwnershipFlags& operator=(const ThreadUnsafeOwnershipFlags& o) =
30-
delete;
31-
ThreadUnsafeOwnershipFlags(ThreadUnsafeOwnershipFlags&& o) = default;
28+
ThreadUnsafeOwnershipFlags(const ThreadUnsafeOwnershipFlags& other) = delete;
29+
ThreadUnsafeOwnershipFlags& operator=(
30+
const ThreadUnsafeOwnershipFlags& other) = delete;
31+
ThreadUnsafeOwnershipFlags(ThreadUnsafeOwnershipFlags&& other) = default;
3232

3333
void SetOwnership(bool flag) { flag_ = flag; }
3434

35+
// Invoke the callback if it is not owned.
3536
template <typename Callback>
3637
void AcquireOwnershipOnce(Callback acquire) {
3738
if (!flag_) {
@@ -44,48 +45,51 @@ class ThreadUnsafeOwnershipFlags {
4445
bool flag_;
4546
};
4647

47-
// Copy On Write pointer.
48+
// Copy-On-Write pointer.
4849
// It will hold a T* pointer, and only copy once when `MutableData` is invoked.
4950
//
5051
// The template parameter OwnershipFlags should have:
5152
// * a constructor takes a bool. True if own.
5253
// * SetOwnership(bool flag).
5354
// * AcquireOwnershipOnce(Callback). It will invoke the callback if it is not
5455
// owned.
56+
//
57+
// https://en.wikipedia.org/wiki/Copy-on-write
5558
template <typename T, typename OwnershipFlags = ThreadUnsafeOwnershipFlags>
5659
class COWPtr {
5760
public:
5861
// Ctor from raw pointer.
5962
explicit COWPtr(T* ptr) : payload_(ptr), ownership_{true} {}
6063

6164
// Move methods. Steal ownership from origin
62-
COWPtr(COWPtr&& o)
63-
: payload_(o.payload_), ownership_{std::move(o.ownership_)} {}
65+
COWPtr(COWPtr&& other)
66+
: payload_(other.payload_), ownership_{std::move(other.ownership_)} {}
6467
COWPtr& operator=(COWPtr&& origin) = default;
6568

6669
// Copy methods. Not own payload
67-
COWPtr(const COWPtr& o) : payload_(o.payload_), ownership_{false} {}
68-
COWPtr& operator=(const COWPtr& o) {
69-
payload_ = o.payload_;
70+
COWPtr(const COWPtr& other) : payload_(other.payload_), ownership_{false} {}
71+
COWPtr& operator=(const COWPtr& other) {
72+
payload_ = other.payload_;
7073
ownership_.SetOwnership(false);
7174
return *this;
7275
}
7376

77+
// Access read only data.
7478
const T& Data() const { return *payload_; }
7579

80+
// Access mutable data. If the data is not owned, the data will be copied
81+
// before.
7682
T* MutableData() {
7783
ownership_.AcquireOwnershipOnce(
7884
[this] { payload_.reset(new T(*payload_)); });
7985
return payload_.get();
8086
}
8187

82-
void Reset() {
83-
ownership_.AcquireOwnershipOnce([this] { payload_.reset(); });
84-
payload_.reset(new T());
85-
}
86-
8788
private:
89+
// Actual data pointer.
8890
std::shared_ptr<T> payload_;
91+
92+
// Ownership flag.
8993
OwnershipFlags ownership_;
9094
};
9195

paddle/framework/details/cow_ptr_test.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ TEST(COWPtr, all) {
2828
*ptr2.MutableData() = 10;
2929
ASSERT_EQ(ptr.Data(), 0);
3030
ASSERT_EQ(ptr2.Data(), 10);
31-
32-
auto ptr_before = ptr2.MutableData();
33-
ptr2.Reset();
34-
ASSERT_NE(ptr2.MutableData(), ptr_before);
3531
}
3632

3733
} // namespace details

0 commit comments

Comments
 (0)