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