From 3734251539d2192aafa938c530fa8eb45e48ecef Mon Sep 17 00:00:00 2001 From: Daniel Schemmel Date: Thu, 29 Jun 2023 01:08:14 +0000 Subject: Have CoWPtr::get and CoWPtr::getOwned return pointers instead of references --- include/klee/KDAlloc/suballocators/cow_ptr.h | 37 +++++++--------------- include/klee/KDAlloc/suballocators/sized_regions.h | 21 ++++++------ 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/include/klee/KDAlloc/suballocators/cow_ptr.h b/include/klee/KDAlloc/suballocators/cow_ptr.h index 20e12701..ce5957df 100644 --- a/include/klee/KDAlloc/suballocators/cow_ptr.h +++ b/include/klee/KDAlloc/suballocators/cow_ptr.h @@ -87,41 +87,26 @@ public: /// Accesses an existing object. /// Must not be called when `*this` is in an empty state. T const &operator*() const noexcept { - assert(ptr && "the `CoWPtr` must not be empty"); - return get(); + assert(!isEmpty() && "the `CoWPtr` must not be empty"); + return *get(); } /// Accesses an existing object. /// Must not be called when `*this` is in an empty state. T const *operator->() const noexcept { - assert(ptr && "the `CoWPtr` must not be empty"); - return &get(); - } - - /// Accesses an existing object. - /// Must not be called when `*this` is in an empty state. - T const &get() const noexcept { - assert(ptr && "the `CoWPtr` must not be empty"); - return ptr->data; + assert(!isEmpty() && "the `CoWPtr` must not be empty"); + return get(); } - /// Accesses an existing, owned object. - /// Must not be called when `*this` does not hold CoW ownership. - T &getOwned() noexcept { - assert(isOwned() && "the `CoWPtr` must be owned"); - return ptr->data; - } + /// Gets a pointer to the managed object. + /// Returns `nullptr` if no object is currently managed. + T const *get() const noexcept { return ptr ? &ptr->data : nullptr; } - /// Accesses an existing, owned object. + /// Gets a pointer to an existing, owned object. /// Must not be called when `*this` does not hold CoW ownership. - /// - /// Note: This function is included for completeness' sake. Instead of calling - /// `getOwned` on a constant, one should probably just call `get` as it - /// produces the same result without requiring the target object to hold - /// ownership of the data. - T const &getOwned() const noexcept { + T *getOwned() noexcept { assert(isOwned() && "the `CoWPtr` must be owned"); - return ptr->data; + return &ptr->data; } /// Acquires CoW ownership of an existing object and returns a reference to @@ -132,7 +117,7 @@ public: assert(ptr->referenceCount > 0); if (ptr->referenceCount > 1) { --ptr->referenceCount; - ptr = new Wrapper(*ptr); + ptr = new Wrapper{*ptr}; ptr->referenceCount = 1; } assert(ptr->referenceCount == 1); diff --git a/include/klee/KDAlloc/suballocators/sized_regions.h b/include/klee/KDAlloc/suballocators/sized_regions.h index bb2a0b69..d8dafa86 100644 --- a/include/klee/KDAlloc/suballocators/sized_regions.h +++ b/include/klee/KDAlloc/suballocators/sized_regions.h @@ -162,7 +162,7 @@ public: [[nodiscard]] std::size_t getSize(char const *const address) const noexcept { assert(root && "Cannot get size from an empty treap"); - Node const *currentNode = &*root; + Node const *currentNode = root.get(); Node const *closestPredecessor = nullptr; Node const *closestSuccessor = nullptr; while (currentNode) { @@ -170,12 +170,12 @@ public: assert(!closestSuccessor || currentNode->getBaseAddress() < closestSuccessor->getBaseAddress()); closestSuccessor = currentNode; - currentNode = &*currentNode->lhs; + currentNode = currentNode->lhs.get(); } else { assert(!closestPredecessor || currentNode->getBaseAddress() > closestPredecessor->getBaseAddress()); closestPredecessor = currentNode; - currentNode = &*currentNode->rhs; + currentNode = currentNode->rhs.get(); } } @@ -195,7 +195,7 @@ public: std::size_t const size) const noexcept { assert(root && "Cannot compute location info for an empty treap"); - Node const *currentNode = &*root; + Node const *currentNode = root.get(); Node const *closestPredecessor = nullptr; for (;;) { if (currentNode->getBaseAddress() <= address) { @@ -213,7 +213,7 @@ public: closestPredecessor->getBaseAddress() + closestPredecessor->getSize()}; } - currentNode = &*currentNode->rhs; + currentNode = currentNode->rhs.get(); } } else { assert(closestPredecessor && @@ -229,7 +229,7 @@ public: closestPredecessor->getBaseAddress() + closestPredecessor->getSize()}; } - currentNode = &*currentNode->lhs; + currentNode = currentNode->lhs.get(); } } } @@ -307,7 +307,7 @@ private: target = &targetNode.lhs; } - update.getOwned().rhs = std::move(rhs); + update.getOwned()->rhs = std::move(rhs); *target = std::move(update); } @@ -324,7 +324,7 @@ private: target = &targetNode.rhs; } - update.getOwned().lhs = std::move(lhs); + update.getOwned()->lhs = std::move(lhs); *target = std::move(update); } @@ -423,9 +423,8 @@ public: CoWPtr *closestSuccessor = nullptr; for (;;) { if (address < (*currentNode)->getBaseAddress()) { - assert(!closestSuccessor || - (*currentNode)->getBaseAddress() < - (*closestSuccessor)->getBaseAddress()); + assert(!closestSuccessor || (*currentNode)->getBaseAddress() < + (*closestSuccessor)->getBaseAddress()); closestSuccessor = currentNode; if ((*currentNode)->lhs) { currentNode = ¤tNode->acquire().lhs; -- cgit 1.4.1