1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* Copyright © 2009, 2010 Codethink Limited
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the licence, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Ryan Lortie <desrt@desrt.ca>
*/
#include "config.h"
#include "gbuffer.h"
#include <glib/gstrfuncs.h>
#include <glib/gatomic.h>
#include <glib/gmem.h>
typedef struct
{
GBuffer buffer;
GDestroyNotify user_destroy;
gpointer user_data;
} GUserNotifyBuffer;
static void
g_buffer_free_gfree (GBuffer *buffer)
{
g_free ((gpointer) buffer->data);
g_slice_free (GBuffer, buffer);
}
/* < private >
* g_buffer_new_from_data:
* @data: the data to be used for the buffer
* @size: the size of @data
* @returns: a reference to a new #GBuffer
*
* Creates a new #GBuffer from @data.
*
* @data is copied.
*/
GBuffer *
g_buffer_new_from_data (gconstpointer data,
gsize size)
{
GBuffer *buffer;
buffer = g_slice_new (GBuffer);
buffer->data = g_memdup (data, size);
buffer->size = size;
buffer->free_func = g_buffer_free_gfree;
buffer->ref_count = 1;
return buffer;
}
/* < private >
* g_buffer_new_take_data:
* @data: the data to be used for the buffer
* @size: the size of @data
* returns: a reference to a new #GBuffer
*
* Creates a new #GBuffer from @data.
*
* @data must have been created by a call to g_malloc(), g_malloc0() or
* g_realloc() or by one of the many functions that wrap these calls
* (such as g_new(), g_strdup(), etc).
*
* After this call, @data belongs to the buffer and may no longer be
* modified by the caller. g_free() will be called on @data when the
* buffer is no longer in use.
*/
GBuffer *
g_buffer_new_take_data (gpointer data,
gsize size)
{
GBuffer *buffer;
buffer = g_slice_new (GBuffer);
buffer->data = data;
buffer->size = size;
buffer->free_func = g_buffer_free_gfree;
buffer->ref_count = 1;
return buffer;
}
static void
g_buffer_free (GBuffer *buffer)
{
g_slice_free (GBuffer, buffer);
}
/* < private >
* g_buffer_new_from_static_data:
* @data: the data to be used for the buffer
* @size: the size of @data
* @returns: a reference to a new #GBuffer
*
* Creates a new #GBuffer from static data.
*
* @data must be static (ie: never modified or freed).
*/
GBuffer *
g_buffer_new_from_static_data (gconstpointer data,
gsize size)
{
GBuffer *buffer;
buffer = g_slice_new (GBuffer);
buffer->data = data;
buffer->size = size;
buffer->free_func = g_buffer_free;
buffer->ref_count = 1;
return buffer;
}
static void
g_buffer_free_usernotify (GBuffer *buffer)
{
GUserNotifyBuffer *ubuffer = (GUserNotifyBuffer *) buffer;
ubuffer->user_destroy (ubuffer->user_data);
g_slice_free (GUserNotifyBuffer, ubuffer);
}
/* < private >
* g_buffer_new_from_pointer:
* @data: the data to be used for the buffer
* @size: the size of @data
* @notify: the function to call to release the data
* @user_data: the data to pass to @notify
* @returns: a reference to a new #GBuffer
*
* Creates a #GBuffer from @data.
*
* When the last reference is dropped, @notify will be called on
* @user_data.
*
* @data must not be modified after this call is made, until @notify has
* been called to indicate that the buffer is no longer in use.
*/
GBuffer *
g_buffer_new_from_pointer (gconstpointer data,
gsize size,
GDestroyNotify notify,
gpointer user_data)
{
GUserNotifyBuffer *ubuffer;
ubuffer = g_slice_new (GUserNotifyBuffer);
ubuffer->buffer.data = data;
ubuffer->buffer.size = size;
ubuffer->buffer.free_func = g_buffer_free_usernotify;
ubuffer->buffer.ref_count = 1;
ubuffer->user_destroy = notify;
ubuffer->user_data = user_data;
return (GBuffer *) ubuffer;
}
/* < private >
* g_buffer_ref:
* @buffer: a #GBuffer
* @returns: @buffer
*
* Increase the reference count on @buffer.
*/
GBuffer *
g_buffer_ref (GBuffer *buffer)
{
g_atomic_int_inc (&buffer->ref_count);
return buffer;
}
/* < private >
* g_buffer_unref:
* @buffer: a #GBuffer
*
* Releases a reference on @buffer. This may result in the buffer being
* freed.
*/
void
g_buffer_unref (GBuffer *buffer)
{
if (g_atomic_int_dec_and_test (&buffer->ref_count))
if (buffer->free_func != NULL)
buffer->free_func (buffer);
}
|