2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* OSSerialize.cpp created by rsulack on Wen 25-Nov-1998 */
32 #include <sys/cdefs.h>
35 #include <vm/vm_kern.h>
38 #include <libkern/c++/OSContainers.h>
39 #include <libkern/c++/OSLib.h>
40 #include <libkern/c++/OSDictionary.h>
42 #define super OSObject
44 OSDefineMetaClassAndStructors(OSSerialize
, OSObject
)
45 OSMetaClassDefineReservedUnused(OSSerialize
, 0);
46 OSMetaClassDefineReservedUnused(OSSerialize
, 1);
47 OSMetaClassDefineReservedUnused(OSSerialize
, 2);
48 OSMetaClassDefineReservedUnused(OSSerialize
, 3);
49 OSMetaClassDefineReservedUnused(OSSerialize
, 4);
50 OSMetaClassDefineReservedUnused(OSSerialize
, 5);
51 OSMetaClassDefineReservedUnused(OSSerialize
, 6);
52 OSMetaClassDefineReservedUnused(OSSerialize
, 7);
56 extern int debug_container_malloc_size
;
58 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
63 char * OSSerialize::text() const
68 void OSSerialize::clearText()
70 bzero((void *)data
, capacity
);
73 tags
->flushCollection();
76 bool OSSerialize::previouslySerialized(const OSMetaClassBase
*o
)
82 tagString
= (OSString
*)tags
->getObject((const OSSymbol
*) o
);
86 addString("<reference IDREF=\"");
87 addString(tagString
->getCStringNoCopy());
93 sprintf(temp
, "%u", tag
++);
94 tagString
= OSString::withCString(temp
);
96 // add to tag dictionary
97 tags
->setObject((const OSSymbol
*) o
, tagString
);// XXX check return
103 bool OSSerialize::addXMLStartTag(const OSMetaClassBase
*o
, const char *tagString
)
106 if (!addChar('<')) return false;
107 if (!addString(tagString
)) return false;
108 if (!addString(" ID=\"")) return false;
109 if (!addString(((OSString
*)tags
->getObject((const OSSymbol
*)o
))->getCStringNoCopy()))
111 if (!addChar('\"')) return false;
112 if (!addChar('>')) return false;
116 bool OSSerialize::addXMLEndTag(const char *tagString
)
119 if (!addChar('<')) return false;
120 if (!addChar('/')) return false;
121 if (!addString(tagString
)) return false;
122 if (!addChar('>')) return false;
126 bool OSSerialize::addChar(const char c
)
128 // add char, possibly extending our capacity
129 if (length
>= capacity
&& length
>=ensureCapacity(capacity
+capacityIncrement
))
132 data
[length
- 1] = c
;
138 bool OSSerialize::addString(const char *s
)
142 while (*s
&& (rc
= addChar(*s
++))) ;
147 bool OSSerialize::initWithCapacity(unsigned int inCapacity
)
152 tags
= OSDictionary::withCapacity(32);
159 capacity
= (inCapacity
) ? round_page_32(inCapacity
) : round_page_32(1);
160 capacityIncrement
= capacity
;
162 // allocate from the kernel map so that we can safely map this data
163 // into user space (the primary use of the OSSerialize object)
165 kern_return_t rc
= kmem_alloc(kernel_map
, (vm_offset_t
*)&data
, capacity
);
171 bzero((void *)data
, capacity
);
179 OSSerialize
*OSSerialize::withCapacity(unsigned int inCapacity
)
181 OSSerialize
*me
= new OSSerialize
;
183 if (me
&& !me
->initWithCapacity(inCapacity
)) {
191 unsigned int OSSerialize::getLength() const { return length
; }
192 unsigned int OSSerialize::getCapacity() const { return capacity
; }
193 unsigned int OSSerialize::getCapacityIncrement() const { return capacityIncrement
; }
194 unsigned int OSSerialize::setCapacityIncrement(unsigned int increment
)
196 capacityIncrement
= (increment
)? increment
: 256;
197 return capacityIncrement
;
200 unsigned int OSSerialize::ensureCapacity(unsigned int newCapacity
)
204 if (newCapacity
<= capacity
)
208 newCapacity
= round_page_32(newCapacity
);
210 kern_return_t rc
= kmem_realloc(kernel_map
,
213 (vm_offset_t
*)&newData
,
216 ACCUMSIZE(newCapacity
);
218 // kmem realloc does not free the old address range
219 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
220 ACCUMSIZE(-capacity
);
222 // kmem realloc does not zero out the new memory
223 // and this could end up going to user land
224 bzero(&newData
[capacity
], newCapacity
- capacity
);
227 capacity
= newCapacity
;
233 void OSSerialize::free()
239 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
240 ACCUMSIZE( -capacity
);
246 OSDefineMetaClassAndStructors(OSSerializer
, OSObject
)
248 OSSerializer
* OSSerializer::forTarget( void * target
,
249 OSSerializerCallback callback
, void * ref
)
251 OSSerializer
* thing
;
253 thing
= new OSSerializer
;
254 if( thing
&& !thing
->init()) {
260 thing
->target
= target
;
262 thing
->callback
= callback
;
267 bool OSSerializer::serialize( OSSerialize
* s
) const
269 return( (*callback
)(target
, ref
, s
) );