memory: make GstAllocator a miniobject
[platform/upstream/gstreamer.git] / gst / gstmemory.h
index 6cebb76..701249d 100644 (file)
@@ -32,30 +32,96 @@ G_BEGIN_DECLS
 #define GST_TYPE_MEMORY (gst_memory_get_type())
 GType gst_memory_get_type(void);
 
+#define GST_TYPE_ALLOCATOR (gst_allocator_get_type())
+GType gst_allocator_get_type(void);
+
+#define GST_TYPE_ALLOCATION_PARAMS (gst_allocation_params_get_type())
+GType gst_allocation_params_get_type(void);
+
 typedef struct _GstMemory GstMemory;
 typedef struct _GstMemoryInfo GstMemoryInfo;
 typedef struct _GstAllocator GstAllocator;
+typedef struct _GstAllocationParams GstAllocationParams;
 
+/**
+ * gst_memory_alignment:
+ *
+ * The default memory alignment in bytes - 1
+ * an alignment of 7 would be the same as what malloc() guarantees.
+ */
 GST_EXPORT gsize gst_memory_alignment;
 
+#define GST_MEMORY_CAST(mem)   ((GstMemory *)(mem))
+
 /**
  * GstMemoryFlags:
  * @GST_MEMORY_FLAG_READONLY: memory is readonly. It is not allowed to map the
  * 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_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_READONLY      = (1 << 0),
+  GST_MEMORY_FLAG_NO_SHARE      = (1 << 1),
+  GST_MEMORY_FLAG_ZERO_PREFIXED = (1 << 2),
+  GST_MEMORY_FLAG_ZERO_PADDED   = (1 << 3),
 
-  GST_MEMORY_FLAG_LAST     = (1 << 16)
+  GST_MEMORY_FLAG_LAST          = (1 << 16)
 } GstMemoryFlags;
 
 /**
+ * GST_MEMORY_FLAGS:
+ * @mem: a #GstMemory.
+ *
+ * A flags word containing #GstMemoryFlags flags set on @mem
+ */
+#define GST_MEMORY_FLAGS(mem)  (GST_MEMORY_CAST (mem)->flags)
+/**
+ * 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_MEMORY_FLAGS (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_MEMORY_FLAGS (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_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)
+
+
+/**
  * GstMemory:
  * @allocator: pointer to the #GstAllocator
  * @flags: memory flags
@@ -71,7 +137,7 @@ typedef enum {
  * as the first member of their structure.
  */
 struct _GstMemory {
-  const GstAllocator *allocator;
+  GstAllocator   *allocator;
 
   GstMemoryFlags  flags;
   gint            refcount;
@@ -105,6 +171,8 @@ typedef enum {
  * @data: 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.
  *
  * A structure containing the result of a map operation such as
  * gst_memory_map(). It contains the data and size.
@@ -115,9 +183,16 @@ typedef struct {
   guint8 *data;
   gsize size;
   gsize maxsize;
+  /*< private >*/
+  gpointer user_data[4];
 } GstMapInfo;
 
-#define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0 }
+/**
+ * GST_MAP_INFO_INIT:
+ *
+ * Initializer for #GstMapInfo
+ */
+#define GST_MAP_INFO_INIT { NULL, 0, NULL, 0, 0, }
 
 /**
  * GST_MAP_READWRITE:
@@ -134,22 +209,45 @@ typedef struct {
 #define GST_ALLOCATOR_SYSMEM   "SystemMemory"
 
 /**
- * GstMemoryAllocFunction:
+ * GstAllocationParams:
+ * @flags: flags to control allocation
+ * @align: the desired alignment of the memory
+ * @prefix: the disired prefix
+ * @padding: the desired padding
+ *
+ * Parameters to control the allocation of memory
+ */
+struct _GstAllocationParams {
+  GstMemoryFlags flags;
+  gsize          align;
+  gsize          prefix;
+  gsize          padding;
+
+  /*< private >*/
+  gpointer _gst_reserved[GST_PADDING];
+};
+
+/**
+ * GstAllocatorAllocFunction:
  * @allocator: a #GstAllocator
- * @maxsize: the maxsize
- * @align: the alignment
+ * @size: the size
+ * @params: allocator params
  * @user_data: user data
  *
- * Allocate a new #GstMemory from @allocator that can hold at least @maxsize bytes
- * and is aligned to (@align + 1) bytes.
+ * Allocate a new #GstMemory from @allocator that can hold at least @size
+ * bytes (+ padding) and is aligned to (@align + 1) bytes.
+ *
+ * The offset and size of the memory should be set and the prefix/padding must
+ * be filled with 0 if @params flags contains #GST_MEMORY_FLAG_ZERO_PREFIXED and
+ * #GST_MEMORY_FLAG_ZERO_PADDED respectively.
  *
- * @user_data is the data that was used when registering @allocator.
+ * @user_data is the data that was used when creating @allocator.
  *
  * Returns: a newly allocated #GstMemory. Free with gst_memory_unref()
  */
-typedef GstMemory *  (*GstMemoryAllocFunction)  (const GstAllocator *allocator,
-                                                 gsize maxsize, gsize align,
-                                                 gpointer user_data);
+typedef GstMemory *  (*GstAllocatorAllocFunction)  (GstAllocator *allocator,
+                                                    gsize size, GstAllocationParams *params,
+                                                    gpointer user_data);
 
 /**
  * GstMemoryMapFunction:
@@ -229,68 +327,120 @@ typedef gboolean    (*GstMemoryIsSpanFunction)    (GstMemory *mem1, GstMemory *m
 
 /**
  * GstMemoryInfo:
- * @alloc: the implementation of the GstMemoryAllocFunction
- * @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
+ * @mem_type: the memory type this allocator provides
+ * @alloc: the implementation of the GstAllocatorAllocFunction
+ * @mem_map: the implementation of the GstMemoryMapFunction
+ * @mem_unmap: the implementation of the GstMemoryUnmapFunction
+ * @mem_free: the implementation of the GstMemoryFreeFunction
+ * @mem_copy: the implementation of the GstMemoryCopyFunction
+ * @mem_share: the implementation of the GstMemoryShareFunction
+ * @mem_is_span: the implementation of the GstMemoryIsSpanFunction
  *
  * The #GstMemoryInfo is used to register new memory allocators and contain
  * the implementations for various memory operations.
  */
 struct _GstMemoryInfo {
-  GstMemoryAllocFunction    alloc;
-  GstMemoryMapFunction      map;
-  GstMemoryUnmapFunction    unmap;
-  GstMemoryFreeFunction     free;
+  const gchar              *mem_type;
 
-  GstMemoryCopyFunction     copy;
-  GstMemoryShareFunction    share;
-  GstMemoryIsSpanFunction   is_span;
+  GstAllocatorAllocFunction alloc;
 
-  gpointer user_data;
+  GstMemoryMapFunction      mem_map;
+  GstMemoryUnmapFunction    mem_unmap;
+  GstMemoryFreeFunction     mem_free;
+
+  GstMemoryCopyFunction     mem_copy;
+  GstMemoryShareFunction    mem_share;
+  GstMemoryIsSpanFunction   mem_is_span;
 
   /*< private >*/
   gpointer _gst_reserved[GST_PADDING];
 };
 
+/**
+ * GstAllocator:
+ *
+ * An opaque type returned from gst_allocator_new() or gst_allocator_find()
+ * that can be used to allocator memory.
+ */
+
 /* allocators */
-const GstAllocator *  gst_allocator_register    (const gchar *name, const GstMemoryInfo *info);
-const GstAllocator *  gst_allocator_find        (const gchar *name);
+GstAllocator * gst_allocator_new             (const GstMemoryInfo * info,
+                                              gpointer user_data, GDestroyNotify notify);
+const gchar *  gst_allocator_get_memory_type (GstAllocator * allocator);
 
-void                  gst_allocator_set_default (const GstAllocator * allocator);
+/**
+ * gst_allocator_ref:
+ * @allocator: The allocator to refcount
+ *
+ * Increase the refcount of this allocator.
+ *
+ * Returns: (transfer full): @allocator (for convenience when doing assignments)
+ */
+#ifdef _FOOL_GTK_DOC_
+G_INLINE_FUNC GstAllocator * gst_allocator_ref (GstAllocator * allocator);
+#endif
+
+static inline GstAllocator *
+gst_allocator_ref (GstAllocator * allocator)
+{
+  return (GstAllocator *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (allocator));
+}
+
+/**
+ * gst_allocator_unref:
+ * @allocator: (transfer full): the allocator to refcount
+ *
+ * Decrease the refcount of an allocator, freeing it if the refcount reaches 0.
+ */
+#ifdef _FOOL_GTK_DOC_
+G_INLINE_FUNC void gst_allocator_unref (GstAllocator * allocator);
+#endif
+
+static inline void
+gst_allocator_unref (GstAllocator * allocator)
+{
+  gst_mini_object_unref (GST_MINI_OBJECT_CAST (allocator));
+}
+
+void           gst_allocator_register        (const gchar *name, GstAllocator *allocator);
+GstAllocator * gst_allocator_find            (const gchar *name);
+
+void           gst_allocator_set_default     (GstAllocator * allocator);
 
 /* allocating memory blocks */
-GstMemory * gst_allocator_alloc        (const GstAllocator * allocator,
-                                        gsize maxsize, gsize align);
+void           gst_allocation_params_init     (GstAllocationParams *params);
+GstAllocationParams *
+               gst_allocation_params_copy     (const GstAllocationParams *params) G_GNUC_MALLOC;
+void           gst_allocation_params_free     (GstAllocationParams *params);
 
-GstMemory * gst_memory_new_wrapped     (GstMemoryFlags flags, gpointer data, GFreeFunc free_func,
-                                        gsize maxsize, gsize offset, gsize size);
+GstMemory *    gst_allocator_alloc           (GstAllocator * allocator, gsize size,
+                                              GstAllocationParams *params);
+
+GstMemory *    gst_memory_new_wrapped  (GstMemoryFlags flags, gpointer data, gsize maxsize,
+                                        gsize offset, gsize size, gpointer user_data,
+                                        GDestroyNotify notify);
 
 /* refcounting */
-GstMemory * gst_memory_ref         (GstMemory *mem);
-void        gst_memory_unref       (GstMemory *mem);
+GstMemory *    gst_memory_ref          (GstMemory *mem);
+void           gst_memory_unref        (GstMemory *mem);
+
+gboolean       gst_memory_is_exclusive (GstMemory *mem);
 
 /* 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);
+gsize          gst_memory_get_sizes    (GstMemory *mem, gsize *offset, gsize *maxsize);
+void           gst_memory_resize       (GstMemory *mem, gssize offset, gsize size);
 
 /* retrieving data */
-gboolean    gst_memory_is_writable (GstMemory *mem);
-
-GstMemory * gst_memory_make_mapped (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
-gboolean    gst_memory_map         (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
-void        gst_memory_unmap       (GstMemory *mem, GstMapInfo *info);
+GstMemory *    gst_memory_make_mapped  (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
+gboolean       gst_memory_map          (GstMemory *mem, GstMapInfo *info, GstMapFlags flags);
+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);
+GstMemory *    gst_memory_copy         (GstMemory *mem, gssize offset, gssize size);
+GstMemory *    gst_memory_share        (GstMemory *mem, gssize offset, gssize size);
 
 /* span memory */
-gboolean    gst_memory_is_span     (GstMemory *mem1, GstMemory *mem2, gsize *offset);
+gboolean       gst_memory_is_span      (GstMemory *mem1, GstMemory *mem2, gsize *offset);
 
 G_END_DECLS