allocators: make GstFdAllocator non-abstract

Make the GstFdAllocator non-abstract because it is perfectly possible
to make memory from a generic fd. Mark the memory as simply "fd".
This commit is contained in:
Wim Taymans 2015-04-17 09:31:40 +02:00
parent 1c01b50ada
commit 7c059b4530
3 changed files with 74 additions and 44 deletions

View file

@ -106,16 +106,9 @@ gst_dmabuf_allocator_new (void)
GstMemory * GstMemory *
gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size) gst_dmabuf_allocator_alloc (GstAllocator * allocator, gint fd, gsize size)
{ {
GstFdAllocator *alloc = GST_FD_ALLOCATOR_CAST (allocator); g_return_val_if_fail (GST_IS_DMABUF_ALLOCATOR (allocator), NULL);
GstFdAllocatorClass *klass = GST_FD_ALLOCATOR_GET_CLASS (alloc);
if (!GST_IS_DMABUF_ALLOCATOR (allocator)) { return gst_fd_allocator_alloc (allocator, fd, size, GST_FD_MEMORY_FLAG_NONE);
GST_WARNING ("it isn't the correct allocator for dmabuf");
return NULL;
}
GST_DEBUG ("alloc from allocator %p", allocator);
return klass->alloc (alloc, fd, size, GST_FD_MEMORY_FLAG_NONE);
} }
/** /**

View file

@ -177,13 +177,75 @@ gst_fd_mem_share (GstMemory * gmem, gssize offset, gssize size)
#endif #endif
} }
static GstMemory * G_DEFINE_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR);
gst_fd_allocator_alloc (GstFdAllocator * allocator, gint fd, gsize size,
static void
gst_fd_allocator_class_init (GstFdAllocatorClass * klass)
{
GstAllocatorClass *allocator_class;
allocator_class = (GstAllocatorClass *) klass;
allocator_class->alloc = NULL;
allocator_class->free = gst_fd_mem_free;
}
static void
gst_fd_allocator_init (GstFdAllocator * allocator)
{
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
alloc->mem_type = GST_ALLOCATOR_FD;
alloc->mem_map = gst_fd_mem_map;
alloc->mem_unmap = gst_fd_mem_unmap;
alloc->mem_share = gst_fd_mem_share;
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
}
/**
* gst_fd_allocator_new:
*
* Return a new fd allocator.
*
* Returns: (transfer full): a new fd allocator, or NULL if the allocator
* isn't available. Use gst_object_unref() to release the allocator after
* usage
*
* Since: 1.6
*/
GstAllocator *
gst_fd_allocator_new (void)
{
return g_object_new (GST_TYPE_FD_ALLOCATOR, NULL);
}
/**
* gst_fd_allocator_alloc:
* @allocator: (allow-none): allocator to be used for this memory
* @fd: file descriptor
* @size: memory size
* @flags: extra #GstFdMemoryFlags
*
* Return a %GstMemory that wraps a generic file descriptor.
*
* Returns: (transfer full): a GstMemory based on @allocator.
* When the buffer will be released the allocator will close the @fd.
* The memory is only mmapped on gst_buffer_mmap() request.
*
* Since: 1.6
*/
GstMemory *
gst_fd_allocator_alloc (GstAllocator * allocator, gint fd, gsize size,
GstFdMemoryFlags flags) GstFdMemoryFlags flags)
{ {
#ifdef HAVE_MMAP #ifdef HAVE_MMAP
GstFdMemory *mem; GstFdMemory *mem;
g_return_val_if_fail (GST_IS_FD_ALLOCATOR (allocator), NULL);
mem = g_slice_new0 (GstFdMemory); mem = g_slice_new0 (GstFdMemory);
gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator), gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator),
NULL, size, 0, 0, size); NULL, size, 0, 0, size);
@ -201,33 +263,6 @@ gst_fd_allocator_alloc (GstFdAllocator * allocator, gint fd, gsize size,
#endif #endif
} }
G_DEFINE_ABSTRACT_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR);
static void
gst_fd_allocator_class_init (GstFdAllocatorClass * klass)
{
GstAllocatorClass *allocator_class;
allocator_class = (GstAllocatorClass *) klass;
allocator_class->alloc = NULL;
allocator_class->free = gst_fd_mem_free;
klass->alloc = gst_fd_allocator_alloc;
}
static void
gst_fd_allocator_init (GstFdAllocator * allocator)
{
GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
alloc->mem_map = gst_fd_mem_map;
alloc->mem_unmap = gst_fd_mem_unmap;
alloc->mem_share = gst_fd_mem_share;
GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
}
/** /**
* gst_is_fd_memory: * gst_is_fd_memory:
* @mem: #GstMemory * @mem: #GstMemory

View file

@ -28,6 +28,8 @@ G_BEGIN_DECLS
typedef struct _GstFdAllocator GstFdAllocator; typedef struct _GstFdAllocator GstFdAllocator;
typedef struct _GstFdAllocatorClass GstFdAllocatorClass; typedef struct _GstFdAllocatorClass GstFdAllocatorClass;
#define GST_ALLOCATOR_FD "fd"
#define GST_TYPE_FD_ALLOCATOR (gst_fd_allocator_get_type()) #define GST_TYPE_FD_ALLOCATOR (gst_fd_allocator_get_type())
#define GST_IS_FD_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_FD_ALLOCATOR)) #define GST_IS_FD_ALLOCATOR(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_FD_ALLOCATOR))
#define GST_IS_FD_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_FD_ALLOCATOR)) #define GST_IS_FD_ALLOCATOR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_FD_ALLOCATOR))
@ -68,14 +70,14 @@ struct _GstFdAllocator
struct _GstFdAllocatorClass struct _GstFdAllocatorClass
{ {
GstAllocatorClass parent_class; GstAllocatorClass parent_class;
/*< protected >*/
GstMemory * (*alloc) (GstFdAllocator *alloc, gint fd,
gsize size, GstFdMemoryFlags flags);
}; };
GType gst_fd_allocator_get_type (void); GType gst_fd_allocator_get_type (void);
GstAllocator * gst_fd_allocator_new (void);
GstMemory * gst_fd_allocator_alloc (GstAllocator * allocator, gint fd,
gsize size, GstFdMemoryFlags flags);
gboolean gst_is_fd_memory (GstMemory *mem); gboolean gst_is_fd_memory (GstMemory *mem);
gint gst_fd_memory_get_fd (GstMemory *mem); gint gst_fd_memory_get_fd (GstMemory *mem);