fe093e377873572fbe5ca74a8ed7115884292b1f
[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 #define GST_TYPE_ALLOCATION_PARAMS (gst_allocation_params_get_type())
39 GType gst_allocation_params_get_type(void);
40
41 typedef struct _GstMemory GstMemory;
42 typedef struct _GstMemoryInfo GstMemoryInfo;
43 typedef struct _GstAllocator GstAllocator;
44 typedef struct _GstAllocationParams GstAllocationParams;
45
46 /**
47  * gst_memory_alignment:
48  *
49  * The default memory alignment in bytes - 1
50  * an alignment of 7 would be the same as what malloc() guarantees.
51  */
52 GST_EXPORT gsize gst_memory_alignment;
53
54 #define GST_MEMORY_CAST(mem)   ((GstMemory *)(mem))
55
56 /**
57  * GstMemoryFlags:
58  * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
59  * memory with #GST_MAP_WRITE.
60  * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
61  * made when this memory needs to be shared between buffers.
62  * @GST_MEMORY_FLAG_ZERO_PREFIXED: the memory prefix is filled with 0 bytes
63  * @GST_MEMORY_FLAG_ZERO_PADDED: the memory padding is filled with 0 bytes
64  * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
65  *
66  * Flags for wrapped memory.
67  */
68 typedef enum {
69   GST_MEMORY_FLAG_READONLY      = GST_MINI_OBJECT_FLAG_LOCK_READONLY,
70   GST_MEMORY_FLAG_NO_SHARE      = (GST_MINI_OBJECT_FLAG_LAST << 0),
71   GST_MEMORY_FLAG_ZERO_PREFIXED = (GST_MINI_OBJECT_FLAG_LAST << 1),
72   GST_MEMORY_FLAG_ZERO_PADDED   = (GST_MINI_OBJECT_FLAG_LAST << 2),
73
74   GST_MEMORY_FLAG_LAST          = (GST_MINI_OBJECT_FLAG_LAST << 16)
75 } GstMemoryFlags;
76
77 /**
78  * GST_MEMORY_FLAGS:
79  * @mem: a #GstMemory.
80  *
81  * A flags word containing #GstMemoryFlags flags set on @mem
82  */
83 #define GST_MEMORY_FLAGS(mem)  GST_MINI_OBJECT_FLAGS (mem)
84 /**
85  * GST_MEMORY_FLAG_IS_SET:
86  * @mem: a #GstMemory.
87  * @flag: the #GstMemoryFlags to check.
88  *
89  * Gives the status of a specific flag on a @mem.
90  */
91 #define GST_MEMORY_FLAG_IS_SET(mem,flag)   GST_MINI_OBJECT_FLAG_IS_SET (mem,flag)
92 /**
93  * GST_MEMORY_FLAG_UNSET:
94  * @mem: a #GstMemory.
95  * @flag: the #GstMemoryFlags to clear.
96  *
97  * Clear a specific flag on a @mem.
98  */
99 #define GST_MEMORY_FLAG_UNSET(mem,flag)   GST_MINI_OBJECT_FLAG_UNSET (mem, flag)
100
101 /**
102  * GST_MEMORY_IS_READONLY:
103  * @mem: a #GstMemory.
104  *
105  * Check if @mem is readonly.
106  */
107 #define GST_MEMORY_IS_READONLY(mem)        GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
108 /**
109  * GST_MEMORY_IS_NO_SHARE:
110  * @mem: a #GstMemory.
111  *
112  * Check if @mem cannot be shared between buffers
113  */
114 #define GST_MEMORY_IS_NO_SHARE(mem)        GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NO_SHARE)
115 /**
116  * GST_MEMORY_IS_ZERO_PREFIXED:
117  * @mem: a #GstMemory.
118  *
119  * Check if the prefix in @mem is 0 filled.
120  */
121 #define GST_MEMORY_IS_ZERO_PREFIXED(mem)   GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
122 /**
123  * GST_MEMORY_IS_ZERO_PADDED:
124  * @mem: a #GstMemory.
125  *
126  * Check if the padding in @mem is 0 filled.
127  */
128 #define GST_MEMORY_IS_ZERO_PADDED(mem)     GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
129
130
131 /**
132  * GstMemory:
133  * @mini_object: parent structure
134  * @allocator: pointer to the #GstAllocator
135  * @parent: parent memory block
136  * @maxsize: the maximum size allocated
137  * @align: the alignment of the memory
138  * @offset: the offset where valid data starts
139  * @size: the size of valid data
140  *
141  * Base structure for memory implementations. Custom memory will put this structure
142  * as the first member of their structure.
143  */
144 struct _GstMemory {
145   GstMiniObject   mini_object;
146
147   GstAllocator   *allocator;
148
149   GstMemory      *parent;
150   gsize           maxsize;
151   gsize           align;
152   gsize           offset;
153   gsize           size;
154 };
155
156 /**
157  * GstMapFlags:
158  * @GST_MAP_READ: map for read access
159  * @GST_MAP_WRITE: map for write access
160  * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
161  *
162  * Flags used when mapping memory
163  */
164 typedef enum {
165   GST_MAP_READ      = GST_LOCK_FLAG_READ,
166   GST_MAP_WRITE     = GST_LOCK_FLAG_WRITE,
167
168   GST_MAP_FLAG_LAST = (1 << 16)
169 } GstMapFlags;
170 /**
171  * GST_MAP_READWRITE:
172  *
173  * GstMapFlags value alias for GST_MAP_READ | GST_MAP_WRITE
174  */
175 #define GST_MAP_READWRITE      (GST_MAP_READ | GST_MAP_WRITE)
176
177
178 /**
179  * GstMapInfo:
180  * @memory: a pointer to the mapped memory
181  * @flags: flags used when mapping the memory
182  * @data: (array length=size): a pointer to the mapped data
183  * @size: the valid size in @data
184  * @maxsize: the maximum bytes in @data
185  * @user_data: extra private user_data that the implementation of the memory
186  *             can use to store extra info.
187  *
188  * A structure containing the result of a map operation such as
189  * gst_memory_map(). It contains the data and size.
190  */
191 typedef struct {
192   GstMemory *memory;
193   GstMapFlags flags;
194   guint8 *data;
195   gsize size;
196   gsize maxsize;
197   /*< private >*/
198   gpointer user_data[4];
199 } GstMapInfo;
200
201 /**
202  * GST_MAP_INFO_INIT:
203  *
204  * Initializer for #GstMapInfo
205  */
206 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, }
207
208 /**
209  * GST_ALLOCATOR_SYSMEM:
210  *
211  * The allocator name for the default system memory allocator
212  */
213 #define GST_ALLOCATOR_SYSMEM   "SystemMemory"
214
215 /**
216  * GstAllocationParams:
217  * @flags: flags to control allocation
218  * @align: the desired alignment of the memory
219  * @prefix: the desired prefix
220  * @padding: the desired padding
221  *
222  * Parameters to control the allocation of memory
223  */
224 struct _GstAllocationParams {
225   GstMemoryFlags flags;
226   gsize          align;
227   gsize          prefix;
228   gsize          padding;
229
230   /*< private >*/
231   gpointer _gst_reserved[GST_PADDING];
232 };
233
234 /**
235  * GstAllocatorAllocFunction:
236  * @allocator: a #GstAllocator
237  * @size: the size
238  * @params: allocator params
239  * @user_data: user data
240  *
241  * Allocate a new #GstMemory from @allocator that can hold at least @size
242  * bytes (+ padding) and is aligned to (@align + 1) bytes.
243  *
244  * The offset and size of the memory should be set and the prefix/padding must
245  * be filled with 0 if @params flags contains #GST_MEMORY_FLAG_ZERO_PREFIXED and
246  * #GST_MEMORY_FLAG_ZERO_PADDED respectively.
247  *
248  * @user_data is extra data passed to this function. The default
249  * gst_allocator_alloc() passes the NULL but other implementations could pass
250  * custom data.
251  *
252  * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
253  */
254 typedef GstMemory *  (*GstAllocatorAllocFunction)  (GstAllocator *allocator,
255                                                     gsize size, GstAllocationParams *params,
256                                                     gpointer user_data);
257
258 /**
259  * GstMemoryMapFunction:
260  * @mem: a #GstMemory
261  * @maxsize: size to map
262  * @flags: access mode for the memory
263  *
264  * Get the memory of @mem that can be accessed according to the mode specified
265  * in @flags. The function should return a pointer that contains at least
266  * @maxsize bytes.
267  *
268  * Returns: a pointer to memory of which at least @maxsize bytes can be
269  * accessed according to the access pattern in @flags.
270  */
271 typedef gpointer    (*GstMemoryMapFunction)       (GstMemory *mem, gsize maxsize, GstMapFlags flags);
272
273 /**
274  * GstMemoryUnmapFunction:
275  * @mem: a #GstMemory
276  *
277  * Return the pointer previously retrieved with gst_memory_map().
278  *
279  * Returns: %TRUE on success.
280  */
281 typedef void        (*GstMemoryUnmapFunction)     (GstMemory *mem);
282
283 /**
284  * GstMemoryFreeFunction:
285  * @mem: a #GstMemory
286  *
287  * Free the memory used by @mem. This function is usually called when the
288  * refcount of the @mem has reached 0.
289  */
290 typedef void        (*GstMemoryFreeFunction)      (GstMemory *mem);
291
292 /**
293  * GstMemoryCopyFunction:
294  * @mem: a #GstMemory
295  * @offset: an offset
296  * @size: a size or -1
297  *
298  * Copy @size bytes from @mem starting at @offset and return them wrapped in a
299  * new GstMemory object.
300  * If @size is set to -1, all bytes starting at @offset are copied.
301  *
302  * Returns: a new #GstMemory object wrapping a copy of the requested region in
303  * @mem.
304  */
305 typedef GstMemory * (*GstMemoryCopyFunction)      (GstMemory *mem, gssize offset, gssize size);
306
307 /**
308  * GstMemoryShareFunction:
309  * @mem: a #GstMemory
310  * @offset: an offset
311  * @size: a size or -1
312  *
313  * Share @size bytes from @mem starting at @offset and return them wrapped in a
314  * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
315  * shared. This function does not make a copy of the bytes in @mem.
316  *
317  * Returns: a new #GstMemory object sharing the requested region in @mem.
318  */
319 typedef GstMemory * (*GstMemoryShareFunction)     (GstMemory *mem, gssize offset, gssize size);
320
321 /**
322  * GstMemoryIsSpanFunction:
323  * @mem1: a #GstMemory
324  * @mem2: a #GstMemory
325  * @offset: a result offset
326  *
327  * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
328  * @mem1 in the parent buffer in @offset.
329  *
330  * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
331  */
332 typedef gboolean    (*GstMemoryIsSpanFunction)    (GstMemory *mem1, GstMemory *mem2, gsize *offset);
333
334 /**
335  * GstAllocatorFlags:
336  * @GST_ALLOCATOR_CUSTOM_ALLOC: The allocator has a custom alloc function.
337  * @GST_ALLOCATOR_FLAG_LAST: first flag that can be used for custom purposes
338  *
339  * Flags for allocators.
340  */
341 typedef enum {
342   GST_ALLOCATOR_FLAG_CUSTOM_ALLOC  = (GST_MINI_OBJECT_FLAG_LAST << 0),
343
344   GST_ALLOCATOR_FLAG_LAST          = (GST_MINI_OBJECT_FLAG_LAST << 16)
345 } GstAllocatorFlags;
346
347
348 /**
349  * GstMemoryInfo:
350  * @mem_type: the memory type this allocator provides
351  * @alloc: the implementation of the GstAllocatorAllocFunction
352  * @mem_map: the implementation of the GstMemoryMapFunction
353  * @mem_unmap: the implementation of the GstMemoryUnmapFunction
354  * @mem_free: the implementation of the GstMemoryFreeFunction
355  * @mem_copy: the implementation of the GstMemoryCopyFunction
356  * @mem_share: the implementation of the GstMemoryShareFunction
357  * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
358  *
359  * The #GstMemoryInfo is used to initialize new memory allocators and contain
360  * the implementations for various memory operations.
361  */
362 struct _GstMemoryInfo {
363   const gchar              *mem_type;
364
365   GstAllocatorAllocFunction alloc;
366
367   GstMemoryMapFunction      mem_map;
368   GstMemoryUnmapFunction    mem_unmap;
369   GstMemoryFreeFunction     mem_free;
370
371   GstMemoryCopyFunction     mem_copy;
372   GstMemoryShareFunction    mem_share;
373   GstMemoryIsSpanFunction   mem_is_span;
374
375   /*< private >*/
376   gpointer _gst_reserved[GST_PADDING];
377 };
378
379 /**
380  * GstAllocator:
381  * @mini_object: parent structure
382  * @info: a #GstMemoryInfo with the implementation
383  *
384  * The #GstAllocator is used to create new memory and should be
385  * initialized with gst_allocator_init().
386  */
387 struct _GstAllocator
388 {
389   GstMiniObject  mini_object;
390
391   GstMemoryInfo  info;
392
393   /*< private >*/
394   gpointer _gst_reserved[GST_PADDING];
395 };
396
397 /* allocators */
398 void           gst_allocator_init            (GstAllocator * allocator,
399                                               GstAllocatorFlags flags,
400                                               const GstMemoryInfo *info,
401                                               GstMiniObjectFreeFunction free_func);
402
403 /**
404  * gst_allocator_ref:
405  * @allocator: The allocator to refcount
406  *
407  * Increase the refcount of this allocator.
408  *
409  * Returns: (transfer full): @allocator (for convenience when doing assignments)
410  */
411 #ifdef _FOOL_GTK_DOC_
412 G_INLINE_FUNC GstAllocator * gst_allocator_ref (GstAllocator * allocator);
413 #endif
414
415 static inline GstAllocator *
416 gst_allocator_ref (GstAllocator * allocator)
417 {
418   return (GstAllocator *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (allocator));
419 }
420
421 /**
422  * gst_allocator_unref:
423  * @allocator: (transfer full): the allocator to refcount
424  *
425  * Decrease the refcount of an allocator, freeing it if the refcount reaches 0.
426  */
427 #ifdef _FOOL_GTK_DOC_
428 G_INLINE_FUNC void gst_allocator_unref (GstAllocator * allocator);
429 #endif
430
431 static inline void
432 gst_allocator_unref (GstAllocator * allocator)
433 {
434   gst_mini_object_unref (GST_MINI_OBJECT_CAST (allocator));
435 }
436
437 void           gst_allocator_register        (const gchar *name, GstAllocator *allocator);
438 GstAllocator * gst_allocator_find            (const gchar *name);
439
440 void           gst_allocator_set_default     (GstAllocator * allocator);
441
442 /* allocating memory blocks */
443 void           gst_allocation_params_init    (GstAllocationParams *params);
444 GstAllocationParams *
445                gst_allocation_params_copy    (const GstAllocationParams *params) G_GNUC_MALLOC;
446 void           gst_allocation_params_free    (GstAllocationParams *params);
447
448 GstMemory *    gst_allocator_alloc           (GstAllocator * allocator, gsize size,
449                                               GstAllocationParams *params);
450
451 GstMemory *    gst_memory_new_wrapped  (GstMemoryFlags flags, gpointer data, gsize maxsize,
452                                         gsize offset, gsize size, gpointer user_data,
453                                         GDestroyNotify notify);
454
455 void           gst_memory_init         (GstMemory *mem, GstMemoryFlags flags,
456                                         GstAllocator *allocator, GstMemory *parent,
457                                         gsize maxsize, gsize align,
458                                         gsize offset, gsize size);
459
460 /* refcounting */
461 /**
462  * gst_memory_ref:
463  * @memory: The memory to refcount
464  *
465  * Increase the refcount of this memory.
466  *
467  * Returns: (transfer full): @memory (for convenience when doing assignments)
468  */
469 #ifdef _FOOL_GTK_DOC_
470 G_INLINE_FUNC GstMemory * gst_memory_ref (GstMemory * memory);
471 #endif
472
473 static inline GstMemory *
474 gst_memory_ref (GstMemory * memory)
475 {
476   return (GstMemory *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (memory));
477 }
478
479 /**
480  * gst_memory_unref:
481  * @memory: (transfer full): the memory to refcount
482  *
483  * Decrease the refcount of an memory, freeing it if the refcount reaches 0.
484  */
485 #ifdef _FOOL_GTK_DOC_
486 G_INLINE_FUNC void gst_memory_unref (GstMemory * memory);
487 #endif
488
489 static inline void
490 gst_memory_unref (GstMemory * memory)
491 {
492   gst_mini_object_unref (GST_MINI_OBJECT_CAST (memory));
493 }
494
495 /* getting/setting memory properties */
496 gsize          gst_memory_get_sizes    (GstMemory *mem, gsize *offset, gsize *maxsize);
497 void           gst_memory_resize       (GstMemory *mem, gssize offset, gsize size);
498
499 #define        gst_memory_lock(m,f)        gst_mini_object_lock (GST_MINI_OBJECT_CAST (m), (f))
500 #define        gst_memory_unlock(m,f)      gst_mini_object_unlock (GST_MINI_OBJECT_CAST (m), (f))
501 #define        gst_memory_is_writable(m)   gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (m))
502
503 /* retrieving data */
504 GstMemory *    gst_memory_make_mapped  (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
505 gboolean       gst_memory_map          (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
506 void           gst_memory_unmap        (GstMemory *mem, GstMapInfo *info);
507
508 /* copy and subregions */
509 GstMemory *    gst_memory_copy         (GstMemory *mem, gssize offset, gssize size);
510 GstMemory *    gst_memory_share        (GstMemory *mem, gssize offset, gssize size);
511
512 /* span memory */
513 gboolean       gst_memory_is_span      (GstMemory *mem1, GstMemory *mem2, gsize *offset);
514
515 G_END_DECLS
516
517 #endif /* __GST_MEMORY_H__ */