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>
40 #define super OSObject
42 OSDefineMetaClassAndStructors(OSSerialize
, OSObject
)
43 OSMetaClassDefineReservedUnused(OSSerialize
, 0);
44 OSMetaClassDefineReservedUnused(OSSerialize
, 1);
45 OSMetaClassDefineReservedUnused(OSSerialize
, 2);
46 OSMetaClassDefineReservedUnused(OSSerialize
, 3);
47 OSMetaClassDefineReservedUnused(OSSerialize
, 4);
48 OSMetaClassDefineReservedUnused(OSSerialize
, 5);
49 OSMetaClassDefineReservedUnused(OSSerialize
, 6);
50 OSMetaClassDefineReservedUnused(OSSerialize
, 7);
54 extern int debug_container_malloc_size
;
56 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
61 char * OSSerialize::text() const
66 void OSSerialize::clearText()
68 bzero((void *)data
, capacity
);
71 tags
->flushCollection();
74 bool OSSerialize::previouslySerialized(const OSMetaClassBase
*o
)
80 tagString
= (OSString
*)tags
->getObject((const OSSymbol
*) o
);
84 addString("<reference IDREF=\"");
85 addString(tagString
->getCStringNoCopy());
91 snprintf(temp
, sizeof(temp
), "%u", tag
++);
92 tagString
= OSString::withCString(temp
);
94 // add to tag dictionary
95 tags
->setObject((const OSSymbol
*) o
, tagString
);// XXX check return
101 bool OSSerialize::addXMLStartTag(const OSMetaClassBase
*o
, const char *tagString
)
104 if (!addChar('<')) return false;
105 if (!addString(tagString
)) return false;
106 if (!addString(" ID=\"")) return false;
107 if (!addString(((OSString
*)tags
->getObject((const OSSymbol
*)o
))->getCStringNoCopy()))
109 if (!addChar('\"')) return false;
110 if (!addChar('>')) return false;
114 bool OSSerialize::addXMLEndTag(const char *tagString
)
117 if (!addChar('<')) return false;
118 if (!addChar('/')) return false;
119 if (!addString(tagString
)) return false;
120 if (!addChar('>')) return false;
124 bool OSSerialize::addChar(const char c
)
126 // add char, possibly extending our capacity
127 if (length
>= capacity
&& length
>=ensureCapacity(capacity
+capacityIncrement
))
130 data
[length
- 1] = c
;
136 bool OSSerialize::addString(const char *s
)
140 while (*s
&& (rc
= addChar(*s
++))) ;
145 bool OSSerialize::initWithCapacity(unsigned int inCapacity
)
150 tags
= OSDictionary::withCapacity(32);
157 capacity
= (inCapacity
) ? round_page_32(inCapacity
) : round_page_32(1);
158 capacityIncrement
= capacity
;
160 // allocate from the kernel map so that we can safely map this data
161 // into user space (the primary use of the OSSerialize object)
163 kern_return_t rc
= kmem_alloc(kernel_map
, (vm_offset_t
*)&data
, capacity
);
169 bzero((void *)data
, capacity
);
177 OSSerialize
*OSSerialize::withCapacity(unsigned int inCapacity
)
179 OSSerialize
*me
= new OSSerialize
;
181 if (me
&& !me
->initWithCapacity(inCapacity
)) {
189 unsigned int OSSerialize::getLength() const { return length
; }
190 unsigned int OSSerialize::getCapacity() const { return capacity
; }
191 unsigned int OSSerialize::getCapacityIncrement() const { return capacityIncrement
; }
192 unsigned int OSSerialize::setCapacityIncrement(unsigned int increment
)
194 capacityIncrement
= (increment
)? increment
: 256;
195 return capacityIncrement
;
198 unsigned int OSSerialize::ensureCapacity(unsigned int newCapacity
)
202 if (newCapacity
<= capacity
)
206 newCapacity
= round_page_32(newCapacity
);
208 kern_return_t rc
= kmem_realloc(kernel_map
,
211 (vm_offset_t
*)&newData
,
214 ACCUMSIZE(newCapacity
);
216 // kmem realloc does not free the old address range
217 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
218 ACCUMSIZE(-capacity
);
220 // kmem realloc does not zero out the new memory
221 // and this could end up going to user land
222 bzero(&newData
[capacity
], newCapacity
- capacity
);
225 capacity
= newCapacity
;
231 void OSSerialize::free()
237 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
238 ACCUMSIZE( -capacity
);
244 OSDefineMetaClassAndStructors(OSSerializer
, OSObject
)
246 OSSerializer
* OSSerializer::forTarget( void * target
,
247 OSSerializerCallback callback
, void * ref
)
249 OSSerializer
* thing
;
251 thing
= new OSSerializer
;
252 if( thing
&& !thing
->init()) {
258 thing
->target
= target
;
260 thing
->callback
= callback
;
265 bool OSSerializer::serialize( OSSerialize
* s
) const
267 return( (*callback
)(target
, ref
, s
) );