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