fdmemory: add gst_fd_allocator_alloc_full()

Allows allocating FD memory with offset != 0 and size != maxsize.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/8025>
This commit is contained in:
Jakub Adam 2024-12-19 23:05:21 +01:00 committed by GStreamer Marge Bot
parent 601c772447
commit 98bcd041d1
3 changed files with 90 additions and 20 deletions

View File

@ -330,6 +330,43 @@ The memory is only mmapped on gst_buffer_map() request.</doc>
</parameter>
</parameters>
</function>
<function name="alloc_full" c:identifier="gst_fd_allocator_alloc_full" version="1.28">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/allocators/gstfdmemory.c">Return a %GstMemory that wraps a generic file descriptor.</doc>
<source-position filename="../subprojects/gst-plugins-base/gst-libs/gst/allocators/gstfdmemory.h"/>
<return-value transfer-ownership="full" nullable="1">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/allocators/gstfdmemory.c">a GstMemory based on @allocator.
When the buffer will be released the allocator will close the @fd unless
the %GST_FD_MEMORY_FLAG_DONT_CLOSE flag is specified.
The memory is only mmapped on gst_buffer_map() request.</doc>
<type name="Gst.Memory" c:type="GstMemory*"/>
</return-value>
<parameters>
<parameter name="allocator" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/allocators/gstfdmemory.c">allocator to be used for this memory</doc>
<type name="Gst.Allocator" c:type="GstAllocator*"/>
</parameter>
<parameter name="fd" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/allocators/gstfdmemory.c">file descriptor</doc>
<type name="gint" c:type="gint"/>
</parameter>
<parameter name="maxsize" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/allocators/gstfdmemory.c">the total size of the memory represented by @fd</doc>
<type name="gsize" c:type="gsize"/>
</parameter>
<parameter name="offset" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/allocators/gstfdmemory.c">the offset of valid data in the memory</doc>
<type name="gsize" c:type="gsize"/>
</parameter>
<parameter name="size" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/allocators/gstfdmemory.c">the size of valid data in the memory</doc>
<type name="gsize" c:type="gsize"/>
</parameter>
<parameter name="flags" transfer-ownership="none">
<doc xml:space="preserve" filename="../subprojects/gst-plugins-base/gst-libs/gst/allocators/gstfdmemory.c">extra #GstFdMemoryFlags</doc>
<type name="FdMemoryFlags" c:type="GstFdMemoryFlags"/>
</parameter>
</parameters>
</function>
<field name="parent">
<type name="Gst.Allocator" c:type="GstAllocator"/>
</field>

View File

@ -262,6 +262,52 @@ gst_fd_allocator_new (void)
return alloc;
}
/**
* gst_fd_allocator_alloc_full:
* @allocator: allocator to be used for this memory
* @fd: file descriptor
* @maxsize: the total size of the memory represented by @fd
* @offset: the offset of valid data in the memory
* @size: the size of valid data in the memory
* @flags: extra #GstFdMemoryFlags
*
* Return a %GstMemory that wraps a generic file descriptor.
*
* Returns: (transfer full) (nullable): a GstMemory based on @allocator.
* When the buffer will be released the allocator will close the @fd unless
* the %GST_FD_MEMORY_FLAG_DONT_CLOSE flag is specified.
* The memory is only mmapped on gst_buffer_map() request.
*
* Since: 1.28
*/
GstMemory *
gst_fd_allocator_alloc_full (GstAllocator * allocator, gint fd, gsize maxsize,
gsize offset, gsize size, GstFdMemoryFlags flags)
{
#ifdef HAVE_MMAP
GstFdMemory *mem;
g_return_val_if_fail (GST_IS_FD_ALLOCATOR (allocator), NULL);
g_return_val_if_fail (offset + size <= maxsize, NULL);
mem = g_new0 (GstFdMemory, 1);
gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator),
NULL, maxsize, 0, offset, size);
mem->flags = flags;
mem->fd = fd;
g_mutex_init (&mem->lock);
GST_DEBUG ("%p: fd: %d maxsize %" G_GSIZE_FORMAT " offset %" G_GSIZE_FORMAT
" size %" G_GSIZE_FORMAT, mem, mem->fd, mem->mem.maxsize, mem->mem.offset,
mem->mem.size);
return (GstMemory *) mem;
#else /* !HAVE_MMAP */
return NULL;
#endif
}
/**
* gst_fd_allocator_alloc:
* @allocator: allocator to be used for this memory
@ -282,26 +328,7 @@ GstMemory *
gst_fd_allocator_alloc (GstAllocator * allocator, gint fd, gsize size,
GstFdMemoryFlags flags)
{
#ifdef HAVE_MMAP
GstFdMemory *mem;
g_return_val_if_fail (GST_IS_FD_ALLOCATOR (allocator), NULL);
mem = g_new0 (GstFdMemory, 1);
gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator),
NULL, size, 0, 0, size);
mem->flags = flags;
mem->fd = fd;
g_mutex_init (&mem->lock);
GST_DEBUG ("%p: fd: %d size %" G_GSIZE_FORMAT, mem, mem->fd,
mem->mem.maxsize);
return (GstMemory *) mem;
#else /* !HAVE_MMAP */
return NULL;
#endif
return gst_fd_allocator_alloc_full (allocator, fd, size, 0, size, flags);
}
/**

View File

@ -87,6 +87,12 @@ GST_ALLOCATORS_API
GstMemory * gst_fd_allocator_alloc (GstAllocator * allocator, gint fd,
gsize size, GstFdMemoryFlags flags);
GST_ALLOCATORS_API
GstMemory * gst_fd_allocator_alloc_full
(GstAllocator * allocator, gint fd,
gsize maxsize, gsize offset, gsize size,
GstFdMemoryFlags flags);
GST_ALLOCATORS_API
gboolean gst_is_fd_memory (GstMemory *mem);