2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* OSSerialize.cpp created by rsulack on Wen 25-Nov-1998 */
27 #include <sys/cdefs.h>
30 #include <vm/vm_kern.h>
33 #include <libkern/c++/OSContainers.h>
34 #include <libkern/c++/OSLib.h>
35 #include <libkern/c++/OSDictionary.h>
37 #define super OSObject
39 OSDefineMetaClassAndStructors(OSSerialize
, OSObject
)
40 OSMetaClassDefineReservedUnused(OSSerialize
, 0);
41 OSMetaClassDefineReservedUnused(OSSerialize
, 1);
42 OSMetaClassDefineReservedUnused(OSSerialize
, 2);
43 OSMetaClassDefineReservedUnused(OSSerialize
, 3);
44 OSMetaClassDefineReservedUnused(OSSerialize
, 4);
45 OSMetaClassDefineReservedUnused(OSSerialize
, 5);
46 OSMetaClassDefineReservedUnused(OSSerialize
, 6);
47 OSMetaClassDefineReservedUnused(OSSerialize
, 7);
51 extern int debug_container_malloc_size
;
53 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
58 char * OSSerialize::text() const
63 void OSSerialize::clearText()
65 bzero((void *)data
, capacity
);
68 tags
->flushCollection();
71 bool OSSerialize::previouslySerialized(const OSMetaClassBase
*o
)
77 tagString
= (OSString
*)tags
->getObject((const OSSymbol
*) o
);
81 addString("<reference IDREF=\"");
82 addString(tagString
->getCStringNoCopy());
88 sprintf(temp
, "%u", tag
++);
89 tagString
= OSString::withCString(temp
);
91 // add to tag dictionary
92 tags
->setObject((const OSSymbol
*) o
, tagString
);// XXX check return
98 bool OSSerialize::addXMLStartTag(const OSMetaClassBase
*o
, const char *tagString
)
101 if (!addChar('<')) return false;
102 if (!addString(tagString
)) return false;
103 if (!addString(" ID=\"")) return false;
104 if (!addString(((OSString
*)tags
->getObject((const OSSymbol
*)o
))->getCStringNoCopy()))
106 if (!addChar('\"')) return false;
107 if (!addChar('>')) return false;
111 bool OSSerialize::addXMLEndTag(const char *tagString
)
114 if (!addChar('<')) return false;
115 if (!addChar('/')) return false;
116 if (!addString(tagString
)) return false;
117 if (!addChar('>')) return false;
121 bool OSSerialize::addChar(const char c
)
123 // add char, possibly extending our capacity
124 if (length
>= capacity
&& length
>=ensureCapacity(capacity
+capacityIncrement
))
127 data
[length
- 1] = c
;
133 bool OSSerialize::addString(const char *s
)
137 while (*s
&& (rc
= addChar(*s
++))) ;
142 bool OSSerialize::initWithCapacity(unsigned int inCapacity
)
147 tags
= OSDictionary::withCapacity(32);
154 capacity
= (inCapacity
) ? round_page(inCapacity
) : round_page(1);
155 capacityIncrement
= capacity
;
157 // allocate from the kernel map so that we can safely map this data
158 // into user space (the primary use of the OSSerialize object)
160 kern_return_t rc
= kmem_alloc(kernel_map
, (vm_offset_t
*)&data
, capacity
);
166 bzero((void *)data
, capacity
);
174 OSSerialize
*OSSerialize::withCapacity(unsigned int inCapacity
)
176 OSSerialize
*me
= new OSSerialize
;
178 if (me
&& !me
->initWithCapacity(inCapacity
)) {
186 unsigned int OSSerialize::getLength() const { return length
; }
187 unsigned int OSSerialize::getCapacity() const { return capacity
; }
188 unsigned int OSSerialize::getCapacityIncrement() const { return capacityIncrement
; }
189 unsigned int OSSerialize::setCapacityIncrement(unsigned int increment
)
191 capacityIncrement
= (increment
)? increment
: 256;
192 return capacityIncrement
;
195 unsigned int OSSerialize::ensureCapacity(unsigned int newCapacity
)
199 if (newCapacity
<= capacity
)
203 newCapacity
= round_page(newCapacity
);
205 kern_return_t rc
= kmem_realloc(kernel_map
,
208 (vm_offset_t
*)&newData
,
211 ACCUMSIZE(newCapacity
);
213 // kmem realloc does not free the old address range
214 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
215 ACCUMSIZE(-capacity
);
217 // kmem realloc does not zero out the new memory
218 // and this could end up going to user land
219 bzero(&newData
[capacity
], newCapacity
- capacity
);
222 capacity
= newCapacity
;
228 void OSSerialize::free()
234 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
235 ACCUMSIZE( -capacity
);
241 OSDefineMetaClassAndStructors(OSSerializer
, OSObject
)
243 OSSerializer
* OSSerializer::forTarget( void * target
,
244 OSSerializerCallback callback
, void * ref
= 0 )
246 OSSerializer
* thing
;
248 thing
= new OSSerializer
;
249 if( thing
&& !thing
->init()) {
255 thing
->target
= target
;
257 thing
->callback
= callback
;
262 bool OSSerializer::serialize( OSSerialize
* s
) const
264 return( (*callback
)(target
, ref
, s
) );