]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
8ad349bb | 4 | * @APPLE_LICENSE_OSREFERENCE_HEADER_START@ |
1c79356b | 5 | * |
8ad349bb A |
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 | |
14 | * agreement. | |
15 | * | |
16 | * Please obtain a copy of the License at | |
17 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
18 | * file. | |
19 | * | |
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. | |
27 | * | |
28 | * @APPLE_LICENSE_OSREFERENCE_HEADER_END@ | |
1c79356b A |
29 | */ |
30 | /* OSSerialize.cpp created by rsulack on Wen 25-Nov-1998 */ | |
31 | ||
9bccf70c A |
32 | #include <sys/cdefs.h> |
33 | ||
34 | __BEGIN_DECLS | |
35 | #include <vm/vm_kern.h> | |
36 | __END_DECLS | |
37 | ||
1c79356b A |
38 | #include <libkern/c++/OSContainers.h> |
39 | #include <libkern/c++/OSLib.h> | |
40 | #include <libkern/c++/OSDictionary.h> | |
41 | ||
42 | #define super OSObject | |
43 | ||
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); | |
53 | ||
54 | #if OSALLOCDEBUG | |
55 | extern "C" { | |
56 | extern int debug_container_malloc_size; | |
57 | }; | |
58 | #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0) | |
59 | #else | |
60 | #define ACCUMSIZE(s) | |
61 | #endif | |
62 | ||
63 | char * OSSerialize::text() const | |
64 | { | |
65 | return data; | |
66 | } | |
67 | ||
68 | void OSSerialize::clearText() | |
69 | { | |
70 | bzero((void *)data, capacity); | |
71 | length = 1; | |
72 | tag = 0; | |
73 | tags->flushCollection(); | |
74 | } | |
75 | ||
76 | bool OSSerialize::previouslySerialized(const OSMetaClassBase *o) | |
77 | { | |
78 | char temp[16]; | |
79 | OSString *tagString; | |
80 | ||
81 | // look it up | |
82 | tagString = (OSString *)tags->getObject((const OSSymbol *) o); | |
83 | ||
84 | // does it exist? | |
85 | if (tagString) { | |
86 | addString("<reference IDREF=\""); | |
87 | addString(tagString->getCStringNoCopy()); | |
88 | addString("\"/>"); | |
89 | return true; | |
90 | } | |
91 | ||
92 | // build a tag | |
93 | sprintf(temp, "%u", tag++); | |
94 | tagString = OSString::withCString(temp); | |
95 | ||
96 | // add to tag dictionary | |
97 | tags->setObject((const OSSymbol *) o, tagString);// XXX check return | |
98 | tagString->release(); | |
99 | ||
100 | return false; | |
101 | } | |
102 | ||
103 | bool OSSerialize::addXMLStartTag(const OSMetaClassBase *o, const char *tagString) | |
104 | { | |
105 | ||
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())) | |
110 | return false; | |
111 | if (!addChar('\"')) return false; | |
112 | if (!addChar('>')) return false; | |
113 | return true; | |
114 | } | |
115 | ||
116 | bool OSSerialize::addXMLEndTag(const char *tagString) | |
117 | { | |
118 | ||
119 | if (!addChar('<')) return false; | |
120 | if (!addChar('/')) return false; | |
121 | if (!addString(tagString)) return false; | |
122 | if (!addChar('>')) return false; | |
123 | return true; | |
124 | } | |
125 | ||
126 | bool OSSerialize::addChar(const char c) | |
127 | { | |
128 | // add char, possibly extending our capacity | |
129 | if (length >= capacity && length >=ensureCapacity(capacity+capacityIncrement)) | |
130 | return false; | |
131 | ||
132 | data[length - 1] = c; | |
133 | length++; | |
134 | ||
135 | return true; | |
136 | } | |
137 | ||
138 | bool OSSerialize::addString(const char *s) | |
139 | { | |
140 | bool rc = false; | |
141 | ||
142 | while (*s && (rc = addChar(*s++))) ; | |
143 | ||
144 | return rc; | |
145 | } | |
146 | ||
147 | bool OSSerialize::initWithCapacity(unsigned int inCapacity) | |
148 | { | |
149 | if (!super::init()) | |
150 | return false; | |
151 | ||
152 | tags = OSDictionary::withCapacity(32); | |
153 | if (!tags) { | |
154 | return false; | |
155 | } | |
156 | ||
157 | tag = 0; | |
158 | length = 1; | |
55e303ae | 159 | capacity = (inCapacity) ? round_page_32(inCapacity) : round_page_32(1); |
9bccf70c A |
160 | capacityIncrement = capacity; |
161 | ||
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) | |
164 | ||
165 | kern_return_t rc = kmem_alloc(kernel_map, (vm_offset_t *)&data, capacity); | |
166 | if (rc) { | |
1c79356b A |
167 | tags->release(); |
168 | tags = 0; | |
169 | return false; | |
170 | } | |
171 | bzero((void *)data, capacity); | |
172 | ||
173 | ||
174 | ACCUMSIZE(capacity); | |
175 | ||
176 | return true; | |
177 | } | |
178 | ||
179 | OSSerialize *OSSerialize::withCapacity(unsigned int inCapacity) | |
180 | { | |
181 | OSSerialize *me = new OSSerialize; | |
182 | ||
183 | if (me && !me->initWithCapacity(inCapacity)) { | |
55e303ae | 184 | me->release(); |
1c79356b A |
185 | return 0; |
186 | } | |
187 | ||
188 | return me; | |
189 | } | |
190 | ||
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) | |
195 | { | |
196 | capacityIncrement = (increment)? increment : 256; | |
197 | return capacityIncrement; | |
198 | } | |
199 | ||
200 | unsigned int OSSerialize::ensureCapacity(unsigned int newCapacity) | |
201 | { | |
202 | char *newData; | |
1c79356b A |
203 | |
204 | if (newCapacity <= capacity) | |
205 | return capacity; | |
206 | ||
207 | // round up | |
55e303ae | 208 | newCapacity = round_page_32(newCapacity); |
9bccf70c A |
209 | |
210 | kern_return_t rc = kmem_realloc(kernel_map, | |
211 | (vm_offset_t)data, | |
212 | capacity, | |
213 | (vm_offset_t *)&newData, | |
214 | newCapacity); | |
215 | if (!rc) { | |
216 | ACCUMSIZE(newCapacity); | |
217 | ||
218 | // kmem realloc does not free the old address range | |
219 | kmem_free(kernel_map, (vm_offset_t)data, capacity); | |
220 | ACCUMSIZE(-capacity); | |
221 | ||
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); | |
225 | ||
226 | data = newData; | |
227 | capacity = newCapacity; | |
1c79356b A |
228 | } |
229 | ||
230 | return capacity; | |
231 | } | |
232 | ||
233 | void OSSerialize::free() | |
234 | { | |
235 | if (tags) | |
236 | tags->release(); | |
237 | ||
238 | if (data) { | |
9bccf70c | 239 | kmem_free(kernel_map, (vm_offset_t)data, capacity); |
1c79356b A |
240 | ACCUMSIZE( -capacity ); |
241 | } | |
242 | super::free(); | |
243 | } | |
244 | ||
245 | ||
246 | OSDefineMetaClassAndStructors(OSSerializer, OSObject) | |
247 | ||
248 | OSSerializer * OSSerializer::forTarget( void * target, | |
55e303ae | 249 | OSSerializerCallback callback, void * ref ) |
1c79356b A |
250 | { |
251 | OSSerializer * thing; | |
252 | ||
253 | thing = new OSSerializer; | |
254 | if( thing && !thing->init()) { | |
255 | thing->release(); | |
256 | thing = 0; | |
257 | } | |
258 | ||
259 | if( thing) { | |
260 | thing->target = target; | |
261 | thing->ref = ref; | |
262 | thing->callback = callback; | |
263 | } | |
264 | return( thing ); | |
265 | } | |
266 | ||
267 | bool OSSerializer::serialize( OSSerialize * s ) const | |
268 | { | |
269 | return( (*callback)(target, ref, s) ); | |
270 | } |