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