memory: more work on refcount and writability
[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 #define GST_MEMORY_CAST(mem)   ((GstMemory *)(mem))
45
46 /**
47  * GstMemoryFlags:
48  * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
49  * memory with #GST_MAP_WRITE.
50  * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
51  * made when this memory needs to be shared between buffers.
52  * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
53  *
54  * Flags for wrapped memory.
55  */
56 typedef enum {
57   GST_MEMORY_FLAG_READONLY = (1 << 0),
58   GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
59
60   GST_MEMORY_FLAG_LAST     = (1 << 16)
61 } GstMemoryFlags;
62
63 /**
64  * GST_MEMORY_FLAGS:
65  * @mem: a #GstMemory.
66  *
67  * A flags word containing #GstMemoryFlags flags set on @mem
68  */
69 #define GST_MEMORY_FLAGS(mem)  (GST_MEMORY_CAST (mem)->flags)
70 /**
71  * GST_MEMORY_FLAG_IS_SET:
72  * @mem: a #GstMemory.
73  * @flag: the #GstMemoryFlags to check.
74  *
75  * Gives the status of a specific flag on a @mem.
76  */
77 #define GST_MEMORY_FLAG_IS_SET(mem,flag)   !!(GST_MEMORY_FLAGS (mem) & (flag))
78
79 /**
80  * GST_MEMORY_IS_READONLY:
81  * @mem: a #GstMemory.
82  *
83  * Check if @mem is readonly.
84  */
85 #define GST_MEMORY_IS_READONLY(mem)        GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
86
87 /**
88  * GstMemory:
89  * @allocator: pointer to the #GstAllocator
90  * @flags: memory flags
91  * @refcount: refcount
92  * @parent: parent memory block
93  * @state: private state
94  * @maxsize: the maximum size allocated
95  * @align: the alignment of the memory
96  * @offset: the offset where valid data starts
97  * @size: the size of valid data
98  *
99  * Base structure for memory implementations. Custom memory will put this structure
100  * as the first member of their structure.
101  */
102 struct _GstMemory {
103   GstAllocator   *allocator;
104
105   GstMemoryFlags  flags;
106   gint            refcount;
107   GstMemory      *parent;
108   volatile gint   state;
109   gsize           maxsize;
110   gsize           align;
111   gsize           offset;
112   gsize           size;
113 };
114
115 /**
116  * GstMapFlags:
117  * @GST_MAP_READ: map for read access
118  * @GST_MAP_WRITE: map for write access
119  * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
120  *
121  * Flags used when mapping memory
122  */
123 typedef enum {
124   GST_MAP_READ      = (1 << 0),
125   GST_MAP_WRITE     = (1 << 1),
126
127   GST_MAP_FLAG_LAST = (1 << 16)
128 } GstMapFlags;
129
130 /**
131  * GstMapInfo:
132  * @memory: a pointer to the mapped memory
133  * @flags: flags used when mapping the memory
134  * @data: a pointer to the mapped data
135  * @size: the valid size in @data
136  * @maxsize: the maximum bytes in @data
137  *
138  * A structure containing the result of a map operation such as
139  * gst_memory_map(). It contains the data and size.
140  */
141 typedef struct {
142   GstMemory *memory;
143   GstMapFlags flags;
144   guint8 *data;
145   gsize size;
146   gsize maxsize;
147 } GstMapInfo;
148
149 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0 }
150
151 /**
152  * GST_MAP_READWRITE:
153  *
154  * Map for readwrite access
155  */
156 #define GST_MAP_READWRITE      (GST_MAP_READ | GST_MAP_WRITE)
157
158 /**
159  * GST_ALLOCATOR_SYSMEM:
160  *
161  * The allocator name for the default system memory allocator
162  */
163 #define GST_ALLOCATOR_SYSMEM   "SystemMemory"
164
165 /**
166  * GstAllocatorAllocFunction:
167  * @allocator: a #GstAllocator
168  * @maxsize: the maxsize
169  * @align: the alignment
170  * @user_data: user data
171  *
172  * Allocate a new #GstMemory from @allocator that can hold at least @maxsize bytes
173  * and is aligned to (@align + 1) bytes.
174  *
175  * @user_data is the data that was used when creating @allocator.
176  *
177  * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
178  */
179 typedef GstMemory *  (*GstAllocatorAllocFunction)  (GstAllocator *allocator,
180                                                     gsize maxsize, gsize align,
181                                                     gpointer user_data);
182
183 /**
184  * GstMemoryMapFunction:
185  * @mem: a #GstMemory
186  * @maxsize: size to map
187  * @flags: access mode for the memory
188  *
189  * Get the memory of @mem that can be accessed according to the mode specified
190  * in @flags. The function should return a pointer that contains at least
191  * @maxsize bytes.
192  *
193  * Returns: a pointer to memory of which at least @maxsize bytes can be
194  * accessed according to the access pattern in @flags.
195  */
196 typedef gpointer    (*GstMemoryMapFunction)       (GstMemory *mem, gsize maxsize, GstMapFlags flags);
197
198 /**
199  * GstMemoryUnmapFunction:
200  * @mem: a #GstMemory
201  *
202  * Return the pointer previously retrieved with gst_memory_map().
203  *
204  * Returns: %TRUE on success.
205  */
206 typedef void        (*GstMemoryUnmapFunction)     (GstMemory *mem);
207
208 /**
209  * GstMemoryFreeFunction:
210  * @mem: a #GstMemory
211  *
212  * Free the memory used by @mem. This function is usually called when the
213  * refcount of the @mem has reached 0.
214  */
215 typedef void        (*GstMemoryFreeFunction)      (GstMemory *mem);
216
217 /**
218  * GstMemoryCopyFunction:
219  * @mem: a #GstMemory
220  * @offset: an offset
221  * @size: a size or -1
222  *
223  * Copy @size bytes from @mem starting at @offset and return them wrapped in a
224  * new GstMemory object.
225  * If @size is set to -1, all bytes starting at @offset are copied.
226  *
227  * Returns: a new #GstMemory object wrapping a copy of the requested region in
228  * @mem.
229  */
230 typedef GstMemory * (*GstMemoryCopyFunction)      (GstMemory *mem, gssize offset, gssize size);
231
232 /**
233  * GstMemoryShareFunction:
234  * @mem: a #GstMemory
235  * @offset: an offset
236  * @size: a size or -1
237  *
238  * Share @size bytes from @mem starting at @offset and return them wrapped in a
239  * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
240  * shared. This function does not make a copy of the bytes in @mem.
241  *
242  * Returns: a new #GstMemory object sharing the requested region in @mem.
243  */
244 typedef GstMemory * (*GstMemoryShareFunction)     (GstMemory *mem, gssize offset, gssize size);
245
246 /**
247  * GstMemoryIsSpanFunction:
248  * @mem1: a #GstMemory
249  * @mem2: a #GstMemory
250  * @offset: a result offset
251  *
252  * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
253  * @mem1 in the parent buffer in @offset.
254  *
255  * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
256  */
257 typedef gboolean    (*GstMemoryIsSpanFunction)    (GstMemory *mem1, GstMemory *mem2, gsize *offset);
258
259 /**
260  * GstMemoryInfo:
261  * @type: the memory type this allocator provides
262  * @alloc: the implementation of the GstAllocatorAllocFunction
263  * @mem_map: the implementation of the GstMemoryMapFunction
264  * @mem_unmap: the implementation of the GstMemoryUnmapFunction
265  * @mem_free: the implementation of the GstMemoryFreeFunction
266  * @mem_copy: the implementation of the GstMemoryCopyFunction
267  * @mem_share: the implementation of the GstMemoryShareFunction
268  * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
269  *
270  * The #GstMemoryInfo is used to register new memory allocators and contain
271  * the implementations for various memory operations.
272  */
273 struct _GstMemoryInfo {
274   const gchar              *mem_type;
275
276   GstAllocatorAllocFunction alloc;
277
278   GstMemoryMapFunction      mem_map;
279   GstMemoryUnmapFunction    mem_unmap;
280   GstMemoryFreeFunction     mem_free;
281
282   GstMemoryCopyFunction     mem_copy;
283   GstMemoryShareFunction    mem_share;
284   GstMemoryIsSpanFunction   mem_is_span;
285
286   /*< private >*/
287   gpointer _gst_reserved[GST_PADDING];
288 };
289
290 /* allocators */
291 GstAllocator * gst_allocator_new             (const GstMemoryInfo * info,
292                                               gpointer user_data, GDestroyNotify notify);
293 const gchar *  gst_allocator_get_memory_type (GstAllocator * allocator);
294
295 GstAllocator * gst_allocator_ref             (GstAllocator * allocator);
296 void           gst_allocator_unref           (GstAllocator * allocator);
297
298 void           gst_allocator_register        (const gchar *name, GstAllocator *alloc);
299 GstAllocator * gst_allocator_find            (const gchar *name);
300
301 void           gst_allocator_set_default     (GstAllocator * allocator);
302
303 /* allocating memory blocks */
304 GstMemory *    gst_allocator_alloc           (GstAllocator * allocator,
305                                               gsize maxsize, gsize align);
306
307 GstMemory *    gst_memory_new_wrapped  (GstMemoryFlags flags, gpointer data, GFreeFunc free_func,
308                                         gsize maxsize, gsize offset, gsize size);
309
310 /* refcounting */
311 GstMemory *    gst_memory_ref          (GstMemory *mem);
312 void           gst_memory_unref        (GstMemory *mem);
313
314 gboolean       gst_memory_is_exclusive (GstMemory *mem);
315
316 /* getting/setting memory properties */
317 gsize          gst_memory_get_sizes    (GstMemory *mem, gsize *offset, gsize *maxsize);
318 void           gst_memory_resize       (GstMemory *mem, gssize offset, gsize size);
319
320 /* retrieving data */
321 GstMemory *    gst_memory_make_mapped  (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
322 gboolean       gst_memory_map          (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
323 void           gst_memory_unmap        (GstMemory *mem, GstMapInfo *info);
324
325 /* copy and subregions */
326 GstMemory *    gst_memory_copy         (GstMemory *mem, gssize offset, gssize size);
327 GstMemory *    gst_memory_share        (GstMemory *mem, gssize offset, gssize size);
328
329 /* span memory */
330 gboolean       gst_memory_is_span      (GstMemory *mem1, GstMemory *mem2, gsize *offset);
331
332 G_END_DECLS
333
334 #endif /* __GST_MEMORY_H__ */