]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSData.h
5c499cc8c13de023b704c0883c2f346bee4ab711
[apple/xnu.git] / libkern / libkern / c++ / OSData.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* IOData.h created by rsulack on Wed 17-Sep-1997 */
29 /* IOData.h converted to C++ by gvdl on Fri 1998-10-30 */
30
31 #ifndef _OS_OSDATA_H
32 #define _OS_OSDATA_H
33
34 #include <libkern/c++/OSObject.h>
35
36 class OSString;
37
38 /*!
39 * @header
40 *
41 * @abstract
42 * This header declares the OSData container class.
43 */
44
45
46 /*!
47 * @class OSData
48 *
49 * @abstract
50 * OSData wraps an array of bytes in a C++ object
51 * for use in Libkern collections.
52 *
53 * @discussion
54 * OSData represents an array of bytes as a Libkern C++ object.
55 * OSData objects are mutable:
56 * You can add bytes to them and
57 * overwrite portions of the byte array.
58 *
59 * <b>Use Restrictions</b>
60 *
61 * With very few exceptions in the I/O Kit, all Libkern-based C++
62 * classes, functions, and macros are <b>unsafe</b>
63 * to use in a primary interrupt context.
64 * Consult the I/O Kit documentation related to primary interrupts
65 * for more information.
66 *
67 * OSData provides no concurrency protection;
68 * it's up to the usage context to provide any protection necessary.
69 * Some portions of the I/O Kit, such as
70 * @link //apple_ref/doc/class/IORegistryEntry IORegistryEntry@/link,
71 * handle synchronization via defined member functions for setting
72 * properties.
73 */
74 class OSData : public OSObject
75 {
76 OSDeclareDefaultStructors(OSData)
77 friend class OSSerialize;
78
79 protected:
80 void * data;
81 unsigned int length;
82 unsigned int capacity;
83 unsigned int capacityIncrement;
84
85 #ifdef XNU_KERNEL_PRIVATE
86 /* Available within xnu source only */
87 public:
88 typedef void (*DeallocFunction)(void * ptr, unsigned int length);
89 protected:
90 struct ExpansionData
91 {
92 DeallocFunction deallocFunction;
93 bool disableSerialization;
94 };
95 #else
96 private:
97 typedef void (*DeallocFunction)(void * ptr, unsigned int length);
98 protected:
99 struct ExpansionData;
100 #endif
101
102 /* Reserved for future use. (Internal use only) */
103 ExpansionData * reserved;
104
105 public:
106
107 /*!
108 * @function withCapacity
109 *
110 * @abstract
111 * Creates and initializes an empty instance of OSData.
112 *
113 * @param capacity The initial capacity of the OSData object in bytes.
114 *
115 * @result
116 * An instance of OSData with a reference count of 1;
117 * <code>NULL</code> on failure.
118 *
119 * @discussion
120 * <code>capacity</code> may be zero.
121 * The OSData object will allocate a buffer internally
122 * when necessary, and will grow as needed to accommodate more bytes
123 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
124 * for which a nonzero initial capacity is a hard limit).
125 */
126 static OSData * withCapacity(unsigned int capacity);
127
128
129 /*!
130 * @function withBytes
131 *
132 * @abstract
133 * Creates and initializes an instance of OSData
134 * with a copy of the provided data buffer.
135 *
136 * @param bytes The buffer of data to copy.
137 * @param numBytes The length of <code>bytes</code>.
138 *
139 * @result
140 * An instance of OSData containing a copy of the provided byte array,
141 * with a reference count of 1;
142 * <code>NULL</code> on failure.
143 *
144 * @discussion
145 * The new OSData object will grow as needed to accommodate more bytes
146 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
147 * for which a nonzero initial capacity is a hard limit).
148 */
149 static OSData * withBytes(
150 const void * bytes,
151 unsigned int numBytes);
152
153
154 /*!
155 * @function withBytesNoCopy
156 *
157 * @abstract
158 * Creates and initializes an instance of OSData
159 * that shares the provided data buffer.
160 *
161 * @param bytes The buffer of data to represent.
162 * @param numBytes The length of <code>bytes</code>.
163 *
164 * @result
165 * A instance of OSData that shares the provided byte array,
166 * with a reference count of 1;
167 * <code>NULL</coe> on failure.
168 *
169 * @discussion
170 * An OSData object created with this function
171 * does not claim ownership
172 * of the data buffer, but shares it with the caller.
173 * When the caller determines that the OSData object has actually been freed,
174 * it can safely dispose of the data buffer.
175 * Conversely, if it frees the shared data buffer,
176 * it must not attempt to use the OSData object and should release it.
177 *
178 * An OSData object created with shared external data cannot append bytes,
179 * but you can get the byte pointer and
180 * modify bytes within the shared buffer.
181 */
182 static OSData * withBytesNoCopy(
183 void * bytes,
184 unsigned int numBytes);
185
186
187 /*!
188 * @function withData
189 *
190 * @abstract
191 * Creates and initializes an instance of OSData
192 * with contents copied from another OSData object.
193 *
194 * @param inData An OSData object that provides the initial data.
195 *
196 * @result
197 * An instance of OSData containing a copy of the data in <code>inData</code>,
198 * with a reference count of 1;
199 * <code>NULL</code> on failure.
200 *
201 * @discussion
202 * The new OSData object will grow as needed to accommodate more bytes
203 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
204 * for which a nonzero initial capacity is a hard limit).
205 */
206 static OSData * withData(const OSData * inData);
207
208
209 /*!
210 * @function withData
211 *
212 * @abstract
213 * Creates and initializes an instance of OSData
214 * with contents copied from a range within another OSData object.
215 *
216 * @param inData An OSData object that provides the initial data.
217 * @param start The starting index from which bytes will be copied.
218 * @param numBytes The number of bytes to be copied from <code>start</code>.
219 *
220 * @result
221 * An instance of OSData containing a copy
222 * of the specified data range from <code>inData</code>,
223 * with a reference count of 1;
224 * <code>NULL</code> on failure.
225 *
226 * @discussion
227 * The new OSData object will grow as needed to accommodate more bytes
228 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
229 * for which a nonzero initial capacity is a hard limit).
230 */
231 static OSData * withData(
232 const OSData * inData,
233 unsigned int start,
234 unsigned int numBytes);
235
236
237 /*!
238 * @function initWithCapacity
239 *
240 * @abstract
241 * Initializes an instance of OSData.
242 *
243 * @param capacity The initial capacity of the OSData object in bytes.
244 *
245 * @result
246 * <code>true</code> on success, <code>false</code> on failure.
247 *
248 * @discussion
249 * Not for general use. Use the static instance creation method
250 * <code>@link
251 * //apple_ref/cpp/clm/OSData/withCapacity/staticOSData*\/(unsignedint)
252 * withCapacity@/link</code> instead.
253 *
254 * <code>capacity</code> may be zero.
255 * The OSData object will allocate a buffer internally
256 * when necessary, and will grow as needed to accommodate more bytes
257 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
258 * for which a nonzero initial capacity is a hard limit).
259 */
260 virtual bool initWithCapacity(unsigned int capacity);
261
262
263 /*!
264 * @function initWithBytes
265 *
266 * @abstract
267 * Initializes an instance of OSData
268 * with a copy of the provided data buffer.
269 *
270 * @param bytes The buffer of data to copy.
271 * @param numBytes The length of <code>bytes</code>.
272 *
273 * @result
274 * <code>true</code> on success, <code>false</code> on failure.
275 *
276 * @discussion
277 * Not for general use. Use the static instance creation method
278 * <code>@link withBytes withBytes@/link</code> instead.
279 *
280 * The new OSData object will grow as needed to accommodate more bytes
281 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
282 * for which a nonzero initial capacity is a hard limit).
283 */
284 virtual bool initWithBytes(
285 const void * bytes,
286 unsigned int numBytes);
287
288
289 /*!
290 * @function initWithBytesNoCopy
291 *
292 * @abstract
293 * Initializes an instance of OSData
294 * to share the provided data buffer.
295 *
296 * @param bytes The buffer of data to represent.
297 * @param numBytes The length of <code>bytes</code>.
298 *
299 * @result
300 * <code>true</code> on success, <code>false</code> on failure.
301 *
302 * @discussion
303 * Not for general use. Use the static instance creation method
304 * <code>@link withBytesNoCopy withBytesNoCopy@/link</code> instead.
305 *
306 * An OSData object initialized with this function
307 * does not claim ownership
308 * of the data buffer, but merely shares it with the caller.
309 *
310 * An OSData object created with shared external data cannot append bytes,
311 * but you can get the byte pointer and
312 * modify bytes within the shared buffer.
313 */
314 virtual bool initWithBytesNoCopy(
315 void * bytes,
316 unsigned int numBytes);
317
318
319 /*!
320 * @function initWithData
321 *
322 * @abstract
323 * Creates and initializes an instance of OSData
324 * with contents copied from another OSData object.
325 *
326 * @param inData An OSData object that provides the initial data.
327 *
328 * @result
329 * <code>true</code> on success, <code>false</code> on failure.
330 *
331 * @discussion
332 * Not for general use. Use the static instance creation method
333 * <code>@link
334 * //apple_ref/cpp/clm/OSData/withData/staticOSData*\/(constOSData*)
335 * withData(OSData *)@/link</code>
336 * instead.
337 *
338 * The new OSData object will grow as needed to accommodate more bytes
339 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
340 * for which a nonzero initial capacity is a hard limit).
341 */
342 virtual bool initWithData(const OSData * inData);
343
344
345 /*!
346 * @function initWithData
347 *
348 * @abstract
349 * Initializes an instance of OSData
350 * with contents copied from a range within another OSData object.
351 *
352 * @param inData An OSData object that provides the initial data.
353 * @param start The starting index from which bytes will be copied.
354 * @param numBytes The number of bytes to be copied from <code>start</code>.
355 *
356 * @result
357 * Returns <code>true</code> on success, <code>false</code> on failure.
358 *
359 * @discussion
360 * Not for general use. Use the static instance creation method
361 * <code>@link
362 * //apple_ref/cpp/clm/OSData/withData/staticOSData*\/(constOSData*,unsignedint,unsignedint)
363 * withData(OSData *, unsigned int, unsigned int)@/link</code>
364 * instead.
365 *
366 * The new OSData object will grow as needed to accommodate more bytes
367 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
368 * for which a nonzero initial capacity is a hard limit).
369 */
370 virtual bool initWithData(
371 const OSData * inData,
372 unsigned int start,
373 unsigned int numBytes);
374
375
376 /*!
377 * @function free
378 *
379 * @abstract
380 * Deallocates or releases any resources
381 * used by the OSDictionary instance.
382 *
383 * @discussion
384 * This function should not be called directly;
385 * use
386 * <code>@link
387 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
388 * release@/link</code>
389 * instead.
390 */
391 virtual void free();
392
393
394 /*!
395 * @function getLength
396 *
397 * @abstract
398 * Returns the number of bytes in or referenced by the OSData object.
399 *
400 * @result
401 * The number of bytes in or referenced by the OSData object.
402 */
403 virtual unsigned int getLength() const;
404
405
406 /*!
407 * @function getCapacity
408 *
409 * @abstract
410 * Returns the total number of bytes the OSData can store without reallocating.
411 *
412 * @result
413 * The total number bytes the OSData can store without reallocating.
414 *
415 * @discussion
416 * OSData objects grow when full to accommodate additional bytes.
417 * See
418 * <code>@link
419 * //apple_ref/cpp/instm/OSData/getCapacityIncrement/virtualunsignedint/()
420 * getCapacityIncrement@/link</code>
421 * and
422 * <code>@link
423 * //apple_ref/cpp/instm/OSData/ensureCapacity/virtualunsignedint/(unsignedint)
424 * ensureCapacity@/link</code>.
425 *
426 * OSData objects created or initialized to use a shared buffer
427 * do not make use of this attribute, and return -1 from this function.
428 */
429 virtual unsigned int getCapacity() const;
430
431
432 /*!
433 * @function getCapacityIncrement
434 *
435 * @abstract
436 * Returns the storage increment of the OSData object.
437 *
438 * @result
439 * The storage increment of the OSData object.
440 *
441 * @discussion
442 * An OSData object allocates storage for bytes in multiples
443 * of the capacity increment.
444 *
445 * OSData objects created or initialized to use a shared buffer
446 * do not make use of this attribute.
447 */
448 virtual unsigned int getCapacityIncrement() const;
449
450
451 /*!
452 * @function setCapacityIncrement
453 *
454 * @abstract
455 * Sets the storage increment of the array.
456 *
457 * @result
458 * The original storage increment of the array.
459 *
460 * @discussion
461 * An OSArray allocates storage for objects in multiples
462 * of the capacity increment.
463 *
464 * OSData objects created or initialized to use a shared buffer
465 * do not make use of this attribute.
466 */
467 virtual unsigned int setCapacityIncrement(unsigned increment);
468
469
470 // xx-review: does not check for capacity == EXTERNAL
471
472 /*!
473 * @function ensureCapacity
474 *
475 * @abstract
476 * Ensures the array has enough space
477 * to store the requested number of bytes.
478 *
479 * @param newCapacity The total number of bytes the OSData object
480 * should be able to store.
481 *
482 * @result
483 * Returns the new capacity of the OSData object,
484 * which may be different from the number requested
485 * (if smaller, reallocation of storage failed).
486 *
487 * @discussion
488 * This function immediately resizes the OSData's buffer, if necessary,
489 * to accommodate at least <code>newCapacity</code> bytes.
490 * If <code>newCapacity</code> is not greater than the current capacity,
491 * or if an allocation error occurs, the original capacity is returned.
492 *
493 * There is no way to reduce the capacity of an OSData.
494 *
495 * An OSData object created "NoCopy" does not allow resizing.
496 */
497 virtual unsigned int ensureCapacity(unsigned int newCapacity);
498
499
500 /*!
501 * @function appendBytes
502 *
503 * @abstract
504 * Appends a buffer of bytes to the OSData object's internal data buffer.
505 *
506 * @param bytes A pointer to the data to append.
507 * If <code>bytes</code> is <code>NULL</code>
508 * then a zero-filled buffer of length <code>numBytes</code>
509 * is appended.
510 * @param numBytes The number of bytes from <code>bytes</code> to append.
511 *
512 * @result
513 * <code>true</code> if the new data was successfully added,
514 * <code>false</code> on failure.
515 *
516 * @discussion
517 * This function immediately resizes the OSData's buffer, if necessary,
518 * to accommodate the new total size.
519 *
520 * An OSData object created "NoCopy" does not allow bytes
521 * to be appended.
522 */
523 virtual bool appendBytes(
524 const void * bytes,
525 unsigned int numBytes);
526
527
528 /*!
529 * @function appendBytes
530 *
531 * @abstract
532 * Appends the data contained in another OSData object.
533 *
534 * @param aDataObj The OSData object whose contents will be appended.
535 *
536 * @result
537 * <code>true</code> if the new data was successfully added,
538 * <code>false</code> on failure.
539 *
540 * @discussion
541 * This function immediately resizes the OSData's buffer, if necessary,
542 * to accommodate the new total size.
543 *
544 * An OSData object created "NoCopy" does not allow bytes
545 * to be appended.
546 */
547 virtual bool appendBytes(const OSData * aDataObj);
548
549
550 /*!
551 * @function getBytesNoCopy
552 *
553 * @abstract
554 * Returns a pointer to the OSData object's internal data buffer.
555 *
556 * @result
557 * A pointer to the OSData object's internal data buffer.
558 *
559 * @discussion
560 * You can modify the existing contents of an OSData object
561 * via this function.
562 * It works with OSData objects that have their own data buffers
563 * as well as with OSData objects that have shared buffers.
564 *
565 * If you append bytes or characters to an OSData object,
566 * it may have to reallocate its internal storage,
567 * rendering invalid an extrated pointer to that storage.
568 */
569 virtual const void * getBytesNoCopy() const;
570
571
572 /*!
573 * @function getBytesNoCopy
574 *
575 * @abstract
576 * Returns a pointer into the OSData object's internal data buffer
577 * with a given offset and length.
578 *
579 * @param start The offset from the base of the internal data buffer.
580 * @param numBytes The length of the window.
581 *
582 * @result
583 * A pointer to the bytes in the specified range
584 * within the OSData object,
585 * or 0 if that range does not lie completely
586 * within the object's buffer.
587 *
588 * @discussion
589 * You can modify the existing contents of an OSData object
590 * via this function.
591 * It works with OSData objects that have their own data buffers
592 * as well as with OSData objects that have shared buffers.
593 *
594 * If you append bytes or characters to an OSData object,
595 * it may have to reallocate its internal storage,
596 * rendering invalid an extrated pointer to that storage.
597 */
598 virtual const void * getBytesNoCopy(
599 unsigned int start,
600 unsigned int numBytes) const;
601
602
603 /*!
604 * @function isEqualTo
605 *
606 * @abstract
607 * Tests the equality of two OSData objects.
608 *
609 * @param aDataObj The OSData object being compared against the receiver.
610 *
611 * @result
612 * <code>true</code> if the two OSData objects are equivalent,
613 * <code>false</code> otherwise.
614 *
615 * @discussion
616 * Two OSData objects are considered equal
617 * if they have same length and if their
618 * byte buffers hold the same contents.
619 */
620 virtual bool isEqualTo(const OSData * aDataObj) const;
621
622
623 /*!
624 * @function isEqualTo
625 *
626 * @abstract
627 * Tests the equality of an OSData object's contents
628 * to a C array of bytes.
629 *
630 * @param bytes A pointer to the bytes to compare.
631 * @param numBytes The number of bytes to compare.
632 *
633 * @result
634 * <code>true</code> if the data buffers are equal
635 * over the given length,
636 * <code>false</code> otherwise.
637 */
638 virtual bool isEqualTo(
639 const void * bytes,
640 unsigned int numBytes) const;
641
642
643 /*!
644 * @function isEqualTo
645 *
646 * @abstract
647 * Tests the equality of an OSData object to an arbitrary object.
648 *
649 * @param anObject The object to be compared against the receiver.
650 *
651 * @result
652 * <code>true</code> if the two objects are equivalent,
653 * <code>false</code> otherwise.
654 *
655 * @discussion
656 * An OSData is considered equal to another object
657 * if that object is derived from OSData
658 * and contains the equivalent bytes of the same length.
659 */
660 virtual bool isEqualTo(const OSMetaClassBase * anObject) const;
661
662
663 /*!
664 * @function isEqualTo
665 *
666 * @abstract
667 * Tests the equality of an OSData object to an OSString.
668 *
669 * @param aString The string object to be compared against the receiver.
670 *
671 * @result
672 * <code>true</code> if the two objects are equivalent,
673 * <code>false</code> otherwise.
674 *
675 * @discussion
676 * This function compares the bytes of the OSData object
677 * against those of the OSString,
678 * accounting for the possibility that an OSData
679 * might explicitly include a nul
680 * character as part of its total length.
681 * Thus, for example, an OSData object containing
682 * either the bytes <'u', 's', 'b', '\0'>
683 * or <'u', 's', 'b'>
684 * will compare as equal to the OSString containing "usb".
685 */
686 virtual bool isEqualTo(const OSString * aString) const;
687
688
689 /*!
690 * @function serialize
691 *
692 * @abstract
693 * Archives the receiver into the provided
694 * @link //apple_ref/doc/class/IORegistryEntry OSSerialize@/link object.
695 *
696 * @param serializer The OSSerialize object.
697 *
698 * @result
699 * <code>true</code> if serialization succeeds, <code>false</code> if not.
700 */
701 virtual bool serialize(OSSerialize * serializer) const;
702
703
704 /*!
705 * @function appendByte
706 *
707 * @abstract
708 * Appends a single byte value
709 * to the OSData object's internal data buffer
710 * a specified number of times.
711 *
712 * @param byte The byte value to append.
713 * @param numBytes The number of copies of <code>byte</code> to append.
714 *
715 * @result
716 * <code>true</code> if the new data was successfully added,
717 * <code>false</code> if not.
718 *
719 * @discussion
720 * This function immediately resizes the OSData's buffer, if necessary,
721 * to accommodate the new total size.
722 *
723 * An OSData object created "NoCopy" does not allow bytes
724 * to be appended.
725 */
726 virtual bool appendByte(
727 unsigned char byte,
728 unsigned int numBytes);
729
730
731 void setSerializable(bool serializable);
732
733 #ifdef XNU_KERNEL_PRIVATE
734 /* Available within xnu source only */
735 public:
736 #else
737 private:
738 #endif
739 virtual void setDeallocFunction(DeallocFunction func);
740 OSMetaClassDeclareReservedUsed(OSData, 0);
741
742 private:
743 OSMetaClassDeclareReservedUnused(OSData, 1);
744 OSMetaClassDeclareReservedUnused(OSData, 2);
745 OSMetaClassDeclareReservedUnused(OSData, 3);
746 OSMetaClassDeclareReservedUnused(OSData, 4);
747 OSMetaClassDeclareReservedUnused(OSData, 5);
748 OSMetaClassDeclareReservedUnused(OSData, 6);
749 OSMetaClassDeclareReservedUnused(OSData, 7);
750 };
751
752 #endif /* !_OS_OSDATA_H */