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