2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* OSSerialize.cpp created by rsulack on Wen 25-Nov-1998 */
30 #include <sys/cdefs.h>
33 #include <vm/vm_kern.h>
36 #include <libkern/c++/OSContainers.h>
37 #include <libkern/c++/OSLib.h>
38 #include <libkern/c++/OSDictionary.h>
39 #include <libkern/OSSerializeBinary.h>
40 #include <libkern/Block.h>
41 #include <IOKit/IOLib.h>
43 #define super OSObject
45 OSDefineMetaClassAndStructors(OSSerialize
, OSObject
)
46 OSMetaClassDefineReservedUnused(OSSerialize
, 0);
47 OSMetaClassDefineReservedUnused(OSSerialize
, 1);
48 OSMetaClassDefineReservedUnused(OSSerialize
, 2);
49 OSMetaClassDefineReservedUnused(OSSerialize
, 3);
50 OSMetaClassDefineReservedUnused(OSSerialize
, 4);
51 OSMetaClassDefineReservedUnused(OSSerialize
, 5);
52 OSMetaClassDefineReservedUnused(OSSerialize
, 6);
53 OSMetaClassDefineReservedUnused(OSSerialize
, 7);
57 OSSerialize::text() const
63 OSSerialize::clearText()
66 length
= sizeof(kOSSerializeBinarySignature
);
67 bzero(&data
[length
], capacity
- length
);
70 bzero((void *)data
, capacity
);
73 tags
->flushCollection();
77 OSSerialize::previouslySerialized(const OSMetaClassBase
*o
)
83 return binarySerialize(o
);
87 tagIdx
= tags
->getNextIndexOfObject(o
, 0);
89 // xx-review: no error checking here for addString calls!
92 addString("<reference IDREF=\"");
93 snprintf(temp
, sizeof(temp
), "%u", tagIdx
);
100 tags
->setObject(o
);// XXX check return
106 OSSerialize::addXMLStartTag(const OSMetaClassBase
*o
, const char *tagString
)
112 printf("class %s: xml serialize\n", o
->getMetaClass()->getClassName());
119 if (!addString(tagString
)) {
122 if (!addString(" ID=\"")) {
125 tagIdx
= tags
->getNextIndexOfObject(o
, 0);
126 assert(tagIdx
!= -1U);
127 snprintf(temp
, sizeof(temp
), "%u", tagIdx
);
128 if (!addString(temp
)) {
131 if (!addChar('\"')) {
141 OSSerialize::addXMLEndTag(const char *tagString
)
149 if (!addString(tagString
)) {
159 OSSerialize::addChar(const char c
)
162 printf("xml serialize\n");
166 // add char, possibly extending our capacity
167 if (length
>= capacity
&& length
>= ensureCapacity(capacity
+ capacityIncrement
)) {
171 data
[length
- 1] = c
;
178 OSSerialize::addString(const char *s
)
182 while (*s
&& (rc
= addChar(*s
++))) {
190 OSSerialize::initWithCapacity(unsigned int inCapacity
)
192 if (!super::init()) {
196 tags
= OSArray::withCapacity(256);
206 if (round_page_overflow(inCapacity
, &capacity
)) {
212 capacityIncrement
= capacity
;
214 // allocate from the kernel map so that we can safely map this data
215 // into user space (the primary use of the OSSerialize object)
217 kern_return_t rc
= kmem_alloc(kernel_map
, (vm_offset_t
*)&data
, capacity
, IOMemoryTag(kernel_map
));
223 bzero((void *)data
, capacity
);
226 OSCONTAINER_ACCUMSIZE(capacity
);
232 OSSerialize::withCapacity(unsigned int inCapacity
)
234 OSSerialize
*me
= new OSSerialize
;
236 if (me
&& !me
->initWithCapacity(inCapacity
)) {
245 OSSerialize::getLength() const
250 OSSerialize::getCapacity() const
255 OSSerialize::getCapacityIncrement() const
257 return capacityIncrement
;
260 OSSerialize::setCapacityIncrement(unsigned int increment
)
262 capacityIncrement
= (increment
)? increment
: 256;
263 return capacityIncrement
;
267 OSSerialize::ensureCapacity(unsigned int newCapacity
)
271 if (newCapacity
<= capacity
) {
275 if (round_page_overflow(newCapacity
, &newCapacity
)) {
279 kern_return_t rc
= kmem_realloc(kernel_map
,
282 (vm_offset_t
*)&newData
,
284 VM_KERN_MEMORY_IOKIT
);
286 OSCONTAINER_ACCUMSIZE(newCapacity
);
288 // kmem realloc does not free the old address range
289 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
290 OSCONTAINER_ACCUMSIZE(-((size_t)capacity
));
292 // kmem realloc does not zero out the new memory
293 // and this could end up going to user land
294 bzero(&newData
[capacity
], newCapacity
- capacity
);
297 capacity
= newCapacity
;
306 OSSafeReleaseNULL(tags
);
307 OSSafeReleaseNULL(indexData
);
310 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
311 OSCONTAINER_ACCUMSIZE( -((size_t)capacity
));
317 OSDefineMetaClassAndStructors(OSSerializer
, OSObject
)
319 OSSerializer
* OSSerializer::forTarget( void * target
,
320 OSSerializerCallback callback
, void * ref
)
322 OSSerializer
* thing
;
324 thing
= new OSSerializer
;
325 if (thing
&& !thing
->init()) {
331 thing
->target
= target
;
333 thing
->callback
= callback
;
339 OSSerializer::callbackToBlock(void * target __unused
, void * ref
,
340 OSSerialize
* serializer
)
342 return ((OSSerializerBlock
)ref
)(serializer
);
346 OSSerializer::withBlock(
347 OSSerializerBlock callback
)
349 OSSerializer
* serializer
;
350 OSSerializerBlock block
;
352 block
= Block_copy(callback
);
357 serializer
= (OSSerializer::forTarget(NULL
, &OSSerializer::callbackToBlock
, block
));
360 Block_release(block
);
367 OSSerializer::free(void)
369 if (callback
== &callbackToBlock
) {
377 OSSerializer::serialize( OSSerialize
* s
) const
379 return (*callback
)(target
, ref
, s
);