]>
git.saurik.com Git - apple/securityd.git/blob - src/structure.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // structure - structural framework for securityd objects
28 #include "structure.h"
32 // NodeCore always has a destructor (because it's virtual),
33 // but its dump support is conditionally included.
37 #if defined(DEBUGDUMP)
38 StLock
<Mutex
> _(mCoreLock
);
39 mCoreNodes
.erase(this);
45 // Basic object mesh maintainance
47 void NodeCore::parent(NodeCore
&p
)
49 StLock
<Mutex
> _(*this);
53 void NodeCore::referent(NodeCore
&r
)
55 StLock
<Mutex
> _(*this);
60 void NodeCore::clearReferent()
62 StLock
<Mutex
> _(*this);
64 assert(!mReferent
->hasReference(*this));
69 void NodeCore::addReference(NodeCore
&p
)
71 StLock
<Mutex
> _(*this);
72 assert(p
.mReferent
== this);
73 mReferences
.insert(&p
);
76 void NodeCore::removeReference(NodeCore
&p
)
78 StLock
<Mutex
> _(*this);
79 assert(hasReference(p
));
80 mReferences
.erase(&p
);
85 bool NodeCore::hasReference(NodeCore
&p
)
87 assert(p
.refCountForDebuggingOnly() > 0);
88 return mReferences
.find(&p
) != mReferences
.end();
95 // ClearReferences clears the reference set but does not propagate
96 // anything; it is NOT recursive.
98 void NodeCore::clearReferences()
100 StLock
<Mutex
> _(*this);
101 secdebug("ssnode", "%p clearing all %d references",
102 this, int(mReferences
.size()));
103 mReferences
.erase(mReferences
.begin(), mReferences
.end());
108 // Kill should be overloaded by Nodes to implement any cleanup and release
109 // operations that should happen at LOGICAL death of the represented object.
110 // This is where you should release ports, close files, etc.
111 // This default behavior, which you MUST include in your override,
112 // propagates kills to all active references, recursively.
114 void NodeCore::kill()
116 StLock
<Mutex
> _(*this);
117 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end(); it
++)
123 void NodeCore::kill(NodeCore
&ref
)
125 StLock
<Mutex
> _(*this);
126 assert(hasReference(ref
));
128 removeReference(ref
);
133 // NodeCore-level support for state dumping.
134 // Call NodeCore::dumpAll() to debug-dump all nodes.
135 // Note that enabling DEBUGDUMP serializes all node creation/destruction
136 // operations, and thus may cause significant shifts in thread interactions.
138 #if defined(DEBUGDUMP)
140 // The (uncounted) set of all known NodeCores in existence, with protective lock
141 set
<NodeCore
*> NodeCore::mCoreNodes
;
142 Mutex
NodeCore::mCoreLock
;
144 // add a new NodeCore to the known set
146 : Mutex(Mutex::recursive
)
148 StLock
<Mutex
> _(mCoreLock
);
149 mCoreNodes
.insert(this);
152 // partial-line common dump text for any NodeCore
153 // override this to add text to your Node type's state dump output
154 void NodeCore::dumpNode()
156 Debug::dump("%s@%p rc=%u", Debug::typeName(*this).c_str(), this, unsigned(refCountForDebuggingOnly()));
158 Debug::dump(" parent=%p", mParent
.get());
160 Debug::dump(" referent=%p", mReferent
.get());
163 // full-line dump of a NodeCore
164 // override this to completely re-implement the dump format for your Node type
165 void NodeCore::dump()
168 if (!mReferences
.empty()) {
170 for (ReferenceSet::const_iterator it
= mReferences
.begin(); it
!= mReferences
.end(); it
++) {
171 Debug::dump(" %p", it
->get());
172 if ((*it
)->mReferent
!= this)
173 Debug::dump("!*INVALID*");
180 // dump all known nodes
181 void NodeCore::dumpAll()
183 StLock
<Mutex
> _(mCoreLock
);
184 time_t now
; time(&now
);
185 Debug::dump("\nNODE DUMP (%24.24s)\n", ctime(&now
));
186 for (set
<NodeCore
*>::const_iterator it
= mCoreNodes
.begin(); it
!= mCoreNodes
.end(); it
++)
188 Debug::dump("END NODE DUMP\n\n");