miniobjects: pass copy, dispose and free function to gst_mini_object_init()
[platform/upstream/gstreamer.git] / gst / gstmemory.c
index 5a7f289..efcb73c 100644 (file)
 #include "gst_private.h"
 #include "gstmemory.h"
 
-#ifndef GST_DISABLE_TRACE
-#include "gsttrace.h"
-static GstAllocTrace *_gst_memory_trace;
-static GstAllocTrace *_gst_allocator_trace;
-#endif
-
-G_DEFINE_BOXED_TYPE (GstMemory, gst_memory, (GBoxedCopyFunc) gst_memory_ref,
-    (GBoxedFreeFunc) gst_memory_unref);
+GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
 
-G_DEFINE_BOXED_TYPE (GstAllocator, gst_allocator,
-    (GBoxedCopyFunc) gst_allocator_ref, (GBoxedFreeFunc) gst_allocator_unref);
+GST_DEFINE_MINI_OBJECT_TYPE (GstAllocator, gst_allocator);
 
 G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
     (GBoxedCopyFunc) gst_allocation_params_copy,
@@ -101,7 +93,7 @@ size_t gst_memory_alignment = 0;
 
 struct _GstAllocator
 {
-  gint refcount;
+  GstMiniObject mini_object;
 
   GstMemoryInfo info;
 
@@ -125,6 +117,20 @@ static GstAllocator *_default_allocator;
 /* our predefined allocators */
 static GstAllocator *_default_mem_impl;
 
+static GstMemory *
+_gst_memory_copy (GstMemory * mem)
+{
+  return gst_memory_copy (mem, 0, -1);
+}
+
+static void
+_gst_memory_free (GstMemory * mem)
+{
+  /* there should be no outstanding mappings */
+  g_return_if_fail (g_atomic_int_get (&mem->state) < 4);
+  mem->allocator->info.mem_free (mem);
+}
+
 /* initialize the fields */
 static void
 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
@@ -132,9 +138,13 @@ _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
     gsize maxsize, gsize offset, gsize size, gsize align,
     gpointer user_data, GDestroyNotify notify)
 {
+  gst_mini_object_init (GST_MINI_OBJECT_CAST (mem), GST_TYPE_MEMORY,
+      (GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
+      (GstMiniObjectFreeFunction) _gst_memory_free);
+
+  mem->mem.mini_object.flags = flags;
+
   mem->mem.allocator = _default_mem_impl;
-  mem->mem.flags = flags;
-  mem->mem.refcount = 1;
   mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
   mem->mem.state = (flags & GST_MEMORY_FLAG_READONLY ? 0x1 : 0);
   mem->mem.maxsize = maxsize;
@@ -222,7 +232,7 @@ _default_alloc_alloc (GstAllocator * allocator, gsize size,
 }
 
 static gpointer
-_default_mem_map (GstMemoryDefault * mem, GstMapFlags flags)
+_default_mem_map (GstMemoryDefault * mem, gsize maxsize, GstMapFlags flags)
 {
   return mem->data;
 }
@@ -258,8 +268,10 @@ _default_mem_copy (GstMemoryDefault * mem, gssize offset, gsize size)
   copy =
       _default_mem_new_block (0, mem->mem.maxsize, 0, mem->mem.offset + offset,
       size);
+  GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
+      "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", mem->mem.maxsize, mem,
+      copy);
   memcpy (copy->data, mem->data, mem->mem.maxsize);
-  GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
 
   return copy;
 }
@@ -278,7 +290,7 @@ _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
     size = mem->mem.size - offset;
 
   sub =
-      _default_mem_new (parent->flags, parent, mem->data,
+      _default_mem_new (GST_MINI_OBJECT_FLAGS (parent), parent, mem->data,
       mem->mem.maxsize, mem->mem.offset + offset, size, mem->mem.align, NULL,
       NULL);
 
@@ -324,8 +336,9 @@ _fallback_mem_copy (GstMemory * mem, gssize offset, gssize size)
     return NULL;
   }
 
+  GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
+      "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
   memcpy (dinfo.data, sinfo.data + offset, size);
-  GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
   gst_memory_unmap (copy, &dinfo);
   gst_memory_unmap (mem, &sinfo);
 
@@ -361,11 +374,6 @@ _priv_gst_memory_initialize (void)
     (GstMemoryIsSpanFunction) _default_mem_is_span,
   };
 
-#ifndef GST_DISABLE_TRACE
-  _gst_memory_trace = _gst_alloc_trace_register ("GstMemory", -1);
-  _gst_allocator_trace = _gst_alloc_trace_register ("GstAllocator", -1);
-#endif
-
   g_rw_lock_init (&lock);
   allocators = g_hash_table_new (g_str_hash, g_str_equal);
 
@@ -416,61 +424,10 @@ gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
       _default_mem_new (flags, NULL, data, maxsize, offset, size, 0, user_data,
       notify);
 
-#ifndef GST_DISABLE_TRACE
-  _gst_alloc_trace_new (_gst_memory_trace, mem);
-#endif
-
   return (GstMemory *) mem;
 }
 
 /**
- * gst_memory_ref:
- * @mem: a #GstMemory
- *
- * Increases the refcount of @mem.
- *
- * Returns: @mem with increased refcount
- */
-GstMemory *
-gst_memory_ref (GstMemory * mem)
-{
-  g_return_val_if_fail (mem != NULL, NULL);
-
-  GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
-      mem->refcount + 1);
-
-  g_atomic_int_inc (&mem->refcount);
-
-  return mem;
-}
-
-/**
- * gst_memory_unref:
- * @mem: a #GstMemory
- *
- * Decreases the refcount of @mem. When the refcount reaches 0, the free
- * function of @mem will be called.
- */
-void
-gst_memory_unref (GstMemory * mem)
-{
-  g_return_if_fail (mem != NULL);
-  g_return_if_fail (mem->allocator != NULL);
-
-  GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
-      mem->refcount - 1);
-
-  if (g_atomic_int_dec_and_test (&mem->refcount)) {
-    /* there should be no outstanding mappings */
-    g_return_if_fail (g_atomic_int_get (&mem->state) < 4);
-#ifndef GST_DISABLE_TRACE
-    _gst_alloc_trace_free (_gst_memory_trace, mem);
-#endif
-    mem->allocator->info.mem_free (mem);
-  }
-}
-
-/**
  * gst_memory_is_exclusive:
  * @mem: a #GstMemory
  *
@@ -482,7 +439,7 @@ gst_memory_is_exclusive (GstMemory * mem)
 {
   g_return_val_if_fail (mem != NULL, FALSE);
 
-  return (g_atomic_int_get (&mem->refcount) == 1);
+  return GST_MINI_OBJECT_REFCOUNT_VALUE (mem) == 1;
 }
 
 /**
@@ -735,10 +692,6 @@ gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
 
   copy = mem->allocator->info.mem_copy (mem, offset, size);
 
-#ifndef GST_DISABLE_TRACE
-  _gst_alloc_trace_new (_gst_memory_trace, copy);
-#endif
-
   return copy;
 }
 
@@ -766,10 +719,6 @@ gst_memory_share (GstMemory * mem, gssize offset, gssize size)
 
   shared = mem->allocator->info.mem_share (mem, offset, size);
 
-#ifndef GST_DISABLE_TRACE
-  _gst_alloc_trace_new (_gst_memory_trace, shared);
-#endif
-
   return shared;
 }
 
@@ -809,6 +758,21 @@ gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
   return TRUE;
 }
 
+static void
+_gst_allocator_free (GstAllocator * allocator)
+{
+  if (allocator->notify)
+    allocator->notify (allocator->user_data);
+
+  g_slice_free1 (sizeof (GstAllocator), allocator);
+}
+
+static GstAllocator *
+_gst_allocator_copy (GstAllocator * allocator)
+{
+  return gst_allocator_ref (allocator);
+}
+
 /**
  * gst_allocator_new:
  * @info: a #GstMemoryInfo
@@ -831,9 +795,6 @@ gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
 {
   GstAllocator *allocator;
 
-#define INSTALL_FALLBACK(_t) \
-  if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
-
   g_return_val_if_fail (info != NULL, NULL);
   g_return_val_if_fail (info->alloc != NULL, NULL);
   g_return_val_if_fail (info->mem_map != NULL, NULL);
@@ -841,21 +802,24 @@ gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
   g_return_val_if_fail (info->mem_free != NULL, NULL);
   g_return_val_if_fail (info->mem_share != NULL, NULL);
 
-  allocator = g_slice_new (GstAllocator);
-  allocator->refcount = 1;
+  allocator = g_slice_new0 (GstAllocator);
+
+  gst_mini_object_init (GST_MINI_OBJECT_CAST (allocator), GST_TYPE_ALLOCATOR,
+      (GstMiniObjectCopyFunction) _gst_allocator_copy, NULL,
+      (GstMiniObjectFreeFunction) _gst_allocator_free);
+
   allocator->info = *info;
   allocator->user_data = user_data;
   allocator->notify = notify;
+
+#define INSTALL_FALLBACK(_t) \
+  if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
   INSTALL_FALLBACK (mem_copy);
   INSTALL_FALLBACK (mem_is_span);
 #undef INSTALL_FALLBACK
 
   GST_CAT_DEBUG (GST_CAT_MEMORY, "new allocator %p", allocator);
 
-#ifndef GST_DISABLE_TRACE
-  _gst_alloc_trace_new (_gst_allocator_trace, allocator);
-#endif
-
   return allocator;
 }
 
@@ -876,52 +840,6 @@ gst_allocator_get_memory_type (GstAllocator * allocator)
 }
 
 /**
- * gst_allocator_ref:
- * @allocator: a #GstAllocator
- *
- * Increases the refcount of @allocator.
- *
- * Returns: @allocator with increased refcount
- */
-GstAllocator *
-gst_allocator_ref (GstAllocator * allocator)
-{
-  g_return_val_if_fail (allocator != NULL, NULL);
-
-  GST_CAT_TRACE (GST_CAT_MEMORY, "allocator %p, %d->%d", allocator,
-      allocator->refcount, allocator->refcount + 1);
-
-  g_atomic_int_inc (&allocator->refcount);
-
-  return allocator;
-}
-
-/**
- * gst_allocator_unref:
- * @allocator: a #GstAllocator
- *
- * Decreases the refcount of @allocator. When the refcount reaches 0, the notify
- * function of @allocator will be called and the allocator will be freed.
- */
-void
-gst_allocator_unref (GstAllocator * allocator)
-{
-  g_return_if_fail (allocator != NULL);
-
-  GST_CAT_TRACE (GST_CAT_MEMORY, "allocator %p, %d->%d", allocator,
-      allocator->refcount, allocator->refcount - 1);
-
-  if (g_atomic_int_dec_and_test (&allocator->refcount)) {
-    if (allocator->notify)
-      allocator->notify (allocator->user_data);
-#ifndef GST_DISABLE_TRACE
-    _gst_alloc_trace_free (_gst_allocator_trace, allocator);
-#endif
-    g_slice_free1 (sizeof (GstAllocator), allocator);
-  }
-}
-
-/**
  * gst_allocator_register:
  * @name: the name of the allocator
  * @allocator: (transfer full): #GstAllocator
@@ -1084,8 +1002,5 @@ gst_allocator_alloc (GstAllocator * allocator, gsize size,
 
   mem = allocator->info.alloc (allocator, size, params, allocator->user_data);
 
-#ifndef GST_DISABLE_TRACE
-  _gst_alloc_trace_new (_gst_memory_trace, mem);
-#endif
   return mem;
 }