2 * Copyright © 2009, 2010 Codethink Limited
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 licence, or (at your option) any later version.
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.
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.
19 * Author: Ryan Lortie <desrt@desrt.ca>
24 #include "gbufferprivate.h"
26 #include <glib/gstrfuncs.h>
27 #include <glib/gatomic.h>
28 #include <glib/gslice.h>
29 #include <glib/gmem.h>
35 GDestroyNotify user_destroy;
40 g_buffer_free_gfree (GBuffer *buffer)
42 g_free ((gpointer) buffer->data);
43 g_slice_free (GBuffer, buffer);
47 * g_buffer_new_from_data:
48 * @data: the data to be used for the buffer
49 * @size: the size of @data
51 * Creates a new #GBuffer from @data.
55 * Returns: a reference to a new #GBuffer
59 g_buffer_new_from_data (gconstpointer data,
64 buffer = g_slice_new (GBuffer);
65 buffer->data = g_memdup (data, size);
67 buffer->free_func = g_buffer_free_gfree;
68 buffer->ref_count = 1;
74 * g_buffer_new_take_data:
75 * @data: the data to be used for the buffer
76 * @size: the size of @data
77 * returns: a reference to a new #GBuffer
79 * Creates a new #GBuffer from @data.
81 * @data must have been created by a call to g_malloc(), g_malloc0() or
82 * g_realloc() or by one of the many functions that wrap these calls
83 * (such as g_new(), g_strdup(), etc).
85 * After this call, @data belongs to the buffer and may no longer be
86 * modified by the caller. g_free() will be called on @data when the
87 * buffer is no longer in use.
90 g_buffer_new_take_data (gpointer data,
95 buffer = g_slice_new (GBuffer);
98 buffer->free_func = g_buffer_free_gfree;
99 buffer->ref_count = 1;
105 g_buffer_free (GBuffer *buffer)
107 g_slice_free (GBuffer, buffer);
111 * g_buffer_new_from_static_data:
112 * @data: the data to be used for the buffer
113 * @size: the size of @data
115 * Creates a new #GBuffer from static data.
117 * @data must be static (ie: never modified or freed).
119 * Returns: a reference to a new #GBuffer
122 g_buffer_new_from_static_data (gconstpointer data,
127 buffer = g_slice_new (GBuffer);
130 buffer->free_func = g_buffer_free;
131 buffer->ref_count = 1;
137 g_buffer_free_usernotify (GBuffer *buffer)
139 GUserNotifyBuffer *ubuffer = (GUserNotifyBuffer *) buffer;
141 ubuffer->user_destroy (ubuffer->user_data);
142 g_slice_free (GUserNotifyBuffer, ubuffer);
146 * g_buffer_new_from_pointer:
147 * @data: the data to be used for the buffer
148 * @size: the size of @data
149 * @notify: the function to call to release the data
150 * @user_data: the data to pass to @notify
152 * Creates a #GBuffer from @data.
154 * When the last reference is dropped, @notify will be called on
157 * @data must not be modified after this call is made, until @notify has
158 * been called to indicate that the buffer is no longer in use.
160 * Returns: a reference to a new #GBuffer
163 g_buffer_new_from_pointer (gconstpointer data,
165 GDestroyNotify notify,
168 GUserNotifyBuffer *ubuffer;
170 ubuffer = g_slice_new (GUserNotifyBuffer);
171 ubuffer->buffer.data = data;
172 ubuffer->buffer.size = size;
173 ubuffer->buffer.free_func = g_buffer_free_usernotify;
174 ubuffer->buffer.ref_count = 1;
175 ubuffer->user_destroy = notify;
176 ubuffer->user_data = user_data;
178 return (GBuffer *) ubuffer;
183 * @buffer: a #GBuffer
185 * Increase the reference count on @buffer.
190 g_buffer_ref (GBuffer *buffer)
192 g_atomic_int_inc (&buffer->ref_count);
199 * @buffer: a #GBuffer
201 * Releases a reference on @buffer. This may result in the buffer being
205 g_buffer_unref (GBuffer *buffer)
207 if (g_atomic_int_dec_and_test (&buffer->ref_count))
208 if (buffer->free_func != NULL)
209 buffer->free_func (buffer);