gstfunnel: avoid access of freed pad
[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      = (1 << 0),
70   GST_MEMORY_FLAG_NO_SHARE      = (1 << 1),
71   GST_MEMORY_FLAG_ZERO_PREFIXED = (1 << 2),
72   GST_MEMORY_FLAG_ZERO_PADDED   = (1 << 3),
73
74   GST_MEMORY_FLAG_LAST          = (1 << 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_MEMORY_CAST (mem)->flags)
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_MEMORY_FLAGS (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_MEMORY_FLAGS (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_ZERO_PREFIXED:
110  * @mem: a #GstMemory.
111  *
112  * Check if the prefix in @mem is 0 filled.
113  */
114 #define GST_MEMORY_IS_ZERO_PREFIXED(mem)   GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
115 /**
116  * GST_MEMORY_IS_ZERO_PADDED:
117  * @mem: a #GstMemory.
118  *
119  * Check if the padding in @mem is 0 filled.
120  */
121 #define GST_MEMORY_IS_ZERO_PADDED(mem)     GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
122
123
124 /**
125  * GstMemory:
126  * @allocator: pointer to the #GstAllocator
127  * @flags: memory flags
128  * @refcount: refcount
129  * @parent: parent memory block
130  * @state: private state
131  * @maxsize: the maximum size allocated
132  * @align: the alignment of the memory
133  * @offset: the offset where valid data starts
134  * @size: the size of valid data
135  *
136  * Base structure for memory implementations. Custom memory will put this structure
137  * as the first member of their structure.
138  */
139 struct _GstMemory {
140   GstAllocator   *allocator;
141
142   GstMemoryFlags  flags;
143   gint            refcount;
144   GstMemory      *parent;
145   volatile gint   state;
146   gsize           maxsize;
147   gsize           align;
148   gsize           offset;
149   gsize           size;
150 };
151
152 /**
153  * GstMapFlags:
154  * @GST_MAP_READ: map for read access
155  * @GST_MAP_WRITE: map for write access
156  * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
157  *
158  * Flags used when mapping memory
159  */
160 typedef enum {
161   GST_MAP_READ      = (1 << 0),
162   GST_MAP_WRITE     = (1 << 1),
163
164   GST_MAP_FLAG_LAST = (1 << 16)
165 } GstMapFlags;
166
167 /**
168  * GstMapInfo:
169  * @memory: a pointer to the mapped memory
170  * @flags: flags used when mapping the memory
171  * @data: a pointer to the mapped data
172  * @size: the valid size in @data
173  * @maxsize: the maximum bytes in @data
174  * @user_data: extra private user_data that the implementation of the memory
175  *             can use to store extra info.
176  *
177  * A structure containing the result of a map operation such as
178  * gst_memory_map(). It contains the data and size.
179  */
180 typedef struct {
181   GstMemory *memory;
182   GstMapFlags flags;
183   guint8 *data;
184   gsize size;
185   gsize maxsize;
186   /*< private >*/
187   gpointer user_data[4];
188 } GstMapInfo;
189
190 /**
191  * GST_MAP_INFO_INIT:
192  *
193  * Initializer for #GstMapInfo
194  */
195 #define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, }
196
197 /**
198  * GST_MAP_READWRITE:
199  *
200  * Map for readwrite access
201  */
202 #define GST_MAP_READWRITE      (GST_MAP_READ | GST_MAP_WRITE)
203
204 /**
205  * GST_ALLOCATOR_SYSMEM:
206  *
207  * The allocator name for the default system memory allocator
208  */
209 #define GST_ALLOCATOR_SYSMEM   "SystemMemory"
210
211 /**
212  * GstAllocationParams:
213  * @flags: flags to control allocation
214  * @align: the desired alignment of the memory
215  * @prefix: the disired prefix
216  * @padding: the desired padding
217  *
218  * Parameters to control the allocation of memory
219  */
220 struct _GstAllocationParams {
221   GstMemoryFlags flags;
222   gsize          align;
223   gsize          prefix;
224   gsize          padding;
225
226   /*< private >*/
227   gpointer _gst_reserved[GST_PADDING];
228 };
229
230 /**
231  * GstAllocatorAllocFunction:
232  * @allocator: a #GstAllocator
233  * @size: the size
234  * @params: allocator params
235  * @user_data: user data
236  *
237  * Allocate a new #GstMemory from @allocator that can hold at least @size
238  * bytes (+ padding) and is aligned to (@align + 1) bytes.
239  *
240  * The offset and size of the memory should be set and the prefix/padding must
241  * be filled with 0 if @params flags contains #GST_MEMORY_FLAG_ZERO_PREFIXED and
242  * #GST_MEMORY_FLAG_ZERO_PADDED respectively.
243  *
244  * @user_data is the data that was used when creating @allocator.
245  *
246  * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
247  */
248 typedef GstMemory *  (*GstAllocatorAllocFunction)  (GstAllocator *allocator,
249                                                     gsize size, GstAllocationParams *params,
250                                                     gpointer user_data);
251
252 /**
253  * GstMemoryMapFunction:
254  * @mem: a #GstMemory
255  * @maxsize: size to map
256  * @flags: access mode for the memory
257  *
258  * Get the memory of @mem that can be accessed according to the mode specified
259  * in @flags. The function should return a pointer that contains at least
260  * @maxsize bytes.
261  *
262  * Returns: a pointer to memory of which at least @maxsize bytes can be
263  * accessed according to the access pattern in @flags.
264  */
265 typedef gpointer    (*GstMemoryMapFunction)       (GstMemory *mem, gsize maxsize, GstMapFlags flags);
266
267 /**
268  * GstMemoryUnmapFunction:
269  * @mem: a #GstMemory
270  *
271  * Return the pointer previously retrieved with gst_memory_map().
272  *
273  * Returns: %TRUE on success.
274  */
275 typedef void        (*GstMemoryUnmapFunction)     (GstMemory *mem);
276
277 /**
278  * GstMemoryFreeFunction:
279  * @mem: a #GstMemory
280  *
281  * Free the memory used by @mem. This function is usually called when the
282  * refcount of the @mem has reached 0.
283  */
284 typedef void        (*GstMemoryFreeFunction)      (GstMemory *mem);
285
286 /**
287  * GstMemoryCopyFunction:
288  * @mem: a #GstMemory
289  * @offset: an offset
290  * @size: a size or -1
291  *
292  * Copy @size bytes from @mem starting at @offset and return them wrapped in a
293  * new GstMemory object.
294  * If @size is set to -1, all bytes starting at @offset are copied.
295  *
296  * Returns: a new #GstMemory object wrapping a copy of the requested region in
297  * @mem.
298  */
299 typedef GstMemory * (*GstMemoryCopyFunction)      (GstMemory *mem, gssize offset, gssize size);
300
301 /**
302  * GstMemoryShareFunction:
303  * @mem: a #GstMemory
304  * @offset: an offset
305  * @size: a size or -1
306  *
307  * Share @size bytes from @mem starting at @offset and return them wrapped in a
308  * new GstMemory object. If @size is set to -1, all bytes starting at @offset are
309  * shared. This function does not make a copy of the bytes in @mem.
310  *
311  * Returns: a new #GstMemory object sharing the requested region in @mem.
312  */
313 typedef GstMemory * (*GstMemoryShareFunction)     (GstMemory *mem, gssize offset, gssize size);
314
315 /**
316  * GstMemoryIsSpanFunction:
317  * @mem1: a #GstMemory
318  * @mem2: a #GstMemory
319  * @offset: a result offset
320  *
321  * Check if @mem1 and @mem2 occupy contiguous memory and return the offset of
322  * @mem1 in the parent buffer in @offset.
323  *
324  * Returns: %TRUE if @mem1 and @mem2 are in contiguous memory.
325  */
326 typedef gboolean    (*GstMemoryIsSpanFunction)    (GstMemory *mem1, GstMemory *mem2, gsize *offset);
327
328 /**
329  * GstMemoryInfo:
330  * @mem_type: the memory type this allocator provides
331  * @alloc: the implementation of the GstAllocatorAllocFunction
332  * @mem_map: the implementation of the GstMemoryMapFunction
333  * @mem_unmap: the implementation of the GstMemoryUnmapFunction
334  * @mem_free: the implementation of the GstMemoryFreeFunction
335  * @mem_copy: the implementation of the GstMemoryCopyFunction
336  * @mem_share: the implementation of the GstMemoryShareFunction
337  * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
338  *
339  * The #GstMemoryInfo is used to register new memory allocators and contain
340  * the implementations for various memory operations.
341  */
342 struct _GstMemoryInfo {
343   const gchar              *mem_type;
344
345   GstAllocatorAllocFunction alloc;
346
347   GstMemoryMapFunction      mem_map;
348   GstMemoryUnmapFunction    mem_unmap;
349   GstMemoryFreeFunction     mem_free;
350
351   GstMemoryCopyFunction     mem_copy;
352   GstMemoryShareFunction    mem_share;
353   GstMemoryIsSpanFunction   mem_is_span;
354
355   /*< private >*/
356   gpointer _gst_reserved[GST_PADDING];
357 };
358
359 /**
360  * GstAllocator:
361  *
362  * An opaque type returned from gst_allocator_new() or gst_allocator_find()
363  * that can be used to allocator memory.
364  */
365
366 /* allocators */
367 GstAllocator * gst_allocator_new             (const GstMemoryInfo * info,
368                                               gpointer user_data, GDestroyNotify notify);
369 const gchar *  gst_allocator_get_memory_type (GstAllocator * allocator);
370
371 GstAllocator * gst_allocator_ref             (GstAllocator * allocator);
372 void           gst_allocator_unref           (GstAllocator * allocator);
373
374 void           gst_allocator_register        (const gchar *name, GstAllocator *allocator);
375 GstAllocator * gst_allocator_find            (const gchar *name);
376
377 void           gst_allocator_set_default     (GstAllocator * allocator);
378
379 /* allocating memory blocks */
380 void           gst_allocation_params_init     (GstAllocationParams *params);
381 GstAllocationParams *
382                gst_allocation_params_copy     (const GstAllocationParams *params) G_GNUC_MALLOC;
383 void           gst_allocation_params_free     (GstAllocationParams *params);
384
385 GstMemory *    gst_allocator_alloc           (GstAllocator * allocator, gsize size,
386                                               GstAllocationParams *params);
387
388 GstMemory *    gst_memory_new_wrapped  (GstMemoryFlags flags, gpointer data, gsize maxsize,
389                                         gsize offset, gsize size, gpointer user_data,
390                                         GDestroyNotify notify);
391
392 /* refcounting */
393 GstMemory *    gst_memory_ref          (GstMemory *mem);
394 void           gst_memory_unref        (GstMemory *mem);
395
396 gboolean       gst_memory_is_exclusive (GstMemory *mem);
397
398 /* getting/setting memory properties */
399 gsize          gst_memory_get_sizes    (GstMemory *mem, gsize *offset, gsize *maxsize);
400 void           gst_memory_resize       (GstMemory *mem, gssize offset, gsize size);
401
402 /* retrieving data */
403 GstMemory *    gst_memory_make_mapped  (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
404 gboolean       gst_memory_map          (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
405 void           gst_memory_unmap        (GstMemory *mem, GstMapInfo *info);
406
407 /* copy and subregions */
408 GstMemory *    gst_memory_copy         (GstMemory *mem, gssize offset, gssize size);
409 GstMemory *    gst_memory_share        (GstMemory *mem, gssize offset, gssize size);
410
411 /* span memory */
412 gboolean       gst_memory_is_span      (GstMemory *mem1, GstMemory *mem2, gsize *offset);
413
414 G_END_DECLS
415
416 #endif /* __GST_MEMORY_H__ */