]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSObject.cpp
xnu-792.21.3.tar.gz
[apple/xnu.git] / libkern / c++ / OSObject.cpp
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* OSObject.cpp created by gvdl on Fri 1998-11-17 */
29
30 #include <libkern/c++/OSObject.h>
31 #include <libkern/c++/OSSerialize.h>
32 #include <libkern/c++/OSLib.h>
33 #include <libkern/c++/OSCPPDebug.h>
34 #include <libkern/OSAtomic.h>
35
36 #include <libkern/c++/OSCollection.h>
37
38 __BEGIN_DECLS
39 int debug_ivars_size;
40 __END_DECLS
41
42 #if OSALLOCDEBUG
43 #define ACCUMSIZE(s) do { debug_ivars_size += (s); } while(0)
44 #else
45 #define ACCUMSIZE(s)
46 #endif
47
48 // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
49 /* Class global data */
50 OSObject::MetaClass OSObject::gMetaClass;
51 const OSMetaClass * const OSObject::metaClass = &OSObject::gMetaClass;
52 const OSMetaClass * const OSObject::superClass = 0;
53
54 /* Class member functions - Can't use defaults */
55 OSObject::OSObject() { retainCount = 1; }
56 OSObject::OSObject(const OSMetaClass *) { retainCount = 1; }
57 OSObject::~OSObject() { }
58 const OSMetaClass * OSObject::getMetaClass() const
59 { return &gMetaClass; }
60 OSObject *OSObject::MetaClass::alloc() const { return 0; }
61
62 /* The OSObject::MetaClass constructor */
63 OSObject::MetaClass::MetaClass()
64 : OSMetaClass("OSObject", OSObject::superClass, sizeof(OSObject))
65 { }
66
67 // Virtual Padding
68 OSMetaClassDefineReservedUnused(OSObject, 0);
69 OSMetaClassDefineReservedUnused(OSObject, 1);
70 OSMetaClassDefineReservedUnused(OSObject, 2);
71 OSMetaClassDefineReservedUnused(OSObject, 3);
72 OSMetaClassDefineReservedUnused(OSObject, 4);
73 OSMetaClassDefineReservedUnused(OSObject, 5);
74 OSMetaClassDefineReservedUnused(OSObject, 6);
75 OSMetaClassDefineReservedUnused(OSObject, 7);
76 OSMetaClassDefineReservedUnused(OSObject, 8);
77 OSMetaClassDefineReservedUnused(OSObject, 9);
78 OSMetaClassDefineReservedUnused(OSObject, 10);
79 OSMetaClassDefineReservedUnused(OSObject, 11);
80 OSMetaClassDefineReservedUnused(OSObject, 12);
81 OSMetaClassDefineReservedUnused(OSObject, 13);
82 OSMetaClassDefineReservedUnused(OSObject, 14);
83 OSMetaClassDefineReservedUnused(OSObject, 15);
84 OSMetaClassDefineReservedUnused(OSObject, 16);
85 OSMetaClassDefineReservedUnused(OSObject, 17);
86 OSMetaClassDefineReservedUnused(OSObject, 18);
87 OSMetaClassDefineReservedUnused(OSObject, 19);
88 OSMetaClassDefineReservedUnused(OSObject, 20);
89 OSMetaClassDefineReservedUnused(OSObject, 21);
90 OSMetaClassDefineReservedUnused(OSObject, 22);
91 OSMetaClassDefineReservedUnused(OSObject, 23);
92 OSMetaClassDefineReservedUnused(OSObject, 24);
93 OSMetaClassDefineReservedUnused(OSObject, 25);
94 OSMetaClassDefineReservedUnused(OSObject, 26);
95 OSMetaClassDefineReservedUnused(OSObject, 27);
96 OSMetaClassDefineReservedUnused(OSObject, 28);
97 OSMetaClassDefineReservedUnused(OSObject, 29);
98 OSMetaClassDefineReservedUnused(OSObject, 30);
99 OSMetaClassDefineReservedUnused(OSObject, 31);
100
101 static const char *getClassName(const OSObject *obj)
102 {
103 const OSMetaClass *meta = obj->getMetaClass();
104 return (meta) ? meta->getClassName() : "unknown class?";
105 }
106
107 bool OSObject::init()
108 { return true; }
109
110 #if (!__ppc__) || (__GNUC__ < 3)
111
112 // Implemented in assembler in post gcc 3.x systems as we have a problem
113 // where the destructor in gcc2.95 gets 2 arguments. The second argument
114 // appears to be a flag argument. I have copied the assembler from Puma xnu
115 // to OSRuntimeSupport.c So for 2.95 builds use the C
116 void OSObject::free()
117 {
118 const OSMetaClass *meta = getMetaClass();
119
120 if (meta)
121 meta->instanceDestructed();
122 delete this;
123 }
124 #endif /* (!__ppc__) || (__GNUC__ < 3) */
125
126 int OSObject::getRetainCount() const
127 {
128 return (int) ((UInt16) retainCount);
129 }
130
131 void OSObject::taggedRetain(const void *tag) const
132 {
133 volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
134 UInt32 inc = 1;
135 UInt32 origCount;
136 UInt32 newCount;
137
138 // Increment the collection bucket.
139 if ((const void *) OSTypeID(OSCollection) == tag)
140 inc |= (1UL<<16);
141
142 do {
143 origCount = *countP;
144 if ( ((UInt16) origCount | 0x1) == 0xffff ) {
145 const char *msg;
146 if (origCount & 0x1) {
147 // If count == 0xffff that means we are freeing now so we can
148 // just return obviously somebody is cleaning up dangling
149 // references.
150 msg = "Attempting to retain a freed object";
151 }
152 else {
153 // If count == 0xfffe then we have wrapped our reference count.
154 // We should stop counting now as this reference must be
155 // leaked rather than accidently wrapping around the clock and
156 // freeing a very active object later.
157
158 #if !DEBUG
159 break; // Break out of update loop which pegs the reference
160 #else DEBUG
161 // @@@ gvdl: eventually need to make this panic optional
162 // based on a boot argument i.e. debug= boot flag
163 msg = "About to wrap the reference count, reference leak?";
164 #endif /* !DEBUG */
165 }
166 panic("OSObject::refcount: %s", msg);
167 }
168
169 newCount = origCount + inc;
170 } while (!OSCompareAndSwap(origCount, newCount, (UInt32 *) countP));
171 }
172
173 void OSObject::taggedRelease(const void *tag) const
174 {
175 taggedRelease(tag, 1);
176 }
177
178 void OSObject::taggedRelease(const void *tag, const int when) const
179 {
180 volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
181 UInt32 dec = 1;
182 UInt32 origCount;
183 UInt32 newCount;
184 UInt32 actualCount;
185
186 // Increment the collection bucket.
187 if ((const void *) OSTypeID(OSCollection) == tag)
188 dec |= (1UL<<16);
189
190 do {
191 origCount = *countP;
192
193 if ( ((UInt16) origCount | 0x1) == 0xffff ) {
194 if (origCount & 0x1) {
195 // If count == 0xffff that means we are freeing now so we can
196 // just return obviously somebody is cleaning up some dangling
197 // references. So we blow out immediately.
198 return;
199 }
200 else {
201 // If count == 0xfffe then we have wrapped our reference
202 // count. We should stop counting now as this reference must be
203 // leaked rather than accidently freeing an active object later.
204
205 #if !DEBUG
206 return; // return out of function which pegs the reference
207 #else DEBUG
208 // @@@ gvdl: eventually need to make this panic optional
209 // based on a boot argument i.e. debug= boot flag
210 panic("OSObject::refcount: %s",
211 "About to unreference a pegged object, reference leak?");
212 #endif /* !DEBUG */
213 }
214 }
215 actualCount = origCount - dec;
216 if ((UInt16) actualCount < when)
217 newCount = 0xffff;
218 else
219 newCount = actualCount;
220
221 } while (!OSCompareAndSwap(origCount, newCount, (UInt32 *) countP));
222
223 //
224 // This panic means that we have just attempted to release an object
225 // who's retain count has gone to less than the number of collections
226 // it is a member off. Take a panic immediately.
227 // In Fact the panic MAY not be a registry corruption but it is
228 // ALWAYS the wrong thing to do. I call it a registry corruption 'cause
229 // the registry is the biggest single use of a network of collections.
230 //
231 if ((UInt16) actualCount < (actualCount >> 16))
232 panic("A driver releasing a(n) %s has corrupted the registry\n",
233 getClassName(this));
234
235 // Check for a 'free' condition and that if we are first through
236 if (newCount == 0xffff)
237 ((OSObject *) this)->free();
238 }
239
240 void OSObject::release() const
241 {
242 taggedRelease(0);
243 }
244
245 void OSObject::retain() const
246 {
247 taggedRetain(0);
248 }
249
250 void OSObject::release(int when) const
251 {
252 taggedRelease(0, when);
253 }
254
255 bool OSObject::serialize(OSSerialize *s) const
256 {
257 if (s->previouslySerialized(this)) return true;
258
259 if (!s->addXMLStartTag(this, "string")) return false;
260
261 if (!s->addString(getClassName(this))) return false;
262 if (!s->addString(" is not serializable")) return false;
263
264 return s->addXMLEndTag("string");
265 }
266
267 void *OSObject::operator new(size_t size)
268 {
269 void *mem = (void *) kalloc(size);
270 assert(mem);
271 bzero(mem, size);
272
273 ACCUMSIZE(size);
274
275 return mem;
276 }
277
278 void OSObject::operator delete(void *mem, size_t size)
279 {
280 kfree((vm_offset_t) mem, size);
281
282 ACCUMSIZE(-size);
283 }