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
);
82 // xx-review: no error checking here for addString calls!
85 addString("<reference IDREF=\"");
86 addString(tagString
->getCStringNoCopy());
92 snprintf(temp
, sizeof(temp
), "%u", tag
++);
93 tagString
= OSString::withCString(temp
);
95 // add to tag dictionary
96 tags
->setObject((const OSSymbol
*) o
, tagString
);// XXX check return
102 bool OSSerialize::addXMLStartTag(const OSMetaClassBase
*o
, const char *tagString
)
105 if (!addChar('<')) return false;
106 if (!addString(tagString
)) return false;
107 if (!addString(" ID=\"")) return false;
108 if (!addString(((OSString
*)tags
->getObject((const OSSymbol
*)o
))->getCStringNoCopy()))
110 if (!addChar('\"')) return false;
111 if (!addChar('>')) return false;
115 bool OSSerialize::addXMLEndTag(const char *tagString
)
118 if (!addChar('<')) return false;
119 if (!addChar('/')) return false;
120 if (!addString(tagString
)) return false;
121 if (!addChar('>')) return false;
125 bool OSSerialize::addChar(const char c
)
127 // add char, possibly extending our capacity
128 if (length
>= capacity
&& length
>=ensureCapacity(capacity
+capacityIncrement
))
131 data
[length
- 1] = c
;
137 bool OSSerialize::addString(const char *s
)
141 while (*s
&& (rc
= addChar(*s
++))) ;
146 bool OSSerialize::initWithCapacity(unsigned int inCapacity
)
151 tags
= OSDictionary::withCapacity(32);
158 capacity
= (inCapacity
) ? round_page_32(inCapacity
) : round_page_32(1);
159 capacityIncrement
= capacity
;
161 // allocate from the kernel map so that we can safely map this data
162 // into user space (the primary use of the OSSerialize object)
164 kern_return_t rc
= kmem_alloc(kernel_map
, (vm_offset_t
*)&data
, capacity
);
170 bzero((void *)data
, capacity
);
178 OSSerialize
*OSSerialize::withCapacity(unsigned int inCapacity
)
180 OSSerialize
*me
= new OSSerialize
;
182 if (me
&& !me
->initWithCapacity(inCapacity
)) {
190 unsigned int OSSerialize::getLength() const { return length
; }
191 unsigned int OSSerialize::getCapacity() const { return capacity
; }
192 unsigned int OSSerialize::getCapacityIncrement() const { return capacityIncrement
; }
193 unsigned int OSSerialize::setCapacityIncrement(unsigned int increment
)
195 capacityIncrement
= (increment
)? increment
: 256;
196 return capacityIncrement
;
199 unsigned int OSSerialize::ensureCapacity(unsigned int newCapacity
)
203 if (newCapacity
<= capacity
)
207 newCapacity
= round_page_32(newCapacity
);
209 kern_return_t rc
= kmem_realloc(kernel_map
,
212 (vm_offset_t
*)&newData
,
215 ACCUMSIZE(newCapacity
);
217 // kmem realloc does not free the old address range
218 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
219 ACCUMSIZE(-capacity
);
221 // kmem realloc does not zero out the new memory
222 // and this could end up going to user land
223 bzero(&newData
[capacity
], newCapacity
- capacity
);
226 capacity
= newCapacity
;
232 void OSSerialize::free()
238 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
239 ACCUMSIZE( -capacity
);
245 OSDefineMetaClassAndStructors(OSSerializer
, OSObject
)
247 OSSerializer
* OSSerializer::forTarget( void * target
,
248 OSSerializerCallback callback
, void * ref
)
250 OSSerializer
* thing
;
252 thing
= new OSSerializer
;
253 if( thing
&& !thing
->init()) {
259 thing
->target
= target
;
261 thing
->callback
= callback
;
266 bool OSSerializer::serialize( OSSerialize
* s
) const
268 return( (*callback
)(target
, ref
, s
) );