From 74f2b78c04ac189fd666ec5623d5c7ff69f2f343 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 6 Apr 2018 08:10:34 -0700 Subject: Refactor some potentially unsafe SharedPtr<> code. --- node/SharedPtr.hpp | 27 +++++++-------------------- node/Topology.cpp | 2 +- node/Topology.hpp | 2 +- 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/node/SharedPtr.hpp b/node/SharedPtr.hpp index aa03cf0b..2f0e50c9 100644 --- a/node/SharedPtr.hpp +++ b/node/SharedPtr.hpp @@ -76,8 +76,9 @@ public: * * @param ptr Naked pointer to assign */ - inline void setToUnsafe(T *ptr) + inline void set(T *ptr) { + zero(); ++ptr->__refCount; _ptr = ptr; } @@ -116,27 +117,13 @@ public: } /** - * Set this pointer to NULL if this is the only pointer holding the object - * - * @return True if object was deleted and SharedPtr is now NULL (or was already NULL) + * @return Number of references according to this object's ref count or 0 if NULL */ - inline bool reclaimIfWeak() + inline int references() { - if (_ptr) { - if (++_ptr->__refCount <= 2) { - if (--_ptr->__refCount <= 1) { - delete _ptr; - _ptr = (T *)0; - return true; - } else { - return false; - } - } else { - return false; - } - } else { - return true; - } + if (_ptr) + return _ptr->__refCount.load(); + return 0; } inline bool operator==(const SharedPtr &sp) const { return (_ptr == sp._ptr); } diff --git a/node/Topology.cpp b/node/Topology.cpp index 2c440d92..7c526b41 100644 --- a/node/Topology.cpp +++ b/node/Topology.cpp @@ -388,7 +388,7 @@ void Topology::doPeriodicTasks(void *tPtr,int64_t now) Path::HashKey *k = (Path::HashKey *)0; SharedPtr *p = (SharedPtr *)0; while (i.next(k,p)) { - if (p->reclaimIfWeak()) + if (p->references() <= 1) _paths.erase(*k); } } diff --git a/node/Topology.hpp b/node/Topology.hpp index 7f33e92b..63946a32 100644 --- a/node/Topology.hpp +++ b/node/Topology.hpp @@ -119,7 +119,7 @@ public: Mutex::Lock _l(_paths_m); SharedPtr &p = _paths[Path::HashKey(l,r)]; if (!p) - p.setToUnsafe(new Path(l,r)); + p.set(new Path(l,r)); return p; } -- cgit v1.2.3