]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSData.h
2b4159604f2af4961e8ed444a209a5cc8cccb570
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
)
81 unsigned int capacity
;
82 unsigned int capacityIncrement
;
84 struct ExpansionData
{ };
86 /* Reserved for future use. (Internal use only) */
87 ExpansionData
* reserved
;
92 * @function withCapacity
95 * Creates and initializes an empty instance of OSData.
97 * @param capacity The initial capacity of the OSData object in bytes.
100 * An instance of OSData with a reference count of 1;
101 * <code>NULL</code> on failure.
104 * <code>capacity</code> may be zero.
105 * The OSData object will allocate a buffer internally
106 * when necessary, and will grow as needed to accommodate more bytes
107 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
108 * for which a nonzero initial capacity is a hard limit).
110 static OSData
* withCapacity(unsigned int capacity
);
114 * @function withBytes
117 * Creates and initializes an instance of OSData
118 * with a copy of the provided data buffer.
120 * @param bytes The buffer of data to copy.
121 * @param numBytes The length of <code>bytes</code>.
124 * An instance of OSData containing a copy of the provided byte array,
125 * with a reference count of 1;
126 * <code>NULL</code> on failure.
129 * The new OSData object will grow as needed to accommodate more bytes
130 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
131 * for which a nonzero initial capacity is a hard limit).
133 static OSData
* withBytes(
135 unsigned int numBytes
);
139 * @function withBytesNoCopy
142 * Creates and initializes an instance of OSData
143 * that shares the provided data buffer.
145 * @param bytes The buffer of data to represent.
146 * @param numBytes The length of <code>bytes</code>.
149 * A instance of OSData that shares the provided byte array,
150 * with a reference count of 1;
151 * <code>NULL</coe> on failure.
154 * An OSData object created with this function
155 * does not claim ownership
156 * of the data buffer, but shares it with the caller.
157 * When the caller determines that the OSData object has actually been freed,
158 * it can safely dispose of the data buffer.
159 * Conversely, if it frees the shared data buffer,
160 * it must not attempt to use the OSData object and should release it.
162 * An OSData object created with shared external data cannot append bytes,
163 * but you can get the byte pointer and
164 * modify bytes within the shared buffer.
166 static OSData
* withBytesNoCopy(
168 unsigned int numBytes
);
175 * Creates and initializes an instance of OSData
176 * with contents copied from another OSData object.
178 * @param inData An OSData object that provides the initial data.
181 * An instance of OSData containing a copy of the data in <code>inData</code>,
182 * with a reference count of 1;
183 * <code>NULL</code> on failure.
186 * The new OSData object will grow as needed to accommodate more bytes
187 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
188 * for which a nonzero initial capacity is a hard limit).
190 static OSData
* withData(const OSData
* inData
);
197 * Creates and initializes an instance of OSData
198 * with contents copied from a range within another OSData object.
200 * @param inData An OSData object that provides the initial data.
201 * @param start The starting index from which bytes will be copied.
202 * @param numBytes The number of bytes to be copied from <code>start</code>.
205 * An instance of OSData containing a copy
206 * of the specified data range from <code>inData</code>,
207 * with a reference count of 1;
208 * <code>NULL</code> on failure.
211 * The new OSData object will grow as needed to accommodate more bytes
212 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
213 * for which a nonzero initial capacity is a hard limit).
215 static OSData
* withData(
216 const OSData
* inData
,
218 unsigned int numBytes
);
222 * @function initWithCapacity
225 * Initializes an instance of OSData.
227 * @param capacity The initial capacity of the OSData object in bytes.
230 * <code>true</code> on success, <code>false</code> on failure.
233 * Not for general use. Use the static instance creation method
235 * //apple_ref/cpp/clm/OSData/withCapacity/staticOSData*\/(unsignedint)
236 * withCapacity@/link</code> instead.
238 * <code>capacity</code> may be zero.
239 * The OSData object will allocate a buffer internally
240 * when necessary, and will grow as needed to accommodate more bytes
241 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
242 * for which a nonzero initial capacity is a hard limit).
244 virtual bool initWithCapacity(unsigned int capacity
);
248 * @function initWithBytes
251 * Initializes an instance of OSData
252 * with a copy of the provided data buffer.
254 * @param bytes The buffer of data to copy.
255 * @param numBytes The length of <code>bytes</code>.
258 * <code>true</code> on success, <code>false</code> on failure.
261 * Not for general use. Use the static instance creation method
262 * <code>@link withBytes withBytes@/link</code> instead.
264 * The new OSData object will grow as needed to accommodate more bytes
265 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
266 * for which a nonzero initial capacity is a hard limit).
268 virtual bool initWithBytes(
270 unsigned int numBytes
);
274 * @function initWithBytesNoCopy
277 * Initializes an instance of OSData
278 * to share the provided data buffer.
280 * @param bytes The buffer of data to represent.
281 * @param numBytes The length of <code>bytes</code>.
284 * <code>true</code> on success, <code>false</code> on failure.
287 * Not for general use. Use the static instance creation method
288 * <code>@link withBytesNoCopy withBytesNoCopy@/link</code> instead.
290 * An OSData object initialized with this function
291 * does not claim ownership
292 * of the data buffer, but merely shares it with the caller.
294 * An OSData object created with shared external data cannot append bytes,
295 * but you can get the byte pointer and
296 * modify bytes within the shared buffer.
298 virtual bool initWithBytesNoCopy(
300 unsigned int numBytes
);
304 * @function initWithData
307 * Creates and initializes an instance of OSData
308 * with contents copied from another OSData object.
310 * @param inData An OSData object that provides the initial data.
313 * <code>true</code> on success, <code>false</code> on failure.
316 * Not for general use. Use the static instance creation method
318 * //apple_ref/cpp/clm/OSData/withData/staticOSData*\/(constOSData*)
319 * withData(OSData *)@/link</code>
322 * The new OSData object will grow as needed to accommodate more bytes
323 * (<i>unlike</i> @link //apple_ref/doc/uid/20001498 CFMutableData@/link,
324 * for which a nonzero initial capacity is a hard limit).
326 virtual bool initWithData(const OSData
* inData
);
330 * @function initWithData
333 * Initializes an instance of OSData
334 * with contents copied from a range within another OSData object.
336 * @param inData An OSData object that provides the initial data.
337 * @param start The starting index from which bytes will be copied.
338 * @param numBytes The number of bytes to be copied from <code>start</code>.
341 * Returns <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*,unsignedint,unsignedint)
347 * withData(OSData *, unsigned int, unsigned int)@/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(
355 const OSData
* inData
,
357 unsigned int numBytes
);
364 * Deallocates or releases any resources
365 * used by the OSDictionary instance.
368 * This function should not be called directly;
371 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
372 * release@/link</code>
379 * @function getLength
382 * Returns the number of bytes in or referenced by the OSData object.
385 * The number of bytes in or referenced by the OSData object.
387 virtual unsigned int getLength() const;
391 * @function getCapacity
394 * Returns the total number of bytes the OSData can store without reallocating.
397 * The total number bytes the OSData can store without reallocating.
400 * OSData objects grow when full to accommodate additional bytes.
403 * //apple_ref/cpp/instm/OSData/getCapacityIncrement/virtualunsignedint/()
404 * getCapacityIncrement@/link</code>
407 * //apple_ref/cpp/instm/OSData/ensureCapacity/virtualunsignedint/(unsignedint)
408 * ensureCapacity@/link</code>.
410 * OSData objects created or initialized to use a shared buffer
411 * do not make use of this attribute, and return -1 from this function.
413 virtual unsigned int getCapacity() const;
417 * @function getCapacityIncrement
420 * Returns the storage increment of the OSData object.
423 * The storage increment of the OSData object.
426 * An OSData object allocates storage for bytes in multiples
427 * of the capacity increment.
429 * OSData objects created or initialized to use a shared buffer
430 * do not make use of this attribute.
432 virtual unsigned int getCapacityIncrement() const;
436 * @function setCapacityIncrement
439 * Sets the storage increment of the array.
442 * The original storage increment of the array.
445 * An OSArray allocates storage for objects in multiples
446 * of the capacity increment.
448 * OSData objects created or initialized to use a shared buffer
449 * do not make use of this attribute.
451 virtual unsigned int setCapacityIncrement(unsigned increment
);
454 // xx-review: does not check for capacity == EXTERNAL
457 * @function ensureCapacity
460 * Ensures the array has enough space
461 * to store the requested number of bytes.
463 * @param newCapacity The total number of bytes the OSData object
464 * should be able to store.
467 * Returns the new capacity of the OSData object,
468 * which may be different from the number requested
469 * (if smaller, reallocation of storage failed).
472 * This function immediately resizes the OSData's buffer, if necessary,
473 * to accommodate at least <code>newCapacity</code> bytes.
474 * If <code>newCapacity</code> is not greater than the current capacity,
475 * or if an allocation error occurs, the original capacity is returned.
477 * There is no way to reduce the capacity of an OSData.
479 * An OSData object created "NoCopy" does not allow resizing.
481 virtual unsigned int ensureCapacity(unsigned int newCapacity
);
485 * @function appendBytes
488 * Appends a buffer of bytes to the OSData object's internal data buffer.
490 * @param bytes A pointer to the data to append.
491 * If <code>bytes</code> is <code>NULL</code>
492 * then a zero-filled buffer of length <code>numBytes</code>
494 * @param numBytes The number of bytes from <code>bytes</code> to append.
497 * <code>true</code> if the new data was successfully added,
498 * <code>false</code> on failure.
501 * This function immediately resizes the OSData's buffer, if necessary,
502 * to accommodate the new total size.
504 * An OSData object created "NoCopy" does not allow bytes
507 virtual bool appendBytes(
509 unsigned int numBytes
);
513 * @function appendBytes
516 * Appends the data contained in another OSData object.
518 * @param aDataObj The OSData object whose contents will be appended.
521 * <code>true</code> if the new data was successfully added,
522 * <code>false</code> on failure.
525 * This function immediately resizes the OSData's buffer, if necessary,
526 * to accommodate the new total size.
528 * An OSData object created "NoCopy" does not allow bytes
531 virtual bool appendBytes(const OSData
* aDataObj
);
535 * @function getBytesNoCopy
538 * Returns a pointer to the OSData object's internal data buffer.
541 * A pointer to the OSData object's internal data buffer.
544 * You can modify the existing contents of an OSData object
546 * It works with OSData objects that have their own data buffers
547 * as well as with OSData objects that have shared buffers.
549 * If you append bytes or characters to an OSData object,
550 * it may have to reallocate its internal storage,
551 * rendering invalid an extrated pointer to that storage.
553 virtual const void * getBytesNoCopy() const;
557 * @function getBytesNoCopy
560 * Returns a pointer into the OSData object's internal data buffer
561 * with a given offset and length.
563 * @param start The offset from the base of the internal data buffer.
564 * @param numBytes The length of the window.
567 * A pointer to the bytes in the specified range
568 * within the OSData object,
569 * or 0 if that range does not lie completely
570 * within the object's buffer.
573 * You can modify the existing contents of an OSData object
575 * It works with OSData objects that have their own data buffers
576 * as well as with OSData objects that have shared buffers.
578 * If you append bytes or characters to an OSData object,
579 * it may have to reallocate its internal storage,
580 * rendering invalid an extrated pointer to that storage.
582 virtual const void * getBytesNoCopy(
584 unsigned int numBytes
) const;
588 * @function isEqualTo
591 * Tests the equality of two OSData objects.
593 * @param aDataObj The OSData object being compared against the receiver.
596 * <code>true</code> if the two OSData objects are equivalent,
597 * <code>false</code> otherwise.
600 * Two OSData objects are considered equal
601 * if they have same length and if their
602 * byte buffers hold the same contents.
604 virtual bool isEqualTo(const OSData
* aDataObj
) const;
608 * @function isEqualTo
611 * Tests the equality of an OSData object's contents
612 * to a C array of bytes.
614 * @param bytes A pointer to the bytes to compare.
615 * @param numBytes The number of bytes to compare.
618 * <code>true</code> if the data buffers are equal
619 * over the given length,
620 * <code>false</code> otherwise.
622 virtual bool isEqualTo(
624 unsigned int numBytes
) const;
628 * @function isEqualTo
631 * Tests the equality of an OSData object to an arbitrary object.
633 * @param anObject The object to be compared against the receiver.
636 * <code>true</code> if the two objects are equivalent,
637 * <code>false</code> otherwise.
640 * An OSData is considered equal to another object
641 * if that object is derived from OSData
642 * and contains the equivalent bytes of the same length.
644 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const;
648 * @function isEqualTo
651 * Tests the equality of an OSData object to an OSString.
653 * @param aString The string object to be compared against the receiver.
656 * <code>true</code> if the two objects are equivalent,
657 * <code>false</code> otherwise.
660 * This function compares the bytes of the OSData object
661 * against those of the OSString,
662 * accounting for the possibility that an OSData
663 * might explicitly include a nul
664 * character as part of its total length.
665 * Thus, for example, an OSData object containing
666 * either the bytes <'u', 's', 'b', '\0'>
668 * will compare as equal to the OSString containing "usb".
670 virtual bool isEqualTo(const OSString
* aString
) const;
674 * @function serialize
677 * Archives the receiver into the provided
678 * @link //apple_ref/doc/class/IORegistryEntry OSSerialize@/link object.
680 * @param serializer The OSSerialize object.
683 * <code>true</code> if serialization succeeds, <code>false</code> if not.
685 virtual bool serialize(OSSerialize
* serializer
) const;
689 * @function appendByte
692 * Appends a single byte value
693 * to the OSData object's internal data buffer
694 * a specified number of times.
696 * @param byte The byte value to append.
697 * @param numBytes The number of copies of <code>byte</code> to append.
700 * <code>true</code> if the new data was successfully added,
701 * <code>false</code> if not.
704 * This function immediately resizes the OSData's buffer, if necessary,
705 * to accommodate the new total size.
707 * An OSData object created "NoCopy" does not allow bytes
710 virtual bool appendByte(
712 unsigned int numBytes
);
714 #ifdef XNU_KERNEL_PRIVATE
715 /* Available within xnu source only */
720 // xxx - DO NOT USE - This interface may change
721 typedef void (*DeallocFunction
)(void * ptr
, unsigned int length
);
722 virtual void setDeallocFunction(DeallocFunction func
);
723 OSMetaClassDeclareReservedUsed(OSData
, 0);
726 OSMetaClassDeclareReservedUnused(OSData
, 1);
727 OSMetaClassDeclareReservedUnused(OSData
, 2);
728 OSMetaClassDeclareReservedUnused(OSData
, 3);
729 OSMetaClassDeclareReservedUnused(OSData
, 4);
730 OSMetaClassDeclareReservedUnused(OSData
, 5);
731 OSMetaClassDeclareReservedUnused(OSData
, 6);
732 OSMetaClassDeclareReservedUnused(OSData
, 7);
735 #endif /* !_OS_OSDATA_H */