2 * Copyright (c) 2000-2016 Apple 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 /* IOData.h created by rsulack on Wed 17-Sep-1997 */
29 /* IOData.h converted to C++ by gvdl on Fri 1998-10-30 */
34 #include <libkern/c++/OSObject.h>
42 * This header declares the OSData container class.
50 * OSData wraps an array of bytes in a C++ object
51 * for use in Libkern collections.
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.
59 * <b>Use Restrictions</b>
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.
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
74 class OSData
: public OSObject
76 friend class OSSerialize
;
78 OSDeclareDefaultStructors(OSData
)
80 #if APPLE_KEXT_ALIGN_CONTAINERS
84 unsigned int capacity
;
85 unsigned int capacityIncrement
;
88 #else /* APPLE_KEXT_ALIGN_CONTAINERS */
93 unsigned int capacity
;
94 unsigned int capacityIncrement
;
96 #endif /* APPLE_KEXT_ALIGN_CONTAINERS */
98 #ifdef XNU_KERNEL_PRIVATE
99 /* Available within xnu source only */
101 typedef void (*DeallocFunction
)(void * ptr
, unsigned int length
);
103 struct ExpansionData
{
104 DeallocFunction deallocFunction
;
105 bool disableSerialization
;
107 #else /* XNU_KERNEL_PRIVATE */
109 typedef void (*DeallocFunction
)(void * ptr
, unsigned int length
);
111 struct ExpansionData
;
112 #endif /* XNU_KERNEL_PRIVATE */
114 /* Reserved for future use. (Internal use only) */
115 ExpansionData
* reserved
;
120 * @function withCapacity
123 * Creates and initializes an empty instance of OSData.
125 * @param capacity The initial capacity of the OSData object in bytes.
128 * An instance of OSData with a reference count of 1;
129 * <code>NULL</code> on failure.
132 * <code>capacity</code> may be zero.
133 * The OSData object will allocate a buffer internally
134 * when necessary, and will grow as needed to accommodate more bytes
135 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
136 * for which a nonzero initial capacity is a hard limit).
138 static OSData
* withCapacity(unsigned int capacity
);
142 * @function withBytes
145 * Creates and initializes an instance of OSData
146 * with a copy of the provided data buffer.
148 * @param bytes The buffer of data to copy.
149 * @param numBytes The length of <code>bytes</code>.
152 * An instance of OSData containing a copy of the provided byte array,
153 * with a reference count of 1;
154 * <code>NULL</code> on failure.
157 * The new OSData object will grow as needed to accommodate more bytes
158 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
159 * for which a nonzero initial capacity is a hard limit).
161 static OSData
* withBytes(
163 unsigned int numBytes
);
167 * @function withBytesNoCopy
170 * Creates and initializes an instance of OSData
171 * that shares the provided data buffer.
173 * @param bytes The buffer of data to represent.
174 * @param numBytes The length of <code>bytes</code>.
177 * A instance of OSData that shares the provided byte array,
178 * with a reference count of 1;
179 * <code>NULL</code> on failure.
182 * An OSData object created with this function
183 * does not claim ownership
184 * of the data buffer, but shares it with the caller.
185 * When the caller determines that the OSData object has actually been freed,
186 * it can safely dispose of the data buffer.
187 * Conversely, if it frees the shared data buffer,
188 * it must not attempt to use the OSData object and should release it.
190 * An OSData object created with shared external data cannot append bytes,
191 * but you can get the byte pointer and
192 * modify bytes within the shared buffer.
194 static OSData
* withBytesNoCopy(
196 unsigned int numBytes
);
203 * Creates and initializes an instance of OSData
204 * with contents copied from another OSData object.
206 * @param inData An OSData object that provides the initial data.
209 * An instance of OSData containing a copy of the data in <code>inData</code>,
210 * with a reference count of 1;
211 * <code>NULL</code> on failure.
214 * The new OSData object will grow as needed to accommodate more bytes
215 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
216 * for which a nonzero initial capacity is a hard limit).
218 static OSData
* withData(const OSData
* inData
);
225 * Creates and initializes an instance of OSData
226 * with contents copied from a range within another OSData object.
228 * @param inData An OSData object that provides the initial data.
229 * @param start The starting index from which bytes will be copied.
230 * @param numBytes The number of bytes to be copied from <code>start</code>.
233 * An instance of OSData containing a copy
234 * of the specified data range from <code>inData</code>,
235 * with a reference count of 1;
236 * <code>NULL</code> on failure.
239 * The new OSData object will grow as needed to accommodate more bytes
240 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
241 * for which a nonzero initial capacity is a hard limit).
243 static OSData
* withData(
244 const OSData
* inData
,
246 unsigned int numBytes
);
250 * @function initWithCapacity
253 * Initializes an instance of OSData.
255 * @param capacity The initial capacity of the OSData object in bytes.
258 * <code>true</code> on success, <code>false</code> on failure.
261 * Not for general use. Use the static instance creation method
263 * //apple_ref/cpp/clm/OSData/withCapacity/staticOSData*\/(unsignedint)
264 * withCapacity@/link</code> instead.
266 * <code>capacity</code> may be zero.
267 * The OSData object will allocate a buffer internally
268 * when necessary, and will grow as needed to accommodate more bytes
269 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
270 * for which a nonzero initial capacity is a hard limit).
272 virtual bool initWithCapacity(unsigned int capacity
);
276 * @function initWithBytes
279 * Initializes an instance of OSData
280 * with a copy of the provided data buffer.
282 * @param bytes The buffer of data to copy.
283 * @param numBytes The length of <code>bytes</code>.
286 * <code>true</code> on success, <code>false</code> on failure.
289 * Not for general use. Use the static instance creation method
290 * <code>@link withBytes withBytes@/link</code> instead.
292 * The new OSData object will grow as needed to accommodate more bytes
293 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
294 * for which a nonzero initial capacity is a hard limit).
296 virtual bool initWithBytes(
298 unsigned int numBytes
);
302 * @function initWithBytesNoCopy
305 * Initializes an instance of OSData
306 * to share the provided data buffer.
308 * @param bytes The buffer of data to represent.
309 * @param numBytes The length of <code>bytes</code>.
312 * <code>true</code> on success, <code>false</code> on failure.
315 * Not for general use. Use the static instance creation method
316 * <code>@link withBytesNoCopy withBytesNoCopy@/link</code> instead.
318 * An OSData object initialized with this function
319 * does not claim ownership
320 * of the data buffer, but merely shares it with the caller.
322 * An OSData object created with shared external data cannot append bytes,
323 * but you can get the byte pointer and
324 * modify bytes within the shared buffer.
326 virtual bool initWithBytesNoCopy(
328 unsigned int numBytes
);
332 * @function initWithData
335 * Creates and initializes an instance of OSData
336 * with contents copied from another OSData object.
338 * @param inData An OSData object that provides the initial data.
341 * <code>true</code> on success, <code>false</code> on failure.
344 * Not for general use. Use the static instance creation method
346 * //apple_ref/cpp/clm/OSData/withData/staticOSData*\/(constOSData*)
347 * withData(OSData *)@/link</code>
350 * The new OSData object will grow as needed to accommodate more bytes
351 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
352 * for which a nonzero initial capacity is a hard limit).
354 virtual bool initWithData(const OSData
* inData
);
358 * @function initWithData
361 * Initializes an instance of OSData
362 * with contents copied from a range within another OSData object.
364 * @param inData An OSData object that provides the initial data.
365 * @param start The starting index from which bytes will be copied.
366 * @param numBytes The number of bytes to be copied from <code>start</code>.
369 * Returns <code>true</code> on success, <code>false</code> on failure.
372 * Not for general use. Use the static instance creation method
374 * //apple_ref/cpp/clm/OSData/withData/staticOSData*\/(constOSData*,unsignedint,unsignedint)
375 * withData(OSData *, unsigned int, unsigned int)@/link</code>
378 * The new OSData object will grow as needed to accommodate more bytes
379 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
380 * for which a nonzero initial capacity is a hard limit).
382 virtual bool initWithData(
383 const OSData
* inData
,
385 unsigned int numBytes
);
392 * Deallocates or releases any resources
393 * used by the OSData instance.
396 * This function should not be called directly;
399 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
400 * release@/link</code>
403 virtual void free() APPLE_KEXT_OVERRIDE
;
407 * @function getLength
410 * Returns the number of bytes in or referenced by the OSData object.
413 * The number of bytes in or referenced by the OSData object.
415 virtual unsigned int getLength() const;
419 * @function getCapacity
422 * Returns the total number of bytes the OSData can store without reallocating.
425 * The total number bytes the OSData can store without reallocating.
428 * OSData objects grow when full to accommodate additional bytes.
431 * //apple_ref/cpp/instm/OSData/getCapacityIncrement/virtualunsignedint/()
432 * getCapacityIncrement@/link</code>
435 * //apple_ref/cpp/instm/OSData/ensureCapacity/virtualunsignedint/(unsignedint)
436 * ensureCapacity@/link</code>.
438 * OSData objects created or initialized to use a shared buffer
439 * do not make use of this attribute, and return -1 from this function.
441 virtual unsigned int getCapacity() const;
445 * @function getCapacityIncrement
448 * Returns the storage increment of the OSData object.
451 * The storage increment of the OSData object.
454 * An OSData object allocates storage for bytes in multiples
455 * of the capacity increment.
457 * OSData objects created or initialized to use a shared buffer
458 * do not make use of this attribute.
460 virtual unsigned int getCapacityIncrement() const;
464 * @function setCapacityIncrement
467 * Sets the storage increment of the array.
470 * The original storage increment of the array.
473 * An OSArray allocates storage for objects in multiples
474 * of the capacity increment.
476 * OSData objects created or initialized to use a shared buffer
477 * do not make use of this attribute.
479 virtual unsigned int setCapacityIncrement(unsigned increment
);
482 // xx-review: does not check for capacity == EXTERNAL
485 * @function ensureCapacity
488 * Ensures the array has enough space
489 * to store the requested number of bytes.
491 * @param newCapacity The total number of bytes the OSData object
492 * should be able to store.
495 * Returns the new capacity of the OSData object,
496 * which may be different from the number requested
497 * (if smaller, reallocation of storage failed).
500 * This function immediately resizes the OSData's buffer, if necessary,
501 * to accommodate at least <code>newCapacity</code> bytes.
502 * If <code>newCapacity</code> is not greater than the current capacity,
503 * or if an allocation error occurs, the original capacity is returned.
505 * There is no way to reduce the capacity of an OSData.
507 * An OSData object created "NoCopy" does not allow resizing.
509 virtual unsigned int ensureCapacity(unsigned int newCapacity
);
513 * @function appendBytes
516 * Appends a buffer of bytes to the OSData object's internal data buffer.
518 * @param bytes A pointer to the data to append.
519 * If <code>bytes</code> is <code>NULL</code>
520 * then a zero-filled buffer of length <code>numBytes</code>
522 * @param numBytes The number of bytes from <code>bytes</code> to append.
525 * <code>true</code> if the new data was successfully added,
526 * <code>false</code> on failure.
529 * This function immediately resizes the OSData's buffer, if necessary,
530 * to accommodate the new total size.
532 * An OSData object created "NoCopy" does not allow bytes
535 virtual bool appendBytes(
537 unsigned int numBytes
);
541 * @function appendBytes
544 * Appends the data contained in another OSData object.
546 * @param aDataObj The OSData object whose contents will be appended.
549 * <code>true</code> if the new data was successfully added,
550 * <code>false</code> on failure.
553 * This function immediately resizes the OSData's buffer, if necessary,
554 * to accommodate the new total size.
556 * An OSData object created "NoCopy" does not allow bytes
559 virtual bool appendBytes(const OSData
* aDataObj
);
563 * @function getBytesNoCopy
566 * Returns a pointer to the OSData object's internal data buffer.
569 * A pointer to the OSData object's internal data buffer.
572 * You can modify the existing contents of an OSData object
574 * It works with OSData objects that have their own data buffers
575 * as well as with OSData objects that have shared buffers.
577 * If you append bytes or characters to an OSData object,
578 * it may have to reallocate its internal storage,
579 * rendering invalid an extrated pointer to that storage.
581 virtual const void * getBytesNoCopy() const;
585 * @function getBytesNoCopy
588 * Returns a pointer into the OSData object's internal data buffer
589 * with a given offset and length.
591 * @param start The offset from the base of the internal data buffer.
592 * @param numBytes The length of the window.
595 * A pointer to the bytes in the specified range
596 * within the OSData object,
597 * or 0 if that range does not lie completely
598 * within the object's buffer.
601 * You can modify the existing contents of an OSData object
603 * It works with OSData objects that have their own data buffers
604 * as well as with OSData objects that have shared buffers.
606 * If you append bytes or characters to an OSData object,
607 * it may have to reallocate its internal storage,
608 * rendering invalid an extrated pointer to that storage.
610 virtual const void * getBytesNoCopy(
612 unsigned int numBytes
) const;
616 * @function isEqualTo
619 * Tests the equality of two OSData objects.
621 * @param aDataObj The OSData object being compared against the receiver.
624 * <code>true</code> if the two OSData objects are equivalent,
625 * <code>false</code> otherwise.
628 * Two OSData objects are considered equal
629 * if they have same length and if their
630 * byte buffers hold the same contents.
632 virtual bool isEqualTo(const OSData
* aDataObj
) const;
636 * @function isEqualTo
639 * Tests the equality of an OSData object's contents
640 * to a C array of bytes.
642 * @param bytes A pointer to the bytes to compare.
643 * @param numBytes The number of bytes to compare.
646 * <code>true</code> if the data buffers are equal
647 * over the given length,
648 * <code>false</code> otherwise.
650 virtual bool isEqualTo(
652 unsigned int numBytes
) const;
656 * @function isEqualTo
659 * Tests the equality of an OSData object to an arbitrary object.
661 * @param anObject The object to be compared against the receiver.
664 * <code>true</code> if the two objects are equivalent,
665 * <code>false</code> otherwise.
668 * An OSData is considered equal to another object
669 * if that object is derived from OSData
670 * and contains the equivalent bytes of the same length.
672 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const APPLE_KEXT_OVERRIDE
;
676 * @function isEqualTo
679 * Tests the equality of an OSData object to an OSString.
681 * @param aString The string object to be compared against the receiver.
684 * <code>true</code> if the two objects are equivalent,
685 * <code>false</code> otherwise.
688 * This function compares the bytes of the OSData object
689 * against those of the OSString,
690 * accounting for the possibility that an OSData
691 * might explicitly include a nul
692 * character as part of its total length.
693 * Thus, for example, an OSData object containing
694 * either the bytes <'u', 's', 'b', '\0'>
696 * will compare as equal to the OSString containing "usb".
698 virtual bool isEqualTo(const OSString
* aString
) const;
702 * @function serialize
705 * Archives the receiver into the provided
706 * @link //apple_ref/doc/class/IORegistryEntry OSSerialize@/link object.
708 * @param serializer The OSSerialize object.
711 * <code>true</code> if serialization succeeds, <code>false</code> if not.
713 virtual bool serialize(OSSerialize
* serializer
) const APPLE_KEXT_OVERRIDE
;
717 * @function appendByte
720 * Appends a single byte value
721 * to the OSData object's internal data buffer
722 * a specified number of times.
724 * @param byte The byte value to append.
725 * @param numBytes The number of copies of <code>byte</code> to append.
728 * <code>true</code> if the new data was successfully added,
729 * <code>false</code> if not.
732 * This function immediately resizes the OSData's buffer, if necessary,
733 * to accommodate the new total size.
735 * An OSData object created "NoCopy" does not allow bytes
738 virtual bool appendByte(
740 unsigned int numBytes
);
743 void setSerializable(bool serializable
);
745 #ifdef XNU_KERNEL_PRIVATE
746 /* Available within xnu source only */
751 virtual void setDeallocFunction(DeallocFunction func
);
752 OSMetaClassDeclareReservedUsed(OSData
, 0);
753 bool isSerializable(void);
756 OSMetaClassDeclareReservedUnused(OSData
, 1);
757 OSMetaClassDeclareReservedUnused(OSData
, 2);
758 OSMetaClassDeclareReservedUnused(OSData
, 3);
759 OSMetaClassDeclareReservedUnused(OSData
, 4);
760 OSMetaClassDeclareReservedUnused(OSData
, 5);
761 OSMetaClassDeclareReservedUnused(OSData
, 6);
762 OSMetaClassDeclareReservedUnused(OSData
, 7);
765 #endif /* !_OS_OSDATA_H */