aa9cb220756acac508bcd7b5fbb6fe49f44e75a8
[platform/upstream/gstreamer.git] / gst / gstmemory.h
1 /* GStreamer
2  * Copyright (C) 2009 Wim Taymans <wim.taymans@gmail.be>
3  *
4  * gstmemory.h: Header for memory blocks
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22
23 #ifndef __GST_MEMORY_H__
24 #define __GST_MEMORY_H__
25
26 #include <gst/gstconfig.h>
27
28 #include <glib-object.h>
29
30 G_BEGIN_DECLS
31
32 #define GST_TYPE_MEMORY (gst_memory_get_type())
33 GType gst_memory_get_type(void);
34
35 #define GST_TYPE_ALLOCATOR (gst_allocator_get_type())
36 GType gst_allocator_get_type(void);
37
38 typedef struct _GstMemory GstMemory;
39 typedef struct _GstMemoryInfo GstMemoryInfo;
40 typedef struct _GstAllocator GstAllocator;
41
42 GST_EXPORT gsize gst_memory_alignment;
43
44 /**
45  * GstMemoryFlags:
46  * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
47  * memory with #GST_MAP_WRITE.
48  * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
49  * made when this memory needs to be shared between buffers.
50  * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
51  *
52  * Flags for wrapped memory.
53  */
54 typedef enum {
55   GST_MEMORY_FLAG_READONLY = (1 << 0),
56   GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
57
58   GST_MEMORY_FLAG_LAST     = (1 << 16)
59 } GstMemoryFlags;
60
61 /**
62  * GstMemory:
63  * @allocator: pointer to the #GstAllocator
64  * @flags: memory flags
65  * @refcount: refcount
66  * @parent: parent memory block
67  * @state: private state
68  * @maxsize: the maximum size allocated
69  * @align: the alignment of the memory
70  * @offset: the offset where valid data starts
71  * @size: the size of valid data
72  *
73  * Base structure for memory implementations. Custom memory will put this structure
74  * as the first member of their structure.
75  */
76 struct _GstMemory {
77   GstAllocator   *allocator;
78
79   GstMemoryFlags  flags;
80   gint            refcount;
81   GstMemory      *parent;
82   volatile gint   state;
83   gsize           maxsize;
84   gsize           align;
85   gsize           offset;
86   gsize           size;
87 };
88
89 /**
90  * GstMapFlags:
91  * @GST_MAP_READ: map for read access
92  * @GST_MAP_WRITE: map for write access
93  * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
94  *
95  * Flags used when mapping memory
96  */
97 typedef enum {
98   GST_MAP_READ      = (1 << 0),
99   GST_MAP_WRITE     = (1 << 1),
100
101   GST_MAP_FLAG_LAST = (1 << 16)
102 } GstMapFlags;
103
104 /**
105  * GstMapInfo:
106  * @memory: a pointer to the mapped memory
107  * @flags: flags used when mapping the memory
108  * @data: a pointer to the mapped data
109  * @size: the valid size in @data
110  * @maxsize: the maximum bytes in @data
111  *
112  * A structure containing the result of a map operation such as
113  * gst_memory_map(). It contains the data and size.
114  */
115 typedef struct {
116   GstMemory *memory;
117   GstMapFlags flags;
118   guint8 *data;
119   gsize size;
120   gsize maxsize;
121 } GstMapInfo;
122
123 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0 }
124
125 /**
126  * GST_MAP_READWRITE:
127  *
128  * Map for readwrite access
129  */
130 #define GST_MAP_READWRITE      (GST_MAP_READ | GST_MAP_WRITE)
131
132 /**
133  * GST_ALLOCATOR_SYSMEM:
134  *
135  * The allocator name for the default system memory allocator
136  */
137 #define GST_ALLOCATOR_SYSMEM   "SystemMemory"
138
139 /**
140  * GstAllocatorAllocFunction:
141  * @allocator: a #GstAllocator
142  * @maxsize: the maxsize
143  * @align: the alignment
144  * @user_data: user data
145  *
146  * Allocate a new #GstMemory from @allocator that can hold at least @maxsize bytes
147  * and is aligned to (@align + 1) bytes.
148  *
149  * @user_data is the data that was used when creating @allocator.
150  *
151  * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
152  */
153 typedef GstMemory *  (*GstAllocatorAllocFunction)  (GstAllocator *allocator,
154                                                     gsize maxsize, gsize align,
155                                                     gpointer user_data);
156
157 /**
158  * GstMemoryMapFunction:
159  * @mem: a #GstMemory
160  * @maxsize: size to map
161  * @flags: access mode for the memory
162  *
163  * Get the memory of @mem that can be accessed according to the mode specified
164  * in @flags. The function should return a pointer that contains at least
165  * @maxsize bytes.
166  *
167  * Returns: a pointer to memory of which at least @maxsize bytes can be
168  * accessed according to the access pattern in @flags.
169  */
170 typedef gpointer    (*GstMemoryMapFunction)       (GstMemory *mem, gsize maxsize, GstMapFlags flags);
171
172 /**
173  * GstMemoryUnmapFunction:
174  * @mem: a #GstMemory
175  *
176  * Return the pointer previously retrieved with gst_memory_map().
177  *
178  * Returns: %TRUE on success.
179  */
180 typedef void        (*GstMemoryUnmapFunction)     (GstMemory *mem);
181
182 /**
183  * GstMemoryFreeFunction:
184  * @mem: a #GstMemory
185  *
186  * Free the memory used by @mem. This function is usually called when the
187  * refcount of the @mem has reached 0.
188  */
189 typedef void        (*GstMemoryFreeFunction)      (GstMemory *mem);
190
191 /**
192  * GstMemoryCopyFunction:
193  * @mem: a #GstMemory
194  * @offset: an offset
195  * @size: a size or -1
196  *
197  * Copy @size bytes from @mem starting at @offset and return them wrapped in a
198  * new GstMemory object.
199  * If @size is set to -1, all bytes starting at @offset are copied.
200  *
201  * Returns: a new #GstMemory object wrapping a copy of the requested region in
202  * @mem.
203  */
204 typedef GstMemory * (*GstMemoryCopyFunction)      (GstMemory *mem, gssize offset, gssize size);
205
206 /**
207  * GstMemoryShareFunction:
208  * @mem: a #GstMemory
209  * @offset: an offset
210  * @size: a size or -1
211  *
212  * Share @size bytes from @mem starting at @offset and return them wrapped in a
213  * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
214  * shared. This function does not make a copy of the bytes in @mem.
215  *
216  * Returns: a new #GstMemory object sharing the requested region in @mem.
217  */
218 typedef GstMemory * (*GstMemoryShareFunction)     (GstMemory *mem, gssize offset, gssize size);
219
220 /**
221  * GstMemoryIsSpanFunction:
222  * @mem1: a #GstMemory
223  * @mem2: a #GstMemory
224  * @offset: a result offset
225  *
226  * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
227  * @mem1 in the parent buffer in @offset.
228  *
229  * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
230  */
231 typedef gboolean    (*GstMemoryIsSpanFunction)    (GstMemory *mem1, GstMemory *mem2, gsize *offset);
232
233 /**
234  * GstMemoryInfo:
235  * @type: the memory type this allocator provides
236  * @alloc: the implementation of the GstAllocatorAllocFunction
237  * @mem_map: the implementation of the GstMemoryMapFunction
238  * @mem_unmap: the implementation of the GstMemoryUnmapFunction
239  * @mem_free: the implementation of the GstMemoryFreeFunction
240  * @mem_copy: the implementation of the GstMemoryCopyFunction
241  * @mem_share: the implementation of the GstMemoryShareFunction
242  * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
243  *
244  * The #GstMemoryInfo is used to register new memory allocators and contain
245  * the implementations for various memory operations.
246  */
247 struct _GstMemoryInfo {
248   const gchar              *mem_type;
249
250   GstAllocatorAllocFunction alloc;
251
252   GstMemoryMapFunction      mem_map;
253   GstMemoryUnmapFunction    mem_unmap;
254   GstMemoryFreeFunction     mem_free;
255
256   GstMemoryCopyFunction     mem_copy;
257   GstMemoryShareFunction    mem_share;
258   GstMemoryIsSpanFunction   mem_is_span;
259
260   /*< private >*/
261   gpointer _gst_reserved[GST_PADDING];
262 };
263
264 /* allocators */
265 GstAllocator * gst_allocator_new             (const GstMemoryInfo * info,
266                                               gpointer user_data, GDestroyNotify notify);
267 const gchar *  gst_allocator_get_memory_type (GstAllocator * allocator);
268
269 GstAllocator * gst_allocator_ref             (GstAllocator * allocator);
270 void           gst_allocator_unref           (GstAllocator * allocator);
271
272 void           gst_allocator_register        (const gchar *name, GstAllocator *alloc);
273 GstAllocator * gst_allocator_find            (const gchar *name);
274
275 void           gst_allocator_set_default     (GstAllocator * allocator);
276
277 /* allocating memory blocks */
278 GstMemory *    gst_allocator_alloc           (GstAllocator * allocator,
279                                               gsize maxsize, gsize align);
280
281 GstMemory *    gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, GFreeFunc free_func,
282                                        gsize maxsize, gsize offset, gsize size);
283
284 /* refcounting */
285 GstMemory *    gst_memory_ref         (GstMemory *mem);
286 void           gst_memory_unref       (GstMemory *mem);
287
288 /* getting/setting memory properties */
289 gsize          gst_memory_get_sizes   (GstMemory *mem, gsize *offset, gsize *maxsize);
290 void           gst_memory_resize      (GstMemory *mem, gssize offset, gsize size);
291
292 /* retrieving data */
293 gboolean       gst_memory_is_writable (GstMemory *mem);
294
295 GstMemory *    gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
296 gboolean       gst_memory_map         (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
297 void           gst_memory_unmap       (GstMemory *mem, GstMapInfo *info);
298
299 /* copy and subregions */
300 GstMemory *    gst_memory_copy        (GstMemory *mem, gssize offset, gssize size);
301 GstMemory *    gst_memory_share       (GstMemory *mem, gssize offset, gssize size);
302
303 /* span memory */
304 gboolean       gst_memory_is_span     (GstMemory *mem1, GstMemory *mem2, gsize *offset);
305
306 G_END_DECLS
307
308 #endif /* __GST_MEMORY_H__ */