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