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