]> git.saurik.com Git - apple/securityd.git/blob - src/structure.cpp
4d6dd364edf5eaaf4e032a3c76017e5adb65365d
[apple/securityd.git] / src / structure.cpp
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26
27 //
28 // structure - structural framework for securityd objects
29 //
30 #include "structure.h"
31
32
33 //
34 // NodeCore always has a destructor (because it's virtual),
35 // but its dump support is conditionally included.
36 //
37 NodeCore::~NodeCore()
38 {
39 #if defined(DEBUGDUMP)
40 StLock<Mutex> _(mCoreLock);
41 mCoreNodes.erase(this);
42 #endif //DEBUGDUMP
43 }
44
45
46 //
47 // ClearReferences clears the reference set but does not propagate
48 // anything; it is NOT recursive.
49 //
50 void NodeCore::clearReferences()
51 {
52 StLock<Mutex> _(*this);
53 secdebug("ssnode", "%p clearing all %d references",
54 this, int(mReferences.size()));
55 mReferences.erase(mReferences.begin(), mReferences.end());
56 }
57
58
59 //
60 // Kill should be overloaded by Nodes to implement any cleanup and release
61 // operations that should happen at LOGICAL death of the represented object.
62 // This is where you should release ports, close files, etc.
63 // This default behavior, which you MUST include in your override,
64 // propagates kills to all active references, recursively.
65 //
66 void NodeCore::kill()
67 {
68 StLock<Mutex> _(*this);
69 for (ReferenceSet::const_iterator it = mReferences.begin(); it != mReferences.end(); it++)
70 (*it)->kill();
71 clearReferences();
72 }
73
74
75 //
76 // NodeCore-level support for state dumping.
77 // Call NodeCore::dumpAll() to debug-dump all nodes.
78 // Note that enabling DEBUGDUMP serializes all node creation/destruction
79 // operations, and thus may cause significant shifts in thread interactions.
80 //
81 #if defined(DEBUGDUMP)
82
83 // The (uncounted) set of all known NodeCores in existence, with protective lock
84 set<NodeCore *> NodeCore::mCoreNodes;
85 Mutex NodeCore::mCoreLock;
86
87 // add a new NodeCore to the known set
88 NodeCore::NodeCore()
89 : Mutex(Mutex::recursive)
90 {
91 StLock<Mutex> _(mCoreLock);
92 mCoreNodes.insert(this);
93 }
94
95 // partial-line common dump text for any NodeCore
96 // override this to add text to your Node type's state dump output
97 void NodeCore::dumpNode()
98 {
99 Debug::dump("%s@%p rc=%u", Debug::typeName(*this).c_str(), this, unsigned(refCountForDebuggingOnly()));
100 if (mParent)
101 Debug::dump(" parent=%p", mParent.get());
102 if (mReferent)
103 Debug::dump(" referent=%p", mReferent.get());
104 }
105
106 // full-line dump of a NodeCore
107 // override this to completely re-implement the dump format for your Node type
108 void NodeCore::dump()
109 {
110 dumpNode();
111 if (!mReferences.empty()) {
112 Debug::dump(" {");
113 for (ReferenceSet::const_iterator it = mReferences.begin(); it != mReferences.end(); it++)
114 Debug::dump(" %p", it->get());
115 Debug::dump(" }");
116 }
117 Debug::dump("\n");
118 }
119
120 // dump all known nodes
121 void NodeCore::dumpAll()
122 {
123 StLock<Mutex> _(mCoreLock);
124 time_t now; time(&now);
125 Debug::dump("\nNODE DUMP (%24.24s)\n", ctime(&now));
126 for (set<NodeCore *>::const_iterator it = mCoreNodes.begin(); it != mCoreNodes.end(); it++)
127 (*it)->dump();
128 Debug::dump("END NODE DUMP\n\n");
129 }
130
131 #endif //DEBUGDUMP