From 0abccb029e55326f9de99563e38ed4c9a87eb4d1 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Mon, 5 Jan 2015 00:14:57 +0100 Subject: Each Managed object now has an unique id that can be used as weak reference. --- src/core/managed/Manager.cpp | 27 +++++++++++++++++++++-- src/core/managed/Manager.hpp | 51 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/core/managed/Manager.cpp b/src/core/managed/Manager.cpp index 823dc88..d92bbb5 100644 --- a/src/core/managed/Manager.cpp +++ b/src/core/managed/Manager.cpp @@ -154,7 +154,9 @@ void Manager::manage(Managed *o) #ifdef MANAGER_DEBUG_PRINT std::cout << "manage " << o << std::endl; #endif - objects.emplace(std::make_pair(o, ObjectDescriptor{})); + objects.emplace(o, ObjectDescriptor{nextUid}); + uids.emplace(nextUid, o); + nextUid++; } void Manager::addRef(Managed *tar, Managed *src) @@ -257,7 +259,8 @@ void Manager::deleteObject(Managed *o, ObjectDescriptor *descr) deleteRef(descr->refOut.begin()->first, o, true); } - // Remove the data store and the event store entry + // Remove the uid, data and event store entry + uids.erase(descr->uid); store.erase(o); events.erase(o); @@ -370,6 +373,26 @@ void Manager::sweep() } } +/* Class Managed: Unique IDs */ + +ManagedUid Manager::getUid(Managed *o) +{ + const auto it = objects.find(o); + if (it != objects.end()) { + return it->second.uid; + } + return 0; +} + +Managed *Manager::getManaged(ManagedUid uid) +{ + const auto it = uids.find(uid); + if (it != uids.end()) { + return it->second; + } + return nullptr; +} + /* Class Manager: Attached data */ void Manager::storeData(Managed *ref, const std::string &key, Managed *data) diff --git a/src/core/managed/Manager.hpp b/src/core/managed/Manager.hpp index 8ee11c4..eb4594a 100644 --- a/src/core/managed/Manager.hpp +++ b/src/core/managed/Manager.hpp @@ -47,6 +47,8 @@ namespace ousia { // Forward declaration class Managed; +using ManagedUid = uint64_t; + /** * The Manager class implements tracing garbage collection. Garbage Collection * is implemented as a simple directed reference graph with connected component @@ -70,6 +72,12 @@ public: */ struct ObjectDescriptor { public: + /** + * Unique ID assigned to the object. Valid unique ids are positive, + * non-zero values. + */ + const ManagedUid uid; + /** * Contains the number of references to rooted handles. A managed * objects @@ -94,7 +102,14 @@ public: /** * Default constructor of the ObjectDescriptor class. */ - ObjectDescriptor() : rootRefCount(0){}; + ObjectDescriptor() : uid(0), rootRefCount(0) {}; + + /** + * Creates a new ObjectDescriptor with the given unique id. + * + * @param uid is the unique id to be stored. + */ + ObjectDescriptor(ManagedUid uid) : uid(uid), rootRefCount(0) {}; /** * Returns true, if the ObjectDescriptor has at least one input @@ -146,12 +161,23 @@ private: */ const size_t threshold; + /** + * Next UID being assigned to the next object for which the "manage" + * function is called. + */ + ManagedUid nextUid = 1; + /** * Map used to store the descriptors for all managed objects. Every object * that has at least one root, in or out reference has an entry in this map. */ std::unordered_map objects; + /** + * Map from Uids to Managed pointers. + */ + std::unordered_map uids; + /** * Set containing the objects marked for sweeping. */ @@ -275,6 +301,29 @@ public: */ void sweep(); + /* Unique IDs */ + + /** + * Returns the unique identifier (UID) of the given object. Valid UIDs are + * positive non-zero values. A value of zero indicates that the given object + * does no longer exists or was not registered in the manager instance. + * + * @param o is a pointer to the managed object for which the UID should be + * returned. + * @return the unique id of the object or zero if the object does not exist. + */ + ManagedUid getUid(Managed *o); + + /** + * Returns a pointer to the given managed object or nullptr if the object + * no longer exists. This behaviour can be used to implement weak + * references. + * + * @param uid is the unique id for which the object should be returned. + * @return a pointer to the object with the given uid. + */ + Managed * getManaged(ManagedUid uid); + /* Data storage */ /** -- cgit v1.2.3