summaryrefslogtreecommitdiff
path: root/glib/gbuffer.h
blob: 6bb29b73c8e979d3366b1f7eacc84edadef7c6cd (plain)
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
/*
 * 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>
 */

#ifndef __G_BUFFER_H__
#define __G_BUFFER_H__

#include <glib/gtypes.h>

/* < private >
 * GBuffer:
 * @data: a pointer to the data held in the buffer
 * @size: the size of @data
 *
 * A simple refcounted data type representing a byte sequence from an
 * unspecified origin.
 *
 * The purpose of a #GBuffer is to keep the memory region that it holds
 * alive for as long as anyone holds a reference to the buffer.  When
 * the last reference count is dropped, the memory is released.
 *
 * A #GBuffer can come from many different origins that may have
 * different procedures for freeing the memory region.  Examples are
 * memory from g_malloc(), from memory slices, from a #GMappedFile or
 * memory from other allocators.
 */
typedef struct _GBuffer GBuffer;

/* < private >
 * GBufferFreeFunc:
 * @buffer: the #GBuffer to be freed
 *
 * This function is provided by creators of a #GBuffer.  It is the
 * function to be called when the reference count of @buffer drops to
 * zero.  It should free any memory associated with the buffer and free
 * @buffer itself.
 */
typedef void (* GBufferFreeFunc)                (GBuffer        *buffer);

struct _GBuffer
{
  gconstpointer data;
  gsize size;

  /*< protected >*/
  GBufferFreeFunc free_func;

  /*< private >*/
  gint ref_count;
};

G_GNUC_INTERNAL
GBuffer *       g_buffer_new_from_data          (gconstpointer   data,
                                                 gsize           size);
G_GNUC_INTERNAL
GBuffer *       g_buffer_new_take_data          (gpointer        data,
                                                 gsize           size);
G_GNUC_INTERNAL
GBuffer *       g_buffer_new_from_static_data   (gconstpointer   data,
                                                 gsize           size);
G_GNUC_INTERNAL
GBuffer *       g_buffer_new_from_pointer       (gconstpointer   data,
                                                 gsize           size,
                                                 GDestroyNotify  notify,
                                                 gpointer        user_data);
G_GNUC_INTERNAL
GBuffer *     g_buffer_ref                      (GBuffer        *buffer);
G_GNUC_INTERNAL
void          g_buffer_unref                    (GBuffer        *buffer);

#endif /* __G_BUFFER_H__ */