]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSObject.cpp
xnu-344.21.74.tar.gz
[apple/xnu.git] / libkern / c++ / OSObject.cpp
1 /*
2 * Copyright (c) 2000 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 /* OSObject.cpp created by gvdl on Fri 1998-11-17 */
26
27 #include <libkern/c++/OSObject.h>
28 #include <libkern/c++/OSSerialize.h>
29 #include <libkern/c++/OSLib.h>
30 #include <libkern/c++/OSCPPDebug.h>
31 #include <libkern/OSAtomic.h>
32
33 #include <libkern/c++/OSCollection.h>
34
35 __BEGIN_DECLS
36 int debug_ivars_size;
37 __END_DECLS
38
39 #if OSALLOCDEBUG
40 #define ACCUMSIZE(s) do { debug_ivars_size += (s); } while(0)
41 #else
42 #define ACCUMSIZE(s)
43 #endif
44
45 // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
46 /* Class global data */
47 OSObject::MetaClass OSObject::gMetaClass;
48 const OSMetaClass * const OSObject::metaClass = &OSObject::gMetaClass;
49 const OSMetaClass * const OSObject::superClass = 0;
50
51 /* Class member functions - Can't use defaults */
52 OSObject::OSObject() { retainCount = 1; }
53 OSObject::OSObject(const OSMetaClass *) { retainCount = 1; }
54 OSObject::~OSObject() { }
55 const OSMetaClass * OSObject::getMetaClass() const
56 { return &gMetaClass; }
57 OSObject *OSObject::MetaClass::alloc() const { return 0; }
58
59 /* The OSObject::MetaClass constructor */
60 OSObject::MetaClass::MetaClass()
61 : OSMetaClass("OSObject", OSObject::superClass, sizeof(OSObject))
62 { }
63
64 // Virtual Padding
65 OSMetaClassDefineReservedUnused(OSObject, 0);
66 OSMetaClassDefineReservedUnused(OSObject, 1);
67 OSMetaClassDefineReservedUnused(OSObject, 2);
68 OSMetaClassDefineReservedUnused(OSObject, 3);
69 OSMetaClassDefineReservedUnused(OSObject, 4);
70 OSMetaClassDefineReservedUnused(OSObject, 5);
71 OSMetaClassDefineReservedUnused(OSObject, 6);
72 OSMetaClassDefineReservedUnused(OSObject, 7);
73 OSMetaClassDefineReservedUnused(OSObject, 8);
74 OSMetaClassDefineReservedUnused(OSObject, 9);
75 OSMetaClassDefineReservedUnused(OSObject, 10);
76 OSMetaClassDefineReservedUnused(OSObject, 11);
77 OSMetaClassDefineReservedUnused(OSObject, 12);
78 OSMetaClassDefineReservedUnused(OSObject, 13);
79 OSMetaClassDefineReservedUnused(OSObject, 14);
80 OSMetaClassDefineReservedUnused(OSObject, 15);
81 OSMetaClassDefineReservedUnused(OSObject, 16);
82 OSMetaClassDefineReservedUnused(OSObject, 17);
83 OSMetaClassDefineReservedUnused(OSObject, 18);
84 OSMetaClassDefineReservedUnused(OSObject, 19);
85 OSMetaClassDefineReservedUnused(OSObject, 20);
86 OSMetaClassDefineReservedUnused(OSObject, 21);
87 OSMetaClassDefineReservedUnused(OSObject, 22);
88 OSMetaClassDefineReservedUnused(OSObject, 23);
89 OSMetaClassDefineReservedUnused(OSObject, 24);
90 OSMetaClassDefineReservedUnused(OSObject, 25);
91 OSMetaClassDefineReservedUnused(OSObject, 26);
92 OSMetaClassDefineReservedUnused(OSObject, 27);
93 OSMetaClassDefineReservedUnused(OSObject, 28);
94 OSMetaClassDefineReservedUnused(OSObject, 29);
95 OSMetaClassDefineReservedUnused(OSObject, 30);
96 OSMetaClassDefineReservedUnused(OSObject, 31);
97
98 static const char *getClassName(const OSObject *obj)
99 {
100 const OSMetaClass *meta = obj->getMetaClass();
101 return (meta) ? meta->getClassName() : "unknown class?";
102 }
103
104 bool OSObject::init()
105 { return true; }
106
107 void OSObject::free()
108 {
109 const OSMetaClass *meta = getMetaClass();
110
111 if (meta)
112 meta->instanceDestructed();
113 delete this;
114 }
115
116 int OSObject::getRetainCount() const
117 {
118 return (int) ((UInt16) retainCount);
119 }
120
121 void OSObject::taggedRetain(const void *tag) const
122 {
123 #if !DEBUG
124 volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
125 UInt32 inc = 1;
126 UInt32 origCount;
127 UInt32 newCount;
128
129 // Increment the collecion bucket.
130 if ((const void *) OSTypeID(OSCollection) == tag)
131 inc |= (1UL<<16);
132
133 do {
134 origCount = *countP;
135 if (-1UL == origCount)
136 // @@@ Pinot: panic("Attempting to retain a freed object");
137 return;
138
139 newCount = origCount + inc;
140 } while (!OSCompareAndSwap(origCount, newCount, (UInt32 *) countP));
141 #else
142 volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
143 UInt32 inc = 1;
144 UInt32 origCount;
145 UInt32 newCount;
146
147 // Increment the collecion bucket.
148 if ((const void *) OSTypeID(OSCollection) == tag)
149 inc |= (1UL<<16);
150
151 do {
152 origCount = *countP;
153 if (-1UL == origCount)
154 return; // We are freeing so leave now.
155
156 newCount = origCount + inc;
157 } while (!OSCompareAndSwap(origCount, newCount, (UInt32 *) countP));
158 #endif
159 }
160
161 void OSObject::taggedRelease(const void *tag) const
162 {
163 taggedRelease(tag, 1);
164 }
165
166 void OSObject::taggedRelease(const void *tag, const int when) const
167 {
168 #if !DEBUG
169 volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
170 UInt32 dec = 1;
171 UInt32 origCount;
172 UInt32 newCount;
173 UInt32 actualCount;
174
175 // Increment the collecion bucket.
176 if ((const void *) OSTypeID(OSCollection) == tag)
177 dec |= (1UL<<16);
178
179 do {
180 origCount = *countP;
181 if (-1UL == origCount)
182 return; // We are freeing already leave now.
183
184 actualCount = origCount - dec;
185 if ((SInt16) actualCount < when)
186 newCount = (UInt32) -1;
187 else
188 newCount = actualCount;
189
190 } while (!OSCompareAndSwap(origCount, newCount, (UInt32 *) countP));
191
192 //
193 // This panic means that we have just attempted to release an object
194 // who's retain count has gone to less than the number of collections
195 // it is a member off. Take a panic immediately.
196 // In Fact the panic MAY not be a registry corruption but it is
197 // ALWAYS the wrong thing to do. I call it a registry corruption 'cause
198 // the registry is the biggest single use of a network of collections.
199 //
200 if ((UInt16) actualCount < (actualCount >> 16))
201 panic("A driver releasing a(n) %s has corrupted the registry\n",
202 getClassName(this));
203
204 // Check for a 'free' condition and that if we are first through
205 if ((UInt32) -1 == newCount)
206 ((OSObject *) this)->free();
207 #else
208 // @@@ Pinot: Need to update the debug build release code.
209 volatile UInt32 *countP = (volatile UInt32 *) &retainCount;
210 UInt32 dec = 1;
211 UInt32 origCount;
212 UInt32 newCount;
213
214 // Increment the collecion bucket.
215 if ((const void *) OSTypeID(OSCollection) == tag)
216 dec |= (1UL<<16);
217
218 do {
219 origCount = *countP;
220 if (-1UL == origCount)
221 return; // We are freeing already leave now.
222
223 newCount = origCount - dec;
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) newCount < (newCount >> 16))
235 panic("A driver releasing a(n) %s has corrupted the registry\n",
236 getClassName(this));
237
238 // Check for a release too many
239 if ((SInt16) newCount < 0)
240 panic("An object has had a release too many\n",
241 getClassName(this));
242
243 // Check for a 'free' condition and that if we are first through
244 if ((SInt16) newCount < when
245 && OSCompareAndSwap(newCount, -1UL, (UInt32 *) countP))
246 ((OSObject *) this)->free();
247 #endif
248 }
249
250 void OSObject::release() const
251 {
252 taggedRelease(0);
253 }
254
255 void OSObject::retain() const
256 {
257 taggedRetain(0);
258 }
259
260 void OSObject::release(int when) const
261 {
262 taggedRelease(0, when);
263 }
264
265 bool OSObject::serialize(OSSerialize *s) const
266 {
267 if (s->previouslySerialized(this)) return true;
268
269 if (!s->addXMLStartTag(this, "string")) return false;
270
271 if (!s->addString(getClassName(this))) return false;
272 if (!s->addString(" is not serializable")) return false;
273
274 return s->addXMLEndTag("string");
275 }
276
277 void *OSObject::operator new(size_t size)
278 {
279 void *mem = (void *) kalloc(size);
280 assert(mem);
281 bzero(mem, size);
282
283 ACCUMSIZE(size);
284
285 return mem;
286 }
287
288 void OSObject::operator delete(void *mem, size_t size)
289 {
290 kfree((vm_offset_t) mem, size);
291
292 ACCUMSIZE(-size);
293 }