]> git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSData.h
2406be7c0d98656d0bb9e6820984f259f3295240
[apple/xnu.git] / libkern / libkern / c++ / OSData.h
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* IOData.h created by rsulack on Wed 17-Sep-1997 */
23 /* IOData.h converted to C++ by gvdl on Fri 1998-10-30 */
24
25 #ifndef _OS_OSDATA_H
26 #define _OS_OSDATA_H
27
28 #include <libkern/c++/OSObject.h>
29
30 class OSString;
31
32 /*!
33 @class OSData
34 @abstract A container class to manage an array of bytes.
35 */
36 class OSData : public OSObject
37 {
38 OSDeclareDefaultStructors(OSData)
39
40 protected:
41 void *data;
42 unsigned int length;
43 unsigned int capacity;
44 unsigned int capacityIncrement;
45
46 struct ExpansionData { };
47
48 /*! @var reserved
49 Reserved for future use. (Internal use only) */
50 ExpansionData *reserved;
51
52 public:
53 /*!
54 @function withCapacity
55 @abstract A static constructor function to create and initialize an empty instance of OSData with a given capacity.
56 @param inCapacity The initial capacity of the OSData object in bytes.
57 @result Returns an instance of OSData or 0 if a failure occurs.
58 */
59 static OSData *withCapacity(unsigned int inCapacity);
60 /*!
61 @function withBytes
62 @abstract A static constructor function to create and initialize an instance of OSData and copies in the provided data.
63 @param bytes A buffer of data.
64 @param inLength The size of the given buffer.
65 @result Returns an instance of OSData or 0 if a failure occurs.
66 */
67 static OSData *withBytes(const void *bytes, unsigned int inLength);
68 /*!
69 @function withBytesNoCopy
70 @abstract A static constructor function to create and initialize an instance of OSData which references a buffer of data.
71 @param bytes A reference to a block of data.
72 @param inLength The size of the data block.
73 @result Returns an instance of OSData or 0 if a failure occurs.
74 */
75 static OSData *withBytesNoCopy(void *bytes, unsigned int inLength);
76 /*!
77 @function withData
78 @abstract A static constructor function to create and initialize an instance of OSData with the data provided.
79 @param inData An OSData object which provides the initial data.
80 @result Returns an instance of OSData or 0 if a failure occurs.
81 */
82 static OSData *withData(const OSData *inData);
83 /*!
84 @function withData
85 @abstract A static constructor function to create and initialize an instance of OSData with a specific range of the data provided.
86 @param inData An OSData object which provides the initial data.
87 @param start The starting index at which the data will be copied.
88 @param inLength The number of bytes to be copied starting at index 'start'.
89 @result Returns an instance of OSData or 0 if a failure occurs.
90 */
91 static OSData *withData(const OSData *inData,
92 unsigned int start, unsigned int inLength);
93
94 /*!
95 @function initWithBytes
96 @abstract A member function to initialize an instance of OSData with the provided data.
97 @param bytes A pointer to a block of data to be copied.
98 @param inLength The length of the block of data.
99 @result Returns true if initialization was successful, false otherwise.
100 */
101 virtual bool initWithCapacity(unsigned int inCapacity);
102 /*!
103 @function initWithBytes
104 @abstract A member function to initialize an instance of OSData which references a block of data.
105 @param bytes A reference to a block of data
106 @param inLength The length of the block of data.
107 @result Returns true if initialization was successful, false otherwise.
108 */
109 virtual bool initWithBytes(const void *bytes, unsigned int inLength);
110 /*!
111 @function initWithBytes
112 @abstract A member function to initialize an instance of OSData which references a block of data.
113 @param bytes A reference to a block of data
114 @param inLength The length of the block of data.
115 @result Returns true if initialization was successful, false otherwise.
116 */
117 virtual bool initWithBytesNoCopy(void *bytes, unsigned int inLength);
118 /*!
119 @function initWithData
120 @abstract A member function to initialize an instance of OSData with the data provided.
121 @param inData An OSData object which provides the data to be copied.
122 @result Returns true if initialization was successful, false otherwise.
123 */
124 virtual bool initWithData(const OSData *inData);
125 /*!
126 @function initWithData
127 @abstract A member function to initialize an instance of OSData with a specific range of the data provided
128 @param inData An OSData object.
129 @param start The starting range of data to be copied.
130 @param inLength The length in bytes of the data to be copied.
131 @result Returns true if initialization was successful, false otherwise.
132 */
133 virtual bool initWithData(const OSData *inData,
134 unsigned int start, unsigned int inLength);
135 /*!
136 @function free
137 @abstract A member function which releases all resources created or used by the OSData object.
138 @discussion Do not call this function directly, use release() instead.
139 */
140 virtual void free();
141
142 /*!
143 @function getLength
144 @abstract A member function which returns the length of the internal data buffer.
145 @result Returns an integer value for the length of data in the object's internal data buffer.
146 */
147 virtual unsigned int getLength() const;
148 /*!
149 @function getCapacity
150 @abstract A member function which returns the capacity of the internal data buffer.
151 @result Returns an integer value for the size of the object's internal data buffer.
152 */
153 virtual unsigned int getCapacity() const;
154 /*!
155 @function getCapacityIncrement
156 @abstract A member function which returns the size by which the data buffer will grow.
157 @result Returns the size by which the data buffer will grow.
158 */
159 virtual unsigned int getCapacityIncrement() const;
160 /*!
161 @function setCapacityIncrement
162 @abstract A member function which sets the growth size of the data buffer.
163 @result Returns the new growth size.
164 */
165 virtual unsigned int setCapacityIncrement(unsigned increment);
166 /*!
167 @function ensureCapacity
168 @abstract A member function which will expand the size of the collection to a given storage capacity.
169 @param newCapacity The new capacity for the data buffer.
170 @result Returns the new capacity of the data buffer or the previous capacity upon error.
171 */
172 virtual unsigned int ensureCapacity(unsigned int newCapacity);
173 /*!
174 @function appendBytes
175 @abstract A member function which appends a buffer of data onto the end of the object's internal data buffer.
176 @param bytes A pointer to the block of data.
177 @param inLength The length of the data block.
178 @result Returns true if the object was able to append the new data, false otherwise.
179 */
180 virtual bool appendBytes(const void *bytes, unsigned int inLength);
181 /*!
182 @function appendBytes
183 @abstract A member function which appends the data contained in an OSData object to the receiver.
184 @param other An OSData object.
185 @result Returns true if appending the new data was successful, false otherwise.
186 */
187 virtual bool appendBytes(const OSData *other);
188
189 /*!
190 @function getBytesNoCopy
191 @abstract A member function to return a pointer to the OSData object's internal data buffer.
192 @result Returns a reference to the OSData object's internal data buffer.
193 */
194 virtual const void *getBytesNoCopy() const;
195 /*!
196 @function getBytesNoCopy
197 @abstract Returns a reference into the OSData object's internal data buffer at particular offset and with a particular length.
198 @param start The offset from the base of the internal data buffer.
199 @param inLength The length of window.
200 @result Returns a pointer at a particular offset into the data buffer, or 0 if the starting offset or length are not valid.
201 */
202 virtual const void *getBytesNoCopy(unsigned int start,
203 unsigned int inLength) const;
204
205 /*!
206 @function isEqualTo
207 @abstract A member function to test the equality of two OSData objects.
208 @param aData The OSData object to be compared to the receiver.
209 @result Returns true if the two objects are equivalent, false otherwise.
210 */
211 virtual bool isEqualTo(const OSData *aData) const;
212 /*!
213 @function isEqualTo
214 @abstract A member function to test the equality of an arbitrary block of data with the OSData object's internal data buffer.
215 @param someData A pointer to a block of data.
216 @param inLength The length of the block of data.
217 @result Returns true if the two blocks of data are equivalent, false otherwise.
218 */
219 virtual bool isEqualTo(const void *someData, unsigned int inLength) const;
220 /*!
221 @function isEqualTo
222 @abstract A member function to test the equality between an OSData object and an arbitrary OSObject derived object.
223 @param obj An OSObject derived object.
224 @result Returns true if the two objects are equivalent.
225 */
226 virtual bool isEqualTo(const OSMetaClassBase *obj) const;
227 /*!
228 @function isEqualTo
229 @abstract A member function to test the equality between an OSData object and an OSString object.
230 @param obj An OSString object
231 @result Returns true if the two objects are equivalent.
232 */
233 virtual bool isEqualTo(const OSString *obj) const;
234 /*!
235 @function serialize
236 @abstract A member function which archives the receiver.
237 @param s The OSSerialize object.
238 @result Returns true if serialization was successful, false if not.
239 */
240 virtual bool serialize(OSSerialize *s) const;
241
242 /*!
243 @function appendByte
244 @abstract A member function which appends a buffer of constant data onto the end of the object's internal data buffer.
245 @param byte A byte value to replicate as the added data.
246 @param inCount The length of the data to add.
247 @result Returns true if the object was able to append the new data, false otherwise.
248 */
249 virtual bool appendByte(unsigned char byte, unsigned int inCount);
250
251
252 OSMetaClassDeclareReservedUnused(OSData, 0);
253 OSMetaClassDeclareReservedUnused(OSData, 1);
254 OSMetaClassDeclareReservedUnused(OSData, 2);
255 OSMetaClassDeclareReservedUnused(OSData, 3);
256 OSMetaClassDeclareReservedUnused(OSData, 4);
257 OSMetaClassDeclareReservedUnused(OSData, 5);
258 OSMetaClassDeclareReservedUnused(OSData, 6);
259 OSMetaClassDeclareReservedUnused(OSData, 7);
260 };
261
262 #endif /* !_OS_OSDATA_H */