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