1 /* GStreamer fd backed memory
2 * Copyright (C) 2013 Linaro SA
3 * Author: Benjamin Gaignard <benjamin.gaignard@linaro.org> for Linaro.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for mordetails.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
23 * @short_description: Memory wrapper for fd backed memory
24 * @see_also: #GstMemory
33 #include "gstfdmemory.h"
40 GST_DEBUG_CATEGORY_STATIC (gst_fdmemory_debug);
41 #define GST_CAT_DEFAULT gst_fdmemory_debug
47 GstFdMemoryFlags flags;
56 gst_fd_mem_free (GstAllocator * allocator, GstMemory * gmem)
59 GstFdMemory *mem = (GstFdMemory *) gmem;
62 if (!(mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED))
63 g_warning (G_STRLOC ":%s: Freeing memory %p still mapped", G_STRFUNC,
66 munmap ((void *) mem->data, gmem->maxsize);
68 if (mem->fd >= 0 && gmem->parent == NULL
69 && !(mem->flags & GST_FD_MEMORY_FLAG_DONT_CLOSE))
71 g_mutex_clear (&mem->lock);
72 g_slice_free (GstFdMemory, mem);
73 GST_DEBUG ("%p: freed", mem);
78 gst_fd_mem_map (GstMemory * gmem, gsize maxsize, GstMapFlags flags)
81 GstFdMemory *mem = (GstFdMemory *) gmem;
86 return gst_fd_mem_map (gmem->parent, maxsize, flags);
88 prot = flags & GST_MAP_READ ? PROT_READ : 0;
89 prot |= flags & GST_MAP_WRITE ? PROT_WRITE : 0;
91 g_mutex_lock (&mem->lock);
92 /* do not mmap twice the buffer */
94 /* only return address if mapping flags are a subset
95 * of the previous flags */
96 if ((mem->mmapping_flags & prot) == prot) {
108 (mem->flags & GST_FD_MEMORY_FLAG_MAP_PRIVATE) ? MAP_PRIVATE :
111 mem->data = mmap (0, gmem->maxsize, prot, flags, mem->fd, 0);
112 if (mem->data == MAP_FAILED) {
114 GST_ERROR ("%p: fd %d: mmap failed: %s", mem, mem->fd,
120 GST_DEBUG ("%p: fd %d: mapped %p", mem, mem->fd, mem->data);
123 mem->mmapping_flags = prot;
129 g_mutex_unlock (&mem->lock);
131 #else /* !HAVE_MMAP */
137 gst_fd_mem_unmap (GstMemory * gmem)
140 GstFdMemory *mem = (GstFdMemory *) gmem;
143 return gst_fd_mem_unmap (gmem->parent);
145 if (mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED)
148 g_mutex_lock (&mem->lock);
149 if (mem->data && !(--mem->mmap_count)) {
150 munmap ((void *) mem->data, gmem->maxsize);
152 mem->mmapping_flags = 0;
153 GST_DEBUG ("%p: fd %d unmapped", mem, mem->fd);
155 g_mutex_unlock (&mem->lock);
160 gst_fd_mem_share (GstMemory * gmem, gssize offset, gssize size)
163 GstFdMemory *mem = (GstFdMemory *) gmem;
167 GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
170 /* find the real parent */
171 if ((parent = mem->mem.parent) == NULL)
172 parent = (GstMemory *) mem;
175 size = gmem->maxsize - offset;
177 sub = g_slice_new0 (GstFdMemory);
178 /* the shared memory is always readonly */
179 gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
180 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
181 mem->mem.maxsize, mem->mem.align, mem->mem.offset + offset, size);
184 g_mutex_init (&sub->lock);
186 return GST_MEMORY_CAST (sub);
187 #else /* !HAVE_MMAP */
192 G_DEFINE_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR);
195 gst_fd_allocator_class_init (GstFdAllocatorClass * klass)
197 GstAllocatorClass *allocator_class;
199 allocator_class = (GstAllocatorClass *) klass;
201 allocator_class->alloc = NULL;
202 allocator_class->free = gst_fd_mem_free;
204 GST_DEBUG_CATEGORY_INIT (gst_fdmemory_debug, "fdmemory", 0,
205 "GstFdMemory and GstFdAllocator");
209 gst_fd_allocator_init (GstFdAllocator * allocator)
211 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
213 alloc->mem_type = GST_ALLOCATOR_FD;
215 alloc->mem_map = gst_fd_mem_map;
216 alloc->mem_unmap = gst_fd_mem_unmap;
217 alloc->mem_share = gst_fd_mem_share;
219 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
223 * gst_fd_allocator_new:
225 * Return a new fd allocator.
227 * Returns: (transfer full): a new fd allocator, or NULL if the allocator
228 * isn't available. Use gst_object_unref() to release the allocator after
234 gst_fd_allocator_new (void)
236 return g_object_new (GST_TYPE_FD_ALLOCATOR, NULL);
240 * gst_fd_allocator_alloc:
241 * @allocator: allocator to be used for this memory
242 * @fd: file descriptor
244 * @flags: extra #GstFdMemoryFlags
246 * Return a %GstMemory that wraps a generic file descriptor.
248 * Returns: (transfer full): a GstMemory based on @allocator.
249 * When the buffer will be released the allocator will close the @fd unless
250 * the %GST_FD_MEMORY_FLAG_DONT_CLOSE flag is specified.
251 * The memory is only mmapped on gst_buffer_mmap() request.
256 gst_fd_allocator_alloc (GstAllocator * allocator, gint fd, gsize size,
257 GstFdMemoryFlags flags)
262 g_return_val_if_fail (GST_IS_FD_ALLOCATOR (allocator), NULL);
264 mem = g_slice_new0 (GstFdMemory);
265 gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator),
266 NULL, size, 0, 0, size);
270 g_mutex_init (&mem->lock);
272 GST_DEBUG ("%p: fd: %d size %" G_GSIZE_FORMAT, mem, mem->fd,
275 return (GstMemory *) mem;
276 #else /* !HAVE_MMAP */
285 * Check if @mem is memory backed by an fd
287 * Returns: %TRUE when @mem has an fd that can be retrieved with
288 * gst_fd_memory_get_fd().
293 gst_is_fd_memory (GstMemory * mem)
295 g_return_val_if_fail (mem != NULL, FALSE);
297 return GST_IS_FD_ALLOCATOR (mem->allocator);
301 * gst_fd_memory_get_fd:
304 * Get the fd from @mem. Call gst_is_fd_memory() to check if @mem has
307 * Returns: the fd of @mem or -1 when there is no fd on @mem
312 gst_fd_memory_get_fd (GstMemory * mem)
314 g_return_val_if_fail (mem != NULL, -1);
315 g_return_val_if_fail (GST_IS_FD_ALLOCATOR (mem->allocator), -1);
317 return ((GstFdMemory *) mem)->fd;