[gstreamermm] Gst::Allocator: fix ref of alloc() return value



commit b7f1504edbdae0c99a513f0dbdd336d37d6feb0a
Author: Michał Wróbel <michal wrobel flytronic pl>
Date:   Sun Jun 28 01:23:32 2015 +0200

    Gst::Allocator: fix ref of alloc() return value
    
    This is https://bugzilla.gnome.org/show_bug.cgi?id=751601 in progress.
    
        * gstreamer/src/allocator.hg: fix virtual method alloc_vfunc.
        * tests/test-allocator.cc: add a few tests for Allocator class.

 gstreamer/src/allocator.hg |    2 +-
 tests/test-allocator.cc    |   77 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+), 1 deletions(-)
---
diff --git a/gstreamer/src/allocator.hg b/gstreamer/src/allocator.hg
index e778c8a..43d2e6c 100644
--- a/gstreamer/src/allocator.hg
+++ b/gstreamer/src/allocator.hg
@@ -94,7 +94,7 @@ public:
   _IGNORE(gst_allocator_free)
 
   _WRAP_METHOD(Glib::RefPtr<Gst::Memory> alloc(gsize size, AllocationParams params), gst_allocator_alloc)
-  _WRAP_VFUNC(Glib::RefPtr<Gst::Memory> alloc(gsize size, AllocationParams params), "alloc")
+  _WRAP_VFUNC(Glib::RefPtr<Gst::Memory> alloc(gsize size, AllocationParams params), "alloc", refreturn_ctype)
 
   // This vfunc is hand-coded because it takes reference to a memory.
   // In generally, arguments are passed to a function by copy, so
diff --git a/tests/test-allocator.cc b/tests/test-allocator.cc
index b820518..1e928b3 100644
--- a/tests/test-allocator.cc
+++ b/tests/test-allocator.cc
@@ -42,3 +42,80 @@ TEST(AllocatorTest, ShouldCorrectAllocateMemory)
 
   allocator->free(mem);
 }
+
+class DerivedFromAllocator : public Gst::Allocator
+{
+    Glib::RefPtr<Gst::Allocator> the_allocator;
+
+public:
+    explicit DerivedFromAllocator(GstAllocator *gobj)
+        : Glib::ObjectBase(typeid (DerivedFromAllocator)),
+          Gst::Allocator(gobj),
+          the_allocator(Allocator::get_default_allocator())
+    {
+    }
+
+    virtual Glib::RefPtr<Gst::Memory> alloc_vfunc(gsize size, AllocationParams params)
+    {
+      Glib::RefPtr<Gst::Memory> r = the_allocator->alloc(size, params);
+      r->gobj()->allocator = gobj(); // pretend that it was us who allocated this memory
+      return r;
+    }
+
+    virtual void free_vfunc(Glib::RefPtr<Gst::Memory>& memory)
+    {
+      memory->gobj()->allocator = the_allocator->gobj(); // pretend that it was the_allocator who allocated 
this memory
+      the_allocator->free(memory);
+    }
+
+    static Glib::RefPtr<Allocator> create()
+    {
+      return Glib::RefPtr<Allocator>(new 
DerivedFromAllocator((GstAllocator*)g_object_new(Allocator::get_type(), NULL)));
+    }
+};
+
+TEST(AllocatorTest, DerivedFromAllocatorShouldReturnProperlyRefcountedWrappedGstMemory)
+{
+  Glib::RefPtr<Allocator> allocator = DerivedFromAllocator::create();
+  ASSERT_TRUE(allocator);
+  ASSERT_TRUE(allocator->gobj());
+  ASSERT_TRUE(GST_IS_ALLOCATOR(allocator->gobj()));
+
+  AllocationParams params;
+  params.set_align(7);
+  MemoryFlags flags = MEMORY_FLAG_NO_SHARE | MEMORY_FLAG_LAST;
+  params.set_flags(flags);
+
+  Glib::RefPtr<Memory> mem = allocator->alloc(10, params);
+  ASSERT_TRUE(mem);
+  ASSERT_TRUE(mem->gobj());
+  ASSERT_TRUE(GST_IS_MINI_OBJECT_TYPE(mem->gobj(), GST_TYPE_MEMORY));
+  ASSERT_EQ(1, mem->get_refcount());
+  EXPECT_EQ(10, mem->get_size());
+  EXPECT_EQ(7, mem->get_align());
+  EXPECT_TRUE(flags & mem->get_flags());
+
+  allocator->free(mem);
+}
+
+TEST(AllocatorTest, DerivedFromAllocatorShouldReturnProperlyRefcountedGstMemory)
+{
+  Glib::RefPtr<Allocator> allocator = DerivedFromAllocator::create();
+  ASSERT_TRUE(allocator);
+  ASSERT_TRUE(allocator->gobj());
+  ASSERT_TRUE(GST_IS_ALLOCATOR(allocator->gobj()));
+
+  AllocationParams params;
+  params.set_align(7);
+  MemoryFlags flags = MEMORY_FLAG_NO_SHARE | MEMORY_FLAG_LAST;
+  params.set_flags(flags);
+
+  GstMemory *mem = gst_allocator_alloc(allocator->gobj(), 10, params.gobj());
+  ASSERT_TRUE(mem);
+  ASSERT_TRUE(GST_IS_MINI_OBJECT_TYPE(mem, GST_TYPE_MEMORY));
+  ASSERT_EQ(1, mem->mini_object.refcount);
+  EXPECT_EQ(10, mem->size);
+  EXPECT_EQ(7, mem->align);
+  EXPECT_TRUE(flags & mem->mini_object.flags);
+  gst_allocator_free(allocator->gobj(), mem);
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]