xref: /xnu-10063.121.3/libkern/c++/OSSerialize.cpp (revision 2c2f96dc2b9a4408a43d3150ae9c105355ca3daa)
1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions  * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions  *
4*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions  *
6*2c2f96dcSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions  *
15*2c2f96dcSApple OSS Distributions  * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions  *
18*2c2f96dcSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions  * limitations under the License.
25*2c2f96dcSApple OSS Distributions  *
26*2c2f96dcSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions  */
28*2c2f96dcSApple OSS Distributions /* OSSerialize.cpp created by rsulack on Wen 25-Nov-1998 */
29*2c2f96dcSApple OSS Distributions 
30*2c2f96dcSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*2c2f96dcSApple OSS Distributions 
32*2c2f96dcSApple OSS Distributions #include <sys/cdefs.h>
33*2c2f96dcSApple OSS Distributions #include <vm/vm_kern.h>
34*2c2f96dcSApple OSS Distributions #include <os/hash.h>
35*2c2f96dcSApple OSS Distributions 
36*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSContainers.h>
37*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSLib.h>
38*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSDictionary.h>
39*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
40*2c2f96dcSApple OSS Distributions #include <libkern/OSSerializeBinary.h>
41*2c2f96dcSApple OSS Distributions #include <libkern/Block.h>
42*2c2f96dcSApple OSS Distributions #include <IOKit/IOLib.h>
43*2c2f96dcSApple OSS Distributions 
44*2c2f96dcSApple OSS Distributions #define super OSObject
45*2c2f96dcSApple OSS Distributions 
46*2c2f96dcSApple OSS Distributions OSDefineMetaClassAndStructors(OSSerialize, OSObject)
47*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 0);
48*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 1);
49*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 2);
50*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 3);
51*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 4);
52*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 5);
53*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 6);
54*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSSerialize, 7);
55*2c2f96dcSApple OSS Distributions 
56*2c2f96dcSApple OSS Distributions static inline kmem_guard_t
OSSerialize_guard()57*2c2f96dcSApple OSS Distributions OSSerialize_guard()
58*2c2f96dcSApple OSS Distributions {
59*2c2f96dcSApple OSS Distributions 	kmem_guard_t guard = {
60*2c2f96dcSApple OSS Distributions 		.kmg_tag     = IOMemoryTag(kernel_map),
61*2c2f96dcSApple OSS Distributions 	};
62*2c2f96dcSApple OSS Distributions 
63*2c2f96dcSApple OSS Distributions 	return guard;
64*2c2f96dcSApple OSS Distributions }
65*2c2f96dcSApple OSS Distributions 
66*2c2f96dcSApple OSS Distributions 
67*2c2f96dcSApple OSS Distributions char *
text() const68*2c2f96dcSApple OSS Distributions OSSerialize::text() const
69*2c2f96dcSApple OSS Distributions {
70*2c2f96dcSApple OSS Distributions 	return data;
71*2c2f96dcSApple OSS Distributions }
72*2c2f96dcSApple OSS Distributions 
73*2c2f96dcSApple OSS Distributions void
clearText()74*2c2f96dcSApple OSS Distributions OSSerialize::clearText()
75*2c2f96dcSApple OSS Distributions {
76*2c2f96dcSApple OSS Distributions 	if (binary) {
77*2c2f96dcSApple OSS Distributions 		length = sizeof(kOSSerializeBinarySignature);
78*2c2f96dcSApple OSS Distributions 		bzero(&data[length], capacity - length);
79*2c2f96dcSApple OSS Distributions 		endCollection = true;
80*2c2f96dcSApple OSS Distributions 	} else {
81*2c2f96dcSApple OSS Distributions 		bzero((void *)data, capacity);
82*2c2f96dcSApple OSS Distributions 		length = 1;
83*2c2f96dcSApple OSS Distributions 	}
84*2c2f96dcSApple OSS Distributions 	tags->flushCollection();
85*2c2f96dcSApple OSS Distributions }
86*2c2f96dcSApple OSS Distributions 
87*2c2f96dcSApple OSS Distributions bool
previouslySerialized(const OSMetaClassBase * o)88*2c2f96dcSApple OSS Distributions OSSerialize::previouslySerialized(const OSMetaClassBase *o)
89*2c2f96dcSApple OSS Distributions {
90*2c2f96dcSApple OSS Distributions 	char temp[16];
91*2c2f96dcSApple OSS Distributions 	unsigned int tagIdx;
92*2c2f96dcSApple OSS Distributions 
93*2c2f96dcSApple OSS Distributions 	if (binary) {
94*2c2f96dcSApple OSS Distributions 		return binarySerialize(o);
95*2c2f96dcSApple OSS Distributions 	}
96*2c2f96dcSApple OSS Distributions 
97*2c2f96dcSApple OSS Distributions 	// look it up
98*2c2f96dcSApple OSS Distributions 	tagIdx = tags->getNextIndexOfObject(o, 0);
99*2c2f96dcSApple OSS Distributions 
100*2c2f96dcSApple OSS Distributions // xx-review: no error checking here for addString calls!
101*2c2f96dcSApple OSS Distributions 	// does it exist?
102*2c2f96dcSApple OSS Distributions 	if (tagIdx != -1U) {
103*2c2f96dcSApple OSS Distributions 		addString("<reference IDREF=\"");
104*2c2f96dcSApple OSS Distributions 		snprintf(temp, sizeof(temp), "%u", tagIdx);
105*2c2f96dcSApple OSS Distributions 		addString(temp);
106*2c2f96dcSApple OSS Distributions 		addString("\"/>");
107*2c2f96dcSApple OSS Distributions 		return true;
108*2c2f96dcSApple OSS Distributions 	}
109*2c2f96dcSApple OSS Distributions 
110*2c2f96dcSApple OSS Distributions 	// add to tag array
111*2c2f96dcSApple OSS Distributions 	tags->setObject(o);// XXX check return
112*2c2f96dcSApple OSS Distributions 
113*2c2f96dcSApple OSS Distributions 	return false;
114*2c2f96dcSApple OSS Distributions }
115*2c2f96dcSApple OSS Distributions 
116*2c2f96dcSApple OSS Distributions bool
addXMLStartTag(const OSMetaClassBase * o,const char * tagString)117*2c2f96dcSApple OSS Distributions OSSerialize::addXMLStartTag(const OSMetaClassBase *o, const char *tagString)
118*2c2f96dcSApple OSS Distributions {
119*2c2f96dcSApple OSS Distributions 	char temp[16];
120*2c2f96dcSApple OSS Distributions 	unsigned int tagIdx;
121*2c2f96dcSApple OSS Distributions 
122*2c2f96dcSApple OSS Distributions 	if (binary) {
123*2c2f96dcSApple OSS Distributions 		printf("class %s: xml serialize\n", o->getMetaClass()->getClassName());
124*2c2f96dcSApple OSS Distributions 		return false;
125*2c2f96dcSApple OSS Distributions 	}
126*2c2f96dcSApple OSS Distributions 
127*2c2f96dcSApple OSS Distributions 	if (!addChar('<')) {
128*2c2f96dcSApple OSS Distributions 		return false;
129*2c2f96dcSApple OSS Distributions 	}
130*2c2f96dcSApple OSS Distributions 	if (!addString(tagString)) {
131*2c2f96dcSApple OSS Distributions 		return false;
132*2c2f96dcSApple OSS Distributions 	}
133*2c2f96dcSApple OSS Distributions 	if (!addString(" ID=\"")) {
134*2c2f96dcSApple OSS Distributions 		return false;
135*2c2f96dcSApple OSS Distributions 	}
136*2c2f96dcSApple OSS Distributions 	tagIdx = tags->getNextIndexOfObject(o, 0);
137*2c2f96dcSApple OSS Distributions 	assert(tagIdx != -1U);
138*2c2f96dcSApple OSS Distributions 	snprintf(temp, sizeof(temp), "%u", tagIdx);
139*2c2f96dcSApple OSS Distributions 	if (!addString(temp)) {
140*2c2f96dcSApple OSS Distributions 		return false;
141*2c2f96dcSApple OSS Distributions 	}
142*2c2f96dcSApple OSS Distributions 	if (!addChar('\"')) {
143*2c2f96dcSApple OSS Distributions 		return false;
144*2c2f96dcSApple OSS Distributions 	}
145*2c2f96dcSApple OSS Distributions 	if (!addChar('>')) {
146*2c2f96dcSApple OSS Distributions 		return false;
147*2c2f96dcSApple OSS Distributions 	}
148*2c2f96dcSApple OSS Distributions 	return true;
149*2c2f96dcSApple OSS Distributions }
150*2c2f96dcSApple OSS Distributions 
151*2c2f96dcSApple OSS Distributions bool
addXMLEndTag(const char * tagString)152*2c2f96dcSApple OSS Distributions OSSerialize::addXMLEndTag(const char *tagString)
153*2c2f96dcSApple OSS Distributions {
154*2c2f96dcSApple OSS Distributions 	if (!addChar('<')) {
155*2c2f96dcSApple OSS Distributions 		return false;
156*2c2f96dcSApple OSS Distributions 	}
157*2c2f96dcSApple OSS Distributions 	if (!addChar('/')) {
158*2c2f96dcSApple OSS Distributions 		return false;
159*2c2f96dcSApple OSS Distributions 	}
160*2c2f96dcSApple OSS Distributions 	if (!addString(tagString)) {
161*2c2f96dcSApple OSS Distributions 		return false;
162*2c2f96dcSApple OSS Distributions 	}
163*2c2f96dcSApple OSS Distributions 	if (!addChar('>')) {
164*2c2f96dcSApple OSS Distributions 		return false;
165*2c2f96dcSApple OSS Distributions 	}
166*2c2f96dcSApple OSS Distributions 	return true;
167*2c2f96dcSApple OSS Distributions }
168*2c2f96dcSApple OSS Distributions 
169*2c2f96dcSApple OSS Distributions bool
addChar(const char c)170*2c2f96dcSApple OSS Distributions OSSerialize::addChar(const char c)
171*2c2f96dcSApple OSS Distributions {
172*2c2f96dcSApple OSS Distributions 	if (binary) {
173*2c2f96dcSApple OSS Distributions 		printf("xml serialize\n");
174*2c2f96dcSApple OSS Distributions 		return false;
175*2c2f96dcSApple OSS Distributions 	}
176*2c2f96dcSApple OSS Distributions 
177*2c2f96dcSApple OSS Distributions 	// add char, possibly extending our capacity
178*2c2f96dcSApple OSS Distributions 	if (length >= capacity && length >= ensureCapacity(capacity + capacityIncrement)) {
179*2c2f96dcSApple OSS Distributions 		return false;
180*2c2f96dcSApple OSS Distributions 	}
181*2c2f96dcSApple OSS Distributions 
182*2c2f96dcSApple OSS Distributions 	data[length - 1] = c;
183*2c2f96dcSApple OSS Distributions 	length++;
184*2c2f96dcSApple OSS Distributions 
185*2c2f96dcSApple OSS Distributions 	return true;
186*2c2f96dcSApple OSS Distributions }
187*2c2f96dcSApple OSS Distributions 
188*2c2f96dcSApple OSS Distributions bool
addString(const char * s)189*2c2f96dcSApple OSS Distributions OSSerialize::addString(const char *s)
190*2c2f96dcSApple OSS Distributions {
191*2c2f96dcSApple OSS Distributions 	bool rc = false;
192*2c2f96dcSApple OSS Distributions 
193*2c2f96dcSApple OSS Distributions 	while (*s && (rc = addChar(*s++))) {
194*2c2f96dcSApple OSS Distributions 		;
195*2c2f96dcSApple OSS Distributions 	}
196*2c2f96dcSApple OSS Distributions 
197*2c2f96dcSApple OSS Distributions 	return rc;
198*2c2f96dcSApple OSS Distributions }
199*2c2f96dcSApple OSS Distributions 
200*2c2f96dcSApple OSS Distributions bool
initWithCapacity(unsigned int inCapacity)201*2c2f96dcSApple OSS Distributions OSSerialize::initWithCapacity(unsigned int inCapacity)
202*2c2f96dcSApple OSS Distributions {
203*2c2f96dcSApple OSS Distributions 	kmem_return_t kmr;
204*2c2f96dcSApple OSS Distributions 
205*2c2f96dcSApple OSS Distributions 	if (!super::init()) {
206*2c2f96dcSApple OSS Distributions 		return false;
207*2c2f96dcSApple OSS Distributions 	}
208*2c2f96dcSApple OSS Distributions 
209*2c2f96dcSApple OSS Distributions 	tags = OSArray::withCapacity(256);
210*2c2f96dcSApple OSS Distributions 	if (!tags) {
211*2c2f96dcSApple OSS Distributions 		return false;
212*2c2f96dcSApple OSS Distributions 	}
213*2c2f96dcSApple OSS Distributions 
214*2c2f96dcSApple OSS Distributions 	length = 1;
215*2c2f96dcSApple OSS Distributions 
216*2c2f96dcSApple OSS Distributions 	if (!inCapacity) {
217*2c2f96dcSApple OSS Distributions 		inCapacity = 1;
218*2c2f96dcSApple OSS Distributions 	}
219*2c2f96dcSApple OSS Distributions 	if (round_page_overflow(inCapacity, &inCapacity)) {
220*2c2f96dcSApple OSS Distributions 		tags.reset();
221*2c2f96dcSApple OSS Distributions 		return false;
222*2c2f96dcSApple OSS Distributions 	}
223*2c2f96dcSApple OSS Distributions 
224*2c2f96dcSApple OSS Distributions 	capacityIncrement = inCapacity;
225*2c2f96dcSApple OSS Distributions 
226*2c2f96dcSApple OSS Distributions 	// allocate from the kernel map so that we can safely map this data
227*2c2f96dcSApple OSS Distributions 	// into user space (the primary use of the OSSerialize object)
228*2c2f96dcSApple OSS Distributions 
229*2c2f96dcSApple OSS Distributions 	kmr = kmem_alloc_guard(kernel_map, inCapacity, /* mask */ 0,
230*2c2f96dcSApple OSS Distributions 	    (kma_flags_t)(KMA_ZERO | KMA_DATA), OSSerialize_guard());
231*2c2f96dcSApple OSS Distributions 
232*2c2f96dcSApple OSS Distributions 	if (kmr.kmr_return == KERN_SUCCESS) {
233*2c2f96dcSApple OSS Distributions 		data = (char *)kmr.kmr_ptr;
234*2c2f96dcSApple OSS Distributions 		capacity = inCapacity;
235*2c2f96dcSApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(capacity);
236*2c2f96dcSApple OSS Distributions 		return true;
237*2c2f96dcSApple OSS Distributions 	}
238*2c2f96dcSApple OSS Distributions 
239*2c2f96dcSApple OSS Distributions 	capacity = 0;
240*2c2f96dcSApple OSS Distributions 	return false;
241*2c2f96dcSApple OSS Distributions }
242*2c2f96dcSApple OSS Distributions 
243*2c2f96dcSApple OSS Distributions OSSharedPtr<OSSerialize>
withCapacity(unsigned int inCapacity)244*2c2f96dcSApple OSS Distributions OSSerialize::withCapacity(unsigned int inCapacity)
245*2c2f96dcSApple OSS Distributions {
246*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSSerialize> me = OSMakeShared<OSSerialize>();
247*2c2f96dcSApple OSS Distributions 
248*2c2f96dcSApple OSS Distributions 	if (me && !me->initWithCapacity(inCapacity)) {
249*2c2f96dcSApple OSS Distributions 		return nullptr;
250*2c2f96dcSApple OSS Distributions 	}
251*2c2f96dcSApple OSS Distributions 
252*2c2f96dcSApple OSS Distributions 	return me;
253*2c2f96dcSApple OSS Distributions }
254*2c2f96dcSApple OSS Distributions 
255*2c2f96dcSApple OSS Distributions unsigned int
getLength() const256*2c2f96dcSApple OSS Distributions OSSerialize::getLength() const
257*2c2f96dcSApple OSS Distributions {
258*2c2f96dcSApple OSS Distributions 	return length;
259*2c2f96dcSApple OSS Distributions }
260*2c2f96dcSApple OSS Distributions unsigned int
getCapacity() const261*2c2f96dcSApple OSS Distributions OSSerialize::getCapacity() const
262*2c2f96dcSApple OSS Distributions {
263*2c2f96dcSApple OSS Distributions 	return capacity;
264*2c2f96dcSApple OSS Distributions }
265*2c2f96dcSApple OSS Distributions unsigned int
getCapacityIncrement() const266*2c2f96dcSApple OSS Distributions OSSerialize::getCapacityIncrement() const
267*2c2f96dcSApple OSS Distributions {
268*2c2f96dcSApple OSS Distributions 	return capacityIncrement;
269*2c2f96dcSApple OSS Distributions }
270*2c2f96dcSApple OSS Distributions unsigned int
setCapacityIncrement(unsigned int increment)271*2c2f96dcSApple OSS Distributions OSSerialize::setCapacityIncrement(unsigned int increment)
272*2c2f96dcSApple OSS Distributions {
273*2c2f96dcSApple OSS Distributions 	capacityIncrement = (increment)? increment : 256;
274*2c2f96dcSApple OSS Distributions 	return capacityIncrement;
275*2c2f96dcSApple OSS Distributions }
276*2c2f96dcSApple OSS Distributions 
277*2c2f96dcSApple OSS Distributions unsigned int
ensureCapacity(unsigned int newCapacity)278*2c2f96dcSApple OSS Distributions OSSerialize::ensureCapacity(unsigned int newCapacity)
279*2c2f96dcSApple OSS Distributions {
280*2c2f96dcSApple OSS Distributions 	kmem_return_t kmr;
281*2c2f96dcSApple OSS Distributions 
282*2c2f96dcSApple OSS Distributions 	if (newCapacity <= capacity) {
283*2c2f96dcSApple OSS Distributions 		return capacity;
284*2c2f96dcSApple OSS Distributions 	}
285*2c2f96dcSApple OSS Distributions 
286*2c2f96dcSApple OSS Distributions 	if (round_page_overflow(newCapacity, &newCapacity)) {
287*2c2f96dcSApple OSS Distributions 		return capacity;
288*2c2f96dcSApple OSS Distributions 	}
289*2c2f96dcSApple OSS Distributions 
290*2c2f96dcSApple OSS Distributions 	kmr = kmem_realloc_guard(kernel_map, (vm_offset_t)data, capacity,
291*2c2f96dcSApple OSS Distributions 	    newCapacity, (kmr_flags_t)(KMR_ZERO | KMR_DATA | KMR_FREEOLD),
292*2c2f96dcSApple OSS Distributions 	    OSSerialize_guard());
293*2c2f96dcSApple OSS Distributions 
294*2c2f96dcSApple OSS Distributions 	if (kmr.kmr_return == KERN_SUCCESS) {
295*2c2f96dcSApple OSS Distributions 		size_t delta = 0;
296*2c2f96dcSApple OSS Distributions 
297*2c2f96dcSApple OSS Distributions 		data     = (char *)kmr.kmr_ptr;
298*2c2f96dcSApple OSS Distributions 		delta   -= capacity;
299*2c2f96dcSApple OSS Distributions 		capacity = newCapacity;
300*2c2f96dcSApple OSS Distributions 		delta   += capacity;
301*2c2f96dcSApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(delta);
302*2c2f96dcSApple OSS Distributions 	}
303*2c2f96dcSApple OSS Distributions 
304*2c2f96dcSApple OSS Distributions 	return capacity;
305*2c2f96dcSApple OSS Distributions }
306*2c2f96dcSApple OSS Distributions 
307*2c2f96dcSApple OSS Distributions void
free()308*2c2f96dcSApple OSS Distributions OSSerialize::free()
309*2c2f96dcSApple OSS Distributions {
310*2c2f96dcSApple OSS Distributions 	if (capacity) {
311*2c2f96dcSApple OSS Distributions 		kmem_free_guard(kernel_map, (vm_offset_t)data, capacity,
312*2c2f96dcSApple OSS Distributions 		    KMF_NONE, OSSerialize_guard());
313*2c2f96dcSApple OSS Distributions 		OSCONTAINER_ACCUMSIZE( -((size_t)capacity));
314*2c2f96dcSApple OSS Distributions 		data = nullptr;
315*2c2f96dcSApple OSS Distributions 		capacity = 0;
316*2c2f96dcSApple OSS Distributions 	}
317*2c2f96dcSApple OSS Distributions 	super::free();
318*2c2f96dcSApple OSS Distributions }
319*2c2f96dcSApple OSS Distributions 
320*2c2f96dcSApple OSS Distributions 
OSDefineMetaClassAndStructors(OSSerializer,OSObject)321*2c2f96dcSApple OSS Distributions OSDefineMetaClassAndStructors(OSSerializer, OSObject)
322*2c2f96dcSApple OSS Distributions 
323*2c2f96dcSApple OSS Distributions OSSharedPtr<OSSerializer>
324*2c2f96dcSApple OSS Distributions OSSerializer::forTarget( void * target,
325*2c2f96dcSApple OSS Distributions     OSSerializerCallback callback, void * ref )
326*2c2f96dcSApple OSS Distributions {
327*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSSerializer> thing = OSMakeShared<OSSerializer>();
328*2c2f96dcSApple OSS Distributions 
329*2c2f96dcSApple OSS Distributions 	if (thing && !thing->init()) {
330*2c2f96dcSApple OSS Distributions 		thing.reset();
331*2c2f96dcSApple OSS Distributions 	}
332*2c2f96dcSApple OSS Distributions 
333*2c2f96dcSApple OSS Distributions 	if (thing) {
334*2c2f96dcSApple OSS Distributions 		thing->target   = target;
335*2c2f96dcSApple OSS Distributions 		thing->ref      = ref;
336*2c2f96dcSApple OSS Distributions 		thing->callback = callback;
337*2c2f96dcSApple OSS Distributions 	}
338*2c2f96dcSApple OSS Distributions 	return thing;
339*2c2f96dcSApple OSS Distributions }
340*2c2f96dcSApple OSS Distributions 
341*2c2f96dcSApple OSS Distributions bool
callbackToBlock(void * target __unused,void * ref,OSSerialize * serializer)342*2c2f96dcSApple OSS Distributions OSSerializer::callbackToBlock(void * target __unused, void * ref,
343*2c2f96dcSApple OSS Distributions     OSSerialize * serializer)
344*2c2f96dcSApple OSS Distributions {
345*2c2f96dcSApple OSS Distributions 	return ((OSSerializerBlock)ref)(serializer);
346*2c2f96dcSApple OSS Distributions }
347*2c2f96dcSApple OSS Distributions 
348*2c2f96dcSApple OSS Distributions OSSharedPtr<OSSerializer>
withBlock(OSSerializerBlock callback)349*2c2f96dcSApple OSS Distributions OSSerializer::withBlock(
350*2c2f96dcSApple OSS Distributions 	OSSerializerBlock callback)
351*2c2f96dcSApple OSS Distributions {
352*2c2f96dcSApple OSS Distributions 	OSSharedPtr<OSSerializer> serializer;
353*2c2f96dcSApple OSS Distributions 	OSSerializerBlock block;
354*2c2f96dcSApple OSS Distributions 
355*2c2f96dcSApple OSS Distributions 	block = Block_copy(callback);
356*2c2f96dcSApple OSS Distributions 	if (!block) {
357*2c2f96dcSApple OSS Distributions 		return NULL;
358*2c2f96dcSApple OSS Distributions 	}
359*2c2f96dcSApple OSS Distributions 
360*2c2f96dcSApple OSS Distributions 	serializer = (OSSerializer::forTarget(NULL, &OSSerializer::callbackToBlock, block));
361*2c2f96dcSApple OSS Distributions 
362*2c2f96dcSApple OSS Distributions 	if (!serializer) {
363*2c2f96dcSApple OSS Distributions 		Block_release(block);
364*2c2f96dcSApple OSS Distributions 	}
365*2c2f96dcSApple OSS Distributions 
366*2c2f96dcSApple OSS Distributions 	return serializer;
367*2c2f96dcSApple OSS Distributions }
368*2c2f96dcSApple OSS Distributions 
369*2c2f96dcSApple OSS Distributions void
free(void)370*2c2f96dcSApple OSS Distributions OSSerializer::free(void)
371*2c2f96dcSApple OSS Distributions {
372*2c2f96dcSApple OSS Distributions 	if (callback == &callbackToBlock) {
373*2c2f96dcSApple OSS Distributions 		Block_release(ref);
374*2c2f96dcSApple OSS Distributions 	}
375*2c2f96dcSApple OSS Distributions 
376*2c2f96dcSApple OSS Distributions 	super::free();
377*2c2f96dcSApple OSS Distributions }
378*2c2f96dcSApple OSS Distributions 
379*2c2f96dcSApple OSS Distributions bool
serialize(OSSerialize * s) const380*2c2f96dcSApple OSS Distributions OSSerializer::serialize( OSSerialize * s ) const
381*2c2f96dcSApple OSS Distributions {
382*2c2f96dcSApple OSS Distributions 	return (*callback)(target, ref, s);
383*2c2f96dcSApple OSS Distributions }
384