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)
70 g_mutex_clear (&mem->lock);
71 g_slice_free (GstFdMemory, mem);
72 GST_DEBUG ("%p: freed", mem);
77 gst_fd_mem_map (GstMemory * gmem, gsize maxsize, GstMapFlags flags)
80 GstFdMemory *mem = (GstFdMemory *) gmem;
85 return gst_fd_mem_map (gmem->parent, maxsize, flags);
87 prot = flags & GST_MAP_READ ? PROT_READ : 0;
88 prot |= flags & GST_MAP_WRITE ? PROT_WRITE : 0;
90 g_mutex_lock (&mem->lock);
91 /* do not mmap twice the buffer */
93 /* only return address if mapping flags are a subset
94 * of the previous flags */
95 if ((mem->mmapping_flags & prot) == prot) {
107 (mem->flags & GST_FD_MEMORY_FLAG_MAP_PRIVATE) ? MAP_PRIVATE :
110 mem->data = mmap (0, gmem->maxsize, prot, flags, mem->fd, 0);
111 if (mem->data == MAP_FAILED) {
113 GST_ERROR ("%p: fd %d: mmap failed: %s", mem, mem->fd,
119 GST_DEBUG ("%p: fd %d: mapped %p", mem, mem->fd, mem->data);
122 mem->mmapping_flags = prot;
128 g_mutex_unlock (&mem->lock);
130 #else /* !HAVE_MMAP */
136 gst_fd_mem_unmap (GstMemory * gmem)
139 GstFdMemory *mem = (GstFdMemory *) gmem;
142 return gst_fd_mem_unmap (gmem->parent);
144 if (mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED)
147 g_mutex_lock (&mem->lock);
148 if (mem->data && !(--mem->mmap_count)) {
149 munmap ((void *) mem->data, gmem->maxsize);
151 mem->mmapping_flags = 0;
152 GST_DEBUG ("%p: fd %d unmapped", mem, mem->fd);
154 g_mutex_unlock (&mem->lock);
159 gst_fd_mem_share (GstMemory * gmem, gssize offset, gssize size)
162 GstFdMemory *mem = (GstFdMemory *) gmem;
166 GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
169 /* find the real parent */
170 if ((parent = mem->mem.parent) == NULL)
171 parent = (GstMemory *) mem;
174 size = gmem->maxsize - offset;
176 sub = g_slice_new0 (GstFdMemory);
177 /* the shared memory is always readonly */
178 gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
179 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
180 mem->mem.maxsize, mem->mem.align, mem->mem.offset + offset, size);
183 g_mutex_init (&sub->lock);
185 return GST_MEMORY_CAST (sub);
186 #else /* !HAVE_MMAP */
191 G_DEFINE_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR);
194 gst_fd_allocator_class_init (GstFdAllocatorClass * klass)
196 GstAllocatorClass *allocator_class;
198 allocator_class = (GstAllocatorClass *) klass;
200 allocator_class->alloc = NULL;
201 allocator_class->free = gst_fd_mem_free;
203 GST_DEBUG_CATEGORY_INIT (gst_fdmemory_debug, "fdmemory", 0,
204 "GstFdMemory and GstFdAllocator");
208 gst_fd_allocator_init (GstFdAllocator * allocator)
210 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
212 alloc->mem_type = GST_ALLOCATOR_FD;
214 alloc->mem_map = gst_fd_mem_map;
215 alloc->mem_unmap = gst_fd_mem_unmap;
216 alloc->mem_share = gst_fd_mem_share;
218 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
222 * gst_fd_allocator_new:
224 * Return a new fd allocator.
226 * Returns: (transfer full): a new fd allocator, or NULL if the allocator
227 * isn't available. Use gst_object_unref() to release the allocator after
233 gst_fd_allocator_new (void)
235 return g_object_new (GST_TYPE_FD_ALLOCATOR, NULL);
239 * gst_fd_allocator_alloc:
240 * @allocator: allocator to be used for this memory
241 * @fd: file descriptor
243 * @flags: extra #GstFdMemoryFlags
245 * Return a %GstMemory that wraps a generic file descriptor.
247 * Returns: (transfer full): a GstMemory based on @allocator.
248 * When the buffer will be released the allocator will close the @fd.
249 * The memory is only mmapped on gst_buffer_mmap() request.
254 gst_fd_allocator_alloc (GstAllocator * allocator, gint fd, gsize size,
255 GstFdMemoryFlags flags)
260 g_return_val_if_fail (GST_IS_FD_ALLOCATOR (allocator), NULL);
262 mem = g_slice_new0 (GstFdMemory);
263 gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator),
264 NULL, size, 0, 0, size);
268 g_mutex_init (&mem->lock);
270 GST_DEBUG ("%p: fd: %d size %" G_GSIZE_FORMAT, mem, mem->fd,
273 return (GstMemory *) mem;
274 #else /* !HAVE_MMAP */
283 * Check if @mem is memory backed by an fd
285 * Returns: %TRUE when @mem has an fd that can be retrieved with
286 * gst_fd_memory_get_fd().
291 gst_is_fd_memory (GstMemory * mem)
293 g_return_val_if_fail (mem != NULL, FALSE);
295 return GST_IS_FD_ALLOCATOR (mem->allocator);
299 * gst_fd_memory_get_fd:
302 * Get the fd from @mem. Call gst_is_fd_memory() to check if @mem has
305 * Returns: the fd of @mem or -1 when there is no fd on @mem
310 gst_fd_memory_get_fd (GstMemory * mem)
312 g_return_val_if_fail (mem != NULL, -1);
313 g_return_val_if_fail (GST_IS_FD_ALLOCATOR (mem->allocator), -1);
315 return ((GstFdMemory *) mem)->fd;