2 * Copyright (c) 2000 Apple Computer, 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 OSDeclareDefaultStructors(OSData
)
77 friend class OSSerialize
;
82 unsigned int capacity
;
83 unsigned int capacityIncrement
;
85 #ifdef XNU_KERNEL_PRIVATE
86 /* Available within xnu source only */
88 typedef void (*DeallocFunction
)(void * ptr
, unsigned int length
);
92 DeallocFunction deallocFunction
;
93 bool disableSerialization
;
97 typedef void (*DeallocFunction
)(void * ptr
, unsigned int length
);
102 /* Reserved for future use. (Internal use only) */
103 ExpansionData
* reserved
;
108 * @function withCapacity
111 * Creates and initializes an empty instance of OSData.
113 * @param capacity The initial capacity of the OSData object in bytes.
116 * An instance of OSData with a reference count of 1;
117 * <code>NULL</code> on failure.
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).
126 static OSData
* withCapacity(unsigned int capacity
);
130 * @function withBytes
133 * Creates and initializes an instance of OSData
134 * with a copy of the provided data buffer.
136 * @param bytes The buffer of data to copy.
137 * @param numBytes The length of <code>bytes</code>.
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.
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).
149 static OSData
* withBytes(
151 unsigned int numBytes
);
155 * @function withBytesNoCopy
158 * Creates and initializes an instance of OSData
159 * that shares the provided data buffer.
161 * @param bytes The buffer of data to represent.
162 * @param numBytes The length of <code>bytes</code>.
165 * A instance of OSData that shares the provided byte array,
166 * with a reference count of 1;
167 * <code>NULL</coe> on failure.
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.
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.
182 static OSData
* withBytesNoCopy(
184 unsigned int numBytes
);
191 * Creates and initializes an instance of OSData
192 * with contents copied from another OSData object.
194 * @param inData An OSData object that provides the initial data.
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.
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).
206 static OSData
* withData(const OSData
* inData
);
213 * Creates and initializes an instance of OSData
214 * with contents copied from a range within another OSData object.
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>.
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.
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).
231 static OSData
* withData(
232 const OSData
* inData
,
234 unsigned int numBytes
);
238 * @function initWithCapacity
241 * Initializes an instance of OSData.
243 * @param capacity The initial capacity of the OSData object in bytes.
246 * <code>true</code> on success, <code>false</code> on failure.
249 * Not for general use. Use the static instance creation method
251 * //apple_ref/cpp/clm/OSData/withCapacity/staticOSData*\/(unsignedint)
252 * withCapacity@/link</code> instead.
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).
260 virtual bool initWithCapacity(unsigned int capacity
);
264 * @function initWithBytes
267 * Initializes an instance of OSData
268 * with a copy of the provided data buffer.
270 * @param bytes The buffer of data to copy.
271 * @param numBytes The length of <code>bytes</code>.
274 * <code>true</code> on success, <code>false</code> on failure.
277 * Not for general use. Use the static instance creation method
278 * <code>@link withBytes withBytes@/link</code> instead.
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).
284 virtual bool initWithBytes(
286 unsigned int numBytes
);
290 * @function initWithBytesNoCopy
293 * Initializes an instance of OSData
294 * to share the provided data buffer.
296 * @param bytes The buffer of data to represent.
297 * @param numBytes The length of <code>bytes</code>.
300 * <code>true</code> on success, <code>false</code> on failure.
303 * Not for general use. Use the static instance creation method
304 * <code>@link withBytesNoCopy withBytesNoCopy@/link</code> instead.
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.
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.
314 virtual bool initWithBytesNoCopy(
316 unsigned int numBytes
);
320 * @function initWithData
323 * Creates and initializes an instance of OSData
324 * with contents copied from another OSData object.
326 * @param inData An OSData object that provides the initial data.
329 * <code>true</code> on success, <code>false</code> on failure.
332 * Not for general use. Use the static instance creation method
334 * //apple_ref/cpp/clm/OSData/withData/staticOSData*\/(constOSData*)
335 * withData(OSData *)@/link</code>
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).
342 virtual bool initWithData(const OSData
* inData
);
346 * @function initWithData
349 * Initializes an instance of OSData
350 * with contents copied from a range within another OSData object.
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>.
357 * Returns <code>true</code> on success, <code>false</code> on failure.
360 * Not for general use. Use the static instance creation method
362 * //apple_ref/cpp/clm/OSData/withData/staticOSData*\/(constOSData*,unsignedint,unsignedint)
363 * withData(OSData *, unsigned int, unsigned int)@/link</code>
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).
370 virtual bool initWithData(
371 const OSData
* inData
,
373 unsigned int numBytes
);
380 * Deallocates or releases any resources
381 * used by the OSDictionary instance.
384 * This function should not be called directly;
387 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
388 * release@/link</code>
395 * @function getLength
398 * Returns the number of bytes in or referenced by the OSData object.
401 * The number of bytes in or referenced by the OSData object.
403 virtual unsigned int getLength() const;
407 * @function getCapacity
410 * Returns the total number of bytes the OSData can store without reallocating.
413 * The total number bytes the OSData can store without reallocating.
416 * OSData objects grow when full to accommodate additional bytes.
419 * //apple_ref/cpp/instm/OSData/getCapacityIncrement/virtualunsignedint/()
420 * getCapacityIncrement@/link</code>
423 * //apple_ref/cpp/instm/OSData/ensureCapacity/virtualunsignedint/(unsignedint)
424 * ensureCapacity@/link</code>.
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.
429 virtual unsigned int getCapacity() const;
433 * @function getCapacityIncrement
436 * Returns the storage increment of the OSData object.
439 * The storage increment of the OSData object.
442 * An OSData object allocates storage for bytes in multiples
443 * of the capacity increment.
445 * OSData objects created or initialized to use a shared buffer
446 * do not make use of this attribute.
448 virtual unsigned int getCapacityIncrement() const;
452 * @function setCapacityIncrement
455 * Sets the storage increment of the array.
458 * The original storage increment of the array.
461 * An OSArray allocates storage for objects in multiples
462 * of the capacity increment.
464 * OSData objects created or initialized to use a shared buffer
465 * do not make use of this attribute.
467 virtual unsigned int setCapacityIncrement(unsigned increment
);
470 // xx-review: does not check for capacity == EXTERNAL
473 * @function ensureCapacity
476 * Ensures the array has enough space
477 * to store the requested number of bytes.
479 * @param newCapacity The total number of bytes the OSData object
480 * should be able to store.
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).
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.
493 * There is no way to reduce the capacity of an OSData.
495 * An OSData object created "NoCopy" does not allow resizing.
497 virtual unsigned int ensureCapacity(unsigned int newCapacity
);
501 * @function appendBytes
504 * Appends a buffer of bytes to the OSData object's internal data buffer.
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>
510 * @param numBytes The number of bytes from <code>bytes</code> to append.
513 * <code>true</code> if the new data was successfully added,
514 * <code>false</code> on failure.
517 * This function immediately resizes the OSData's buffer, if necessary,
518 * to accommodate the new total size.
520 * An OSData object created "NoCopy" does not allow bytes
523 virtual bool appendBytes(
525 unsigned int numBytes
);
529 * @function appendBytes
532 * Appends the data contained in another OSData object.
534 * @param aDataObj The OSData object whose contents will be appended.
537 * <code>true</code> if the new data was successfully added,
538 * <code>false</code> on failure.
541 * This function immediately resizes the OSData's buffer, if necessary,
542 * to accommodate the new total size.
544 * An OSData object created "NoCopy" does not allow bytes
547 virtual bool appendBytes(const OSData
* aDataObj
);
551 * @function getBytesNoCopy
554 * Returns a pointer to the OSData object's internal data buffer.
557 * A pointer to the OSData object's internal data buffer.
560 * You can modify the existing contents of an OSData object
562 * It works with OSData objects that have their own data buffers
563 * as well as with OSData objects that have shared buffers.
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.
569 virtual const void * getBytesNoCopy() const;
573 * @function getBytesNoCopy
576 * Returns a pointer into the OSData object's internal data buffer
577 * with a given offset and length.
579 * @param start The offset from the base of the internal data buffer.
580 * @param numBytes The length of the window.
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.
589 * You can modify the existing contents of an OSData object
591 * It works with OSData objects that have their own data buffers
592 * as well as with OSData objects that have shared buffers.
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.
598 virtual const void * getBytesNoCopy(
600 unsigned int numBytes
) const;
604 * @function isEqualTo
607 * Tests the equality of two OSData objects.
609 * @param aDataObj The OSData object being compared against the receiver.
612 * <code>true</code> if the two OSData objects are equivalent,
613 * <code>false</code> otherwise.
616 * Two OSData objects are considered equal
617 * if they have same length and if their
618 * byte buffers hold the same contents.
620 virtual bool isEqualTo(const OSData
* aDataObj
) const;
624 * @function isEqualTo
627 * Tests the equality of an OSData object's contents
628 * to a C array of bytes.
630 * @param bytes A pointer to the bytes to compare.
631 * @param numBytes The number of bytes to compare.
634 * <code>true</code> if the data buffers are equal
635 * over the given length,
636 * <code>false</code> otherwise.
638 virtual bool isEqualTo(
640 unsigned int numBytes
) const;
644 * @function isEqualTo
647 * Tests the equality of an OSData object to an arbitrary object.
649 * @param anObject The object to be compared against the receiver.
652 * <code>true</code> if the two objects are equivalent,
653 * <code>false</code> otherwise.
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.
660 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
664 * @function isEqualTo
667 * Tests the equality of an OSData object to an OSString.
669 * @param aString The string object to be compared against the receiver.
672 * <code>true</code> if the two objects are equivalent,
673 * <code>false</code> otherwise.
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'>
684 * will compare as equal to the OSString containing "usb".
686 virtual bool isEqualTo(const OSString
* aString
) const;
690 * @function serialize
693 * Archives the receiver into the provided
694 * @link //apple_ref/doc/class/IORegistryEntry OSSerialize@/link object.
696 * @param serializer The OSSerialize object.
699 * <code>true</code> if serialization succeeds, <code>false</code> if not.
701 virtual bool serialize(OSSerialize
* serializer
) const;
705 * @function appendByte
708 * Appends a single byte value
709 * to the OSData object's internal data buffer
710 * a specified number of times.
712 * @param byte The byte value to append.
713 * @param numBytes The number of copies of <code>byte</code> to append.
716 * <code>true</code> if the new data was successfully added,
717 * <code>false</code> if not.
720 * This function immediately resizes the OSData's buffer, if necessary,
721 * to accommodate the new total size.
723 * An OSData object created "NoCopy" does not allow bytes
726 virtual bool appendByte(
728 unsigned int numBytes
);
731 void setSerializable(bool serializable
);
733 #ifdef XNU_KERNEL_PRIVATE
734 /* Available within xnu source only */
739 virtual void setDeallocFunction(DeallocFunction func
);
740 OSMetaClassDeclareReservedUsed(OSData
, 0);
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);
752 #endif /* !_OS_OSDATA_H */