daf564245d95fe9ae76d297f8eafa39c5769b40a
[platform/upstream/glib.git] / glib / gbytes.c
1 /*
2  * Copyright © 2009, 2010 Codethink Limited
3  * Copyright © 2011 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the licence, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Ryan Lortie <desrt@desrt.ca>
21  *         Stef Walter <stefw@collabora.co.uk>
22  */
23
24 #include "config.h"
25
26 #include "gbytes.h"
27
28 #include <glib/garray.h>
29 #include <glib/gstrfuncs.h>
30 #include <glib/gatomic.h>
31 #include <glib/gslice.h>
32 #include <glib/gtestutils.h>
33 #include <glib/gmem.h>
34 #include <glib/gmessages.h>
35
36 #include <string.h>
37
38 /**
39  * GBytes:
40  *
41  * A simple refcounted data type representing an immutable byte sequence
42  * from an unspecified origin.
43  *
44  * The purpose of a #GBytes is to keep the memory region that it holds
45  * alive for as long as anyone holds a reference to the bytes.  When
46  * the last reference count is dropped, the memory is released. Multiple
47  * unrelated callers can use byte data in the #GBytes without coordinating
48  * their activities, resting assured that the byte data will not change or
49  * move while they hold a reference.
50  *
51  * A #GBytes can come from many different origins that may have
52  * different procedures for freeing the memory region.  Examples are
53  * memory from g_malloc(), from memory slices, from a #GMappedFile or
54  * memory from other allocators.
55  *
56  * #GBytes work well as keys in #GHashTable. Use g_bytes_equal() and
57  * g_bytes_hash() as parameters to g_hash_table_new() or g_hash_table_new_full().
58  * #GBytes can also be used as keys in a #GTree by passing the g_bytes_compare()
59  * function to g_tree_new().
60  *
61  * The data pointed to by this bytes must not be modified. For a mutable
62  * array of bytes see #GByteArray. Use g_bytes_unref_to_array() to create a
63  * mutable array for a #GBytes sequence. To create an immutable #GBytes from
64  * a mutable #GByteArray, use the g_byte_array_free_to_bytes() function.
65  *
66  * Since: 2.32
67  **/
68
69 struct _GBytes
70 {
71   gconstpointer data;
72   gsize size;
73   gint ref_count;
74   GDestroyNotify free_func;
75   gpointer user_data;
76 };
77
78 /**
79  * g_bytes_new:
80  * @data: (array length=size): the data to be used for the bytes
81  * @size: the size of @data
82  *
83  * Creates a new #GBytes from @data.
84  *
85  * @data is copied.
86  *
87  * Returns: (transfer full): a new #GBytes
88  *
89  * Since: 2.32
90  */
91 GBytes *
92 g_bytes_new (gconstpointer data,
93              gsize         size)
94 {
95   return g_bytes_new_take (g_memdup (data, size), size);
96 }
97
98 /**
99  * g_bytes_new_take:
100  * @data: (transfer full) (array length=size): the data to be used for the bytes
101  * @size: the size of @data
102  *
103  * Creates a new #GBytes from @data.
104  *
105  * After this call, @data belongs to the bytes and may no longer be
106  * modified by the caller.  g_free() will be called on @data when the
107  * bytes is no longer in use. Because of this @data must have been created by
108  * a call to g_malloc(), g_malloc0() or g_realloc() or by one of the many
109  * functions that wrap these calls (such as g_new(), g_strdup(), etc).
110  *
111  * For creating #GBytes with memory from other allocators, see
112  * g_bytes_new_with_free_func().
113  *
114  * Returns: (transfer full): a new #GBytes
115  *
116  * Since: 2.32
117  */
118 GBytes *
119 g_bytes_new_take (gpointer data,
120                   gsize    size)
121 {
122   return g_bytes_new_with_free_func (data, size, g_free, data);
123 }
124
125
126 /**
127  * g_bytes_new_static:
128  * @data: (array length=size): the data to be used for the bytes
129  * @size: the size of @data
130  *
131  * Creates a new #GBytes from static data.
132  *
133  * @data must be static (ie: never modified or freed).
134  *
135  * Returns: (transfer full): a new #GBytes
136  *
137  * Since: 2.32
138  */
139 GBytes *
140 g_bytes_new_static (gconstpointer data,
141                     gsize         size)
142 {
143   return g_bytes_new_with_free_func (data, size, NULL, NULL);
144 }
145
146 /**
147  * g_bytes_new_with_free_func:
148  * @data: (array length=size): the data to be used for the bytes
149  * @size: the size of @data
150  * @free_func: the function to call to release the data
151  * @user_data: data to pass to @free_func
152  *
153  * Creates a #GBytes from @data.
154  *
155  * When the last reference is dropped, @free_func will be called with the
156  * @user_data argument.
157  *
158  * @data must not be modified after this call is made until @free_func has
159  * been called to indicate that the bytes is no longer in use.
160  *
161  * Returns: (transfer full): a new #GBytes
162  *
163  * Since: 2.32
164  */
165 GBytes *
166 g_bytes_new_with_free_func (gconstpointer  data,
167                             gsize          size,
168                             GDestroyNotify free_func,
169                             gpointer       user_data)
170 {
171   GBytes *bytes;
172
173   bytes = g_slice_new (GBytes);
174   bytes->data = data;
175   bytes->size = size;
176   bytes->free_func = free_func;
177   bytes->user_data = user_data;
178   bytes->ref_count = 1;
179
180   return (GBytes *)bytes;
181 }
182
183 /**
184  * g_bytes_new_from_bytes:
185  * @bytes: a #GBytes
186  * @offset: offset which subsection starts at
187  * @length: length of subsection
188  *
189  * Creates a #GBytes which is a subsection of another #GBytes. The @offset +
190  * @length may not be longer than the size of @bytes.
191  *
192  * A reference to @bytes will be held by the newly created #GBytes until
193  * the byte data is no longer needed.
194  *
195  * Returns: (transfer full): a new #GBytes
196  *
197  * Since: 2.32
198  */
199 GBytes *
200 g_bytes_new_from_bytes (GBytes  *bytes,
201                         gsize    offset,
202                         gsize    length)
203 {
204   g_return_val_if_fail (bytes != NULL, NULL);
205   g_return_val_if_fail (offset <= bytes->size, NULL);
206   g_return_val_if_fail (offset + length <= bytes->size, NULL);
207
208   return g_bytes_new_with_free_func ((gchar *)bytes->data + offset, length,
209                                      (GDestroyNotify)g_bytes_unref, g_bytes_ref (bytes));
210 }
211
212 /**
213  * g_bytes_get_data:
214  * @bytes: a #GBytes
215  * @size: (out) (allow-none): location to return size of byte data
216  *
217  * Get the byte data in the #GBytes. This data should not be modified.
218  *
219  * This function will always return the same pointer for a given #GBytes.
220  *
221  * Returns: (array length=size) (type guint8): a pointer to the byte data
222  *
223  * Since: 2.32
224  */
225 gconstpointer
226 g_bytes_get_data (GBytes *bytes,
227                   gsize *size)
228 {
229   g_return_val_if_fail (bytes != NULL, NULL);
230   if (size)
231     *size = bytes->size;
232   return bytes->data;
233 }
234
235 /**
236  * g_bytes_get_size:
237  * @bytes: a #GBytes
238  *
239  * Get the size of the byte data in the #GBytes.
240  *
241  * This function will always return the same value for a given #GBytes.
242  *
243  * Returns: the size
244  *
245  * Since: 2.32
246  */
247 gsize
248 g_bytes_get_size (GBytes *bytes)
249 {
250   g_return_val_if_fail (bytes != NULL, 0);
251   return bytes->size;
252 }
253
254
255 /**
256  * g_bytes_ref:
257  * @bytes: a #GBytes
258  *
259  * Increase the reference count on @bytes.
260  *
261  * Returns: the #GBytes
262  *
263  * Since: 2.32
264  */
265 GBytes *
266 g_bytes_ref (GBytes *bytes)
267 {
268   g_return_val_if_fail (bytes != NULL, NULL);
269
270   g_atomic_int_inc (&bytes->ref_count);
271
272   return bytes;
273 }
274
275 /**
276  * g_bytes_unref:
277  * @bytes: (allow-none): a #GBytes
278  *
279  * Releases a reference on @bytes.  This may result in the bytes being
280  * freed.
281  *
282  * Since: 2.32
283  */
284 void
285 g_bytes_unref (GBytes *bytes)
286 {
287   if (bytes == NULL)
288     return;
289
290   if (g_atomic_int_dec_and_test (&bytes->ref_count))
291     {
292       if (bytes->free_func != NULL)
293         bytes->free_func (bytes->user_data);
294       g_slice_free (GBytes, bytes);
295     }
296 }
297
298 /**
299  * g_bytes_equal:
300  * @bytes1: (type GLib.Bytes): a pointer to a #GBytes
301  * @bytes2: (type GLib.Bytes): a pointer to a #GBytes to compare with @bytes1
302  *
303  * Compares the two #GBytes values being pointed to and returns
304  * %TRUE if they are equal.
305  *
306  * This function can be passed to g_hash_table_new() as the @key_equal_func
307  * parameter, when using non-%NULL #GBytes pointers as keys in a #GHashTable.
308  *
309  * Returns: %TRUE if the two keys match.
310  *
311  * Since: 2.32
312  */
313 gboolean
314 g_bytes_equal (gconstpointer bytes1,
315                gconstpointer bytes2)
316 {
317   const GBytes *b1 = bytes1;
318   const GBytes *b2 = bytes2;
319
320   g_return_val_if_fail (bytes1 != NULL, FALSE);
321   g_return_val_if_fail (bytes2 != NULL, FALSE);
322
323   return b1->size == b2->size &&
324          memcmp (b1->data, b2->data, b1->size) == 0;
325 }
326
327 /**
328  * g_bytes_hash:
329  * @bytes: (type GLib.Bytes): a pointer to a #GBytes key
330  *
331  * Creates an integer hash code for the byte data in the #GBytes.
332  *
333  * This function can be passed to g_hash_table_new() as the @key_equal_func
334  * parameter, when using non-%NULL #GBytes pointers as keys in a #GHashTable.
335  *
336  * Returns: a hash value corresponding to the key.
337  *
338  * Since: 2.32
339  */
340 guint
341 g_bytes_hash (gconstpointer bytes)
342 {
343   const GBytes *a = bytes;
344   const signed char *p, *e;
345   guint32 h = 5381;
346
347   g_return_val_if_fail (bytes != NULL, 0);
348
349   for (p = (signed char *)a->data, e = (signed char *)a->data + a->size; p != e; p++)
350     h = (h << 5) + h + *p;
351
352   return h;
353 }
354
355 /**
356  * g_bytes_compare:
357  * @bytes1: (type GLib.Bytes): a pointer to a #GBytes
358  * @bytes2: (type GLib.Bytes): a pointer to a #GBytes to compare with @bytes1
359  *
360  * Compares the two #GBytes values.
361  *
362  * This function can be used to sort GBytes instances in lexographical order.
363  *
364  * Returns: a negative value if bytes2 is lesser, a positive value if bytes2 is
365  *          greater, and zero if bytes2 is equal to bytes1
366  *
367  * Since: 2.32
368  */
369 gint
370 g_bytes_compare (gconstpointer bytes1,
371                  gconstpointer bytes2)
372 {
373   const GBytes *b1 = bytes1;
374   const GBytes *b2 = bytes2;
375   gint ret;
376
377   g_return_val_if_fail (bytes1 != NULL, 0);
378   g_return_val_if_fail (bytes2 != NULL, 0);
379
380   ret = memcmp (b1->data, b2->data, MIN (b1->size, b2->size));
381   if (ret == 0 && b1->size != b2->size)
382       ret = b1->size < b2->size ? -1 : 1;
383   return ret;
384 }
385
386 static gpointer
387 try_steal_and_unref (GBytes         *bytes,
388                      GDestroyNotify  free_func,
389                      gsize          *size)
390 {
391   gpointer result;
392
393   if (bytes->free_func != free_func)
394     return NULL;
395
396   /* Are we the only reference? */
397   if (g_atomic_int_get (&bytes->ref_count) == 1)
398     {
399       *size = bytes->size;
400       result = (gpointer)bytes->data;
401       g_slice_free (GBytes, bytes);
402       return result;
403     }
404
405   return NULL;
406 }
407
408
409 /**
410  * g_bytes_unref_to_data:
411  * @bytes: (transfer full): a #GBytes
412  * @size: location to place the length of the returned data
413  *
414  * Unreferences the bytes, and returns a pointer the same byte data
415  * contents.
416  *
417  * As an optimization, the byte data is returned without copying if this was
418  * the last reference to bytes and bytes was created with g_bytes_new(),
419  * g_bytes_new_take() or g_byte_array_free_to_bytes(). In all other cases the
420  * data is copied.
421  *
422  * Returns: (transfer full): a pointer to the same byte data, which should
423  *          be freed with g_free()
424  *
425  * Since: 2.32
426  */
427 gpointer
428 g_bytes_unref_to_data (GBytes *bytes,
429                        gsize  *size)
430 {
431   gpointer result;
432
433   g_return_val_if_fail (bytes != NULL, NULL);
434   g_return_val_if_fail (size != NULL, NULL);
435
436   /*
437    * Optimal path: if this is was the last reference, then we can return
438    * the data from this GBytes without copying.
439    */
440
441   result = try_steal_and_unref (bytes, g_free, size);
442   if (result == NULL)
443     {
444       /*
445        * Copy: Non g_malloc (or compatible) allocator, or static memory,
446        * so we have to copy, and then unref.
447        */
448       result = g_memdup (bytes->data, bytes->size);
449       *size = bytes->size;
450       g_bytes_unref (bytes);
451     }
452
453   return result;
454 }
455
456 /**
457  * g_bytes_unref_to_array:
458  * @bytes: (transfer full): a #GBytes
459  *
460  * Unreferences the bytes, and returns a new mutable #GByteArray containing
461  * the same byte data.
462  *
463  * As an optimization, the byte data is transferred to the array without copying
464  * if this was the last reference to bytes and bytes was created with
465  * g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all
466  * other cases the data is copied.
467  *
468  * Returns: (transfer full): a new mutable #GByteArray containing the same byte data
469  *
470  * Since: 2.32
471  */
472 GByteArray *
473 g_bytes_unref_to_array (GBytes *bytes)
474 {
475   gpointer data;
476   gsize size;
477
478   g_return_val_if_fail (bytes != NULL, NULL);
479
480   data = g_bytes_unref_to_data (bytes, &size);
481   return g_byte_array_new_take (data, size);
482 }