GArray: initialize the clear_func pointer
[platform/upstream/glib.git] / glib / garray.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #include "config.h"
32
33 #include <string.h>
34 #include <stdlib.h>
35
36 #include "garray.h"
37
38 #include "gbytes.h"
39 #include "gslice.h"
40 #include "gmem.h"
41 #include "gtestutils.h"
42 #include "gthread.h"
43 #include "gmessages.h"
44 #include "gqsort.h"
45
46
47 /**
48  * SECTION:arrays
49  * @title: Arrays
50  * @short_description: arrays of arbitrary elements which grow
51  *                     automatically as elements are added
52  *
53  * Arrays are similar to standard C arrays, except that they grow
54  * automatically as elements are added.
55  *
56  * Array elements can be of any size (though all elements of one array
57  * are the same size), and the array can be automatically cleared to
58  * '0's and zero-terminated.
59  *
60  * To create a new array use g_array_new().
61  *
62  * To add elements to an array, use g_array_append_val(),
63  * g_array_append_vals(), g_array_prepend_val(), and
64  * g_array_prepend_vals().
65  *
66  * To access an element of an array, use g_array_index().
67  *
68  * To set the size of an array, use g_array_set_size().
69  *
70  * To free an array, use g_array_free().
71  *
72  * <example>
73  *  <title>Using a #GArray to store #gint values</title>
74  *  <programlisting>
75  *   GArray *garray;
76  *   gint i;
77  *   /<!-- -->* We create a new array to store gint values.
78  *      We don't want it zero-terminated or cleared to 0's. *<!-- -->/
79  *   garray = g_array_new (FALSE, FALSE, sizeof (gint));
80  *   for (i = 0; i &lt; 10000; i++)
81  *     g_array_append_val (garray, i);
82  *   for (i = 0; i &lt; 10000; i++)
83  *     if (g_array_index (garray, gint, i) != i)
84  *       g_print ("ERROR: got &percnt;d instead of &percnt;d\n",
85  *                g_array_index (garray, gint, i), i);
86  *   g_array_free (garray, TRUE);
87  *  </programlisting>
88  * </example>
89  **/
90
91 #define MIN_ARRAY_SIZE  16
92
93 typedef struct _GRealArray  GRealArray;
94
95 /**
96  * GArray:
97  * @data: a pointer to the element data. The data may be moved as
98  *        elements are added to the #GArray.
99  * @len: the number of elements in the #GArray not including the
100  *       possible terminating zero element.
101  *
102  * Contains the public fields of an <link linkend="glib-Arrays">Array</link>.
103  **/
104 struct _GRealArray
105 {
106   guint8 *data;
107   guint   len;
108   guint   alloc;
109   guint   elt_size;
110   guint   zero_terminated : 1;
111   guint   clear : 1;
112   gint    ref_count;
113   GDestroyNotify clear_func;
114 };
115
116 /**
117  * g_array_index:
118  * @a: a #GArray.
119  * @t: the type of the elements.
120  * @i: the index of the element to return.
121  * @Returns: the element of the #GArray at the index given by @i.
122  *
123  * Returns the element of a #GArray at the given index. The return
124  * value is cast to the given type.
125  *
126  * <example>
127  *  <title>Getting a pointer to an element in a #GArray</title>
128  *  <programlisting>
129  *   EDayViewEvent *event;
130  *   /<!-- -->* This gets a pointer to the 4th element
131  *      in the array of EDayViewEvent structs. *<!-- -->/
132  *   event = &amp;g_array_index (events, EDayViewEvent, 3);
133  *  </programlisting>
134  * </example>
135  **/
136
137 #define g_array_elt_len(array,i) ((array)->elt_size * (i))
138 #define g_array_elt_pos(array,i) ((array)->data + g_array_elt_len((array),(i)))
139 #define g_array_elt_zero(array, pos, len)                               \
140   (memset (g_array_elt_pos ((array), pos), 0,  g_array_elt_len ((array), len)))
141 #define g_array_zero_terminate(array) G_STMT_START{                     \
142   if ((array)->zero_terminated)                                         \
143     g_array_elt_zero ((array), (array)->len, 1);                        \
144 }G_STMT_END
145
146 static guint g_nearest_pow        (gint        num) G_GNUC_CONST;
147 static void  g_array_maybe_expand (GRealArray *array,
148                                    gint        len);
149
150 /**
151  * g_array_new:
152  * @zero_terminated: %TRUE if the array should have an extra element at
153  *                   the end which is set to 0.
154  * @clear_: %TRUE if #GArray elements should be automatically cleared
155  *          to 0 when they are allocated.
156  * @element_size: the size of each element in bytes.
157  * @Returns: the new #GArray.
158  *
159  * Creates a new #GArray with a reference count of 1.
160  **/
161 GArray*
162 g_array_new (gboolean zero_terminated,
163              gboolean clear,
164              guint    elt_size)
165 {
166   g_return_val_if_fail (elt_size > 0, NULL);
167
168   return g_array_sized_new (zero_terminated, clear, elt_size, 0);
169 }
170
171 /**
172  * g_array_sized_new:
173  * @zero_terminated: %TRUE if the array should have an extra element at
174  *                   the end with all bits cleared.
175  * @clear_: %TRUE if all bits in the array should be cleared to 0 on
176  *          allocation.
177  * @element_size: size of each element in the array.
178  * @reserved_size: number of elements preallocated.
179  * @Returns: the new #GArray.
180  *
181  * Creates a new #GArray with @reserved_size elements preallocated and
182  * a reference count of 1. This avoids frequent reallocation, if you
183  * are going to add many elements to the array. Note however that the
184  * size of the array is still 0.
185  **/
186 GArray* g_array_sized_new (gboolean zero_terminated,
187                            gboolean clear,
188                            guint    elt_size,
189                            guint    reserved_size)
190 {
191   GRealArray *array;
192   
193   g_return_val_if_fail (elt_size > 0, NULL);
194
195   array = g_slice_new (GRealArray);
196
197   array->data            = NULL;
198   array->len             = 0;
199   array->alloc           = 0;
200   array->zero_terminated = (zero_terminated ? 1 : 0);
201   array->clear           = (clear ? 1 : 0);
202   array->elt_size        = elt_size;
203   array->ref_count       = 1;
204   array->clear_func      = NULL;
205
206   if (array->zero_terminated || reserved_size != 0)
207     {
208       g_array_maybe_expand (array, reserved_size);
209       g_array_zero_terminate(array);
210     }
211
212   return (GArray*) array;
213 }
214
215 /**
216  * g_array_set_clear_func:
217  * @array: A #GArray
218  * @clear_func: a function to clear an element of @array
219  *
220  * Sets a function to clear an element of @array.
221  *
222  * The @clear_func will be called when an element in the array
223  * data segment is removed and when the array is freed and data
224  * segment is deallocated as well.
225  *
226  * Note that in contrast with other uses of #GDestroyNotify
227  * functions, @clear_func is expected to clear the contents of
228  * the array element it is given, but not free the element itself.
229  *
230  * Since: 2.32
231  */
232 void
233 g_array_set_clear_func (GArray         *array,
234                         GDestroyNotify  clear_func)
235 {
236   GRealArray *rarray = (GRealArray *) array;
237
238   g_return_if_fail (array != NULL);
239
240   rarray->clear_func = clear_func;
241 }
242
243 /**
244  * g_array_ref:
245  * @array: A #GArray.
246  *
247  * Atomically increments the reference count of @array by one. This
248  * function is MT-safe and may be called from any thread.
249  *
250  * Returns: The passed in #GArray.
251  *
252  * Since: 2.22
253  **/
254 GArray *
255 g_array_ref (GArray *array)
256 {
257   GRealArray *rarray = (GRealArray*) array;
258   g_return_val_if_fail (array, NULL);
259
260   g_atomic_int_inc (&rarray->ref_count);
261
262   return array;
263 }
264
265 typedef enum
266 {
267   FREE_SEGMENT = 1 << 0,
268   PRESERVE_WRAPPER = 1 << 1
269 } ArrayFreeFlags;
270
271 static gchar *array_free (GRealArray *, ArrayFreeFlags);
272
273 /**
274  * g_array_unref:
275  * @array: A #GArray.
276  *
277  * Atomically decrements the reference count of @array by one. If the
278  * reference count drops to 0, all memory allocated by the array is
279  * released. This function is MT-safe and may be called from any
280  * thread.
281  *
282  * Since: 2.22
283  **/
284 void
285 g_array_unref (GArray *array)
286 {
287   GRealArray *rarray = (GRealArray*) array;
288   g_return_if_fail (array);
289
290   if (g_atomic_int_dec_and_test (&rarray->ref_count))
291     array_free (rarray, FREE_SEGMENT);
292 }
293
294 /**
295  * g_array_get_element_size:
296  * @array: A #GArray.
297  *
298  * Gets the size of the elements in @array.
299  *
300  * Returns: Size of each element, in bytes.
301  *
302  * Since: 2.22
303  **/
304 guint
305 g_array_get_element_size (GArray *array)
306 {
307   GRealArray *rarray = (GRealArray*) array;
308
309   g_return_val_if_fail (array, 0);
310
311   return rarray->elt_size;
312 }
313
314 /**
315  * g_array_free:
316  * @array: a #GArray.
317  * @free_segment: if %TRUE the actual element data is freed as well.
318  * @Returns: the element data if @free_segment is %FALSE, otherwise
319  *           %NULL.  The element data should be freed using g_free().
320  *
321  * Frees the memory allocated for the #GArray. If @free_segment is
322  * %TRUE it frees the memory block holding the elements as well and
323  * also each element if @array has a @element_free_func set. Pass
324  * %FALSE if you want to free the #GArray wrapper but preserve the
325  * underlying array for use elsewhere. If the reference count of @array
326  * is greater than one, the #GArray wrapper is preserved but the size
327  * of @array will be set to zero.
328  *
329  * <note><para>If array elements contain dynamically-allocated memory,
330  * they should be freed separately.</para></note>
331  **/
332 gchar*
333 g_array_free (GArray   *farray,
334               gboolean  free_segment)
335 {
336   GRealArray *array = (GRealArray*) farray;
337   ArrayFreeFlags flags;
338
339   g_return_val_if_fail (array, NULL);
340
341   flags = (free_segment ? FREE_SEGMENT : 0);
342
343   /* if others are holding a reference, preserve the wrapper but do free/return the data */
344   if (!g_atomic_int_dec_and_test (&array->ref_count))
345     flags |= PRESERVE_WRAPPER;
346
347   return array_free (array, flags);
348 }
349
350 static gchar *
351 array_free (GRealArray     *array,
352             ArrayFreeFlags  flags)
353 {
354   gchar *segment;
355
356   if (flags & FREE_SEGMENT)
357     {
358       if (array->clear_func != NULL)
359         {
360           guint i;
361
362           for (i = 0; i < array->len; i++)
363             array->clear_func (g_array_elt_pos (array, i));
364         }
365
366       g_free (array->data);
367       segment = NULL;
368     }
369   else
370     segment = (gchar*) array->data;
371
372   if (flags & PRESERVE_WRAPPER)
373     {
374       array->data            = NULL;
375       array->len             = 0;
376       array->alloc           = 0;
377     }
378   else
379     {
380       g_slice_free1 (sizeof (GRealArray), array);
381     }
382
383   return segment;
384 }
385
386 /**
387  * g_array_append_vals:
388  * @array: a #GArray.
389  * @data: a pointer to the elements to append to the end of the array.
390  * @len: the number of elements to append.
391  * @Returns: the #GArray.
392  *
393  * Adds @len elements onto the end of the array.
394  **/
395 /**
396  * g_array_append_val:
397  * @a: a #GArray.
398  * @v: the value to append to the #GArray.
399  * @Returns: the #GArray.
400  *
401  * Adds the value on to the end of the array. The array will grow in
402  * size automatically if necessary.
403  *
404  * <note><para>g_array_append_val() is a macro which uses a reference
405  * to the value parameter @v. This means that you cannot use it with
406  * literal values such as "27". You must use variables.</para></note>
407  **/
408 GArray*
409 g_array_append_vals (GArray       *farray,
410                      gconstpointer data,
411                      guint         len)
412 {
413   GRealArray *array = (GRealArray*) farray;
414
415   g_return_val_if_fail (array, NULL);
416
417   g_array_maybe_expand (array, len);
418
419   memcpy (g_array_elt_pos (array, array->len), data, 
420           g_array_elt_len (array, len));
421
422   array->len += len;
423
424   g_array_zero_terminate (array);
425
426   return farray;
427 }
428
429 /**
430  * g_array_prepend_vals:
431  * @array: a #GArray.
432  * @data: a pointer to the elements to prepend to the start of the
433  *        array.
434  * @len: the number of elements to prepend.
435  * @Returns: the #GArray.
436  *
437  * Adds @len elements onto the start of the array.
438  *
439  * This operation is slower than g_array_append_vals() since the
440  * existing elements in the array have to be moved to make space for
441  * the new elements.
442  **/
443 /**
444  * g_array_prepend_val:
445  * @a: a #GArray.
446  * @v: the value to prepend to the #GArray.
447  * @Returns: the #GArray.
448  *
449  * Adds the value on to the start of the array. The array will grow in
450  * size automatically if necessary.
451  *
452  * This operation is slower than g_array_append_val() since the
453  * existing elements in the array have to be moved to make space for
454  * the new element.
455  *
456  * <note><para>g_array_prepend_val() is a macro which uses a reference
457  * to the value parameter @v. This means that you cannot use it with
458  * literal values such as "27". You must use variables.</para></note>
459  **/
460 GArray*
461 g_array_prepend_vals (GArray        *farray,
462                       gconstpointer  data,
463                       guint          len)
464 {
465   GRealArray *array = (GRealArray*) farray;
466
467   g_return_val_if_fail (array, NULL);
468
469   g_array_maybe_expand (array, len);
470
471   g_memmove (g_array_elt_pos (array, len), g_array_elt_pos (array, 0), 
472              g_array_elt_len (array, array->len));
473
474   memcpy (g_array_elt_pos (array, 0), data, g_array_elt_len (array, len));
475
476   array->len += len;
477
478   g_array_zero_terminate (array);
479
480   return farray;
481 }
482
483 /**
484  * g_array_insert_vals:
485  * @array: a #GArray.
486  * @index_: the index to place the elements at.
487  * @data: a pointer to the elements to insert.
488  * @len: the number of elements to insert.
489  * @Returns: the #GArray.
490  *
491  * Inserts @len elements into a #GArray at the given index.
492  **/
493 /**
494  * g_array_insert_val:
495  * @a: a #GArray.
496  * @i: the index to place the element at.
497  * @v: the value to insert into the array.
498  * @Returns: the #GArray.
499  *
500  * Inserts an element into an array at the given index.
501  *
502  * <note><para>g_array_insert_val() is a macro which uses a reference
503  * to the value parameter @v. This means that you cannot use it with
504  * literal values such as "27". You must use variables.</para></note>
505  **/
506 GArray*
507 g_array_insert_vals (GArray        *farray,
508                      guint          index_,
509                      gconstpointer  data,
510                      guint          len)
511 {
512   GRealArray *array = (GRealArray*) farray;
513
514   g_return_val_if_fail (array, NULL);
515
516   g_array_maybe_expand (array, len);
517
518   g_memmove (g_array_elt_pos (array, len + index_), 
519              g_array_elt_pos (array, index_), 
520              g_array_elt_len (array, array->len - index_));
521
522   memcpy (g_array_elt_pos (array, index_), data, g_array_elt_len (array, len));
523
524   array->len += len;
525
526   g_array_zero_terminate (array);
527
528   return farray;
529 }
530
531 /**
532  * g_array_set_size:
533  * @array: a #GArray.
534  * @length: the new size of the #GArray.
535  * @Returns: the #GArray.
536  *
537  * Sets the size of the array, expanding it if necessary. If the array
538  * was created with @clear_ set to %TRUE, the new elements are set to 0.
539  **/
540 GArray*
541 g_array_set_size (GArray *farray,
542                   guint   length)
543 {
544   GRealArray *array = (GRealArray*) farray;
545
546   g_return_val_if_fail (array, NULL);
547
548   if (length > array->len)
549     {
550       g_array_maybe_expand (array, length - array->len);
551       
552       if (array->clear)
553         g_array_elt_zero (array, array->len, length - array->len);
554     }
555   else if (length < array->len)
556     g_array_remove_range (farray, length, array->len - length);
557   
558   array->len = length;
559   
560   g_array_zero_terminate (array);
561   
562   return farray;
563 }
564
565 /**
566  * g_array_remove_index:
567  * @array: a #GArray.
568  * @index_: the index of the element to remove.
569  * @Returns: the #GArray.
570  *
571  * Removes the element at the given index from a #GArray. The following
572  * elements are moved down one place.
573  **/
574 GArray*
575 g_array_remove_index (GArray *farray,
576                       guint   index_)
577 {
578   GRealArray* array = (GRealArray*) farray;
579
580   g_return_val_if_fail (array, NULL);
581
582   g_return_val_if_fail (index_ < array->len, NULL);
583
584   if (array->clear_func != NULL)
585     array->clear_func (g_array_elt_pos (array, index_));
586
587   if (index_ != array->len - 1)
588     g_memmove (g_array_elt_pos (array, index_),
589                g_array_elt_pos (array, index_ + 1),
590                g_array_elt_len (array, array->len - index_ - 1));
591
592   array->len -= 1;
593
594   if (G_UNLIKELY (g_mem_gc_friendly))
595     g_array_elt_zero (array, array->len, 1);
596   else
597     g_array_zero_terminate (array);
598
599   return farray;
600 }
601
602 /**
603  * g_array_remove_index_fast:
604  * @array: a @GArray.
605  * @index_: the index of the element to remove.
606  * @Returns: the #GArray.
607  *
608  * Removes the element at the given index from a #GArray. The last
609  * element in the array is used to fill in the space, so this function
610  * does not preserve the order of the #GArray. But it is faster than
611  * g_array_remove_index().
612  **/
613 GArray*
614 g_array_remove_index_fast (GArray *farray,
615                            guint   index_)
616 {
617   GRealArray* array = (GRealArray*) farray;
618
619   g_return_val_if_fail (array, NULL);
620
621   g_return_val_if_fail (index_ < array->len, NULL);
622
623   if (array->clear_func != NULL)
624     array->clear_func (g_array_elt_pos (array, index_));
625
626   if (index_ != array->len - 1)
627     memcpy (g_array_elt_pos (array, index_),
628             g_array_elt_pos (array, array->len - 1),
629             g_array_elt_len (array, 1));
630   
631   array->len -= 1;
632
633   if (G_UNLIKELY (g_mem_gc_friendly))
634     g_array_elt_zero (array, array->len, 1);
635   else
636     g_array_zero_terminate (array);
637
638   return farray;
639 }
640
641 /**
642  * g_array_remove_range:
643  * @array: a @GArray.
644  * @index_: the index of the first element to remove.
645  * @length: the number of elements to remove.
646  * @Returns: the #GArray.
647  *
648  * Removes the given number of elements starting at the given index
649  * from a #GArray.  The following elements are moved to close the gap.
650  *
651  * Since: 2.4
652  **/
653 GArray*
654 g_array_remove_range (GArray *farray,
655                       guint   index_,
656                       guint   length)
657 {
658   GRealArray *array = (GRealArray*) farray;
659
660   g_return_val_if_fail (array, NULL);
661   g_return_val_if_fail (index_ < array->len, NULL);
662   g_return_val_if_fail (index_ + length <= array->len, NULL);
663
664   if (array->clear_func != NULL)
665     {
666       guint i;
667
668       for (i = 0; i < length; i++)
669         array->clear_func (g_array_elt_pos (array, index_ + i));
670     }
671
672   if (index_ + length != array->len)
673     g_memmove (g_array_elt_pos (array, index_),
674                g_array_elt_pos (array, index_ + length),
675                (array->len - (index_ + length)) * array->elt_size);
676
677   array->len -= length;
678   if (G_UNLIKELY (g_mem_gc_friendly))
679     g_array_elt_zero (array, array->len, length);
680   else
681     g_array_zero_terminate (array);
682
683   return farray;
684 }
685
686 /**
687  * g_array_sort:
688  * @array: a #GArray.
689  * @compare_func: comparison function.
690  *
691  * Sorts a #GArray using @compare_func which should be a qsort()-style
692  * comparison function (returns less than zero for first arg is less
693  * than second arg, zero for equal, greater zero if first arg is
694  * greater than second arg).
695  *
696  * If two array elements compare equal, their order in the sorted array
697  * is undefined. If you want equal elements to keep their order (i.e.
698  * you want a stable sort) you can write a comparison function that,
699  * if two elements would otherwise compare equal, compares them by
700  * their addresses.
701  **/
702 void
703 g_array_sort (GArray       *farray,
704               GCompareFunc  compare_func)
705 {
706   GRealArray *array = (GRealArray*) farray;
707
708   g_return_if_fail (array != NULL);
709
710   qsort (array->data,
711          array->len,
712          array->elt_size,
713          compare_func);
714 }
715
716 /**
717  * g_array_sort_with_data:
718  * @array: a #GArray.
719  * @compare_func: comparison function.
720  * @user_data: data to pass to @compare_func.
721  *
722  * Like g_array_sort(), but the comparison function receives an extra
723  * user data argument.
724  **/
725 void
726 g_array_sort_with_data (GArray           *farray,
727                         GCompareDataFunc  compare_func,
728                         gpointer          user_data)
729 {
730   GRealArray *array = (GRealArray*) farray;
731
732   g_return_if_fail (array != NULL);
733
734   g_qsort_with_data (array->data,
735                      array->len,
736                      array->elt_size,
737                      compare_func,
738                      user_data);
739 }
740
741 /* Returns the smallest power of 2 greater than n, or n if
742  * such power does not fit in a guint
743  */
744 static guint
745 g_nearest_pow (gint num)
746 {
747   guint n = 1;
748
749   while (n < num && n > 0)
750     n <<= 1;
751
752   return n ? n : num;
753 }
754
755 static void
756 g_array_maybe_expand (GRealArray *array,
757                       gint        len)
758 {
759   guint want_alloc = g_array_elt_len (array, array->len + len + 
760                                       array->zero_terminated);
761
762   if (want_alloc > array->alloc)
763     {
764       want_alloc = g_nearest_pow (want_alloc);
765       want_alloc = MAX (want_alloc, MIN_ARRAY_SIZE);
766
767       array->data = g_realloc (array->data, want_alloc);
768
769       if (G_UNLIKELY (g_mem_gc_friendly))
770         memset (array->data + array->alloc, 0, want_alloc - array->alloc);
771
772       array->alloc = want_alloc;
773     }
774 }
775
776 /**
777  * SECTION:arrays_pointer
778  * @title: Pointer Arrays
779  * @short_description: arrays of pointers to any type of data, which
780  *                     grow automatically as new elements are added
781  *
782  * Pointer Arrays are similar to Arrays but are used only for storing
783  * pointers.
784  *
785  * <note><para>If you remove elements from the array, elements at the
786  * end of the array are moved into the space previously occupied by the
787  * removed element. This means that you should not rely on the index of
788  * particular elements remaining the same. You should also be careful
789  * when deleting elements while iterating over the array.</para></note>
790  *
791  * To create a pointer array, use g_ptr_array_new().
792  *
793  * To add elements to a pointer array, use g_ptr_array_add().
794  *
795  * To remove elements from a pointer array, use g_ptr_array_remove(),
796  * g_ptr_array_remove_index() or g_ptr_array_remove_index_fast().
797  *
798  * To access an element of a pointer array, use g_ptr_array_index().
799  *
800  * To set the size of a pointer array, use g_ptr_array_set_size().
801  *
802  * To free a pointer array, use g_ptr_array_free().
803  *
804  * <example>
805  *  <title>Using a #GPtrArray</title>
806  *  <programlisting>
807  *   GPtrArray *gparray;
808  *   gchar *string1 = "one", *string2 = "two", *string3 = "three";
809  *
810  *   gparray = g_ptr_array_new (<!-- -->);
811  *   g_ptr_array_add (gparray, (gpointer) string1);
812  *   g_ptr_array_add (gparray, (gpointer) string2);
813  *   g_ptr_array_add (gparray, (gpointer) string3);
814  *
815  *   if (g_ptr_array_index (gparray, 0) != (gpointer) string1)
816  *     g_print ("ERROR: got &percnt;p instead of &percnt;p\n",
817  *              g_ptr_array_index (gparray, 0), string1);
818  *
819  *   g_ptr_array_free (gparray, TRUE);
820  *  </programlisting>
821  * </example>
822  **/
823
824 typedef struct _GRealPtrArray  GRealPtrArray;
825
826 /**
827  * GPtrArray:
828  * @pdata: points to the array of pointers, which may be moved when the
829  *         array grows.
830  * @len: number of pointers in the array.
831  *
832  * Contains the public fields of a pointer array.
833  **/
834 struct _GRealPtrArray
835 {
836   gpointer     *pdata;
837   guint         len;
838   guint         alloc;
839   gint          ref_count;
840   GDestroyNotify element_free_func;
841 };
842
843 /**
844  * g_ptr_array_index:
845  * @array: a #GPtrArray.
846  * @index_: the index of the pointer to return.
847  * @Returns: the pointer at the given index.
848  *
849  * Returns the pointer at the given index of the pointer array.
850  **/
851
852 static void g_ptr_array_maybe_expand (GRealPtrArray *array,
853                                       gint           len);
854
855 /**
856  * g_ptr_array_new:
857  * @Returns: the new #GPtrArray.
858  *
859  * Creates a new #GPtrArray with a reference count of 1.
860  **/
861 GPtrArray*
862 g_ptr_array_new (void)
863 {
864   return g_ptr_array_sized_new (0);
865 }
866
867 /**
868  * g_ptr_array_sized_new:
869  * @reserved_size: number of pointers preallocated.
870  * @Returns: the new #GPtrArray.
871  *
872  * Creates a new #GPtrArray with @reserved_size pointers preallocated
873  * and a reference count of 1. This avoids frequent reallocation, if
874  * you are going to add many pointers to the array. Note however that
875  * the size of the array is still 0.
876  **/
877 GPtrArray*  
878 g_ptr_array_sized_new (guint reserved_size)
879 {
880   GRealPtrArray *array = g_slice_new (GRealPtrArray);
881
882   array->pdata = NULL;
883   array->len = 0;
884   array->alloc = 0;
885   array->ref_count = 1;
886   array->element_free_func = NULL;
887
888   if (reserved_size != 0)
889     g_ptr_array_maybe_expand (array, reserved_size);
890
891   return (GPtrArray*) array;  
892 }
893
894 /**
895  * g_ptr_array_new_with_free_func:
896  * @element_free_func: A function to free elements with destroy @array or %NULL.
897  *
898  * Creates a new #GPtrArray with a reference count of 1 and use @element_free_func
899  * for freeing each element when the array is destroyed either via
900  * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
901  * set to %TRUE or when removing elements.
902  *
903  * Returns: A new #GPtrArray.
904  *
905  * Since: 2.22
906  **/
907 GPtrArray *
908 g_ptr_array_new_with_free_func (GDestroyNotify element_free_func)
909 {
910   GPtrArray *array;
911
912   array = g_ptr_array_new ();
913   g_ptr_array_set_free_func (array, element_free_func);
914   return array;
915 }
916
917 /**
918  * g_ptr_array_new_full:
919  * @reserved_size: number of pointers preallocated.
920  * @element_free_func: A function to free elements with destroy @array or %NULL.
921  *
922  * Creates a new #GPtrArray with @reserved_size pointers preallocated
923  * and a reference count of 1. This avoids frequent reallocation, if
924  * you are going to add many pointers to the array. Note however that
925  * the size of the array is still 0. It also set @element_free_func
926  * for freeing each element when the array is destroyed either via
927  * g_ptr_array_unref(), when g_ptr_array_free() is called with @free_segment
928  * set to %TRUE or when removing elements.
929  *
930  * Returns: A new #GPtrArray.
931  *
932  * Since: 2.30
933  **/
934 GPtrArray *
935 g_ptr_array_new_full (guint          reserved_size,
936                       GDestroyNotify element_free_func)
937 {
938   GPtrArray *array;
939
940   array = g_ptr_array_sized_new (reserved_size);
941   g_ptr_array_set_free_func (array, element_free_func);
942   return array;
943 }
944
945 /**
946  * g_ptr_array_set_free_func:
947  * @array: A #GPtrArray.
948  * @element_free_func: A function to free elements with destroy @array or %NULL.
949  *
950  * Sets a function for freeing each element when @array is destroyed
951  * either via g_ptr_array_unref(), when g_ptr_array_free() is called
952  * with @free_segment set to %TRUE or when removing elements.
953  *
954  * Since: 2.22
955  **/
956 void
957 g_ptr_array_set_free_func (GPtrArray        *array,
958                            GDestroyNotify    element_free_func)
959 {
960   GRealPtrArray* rarray = (GRealPtrArray*) array;
961
962   g_return_if_fail (array);
963
964   rarray->element_free_func = element_free_func;
965 }
966
967 /**
968  * g_ptr_array_ref:
969  * @array: A #GArray.
970  *
971  * Atomically increments the reference count of @array by one. This
972  * function is MT-safe and may be called from any thread.
973  *
974  * Returns: The passed in #GPtrArray.
975  *
976  * Since: 2.22
977  **/
978 GPtrArray *
979 g_ptr_array_ref (GPtrArray *array)
980 {
981   GRealPtrArray *rarray = (GRealPtrArray*) array;
982
983   g_return_val_if_fail (array, NULL);
984
985   g_atomic_int_inc (&rarray->ref_count);
986
987   return array;
988 }
989
990 static gpointer *ptr_array_free (GPtrArray *, ArrayFreeFlags);
991
992 /**
993  * g_ptr_array_unref:
994  * @array: A #GPtrArray.
995  *
996  * Atomically decrements the reference count of @array by one. If the
997  * reference count drops to 0, the effect is the same as calling
998  * g_ptr_array_free() with @free_segment set to %TRUE. This function
999  * is MT-safe and may be called from any thread.
1000  *
1001  * Since: 2.22
1002  **/
1003 void
1004 g_ptr_array_unref (GPtrArray *array)
1005 {
1006   GRealPtrArray *rarray = (GRealPtrArray*) array;
1007   g_return_if_fail (array);
1008
1009   if (g_atomic_int_dec_and_test (&rarray->ref_count))
1010     ptr_array_free (array, FREE_SEGMENT);
1011 }
1012
1013 /**
1014  * g_ptr_array_free:
1015  * @array: a #GPtrArray.
1016  * @free_seg: if %TRUE the actual pointer array is freed as well.
1017  * @Returns: the pointer array if @free_seg is %FALSE, otherwise %NULL.
1018  *           The pointer array should be freed using g_free().
1019  *
1020  * Frees the memory allocated for the #GPtrArray. If @free_seg is %TRUE
1021  * it frees the memory block holding the elements as well. Pass %FALSE
1022  * if you want to free the #GPtrArray wrapper but preserve the
1023  * underlying array for use elsewhere. If the reference count of @array
1024  * is greater than one, the #GPtrArray wrapper is preserved but the
1025  * size of @array will be set to zero.
1026  *
1027  * <note><para>If array contents point to dynamically-allocated
1028  * memory, they should be freed separately if @free_seg is %TRUE and no
1029  * #GDestroyNotify function has been set for @array.</para></note>
1030  **/
1031 gpointer*
1032 g_ptr_array_free (GPtrArray *farray,
1033                   gboolean   free_segment)
1034 {
1035   GRealPtrArray *array = (GRealPtrArray*) farray;
1036   ArrayFreeFlags flags;
1037
1038   g_return_val_if_fail (array, NULL);
1039
1040   flags = (free_segment ? FREE_SEGMENT : 0);
1041
1042   /* if others are holding a reference, preserve the wrapper but do free/return the data */
1043   if (!g_atomic_int_dec_and_test (&array->ref_count))
1044     flags |= PRESERVE_WRAPPER;
1045
1046   return ptr_array_free (farray, flags);
1047 }
1048
1049 static gpointer *
1050 ptr_array_free (GPtrArray      *farray,
1051                 ArrayFreeFlags  flags)
1052 {
1053   GRealPtrArray *array = (GRealPtrArray*) farray;
1054   gpointer *segment;
1055
1056   if (flags & FREE_SEGMENT)
1057     {
1058       if (array->element_free_func != NULL)
1059         g_ptr_array_foreach (farray, (GFunc) array->element_free_func, NULL);
1060       g_free (array->pdata);
1061       segment = NULL;
1062     }
1063   else
1064     segment = array->pdata;
1065
1066   if (flags & PRESERVE_WRAPPER)
1067     {
1068       array->pdata = NULL;
1069       array->len = 0;
1070       array->alloc = 0;
1071     }
1072   else
1073     {
1074       g_slice_free1 (sizeof (GRealPtrArray), array);
1075     }
1076
1077   return segment;
1078 }
1079
1080 static void
1081 g_ptr_array_maybe_expand (GRealPtrArray *array,
1082                           gint           len)
1083 {
1084   if ((array->len + len) > array->alloc)
1085     {
1086       guint old_alloc = array->alloc;
1087       array->alloc = g_nearest_pow (array->len + len);
1088       array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
1089       array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
1090       if (G_UNLIKELY (g_mem_gc_friendly))
1091         for ( ; old_alloc < array->alloc; old_alloc++)
1092           array->pdata [old_alloc] = NULL;
1093     }
1094 }
1095
1096 /**
1097  * g_ptr_array_set_size:
1098  * @array: a #GPtrArray.
1099  * @length: the new length of the pointer array.
1100  *
1101  * Sets the size of the array. When making the array larger,
1102  * newly-added elements will be set to %NULL. When making it smaller,
1103  * if @array has a non-%NULL #GDestroyNotify function then it will be
1104  * called for the removed elements.
1105  **/
1106 void
1107 g_ptr_array_set_size  (GPtrArray *farray,
1108                        gint       length)
1109 {
1110   GRealPtrArray* array = (GRealPtrArray*) farray;
1111
1112   g_return_if_fail (array);
1113
1114   if (length > array->len)
1115     {
1116       int i;
1117       g_ptr_array_maybe_expand (array, (length - array->len));
1118       /* This is not 
1119        *     memset (array->pdata + array->len, 0,
1120        *            sizeof (gpointer) * (length - array->len));
1121        * to make it really portable. Remember (void*)NULL needn't be
1122        * bitwise zero. It of course is silly not to use memset (..,0,..).
1123        */
1124       for (i = array->len; i < length; i++)
1125         array->pdata[i] = NULL;
1126     }
1127   else if (length < array->len)
1128     g_ptr_array_remove_range (farray, length, array->len - length);
1129
1130   array->len = length;
1131 }
1132
1133 /**
1134  * g_ptr_array_remove_index:
1135  * @array: a #GPtrArray.
1136  * @index_: the index of the pointer to remove.
1137  * @Returns: the pointer which was removed.
1138  *
1139  * Removes the pointer at the given index from the pointer array. The
1140  * following elements are moved down one place. If @array has a
1141  * non-%NULL #GDestroyNotify function it is called for the removed
1142  * element.
1143  **/
1144 gpointer
1145 g_ptr_array_remove_index (GPtrArray *farray,
1146                           guint      index_)
1147 {
1148   GRealPtrArray* array = (GRealPtrArray*) farray;
1149   gpointer result;
1150
1151   g_return_val_if_fail (array, NULL);
1152
1153   g_return_val_if_fail (index_ < array->len, NULL);
1154
1155   result = array->pdata[index_];
1156   
1157   if (array->element_free_func != NULL)
1158     array->element_free_func (array->pdata[index_]);
1159
1160   if (index_ != array->len - 1)
1161     g_memmove (array->pdata + index_, array->pdata + index_ + 1, 
1162                sizeof (gpointer) * (array->len - index_ - 1));
1163   
1164   array->len -= 1;
1165
1166   if (G_UNLIKELY (g_mem_gc_friendly))
1167     array->pdata[array->len] = NULL;
1168
1169   return result;
1170 }
1171
1172 /**
1173  * g_ptr_array_remove_index_fast:
1174  * @array: a #GPtrArray.
1175  * @index_: the index of the pointer to remove.
1176  * @Returns: the pointer which was removed.
1177  *
1178  * Removes the pointer at the given index from the pointer array. The
1179  * last element in the array is used to fill in the space, so this
1180  * function does not preserve the order of the array. But it is faster
1181  * than g_ptr_array_remove_index(). If @array has a non-%NULL
1182  * #GDestroyNotify function it is called for the removed element.
1183  **/
1184 gpointer
1185 g_ptr_array_remove_index_fast (GPtrArray *farray,
1186                                guint      index_)
1187 {
1188   GRealPtrArray* array = (GRealPtrArray*) farray;
1189   gpointer result;
1190
1191   g_return_val_if_fail (array, NULL);
1192
1193   g_return_val_if_fail (index_ < array->len, NULL);
1194
1195   result = array->pdata[index_];
1196
1197   if (array->element_free_func != NULL)
1198     array->element_free_func (array->pdata[index_]);
1199
1200   if (index_ != array->len - 1)
1201     array->pdata[index_] = array->pdata[array->len - 1];
1202
1203   array->len -= 1;
1204
1205   if (G_UNLIKELY (g_mem_gc_friendly))
1206     array->pdata[array->len] = NULL;
1207
1208   return result;
1209 }
1210
1211 /**
1212  * g_ptr_array_remove_range:
1213  * @array: a @GPtrArray.
1214  * @index_: the index of the first pointer to remove.
1215  * @length: the number of pointers to remove.
1216  *
1217  * Removes the given number of pointers starting at the given index
1218  * from a #GPtrArray.  The following elements are moved to close the
1219  * gap. If @array has a non-%NULL #GDestroyNotify function it is called
1220  * for the removed elements.
1221  *
1222  * Since: 2.4
1223  **/
1224 void
1225 g_ptr_array_remove_range (GPtrArray *farray,
1226                           guint      index_,
1227                           guint      length)
1228 {
1229   GRealPtrArray* array = (GRealPtrArray*) farray;
1230   guint n;
1231
1232   g_return_if_fail (array);
1233   g_return_if_fail (index_ < array->len);
1234   g_return_if_fail (index_ + length <= array->len);
1235
1236   if (array->element_free_func != NULL)
1237     {
1238       for (n = index_; n < index_ + length; n++)
1239         array->element_free_func (array->pdata[n]);
1240     }
1241
1242   if (index_ + length != array->len)
1243     {
1244       g_memmove (&array->pdata[index_],
1245                  &array->pdata[index_ + length], 
1246                  (array->len - (index_ + length)) * sizeof (gpointer));
1247     }
1248
1249   array->len -= length;
1250   if (G_UNLIKELY (g_mem_gc_friendly))
1251     {
1252       guint i;
1253       for (i = 0; i < length; i++)
1254         array->pdata[array->len + i] = NULL;
1255     }
1256 }
1257
1258 /**
1259  * g_ptr_array_remove:
1260  * @array: a #GPtrArray.
1261  * @data: the pointer to remove.
1262  * @Returns: %TRUE if the pointer is removed. %FALSE if the pointer is
1263  *           not found in the array.
1264  *
1265  * Removes the first occurrence of the given pointer from the pointer
1266  * array. The following elements are moved down one place. If @array
1267  * has a non-%NULL #GDestroyNotify function it is called for the
1268  * removed element.
1269  *
1270  * It returns %TRUE if the pointer was removed, or %FALSE if the
1271  * pointer was not found.
1272  **/
1273 gboolean
1274 g_ptr_array_remove (GPtrArray *farray,
1275                     gpointer   data)
1276 {
1277   GRealPtrArray* array = (GRealPtrArray*) farray;
1278   guint i;
1279
1280   g_return_val_if_fail (array, FALSE);
1281
1282   for (i = 0; i < array->len; i += 1)
1283     {
1284       if (array->pdata[i] == data)
1285         {
1286           g_ptr_array_remove_index (farray, i);
1287           return TRUE;
1288         }
1289     }
1290
1291   return FALSE;
1292 }
1293
1294 /**
1295  * g_ptr_array_remove_fast:
1296  * @array: a #GPtrArray.
1297  * @data: the pointer to remove.
1298  * @Returns: %TRUE if the pointer was found in the array.
1299  *
1300  * Removes the first occurrence of the given pointer from the pointer
1301  * array. The last element in the array is used to fill in the space,
1302  * so this function does not preserve the order of the array. But it is
1303  * faster than g_ptr_array_remove(). If @array has a non-%NULL
1304  * #GDestroyNotify function it is called for the removed element.
1305  *
1306  * It returns %TRUE if the pointer was removed, or %FALSE if the
1307  * pointer was not found.
1308  **/
1309 gboolean
1310 g_ptr_array_remove_fast (GPtrArray *farray,
1311                          gpointer   data)
1312 {
1313   GRealPtrArray* array = (GRealPtrArray*) farray;
1314   guint i;
1315
1316   g_return_val_if_fail (array, FALSE);
1317
1318   for (i = 0; i < array->len; i += 1)
1319     {
1320       if (array->pdata[i] == data)
1321         {
1322           g_ptr_array_remove_index_fast (farray, i);
1323           return TRUE;
1324         }
1325     }
1326
1327   return FALSE;
1328 }
1329
1330 /**
1331  * g_ptr_array_add:
1332  * @array: a #GPtrArray.
1333  * @data: the pointer to add.
1334  *
1335  * Adds a pointer to the end of the pointer array. The array will grow
1336  * in size automatically if necessary.
1337  **/
1338 void
1339 g_ptr_array_add (GPtrArray *farray,
1340                  gpointer   data)
1341 {
1342   GRealPtrArray* array = (GRealPtrArray*) farray;
1343
1344   g_return_if_fail (array);
1345
1346   g_ptr_array_maybe_expand (array, 1);
1347
1348   array->pdata[array->len++] = data;
1349 }
1350
1351 /**
1352  * g_ptr_array_sort:
1353  * @array: a #GPtrArray.
1354  * @compare_func: comparison function.
1355  *
1356  * Sorts the array, using @compare_func which should be a qsort()-style
1357  * comparison function (returns less than zero for first arg is less
1358  * than second arg, zero for equal, greater than zero if irst arg is
1359  * greater than second arg).
1360  *
1361  * If two array elements compare equal, their order in the sorted array
1362  * is undefined. If you want equal elements to keep their order (i.e.
1363  * you want a stable sort) you can write a comparison function that,
1364  * if two elements would otherwise compare equal, compares them by
1365  * their addresses.
1366  *
1367  * <note><para>The comparison function for g_ptr_array_sort() doesn't
1368  * take the pointers from the array as arguments, it takes pointers to
1369  * the pointers in the array.</para></note>
1370  **/
1371 void
1372 g_ptr_array_sort (GPtrArray    *array,
1373                   GCompareFunc  compare_func)
1374 {
1375   g_return_if_fail (array != NULL);
1376
1377   qsort (array->pdata,
1378          array->len,
1379          sizeof (gpointer),
1380          compare_func);
1381 }
1382
1383 /**
1384  * g_ptr_array_sort_with_data:
1385  * @array: a #GPtrArray.
1386  * @compare_func: comparison function.
1387  * @user_data: data to pass to @compare_func.
1388  *
1389  * Like g_ptr_array_sort(), but the comparison function has an extra
1390  * user data argument.
1391  *
1392  * <note><para>The comparison function for g_ptr_array_sort_with_data()
1393  * doesn't take the pointers from the array as arguments, it takes
1394  * pointers to the pointers in the array.</para></note>
1395  **/
1396 void
1397 g_ptr_array_sort_with_data (GPtrArray        *array,
1398                             GCompareDataFunc  compare_func,
1399                             gpointer          user_data)
1400 {
1401   g_return_if_fail (array != NULL);
1402
1403   g_qsort_with_data (array->pdata,
1404                      array->len,
1405                      sizeof (gpointer),
1406                      compare_func,
1407                      user_data);
1408 }
1409
1410 /**
1411  * g_ptr_array_foreach:
1412  * @array: a #GPtrArray
1413  * @func: the function to call for each array element
1414  * @user_data: user data to pass to the function
1415  * 
1416  * Calls a function for each element of a #GPtrArray.
1417  *
1418  * Since: 2.4
1419  **/
1420 void
1421 g_ptr_array_foreach (GPtrArray *array,
1422                      GFunc      func,
1423                      gpointer   user_data)
1424 {
1425   guint i;
1426
1427   g_return_if_fail (array);
1428
1429   for (i = 0; i < array->len; i++)
1430     (*func) (array->pdata[i], user_data);
1431 }
1432
1433 /**
1434  * SECTION:arrays_byte
1435  * @title: Byte Arrays
1436  * @short_description: arrays of bytes
1437  *
1438  * #GByteArray is a mutable array of bytes based on #GArray, to provide arrays
1439  * of bytes which grow automatically as elements are added.
1440  *
1441  * To create a new #GByteArray use g_byte_array_new(). To add elements to a
1442  * #GByteArray, use g_byte_array_append(), and g_byte_array_prepend().
1443  *
1444  * To set the size of a #GByteArray, use g_byte_array_set_size().
1445  *
1446  * To free a #GByteArray, use g_byte_array_free().
1447  *
1448  * <example>
1449  *  <title>Using a #GByteArray</title>
1450  *  <programlisting>
1451  *   GByteArray *gbarray;
1452  *   gint i;
1453  *
1454  *   gbarray = g_byte_array_new (<!-- -->);
1455  *   for (i = 0; i &lt; 10000; i++)
1456  *     g_byte_array_append (gbarray, (guint8*) "abcd", 4);
1457  *
1458  *   for (i = 0; i &lt; 10000; i++)
1459  *     {
1460  *       g_assert (gbarray->data[4*i] == 'a');
1461  *       g_assert (gbarray->data[4*i+1] == 'b');
1462  *       g_assert (gbarray->data[4*i+2] == 'c');
1463  *       g_assert (gbarray->data[4*i+3] == 'd');
1464  *     }
1465  *
1466  *   g_byte_array_free (gbarray, TRUE);
1467  *  </programlisting>
1468  * </example>
1469  *
1470  * See #GBytes if you are interested in an immutable object representing a
1471  * sequence of bytes.
1472  **/
1473
1474 /**
1475  * GByteArray:
1476  * @data: a pointer to the element data. The data may be moved as
1477  *        elements are added to the #GByteArray.
1478  * @len: the number of elements in the #GByteArray.
1479  *
1480  * The <structname>GByteArray</structname> struct allows access to the
1481  * public fields of a <structname>GByteArray</structname>.
1482  **/
1483
1484 /**
1485  * g_byte_array_new:
1486  * @Returns: the new #GByteArray.
1487  *
1488  * Creates a new #GByteArray with a reference count of 1.
1489  **/
1490 GByteArray* g_byte_array_new (void)
1491 {
1492   return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, 0);
1493 }
1494
1495 /**
1496  * g_byte_array_new_take:
1497  * @data: (array length=len): byte data for the array
1498  * @len: length of @data
1499  *
1500  * Create byte array containing the data. The data will be owned by the array
1501  * and will be freed with g_free(), i.e. it could be allocated using g_strdup().
1502  *
1503  * Since: 2.32
1504  *
1505  * Returns: (transfer full): a new #GByteArray
1506  */
1507 GByteArray *
1508 g_byte_array_new_take (guint8 *data,
1509                        gsize   len)
1510 {
1511   GByteArray *array;
1512   GRealArray *real;
1513
1514   array = g_byte_array_new ();
1515   real = (GRealArray *)array;
1516   g_assert (real->data == NULL);
1517   g_assert (real->len == 0);
1518
1519   real->data = data;
1520   real->len = len;
1521
1522   return array;
1523 }
1524
1525 /**
1526  * g_byte_array_sized_new:
1527  * @reserved_size: number of bytes preallocated.
1528  * @Returns: the new #GByteArray.
1529  *
1530  * Creates a new #GByteArray with @reserved_size bytes preallocated.
1531  * This avoids frequent reallocation, if you are going to add many
1532  * bytes to the array. Note however that the size of the array is still
1533  * 0.
1534  **/
1535 GByteArray* g_byte_array_sized_new (guint reserved_size)
1536 {
1537   return (GByteArray*) g_array_sized_new (FALSE, FALSE, 1, reserved_size);
1538 }
1539
1540 /**
1541  * g_byte_array_free:
1542  * @array: a #GByteArray.
1543  * @free_segment: if %TRUE the actual byte data is freed as well.
1544  * @Returns: the element data if @free_segment is %FALSE, otherwise
1545  *           %NULL.  The element data should be freed using g_free().
1546  *
1547  * Frees the memory allocated by the #GByteArray. If @free_segment is
1548  * %TRUE it frees the actual byte data. If the reference count of
1549  * @array is greater than one, the #GByteArray wrapper is preserved but
1550  * the size of @array will be set to zero.
1551  **/
1552 guint8*     g_byte_array_free     (GByteArray *array,
1553                                    gboolean    free_segment)
1554 {
1555   return (guint8*) g_array_free ((GArray*) array, free_segment);
1556 }
1557
1558 /**
1559  * g_byte_array_free_to_bytes:
1560  * @array: (transfer full): a #GByteArray
1561  *
1562  * Transfers the data from the #GByteArray into a new immutable #GBytes.
1563  *
1564  * The #GByteArray is freed unless the reference count of @array is greater
1565  * than one, the #GByteArray wrapper is preserved but the size of @array
1566  * will be set to zero.
1567  *
1568  * This is identical to using g_bytes_new_take() and g_byte_array_free()
1569  * together.
1570  *
1571  * Since: 2.32
1572  *
1573  * Returns: (transfer full): a new immutable #GBytes representing same byte
1574  *          data that was in the array
1575  */
1576 GBytes *
1577 g_byte_array_free_to_bytes (GByteArray *array)
1578 {
1579   gsize length;
1580
1581   g_return_val_if_fail (array != NULL, NULL);
1582
1583   length = array->len;
1584   return g_bytes_new_take (g_byte_array_free (array, FALSE), length);
1585 }
1586
1587 /**
1588  * g_byte_array_ref:
1589  * @array: A #GByteArray.
1590  *
1591  * Atomically increments the reference count of @array by one. This
1592  * function is MT-safe and may be called from any thread.
1593  *
1594  * Returns: The passed in #GByteArray.
1595  *
1596  * Since: 2.22
1597  **/
1598 GByteArray *
1599 g_byte_array_ref (GByteArray *array)
1600 {
1601   return (GByteArray *) g_array_ref ((GArray *) array);
1602 }
1603
1604 /**
1605  * g_byte_array_unref:
1606  * @array: A #GByteArray.
1607  *
1608  * Atomically decrements the reference count of @array by one. If the
1609  * reference count drops to 0, all memory allocated by the array is
1610  * released. This function is MT-safe and may be called from any
1611  * thread.
1612  *
1613  * Since: 2.22
1614  **/
1615 void
1616 g_byte_array_unref (GByteArray *array)
1617 {
1618   g_array_unref ((GArray *) array);
1619 }
1620
1621 /**
1622  * g_byte_array_append:
1623  * @array: a #GByteArray.
1624  * @data: the byte data to be added.
1625  * @len: the number of bytes to add.
1626  * @Returns: the #GByteArray.
1627  *
1628  * Adds the given bytes to the end of the #GByteArray. The array will
1629  * grow in size automatically if necessary.
1630  **/
1631 GByteArray* g_byte_array_append   (GByteArray   *array,
1632                                    const guint8 *data,
1633                                    guint         len)
1634 {
1635   g_array_append_vals ((GArray*) array, (guint8*)data, len);
1636
1637   return array;
1638 }
1639
1640 /**
1641  * g_byte_array_prepend:
1642  * @array: a #GByteArray.
1643  * @data: the byte data to be added.
1644  * @len: the number of bytes to add.
1645  * @Returns: the #GByteArray.
1646  *
1647  * Adds the given data to the start of the #GByteArray. The array will
1648  * grow in size automatically if necessary.
1649  **/
1650 GByteArray* g_byte_array_prepend  (GByteArray   *array,
1651                                    const guint8 *data,
1652                                    guint         len)
1653 {
1654   g_array_prepend_vals ((GArray*) array, (guint8*)data, len);
1655
1656   return array;
1657 }
1658
1659 /**
1660  * g_byte_array_set_size:
1661  * @array: a #GByteArray.
1662  * @length: the new size of the #GByteArray.
1663  * @Returns: the #GByteArray.
1664  *
1665  * Sets the size of the #GByteArray, expanding it if necessary.
1666  **/
1667 GByteArray* g_byte_array_set_size (GByteArray *array,
1668                                    guint       length)
1669 {
1670   g_array_set_size ((GArray*) array, length);
1671
1672   return array;
1673 }
1674
1675 /**
1676  * g_byte_array_remove_index:
1677  * @array: a #GByteArray.
1678  * @index_: the index of the byte to remove.
1679  * @Returns: the #GByteArray.
1680  *
1681  * Removes the byte at the given index from a #GByteArray. The
1682  * following bytes are moved down one place.
1683  **/
1684 GByteArray* g_byte_array_remove_index (GByteArray *array,
1685                                        guint       index_)
1686 {
1687   g_array_remove_index ((GArray*) array, index_);
1688
1689   return array;
1690 }
1691
1692 /**
1693  * g_byte_array_remove_index_fast:
1694  * @array: a #GByteArray.
1695  * @index_: the index of the byte to remove.
1696  * @Returns: the #GByteArray.
1697  *
1698  * Removes the byte at the given index from a #GByteArray. The last
1699  * element in the array is used to fill in the space, so this function
1700  * does not preserve the order of the #GByteArray. But it is faster
1701  * than g_byte_array_remove_index().
1702  **/
1703 GByteArray* g_byte_array_remove_index_fast (GByteArray *array,
1704                                             guint       index_)
1705 {
1706   g_array_remove_index_fast ((GArray*) array, index_);
1707
1708   return array;
1709 }
1710
1711 /**
1712  * g_byte_array_remove_range:
1713  * @array: a @GByteArray.
1714  * @index_: the index of the first byte to remove.
1715  * @length: the number of bytes to remove.
1716  * @Returns: the #GByteArray.
1717  *
1718  * Removes the given number of bytes starting at the given index from a
1719  * #GByteArray.  The following elements are moved to close the gap.
1720  *
1721  * Since: 2.4
1722  **/
1723 GByteArray*
1724 g_byte_array_remove_range (GByteArray *array,
1725                            guint       index_,
1726                            guint       length)
1727 {
1728   g_return_val_if_fail (array, NULL);
1729   g_return_val_if_fail (index_ < array->len, NULL);
1730   g_return_val_if_fail (index_ + length <= array->len, NULL);
1731
1732   return (GByteArray *)g_array_remove_range ((GArray*) array, index_, length);
1733 }
1734
1735 /**
1736  * g_byte_array_sort:
1737  * @array: a #GByteArray.
1738  * @compare_func: comparison function.
1739  *
1740  * Sorts a byte array, using @compare_func which should be a
1741  * qsort()-style comparison function (returns less than zero for first
1742  * arg is less than second arg, zero for equal, greater than zero if
1743  * first arg is greater than second arg).
1744  *
1745  * If two array elements compare equal, their order in the sorted array
1746  * is undefined. If you want equal elements to keep their order (i.e.
1747  * you want a stable sort) you can write a comparison function that,
1748  * if two elements would otherwise compare equal, compares them by
1749  * their addresses.
1750  **/
1751 void
1752 g_byte_array_sort (GByteArray   *array,
1753                    GCompareFunc  compare_func)
1754 {
1755   g_array_sort ((GArray *) array, compare_func);
1756 }
1757
1758 /**
1759  * g_byte_array_sort_with_data:
1760  * @array: a #GByteArray.
1761  * @compare_func: comparison function.
1762  * @user_data: data to pass to @compare_func.
1763  *
1764  * Like g_byte_array_sort(), but the comparison function takes an extra
1765  * user data argument.
1766  **/
1767 void
1768 g_byte_array_sort_with_data (GByteArray       *array,
1769                              GCompareDataFunc  compare_func,
1770                              gpointer          user_data)
1771 {
1772   g_array_sort_with_data ((GArray *) array, compare_func, user_data);
1773 }