1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 arrays of bytes, which grow automatically as elements are added
7 <!-- ##### SECTION Long_Description ##### -->
9 #GByteArray is based on #GArray, to provide arrays of bytes which grow
10 automatically as elements are added.
13 To create a new #GByteArray use g_byte_array_new().
16 To add elements to a #GByteArray, use g_byte_array_append(), and
17 g_byte_array_prepend().
20 To set the size of a #GByteArray, use g_byte_array_set_size().
23 To free a #GByteArray, use g_byte_array_free().
27 <title>Using a <structname>GByteArray</structname></title>
32 gbarray = g_byte_array_new (<!-- -->);
33 for (i = 0; i < 10000; i++)
34 g_byte_array_append (gbarray, (guint8*) "abcd", 4);
36 for (i = 0; i < 10000; i++)
38 g_assert (gbarray->data[4*i] == 'a');
39 g_assert (gbarray->data[4*i+1] == 'b');
40 g_assert (gbarray->data[4*i+2] == 'c');
41 g_assert (gbarray->data[4*i+3] == 'd');
44 g_byte_array_free (gbarray, TRUE);
45 </programlisting></example>
47 <!-- ##### SECTION See_Also ##### -->
52 <!-- ##### SECTION Stability_Level ##### -->
55 <!-- ##### STRUCT GByteArray ##### -->
57 The <structname>GByteArray</structname> struct allows access to the public fields of a <structname>GByteArray</structname>.
60 @data: a pointer to the element data. The data may be moved as elements are
61 added to the #GByteArray.
62 @len: the number of elements in the #GByteArray.
64 <!-- ##### FUNCTION g_byte_array_new ##### -->
66 Creates a new #GByteArray with a reference count of 1.
69 @Returns: the new #GByteArray.
72 <!-- ##### FUNCTION g_byte_array_sized_new ##### -->
74 Creates a new #GByteArray with @reserved_size bytes preallocated. This
75 avoids frequent reallocation, if you are going to add many bytes to
76 the array. Note however that the size of the array is still 0.
79 @reserved_size: number of bytes preallocated.
80 @Returns: the new #GByteArray.
83 <!-- ##### FUNCTION g_byte_array_ref ##### -->
92 <!-- ##### FUNCTION g_byte_array_unref ##### -->
100 <!-- ##### FUNCTION g_byte_array_append ##### -->
102 Adds the given bytes to the end of the #GByteArray.
103 The array will grow in size automatically if necessary.
106 @array: a #GByteArray.
107 @data: the byte data to be added.
108 @len: the number of bytes to add.
109 @Returns: the #GByteArray.
112 <!-- ##### FUNCTION g_byte_array_prepend ##### -->
114 Adds the given data to the start of the #GByteArray.
115 The array will grow in size automatically if necessary.
118 @array: a #GByteArray.
119 @data: the byte data to be added.
120 @len: the number of bytes to add.
121 @Returns: the #GByteArray.
124 <!-- ##### FUNCTION g_byte_array_remove_index ##### -->
126 Removes the byte at the given index from a #GByteArray.
127 The following bytes are moved down one place.
130 @array: a #GByteArray.
131 @index_: the index of the byte to remove.
132 @Returns: the #GByteArray.
135 <!-- ##### FUNCTION g_byte_array_remove_index_fast ##### -->
137 Removes the byte at the given index from a #GByteArray.
138 The last element in the array is used to fill in the space, so this function
139 does not preserve the order of the #GByteArray. But it is faster than
140 g_byte_array_remove_index().
143 @array: a #GByteArray.
144 @index_: the index of the byte to remove.
145 @Returns: the #GByteArray.
148 <!-- ##### FUNCTION g_byte_array_remove_range ##### -->
150 Removes the given number of bytes starting at the given index from a
151 #GByteArray. The following elements are moved to close the gap.
154 @array: a @GByteArray.
155 @index_: the index of the first byte to remove.
156 @length: the number of bytes to remove.
157 @Returns: the #GByteArray.
161 <!-- ##### FUNCTION g_byte_array_sort ##### -->
163 Sorts a byte array, using @compare_func which should be a qsort()-style
164 comparison function (returns less than zero for first arg is less than second
165 arg, zero for equal, greater than zero if first arg is greater than second
169 If two array elements compare equal, their order in the sorted array is
173 @array: a #GByteArray.
174 @compare_func: comparison function.
177 <!-- ##### FUNCTION g_byte_array_sort_with_data ##### -->
179 Like g_byte_array_sort(), but the comparison function takes an extra user data
183 @array: a #GByteArray.
184 @compare_func: comparison function.
185 @user_data: data to pass to @compare_func.
188 <!-- ##### FUNCTION g_byte_array_set_size ##### -->
190 Sets the size of the #GByteArray, expanding it if necessary.
193 @array: a #GByteArray.
194 @length: the new size of the #GByteArray.
195 @Returns: the #GByteArray.
198 <!-- ##### FUNCTION g_byte_array_free ##### -->
200 Frees the memory allocated by the #GByteArray.
201 If @free_segment is %TRUE it frees the actual byte data. If the reference
202 count of @array is greater than one, the #GByteArray wrapper is preserved but
203 the size of @array will be set to zero.
206 @array: a #GByteArray.
207 @free_segment: if %TRUE the actual byte data is freed as well.
208 @Returns: the element data if @free_segment is %FALSE, otherwise %NULL.
209 The element data should be freed using g_free().