tracer: latency: Show element id, element name and pad name
[platform/upstream/gstreamer.git] / gst / gstmemory.h
index da1bf6b..8244da9 100644 (file)
@@ -15,8 +15,8 @@
  *
  * You should have received a copy of the GNU Library 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.
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
  */
 
 
 #include <gst/gstconfig.h>
 
 #include <glib-object.h>
+#include <gst/gstminiobject.h>
+#include <gst/gstobject.h>
 
 G_BEGIN_DECLS
 
-#define GST_TYPE_MEMORY (gst_memory_get_type())
+GST_API GType _gst_memory_type;
+#define GST_TYPE_MEMORY (_gst_memory_type)
+
+GST_API
 GType gst_memory_get_type(void);
 
 typedef struct _GstMemory GstMemory;
-typedef struct _GstMemoryInfo GstMemoryInfo;
 typedef struct _GstAllocator GstAllocator;
 
-extern gsize gst_memory_alignment;
+#define GST_MEMORY_CAST(mem)   ((GstMemory *)(mem))
 
 /**
  * GstMemoryFlags:
@@ -44,155 +48,225 @@ extern gsize gst_memory_alignment;
  * memory with #GST_MAP_WRITE.
  * @GST_MEMORY_FLAG_NO_SHARE: memory must not be shared. Copies will have to be
  * made when this memory needs to be shared between buffers.
+ * @GST_MEMORY_FLAG_ZERO_PREFIXED: the memory prefix is filled with 0 bytes
+ * @GST_MEMORY_FLAG_ZERO_PADDED: the memory padding is filled with 0 bytes
+ * @GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS: the memory is physically contiguous. (Since 1.2)
+ * @GST_MEMORY_FLAG_NOT_MAPPABLE: the memory can't be mapped via gst_memory_map() without any preconditions. (Since 1.2)
  * @GST_MEMORY_FLAG_LAST: first flag that can be used for custom purposes
  *
  * Flags for wrapped memory.
  */
 typedef enum {
-  GST_MEMORY_FLAG_READONLY = (1 << 0),
-  GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
-
-  GST_MEMORY_FLAG_LAST = (1 << 24)
+  GST_MEMORY_FLAG_READONLY      = GST_MINI_OBJECT_FLAG_LOCK_READONLY,
+  GST_MEMORY_FLAG_NO_SHARE      = (GST_MINI_OBJECT_FLAG_LAST << 0),
+  GST_MEMORY_FLAG_ZERO_PREFIXED = (GST_MINI_OBJECT_FLAG_LAST << 1),
+  GST_MEMORY_FLAG_ZERO_PADDED   = (GST_MINI_OBJECT_FLAG_LAST << 2),
+  GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS = (GST_MINI_OBJECT_FLAG_LAST << 3),
+  GST_MEMORY_FLAG_NOT_MAPPABLE  = (GST_MINI_OBJECT_FLAG_LAST << 4),
+
+  GST_MEMORY_FLAG_LAST          = (GST_MINI_OBJECT_FLAG_LAST << 16)
 } GstMemoryFlags;
 
 /**
- * GST_MEMORY_IS_WRITABLE:
- * @mem: a #GstMemory
+ * GST_MEMORY_FLAGS:
+ * @mem: a #GstMemory.
+ *
+ * A flags word containing #GstMemoryFlags flags set on @mem
+ */
+#define GST_MEMORY_FLAGS(mem)  GST_MINI_OBJECT_FLAGS (mem)
+/**
+ * GST_MEMORY_FLAG_IS_SET:
+ * @mem: a #GstMemory.
+ * @flag: the #GstMemoryFlags to check.
+ *
+ * Gives the status of a specific flag on a @mem.
+ */
+#define GST_MEMORY_FLAG_IS_SET(mem,flag)   GST_MINI_OBJECT_FLAG_IS_SET (mem,flag)
+/**
+ * GST_MEMORY_FLAG_UNSET:
+ * @mem: a #GstMemory.
+ * @flag: the #GstMemoryFlags to clear.
+ *
+ * Clear a specific flag on a @mem.
+ */
+#define GST_MEMORY_FLAG_UNSET(mem,flag)   GST_MINI_OBJECT_FLAG_UNSET (mem, flag)
+
+/**
+ * GST_MEMORY_IS_READONLY:
+ * @mem: a #GstMemory.
+ *
+ * Check if @mem is readonly.
+ */
+#define GST_MEMORY_IS_READONLY(mem)        GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
+/**
+ * GST_MEMORY_IS_NO_SHARE:
+ * @mem: a #GstMemory.
+ *
+ * Check if @mem cannot be shared between buffers
+ */
+#define GST_MEMORY_IS_NO_SHARE(mem)        GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NO_SHARE)
+/**
+ * GST_MEMORY_IS_ZERO_PREFIXED:
+ * @mem: a #GstMemory.
+ *
+ * Check if the prefix in @mem is 0 filled.
+ */
+#define GST_MEMORY_IS_ZERO_PREFIXED(mem)   GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PREFIXED)
+/**
+ * GST_MEMORY_IS_ZERO_PADDED:
+ * @mem: a #GstMemory.
+ *
+ * Check if the padding in @mem is 0 filled.
+ */
+#define GST_MEMORY_IS_ZERO_PADDED(mem)     GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_ZERO_PADDED)
+
+/**
+ * GST_MEMORY_IS_PHYSICALLY_CONTIGUOUS:
+ * @mem: a #GstMemory.
+ *
+ * Check if @mem is physically contiguous.
  *
- * Check if @mem is writable.
+ * Since: 1.2
  */
-#define GST_MEMORY_IS_WRITABLE(mem) (((mem)->refcount == 1) && \
-    (((mem)->parent == NULL) || ((mem)->parent->refcount == 1)) && \
-    (((mem)->flags & GST_MEMORY_FLAG_READONLY) == 0))
+#define GST_MEMORY_IS_PHYSICALLY_CONTIGUOUS(mem)     GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_PHYSICALLY_CONTIGUOUS)
+
+/**
+ * GST_MEMORY_IS_NOT_MAPPABLE:
+ * @mem: a #GstMemory.
+ *
+ * Check if @mem can't be mapped via gst_memory_map() without any preconditions
+ *
+ * Since: 1.2
+ */
+#define GST_MEMORY_IS_NOT_MAPPABLE(mem)     GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NOT_MAPPABLE)
 
 /**
  * GstMemory:
+ * @mini_object: parent structure
  * @allocator: pointer to the #GstAllocator
- * @flags: memory flags
- * @refcount: refcount
  * @parent: parent memory block
+ * @maxsize: the maximum size allocated
+ * @align: the alignment of the memory
+ * @offset: the offset where valid data starts
+ * @size: the size of valid data
  *
  * Base structure for memory implementations. Custom memory will put this structure
  * as the first member of their structure.
  */
 struct _GstMemory {
-  const GstAllocator *allocator;
+  GstMiniObject   mini_object;
+
+  GstAllocator   *allocator;
 
-  GstMemoryFlags  flags;
-  gint            refcount;
   GstMemory      *parent;
+  gsize           maxsize;
+  gsize           align;
+  gsize           offset;
+  gsize           size;
 };
 
 /**
  * GstMapFlags:
  * @GST_MAP_READ: map for read access
  * @GST_MAP_WRITE: map for write access
+ * @GST_MAP_FLAG_LAST: first flag that can be used for custom purposes
  *
  * Flags used when mapping memory
  */
 typedef enum {
-  GST_MAP_READ =  (1 << 0),
-  GST_MAP_WRITE = (1 << 1),
+  GST_MAP_READ      = GST_LOCK_FLAG_READ,
+  GST_MAP_WRITE     = GST_LOCK_FLAG_WRITE,
+
+  GST_MAP_FLAG_LAST = (1 << 16)
 } GstMapFlags;
 
 /**
- * GST_MAP_READWRITE:
+ * GST_MAP_READWRITE: (value 3) (type GstMapFlags)
  *
- * Map for readwrite access
+ * GstMapFlags value alias for GST_MAP_READ | GST_MAP_WRITE
  */
-#define GST_MAP_READWRITE      (GST_MAP_READ | GST_MAP_WRITE)
+#define GST_MAP_READWRITE      ((GstMapFlags) (GST_MAP_READ | GST_MAP_WRITE))
 
-/**
- * GST_ALLOCATOR_SYSMEM:
- *
- * The allocator name for the default system memory allocator
- */
-#define GST_ALLOCATOR_SYSMEM   "SystemMemory"
 
 /**
- * GstMemoryAllocFunction:
- * @allocator: a #GstAllocator
- * @maxsize: the maxsize
- * @align: the alignment
- * @user_data: user data
- *
- * Allocate a new #GstMemory from @allocator that can hold at least @maxsize bytes
- * and is aligned to (@align + 1) bytes.
- *
- * @user_data is the data that was used when registering @allocator.
+ * GstMapInfo:
+ * @memory: a pointer to the mapped memory
+ * @flags: flags used when mapping the memory
+ * @data: (array length=size): a pointer to the mapped data
+ * @size: the valid size in @data
+ * @maxsize: the maximum bytes in @data
+ * @user_data: extra private user_data that the implementation of the memory
+ *             can use to store extra info.
  *
- * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
+ * A structure containing the result of a map operation such as
+ * gst_memory_map(). It contains the data and size.
  */
-typedef GstMemory *  (*GstMemoryAllocFunction)  (const GstAllocator *allocator,
-                                                 gsize maxsize, gsize align,
-                                                 gpointer user_data);
+typedef struct {
+  GstMemory *memory;
+  GstMapFlags flags;
+  guint8 *data;
+  gsize size;
+  gsize maxsize;
+  /*< protected >*/
+  gpointer user_data[4];
+
+  /*< private >*/
+  gpointer _gst_reserved[GST_PADDING];
+} GstMapInfo;
 
 /**
- * GstMemoryGetSizesFunction:
- * @mem: a #GstMemory
- * @offset: result pointer for offset
- * @maxsize: result pointer for maxsize
+ * GST_MAP_INFO_INIT:
  *
- * Retrieve the size, offset and maxsize of @mem.
- *
- * Returns: the size of @mem, the offset and the maximum allocated size in @maxsize.
+ * Initializer for #GstMapInfo
  */
-typedef gsize       (*GstMemoryGetSizesFunction)  (GstMemory *mem, gsize *offset, gsize *maxsize);
+#define GST_MAP_INFO_INIT { NULL, (GstMapFlags) 0, NULL, 0, 0, { NULL, NULL, NULL, NULL}, {NULL, NULL, NULL, NULL}}
 
 /**
- * GstMemoryResizeFunction:
+ * GstMemoryMapFunction:
  * @mem: a #GstMemory
- * @offset: the offset adjustement
- * @size: the new size or -1 to just adjust the offset
+ * @maxsize: size to map
+ * @flags: access mode for the memory
  *
- * Adjust the size and offset of @mem. @offset bytes will be adjusted from the
- * current first byte in @mem as retrieved with gst_memory_map() and the new
- * size will be set to @size.
+ * Get the memory of @mem that can be accessed according to the mode specified
+ * in @flags. The function should return a pointer that contains at least
+ * @maxsize bytes.
  *
- * @size can be set to -1, which will only adjust the offset.
+ * Returns: a pointer to memory of which at least @maxsize bytes can be
+ * accessed according to the access pattern in @flags.
  */
-typedef void        (*GstMemoryResizeFunction)    (GstMemory *mem, gssize offset, gssize size);
+typedef gpointer    (*GstMemoryMapFunction)       (GstMemory *mem, gsize maxsize, GstMapFlags flags);
 
 /**
- * GstMemoryMapFunction:
+ * GstMemoryMapFullFunction:
  * @mem: a #GstMemory
- * @size: pointer for the size
- * @maxsize: pointer for the maxsize
- * @flags: access mode for the memory
+ * @info: the #GstMapInfo to map with
+ * @maxsize: size to map
  *
  * Get the memory of @mem that can be accessed according to the mode specified
- * in @flags. @size and @maxsize will respectively contain the current amount of
- * valid bytes in the returned memory and the maximum allocated memory.
- * @size and @maxsize can optionally be set to NULL.
+ * in @info's flags. The function should return a pointer that contains at least
+ * @maxsize bytes.
  *
- * Returns: a pointer to memory. @size bytes are currently used from the
- * returned pointer and @maxsize bytes can potentially be used.
+ * Returns: a pointer to memory of which at least @maxsize bytes can be
+ * accessed according to the access pattern in @info's flags.
  */
-typedef gpointer    (*GstMemoryMapFunction)       (GstMemory *mem, gsize *size, gsize *maxsize,
-                                                   GstMapFlags flags);
+typedef gpointer    (*GstMemoryMapFullFunction)       (GstMemory *mem, GstMapInfo * info, gsize maxsize);
 
 /**
  * GstMemoryUnmapFunction:
  * @mem: a #GstMemory
- * @data: the data pointer
- * @size: the new size, or -1 to not modify the size
- *
- * Return the pointer previously retrieved with gst_memory_map() and adjust the
- * size of the memory with @size. @size can optionally be set to -1 to not
- * modify the size.
  *
- * Returns: %TRUE on success.
+ * Return the pointer previously retrieved with gst_memory_map().
  */
-typedef gboolean    (*GstMemoryUnmapFunction)     (GstMemory *mem, gpointer data, gssize size);
+typedef void        (*GstMemoryUnmapFunction)     (GstMemory *mem);
 
 /**
- * GstMemoryFreeFunction:
+ * GstMemoryUnmapFullFunction:
  * @mem: a #GstMemory
+ * @info: a #GstMapInfo
  *
- * Free the memory used by @mem. This function is usually called when the
- * refcount of the @mem has reached 0.
+ * Return the pointer previously retrieved with gst_memory_map() with @info.
  */
-typedef void        (*GstMemoryFreeFunction)      (GstMemory *mem);
+typedef void        (*GstMemoryUnmapFullFunction)     (GstMemory *mem, GstMapInfo * info);
 
 /**
  * GstMemoryCopyFunction:
@@ -236,72 +310,85 @@ typedef GstMemory * (*GstMemoryShareFunction)     (GstMemory *mem, gssize offset
  */
 typedef gboolean    (*GstMemoryIsSpanFunction)    (GstMemory *mem1, GstMemory *mem2, gsize *offset);
 
+GST_API
+void           gst_memory_init         (GstMemory *mem, GstMemoryFlags flags,
+                                        GstAllocator *allocator, GstMemory *parent,
+                                        gsize maxsize, gsize align,
+                                        gsize offset, gsize size);
+GST_API
+gboolean       gst_memory_is_type      (GstMemory *mem, const gchar *mem_type);
+
+/* refcounting */
 /**
- * GstMemoryInfo:
- * @alloc: the implementation of the GstMemoryAllocFunction
- * @get_sizes: the implementation of the GstMemoryGetSizesFunction
- * @resize: the implementation of the GstMemoryResizeFunction
- * @map: the implementation of the GstMemoryMapFunction
- * @unmap: the implementation of the GstMemoryUnmapFunction
- * @free: the implementation of the GstMemoryFreeFunction
- * @copy: the implementation of the GstMemoryCopyFunction
- * @share: the implementation of the GstMemoryShareFunction
- * @is_span: the implementation of the GstMemoryIsSpanFunction
- * @user_data: generic user data for the allocator
- *
- * The #GstMemoryInfo is used to register new memory allocators and contain
- * the implementations for various memory operations.
+ * gst_memory_ref:
+ * @memory: The memory to refcount
+ *
+ * Increase the refcount of this memory.
+ *
+ * Returns: (transfer full): @memory (for convenience when doing assignments)
  */
-struct _GstMemoryInfo {
-  GstMemoryAllocFunction    alloc;
-  GstMemoryGetSizesFunction get_sizes;
-  GstMemoryResizeFunction   resize;
-  GstMemoryMapFunction      map;
-  GstMemoryUnmapFunction    unmap;
-  GstMemoryFreeFunction     free;
+static inline GstMemory *
+gst_memory_ref (GstMemory * memory)
+{
+  return (GstMemory *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (memory));
+}
 
-  GstMemoryCopyFunction     copy;
-  GstMemoryShareFunction    share;
-  GstMemoryIsSpanFunction   is_span;
+/**
+ * gst_memory_unref:
+ * @memory: (transfer full): the memory to refcount
+ *
+ * Decrease the refcount of an memory, freeing it if the refcount reaches 0.
+ */
+static inline void
+gst_memory_unref (GstMemory * memory)
+{
+  gst_mini_object_unref (GST_MINI_OBJECT_CAST (memory));
+}
 
-  gpointer user_data;
+/* getting/setting memory properties */
 
-  /*< private >*/
-  gpointer _gst_reserved[GST_PADDING];
-};
+GST_API
+gsize          gst_memory_get_sizes    (GstMemory *mem, gsize *offset, gsize *maxsize);
 
-/* allocators */
-const GstAllocator *  gst_allocator_register    (const gchar *name, const GstMemoryInfo *info);
-const GstAllocator *  gst_allocator_find        (const gchar *name);
+GST_API
+void           gst_memory_resize       (GstMemory *mem, gssize offset, gsize size);
 
-void                  gst_allocator_set_default (const GstAllocator * allocator);
+#define        gst_memory_lock(m,f)        gst_mini_object_lock (GST_MINI_OBJECT_CAST (m), (f))
+#define        gst_memory_unlock(m,f)      gst_mini_object_unlock (GST_MINI_OBJECT_CAST (m), (f))
+#define        gst_memory_is_writable(m)   gst_mini_object_is_writable (GST_MINI_OBJECT_CAST (m))
+#define        gst_memory_make_writable(m) GST_MEMORY_CAST (gst_mini_object_make_writable (GST_MINI_OBJECT_CAST (m)))
 
-/* allocating memory blocks */
-GstMemory * gst_allocator_alloc        (const GstAllocator * allocator,
-                                        gsize maxsize, gsize align);
+/* retrieving data */
 
-GstMemory * gst_memory_new_wrapped     (GstMemoryFlags flags, gpointer data, GFreeFunc free_func,
-                                        gsize maxsize, gsize offset, gsize size);
+GST_API
+GstMemory *    gst_memory_make_mapped  (GstMemory *mem, GstMapInfo *info, GstMapFlags flags) G_GNUC_WARN_UNUSED_RESULT;
 
-/* refcounting */
-GstMemory * gst_memory_ref        (GstMemory *mem);
-void        gst_memory_unref      (GstMemory *mem);
+GST_API
+gboolean       gst_memory_map          (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
 
-/* getting/setting memory properties */
-gsize       gst_memory_get_sizes  (GstMemory *mem, gsize *offset, gsize *maxsize);
-void        gst_memory_resize     (GstMemory *mem, gssize offset, gsize size);
-
-/* retrieving data */
-gpointer    gst_memory_map        (GstMemory *mem, gsize *size, gsize *maxsize,
-                                   GstMapFlags flags);
-gboolean    gst_memory_unmap      (GstMemory *mem, gpointer data, gssize size);
+GST_API
+void           gst_memory_unmap        (GstMemory *mem, GstMapInfo *info);
 
 /* copy and subregions */
-GstMemory * gst_memory_copy       (GstMemory *mem, gssize offset, gssize size);
-GstMemory * gst_memory_share      (GstMemory *mem, gssize offset, gssize size);
+
+GST_API
+GstMemory *    gst_memory_copy         (GstMemory *mem, gssize offset, gssize size) G_GNUC_WARN_UNUSED_RESULT;
+
+GST_API
+GstMemory *    gst_memory_share        (GstMemory *mem, gssize offset, gssize size) G_GNUC_WARN_UNUSED_RESULT;
 
 /* span memory */
-gboolean    gst_memory_is_span    (GstMemory *mem1, GstMemory *mem2, gsize *offset);
+
+GST_API
+gboolean       gst_memory_is_span      (GstMemory *mem1, GstMemory *mem2, gsize *offset);
+
+#ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstMemory, gst_memory_unref)
+#endif
+
+#ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(GstAllocator, gst_object_unref)
+#endif
 
 G_END_DECLS