2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* OSSerialize.cpp created by rsulack on Wen 25-Nov-1998 */
24 #include <sys/cdefs.h>
27 #include <vm/vm_kern.h>
30 #include <libkern/c++/OSContainers.h>
31 #include <libkern/c++/OSLib.h>
32 #include <libkern/c++/OSDictionary.h>
34 #define super OSObject
36 OSDefineMetaClassAndStructors(OSSerialize
, OSObject
)
37 OSMetaClassDefineReservedUnused(OSSerialize
, 0);
38 OSMetaClassDefineReservedUnused(OSSerialize
, 1);
39 OSMetaClassDefineReservedUnused(OSSerialize
, 2);
40 OSMetaClassDefineReservedUnused(OSSerialize
, 3);
41 OSMetaClassDefineReservedUnused(OSSerialize
, 4);
42 OSMetaClassDefineReservedUnused(OSSerialize
, 5);
43 OSMetaClassDefineReservedUnused(OSSerialize
, 6);
44 OSMetaClassDefineReservedUnused(OSSerialize
, 7);
48 extern int debug_container_malloc_size
;
50 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
55 char * OSSerialize::text() const
60 void OSSerialize::clearText()
62 bzero((void *)data
, capacity
);
65 tags
->flushCollection();
68 bool OSSerialize::previouslySerialized(const OSMetaClassBase
*o
)
74 tagString
= (OSString
*)tags
->getObject((const OSSymbol
*) o
);
78 addString("<reference IDREF=\"");
79 addString(tagString
->getCStringNoCopy());
85 sprintf(temp
, "%u", tag
++);
86 tagString
= OSString::withCString(temp
);
88 // add to tag dictionary
89 tags
->setObject((const OSSymbol
*) o
, tagString
);// XXX check return
95 bool OSSerialize::addXMLStartTag(const OSMetaClassBase
*o
, const char *tagString
)
98 if (!addChar('<')) return false;
99 if (!addString(tagString
)) return false;
100 if (!addString(" ID=\"")) return false;
101 if (!addString(((OSString
*)tags
->getObject((const OSSymbol
*)o
))->getCStringNoCopy()))
103 if (!addChar('\"')) return false;
104 if (!addChar('>')) return false;
108 bool OSSerialize::addXMLEndTag(const char *tagString
)
111 if (!addChar('<')) return false;
112 if (!addChar('/')) return false;
113 if (!addString(tagString
)) return false;
114 if (!addChar('>')) return false;
118 bool OSSerialize::addChar(const char c
)
120 // add char, possibly extending our capacity
121 if (length
>= capacity
&& length
>=ensureCapacity(capacity
+capacityIncrement
))
124 data
[length
- 1] = c
;
130 bool OSSerialize::addString(const char *s
)
134 while (*s
&& (rc
= addChar(*s
++))) ;
139 bool OSSerialize::initWithCapacity(unsigned int inCapacity
)
144 tags
= OSDictionary::withCapacity(32);
151 capacity
= (inCapacity
) ? round_page_32(inCapacity
) : round_page_32(1);
152 capacityIncrement
= capacity
;
154 // allocate from the kernel map so that we can safely map this data
155 // into user space (the primary use of the OSSerialize object)
157 kern_return_t rc
= kmem_alloc(kernel_map
, (vm_offset_t
*)&data
, capacity
);
163 bzero((void *)data
, capacity
);
171 OSSerialize
*OSSerialize::withCapacity(unsigned int inCapacity
)
173 OSSerialize
*me
= new OSSerialize
;
175 if (me
&& !me
->initWithCapacity(inCapacity
)) {
183 unsigned int OSSerialize::getLength() const { return length
; }
184 unsigned int OSSerialize::getCapacity() const { return capacity
; }
185 unsigned int OSSerialize::getCapacityIncrement() const { return capacityIncrement
; }
186 unsigned int OSSerialize::setCapacityIncrement(unsigned int increment
)
188 capacityIncrement
= (increment
)? increment
: 256;
189 return capacityIncrement
;
192 unsigned int OSSerialize::ensureCapacity(unsigned int newCapacity
)
196 if (newCapacity
<= capacity
)
200 newCapacity
= round_page_32(newCapacity
);
202 kern_return_t rc
= kmem_realloc(kernel_map
,
205 (vm_offset_t
*)&newData
,
208 ACCUMSIZE(newCapacity
);
210 // kmem realloc does not free the old address range
211 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
212 ACCUMSIZE(-capacity
);
214 // kmem realloc does not zero out the new memory
215 // and this could end up going to user land
216 bzero(&newData
[capacity
], newCapacity
- capacity
);
219 capacity
= newCapacity
;
225 void OSSerialize::free()
231 kmem_free(kernel_map
, (vm_offset_t
)data
, capacity
);
232 ACCUMSIZE( -capacity
);
238 OSDefineMetaClassAndStructors(OSSerializer
, OSObject
)
240 OSSerializer
* OSSerializer::forTarget( void * target
,
241 OSSerializerCallback callback
, void * ref
)
243 OSSerializer
* thing
;
245 thing
= new OSSerializer
;
246 if( thing
&& !thing
->init()) {
252 thing
->target
= target
;
254 thing
->callback
= callback
;
259 bool OSSerializer::serialize( OSSerialize
* s
) const
261 return( (*callback
)(target
, ref
, s
) );