xref: /xnu-11215/libkern/c++/OSObject.cpp (revision c1dac77f)
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++/OSSymbol.h>
27 #include <libkern/c++/OSLib.h>
28 #include <libkern/c++/OSCPPDebug.h>
29 #include <libkern/OSAtomic.h>
30 
31 __BEGIN_DECLS
32 int debug_ivars_size;
33 __END_DECLS
34 
35 #if OSALLOCDEBUG
36 #define ACCUMSIZE(s) do { debug_ivars_size += (s); } while(0)
37 #else
38 #define ACCUMSIZE(s)
39 #endif
40 
41 // OSDefineMetaClassAndAbstractStructors(OSObject, 0);
42 /* Class global data */
43 OSObject::MetaClass OSObject::gMetaClass;
44 const OSMetaClass * const OSObject::metaClass = &OSObject::gMetaClass;
45 const OSMetaClass * const OSObject::superClass = 0;
46 
47 /* Class member functions - Can't use defaults */
48 OSObject::OSObject()			{ retainCount = 1; }
49 OSObject::OSObject(const OSMetaClass *)	{ retainCount = 1; }
50 OSObject::~OSObject()			{ }
51 const OSMetaClass * OSObject::getMetaClass() const
52     { return &gMetaClass; }
53 OSObject *OSObject::MetaClass::alloc() const { return 0; }
54 
55 /* The OSObject::MetaClass constructor */
56 OSObject::MetaClass::MetaClass()
57     : OSMetaClass("OSObject", OSObject::superClass, sizeof(OSObject))
58     { }
59 
60 // Virtual Padding
61 OSMetaClassDefineReservedUnused(OSObject,  0);
62 OSMetaClassDefineReservedUnused(OSObject,  1);
63 OSMetaClassDefineReservedUnused(OSObject,  2);
64 OSMetaClassDefineReservedUnused(OSObject,  3);
65 OSMetaClassDefineReservedUnused(OSObject,  4);
66 OSMetaClassDefineReservedUnused(OSObject,  5);
67 OSMetaClassDefineReservedUnused(OSObject,  6);
68 OSMetaClassDefineReservedUnused(OSObject,  7);
69 OSMetaClassDefineReservedUnused(OSObject,  8);
70 OSMetaClassDefineReservedUnused(OSObject,  9);
71 OSMetaClassDefineReservedUnused(OSObject, 10);
72 OSMetaClassDefineReservedUnused(OSObject, 11);
73 OSMetaClassDefineReservedUnused(OSObject, 12);
74 OSMetaClassDefineReservedUnused(OSObject, 13);
75 OSMetaClassDefineReservedUnused(OSObject, 14);
76 OSMetaClassDefineReservedUnused(OSObject, 15);
77 OSMetaClassDefineReservedUnused(OSObject, 16);
78 OSMetaClassDefineReservedUnused(OSObject, 17);
79 OSMetaClassDefineReservedUnused(OSObject, 18);
80 OSMetaClassDefineReservedUnused(OSObject, 19);
81 OSMetaClassDefineReservedUnused(OSObject, 20);
82 OSMetaClassDefineReservedUnused(OSObject, 21);
83 OSMetaClassDefineReservedUnused(OSObject, 22);
84 OSMetaClassDefineReservedUnused(OSObject, 23);
85 OSMetaClassDefineReservedUnused(OSObject, 24);
86 OSMetaClassDefineReservedUnused(OSObject, 25);
87 OSMetaClassDefineReservedUnused(OSObject, 26);
88 OSMetaClassDefineReservedUnused(OSObject, 27);
89 OSMetaClassDefineReservedUnused(OSObject, 28);
90 OSMetaClassDefineReservedUnused(OSObject, 29);
91 OSMetaClassDefineReservedUnused(OSObject, 30);
92 OSMetaClassDefineReservedUnused(OSObject, 31);
93 
94 
95 bool OSObject::init()			{ return true; }
96 void OSObject::free()
97 {
98     const OSMetaClass *meta = getMetaClass();
99 
100     if (meta)
101 	meta->instanceDestructed();
102     delete this;
103 }
104 
105 int OSObject::getRetainCount() const
106 {
107     return retainCount;
108 }
109 
110 void OSObject::retain() const
111 {
112     OSIncrementAtomic((SInt32 *) &retainCount);
113 }
114 
115 void OSObject::release(int when) const
116 {
117     if (OSDecrementAtomic((SInt32 *) &retainCount) <= when)
118 	((OSObject *) this)->free();
119 }
120 
121 void OSObject::release() const
122 {
123     release(1);
124 }
125 
126 bool OSObject::serialize(OSSerialize *s) const
127 {
128     if (s->previouslySerialized(this)) return true;
129 
130     if (!s->addXMLStartTag(this, "string")) return false;
131 
132     const OSMetaClass *meta = getMetaClass();
133     const char *className = (meta)? meta->getClassName() : "unknown class?";
134 
135     if (!s->addString(className)) return false;
136     if (!s->addString(" is not serializable")) return false;
137 
138     return s->addXMLEndTag("string");
139 }
140 
141 void *OSObject::operator new(size_t size)
142 {
143     void *mem = (void *) kalloc(size);
144     assert(mem);
145     bzero(mem, size);
146 
147     ACCUMSIZE(size);
148 
149     return mem;
150 }
151 
152 void OSObject::operator delete(void *mem, size_t size)
153 {
154     kfree((vm_offset_t) mem, size);
155 
156     ACCUMSIZE(-size);
157 }
158