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