1 /* GStreamer fd backed memory
2 * Copyright (C) 2013 Linaro SA
3 * Author: Benjamin Gaignard <benjamin.gaignard@linaro.org> for Linaro.
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 /**
22 * SECTION:gstfdmemory
23 * @title: GstFdAllocator
24 * @short_description: Memory wrapper for fd backed memory
25 * @see_also: #GstMemory
26 *
27 * Since: 1.4
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "gstfdmemory.h"
35
36 #ifdef HAVE_MMAP
37 #include <sys/mman.h>
38 #include <unistd.h>
39 #endif
40
41 GST_DEBUG_CATEGORY_STATIC (gst_fdmemory_debug);
42 #define GST_CAT_DEFAULT gst_fdmemory_debug
43
44 typedef struct
45 {
46 GstMemory mem;
47
48 GstFdMemoryFlags flags;
49 gint fd;
50 gpointer data;
51 gint mmapping_flags;
52 gint mmap_count;
53 GMutex lock;
54 } GstFdMemory;
55
56 static void
gst_fd_mem_free(GstAllocator * allocator,GstMemory * gmem)57 gst_fd_mem_free (GstAllocator * allocator, GstMemory * gmem)
58 {
59 #ifdef HAVE_MMAP
60 GstFdMemory *mem = (GstFdMemory *) gmem;
61
62 if (mem->data) {
63 if (!(mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED))
64 g_warning (G_STRLOC ":%s: Freeing memory %p still mapped", G_STRFUNC,
65 mem);
66
67 munmap ((void *) mem->data, gmem->maxsize);
68 }
69 if (mem->fd >= 0 && gmem->parent == NULL
70 && !(mem->flags & GST_FD_MEMORY_FLAG_DONT_CLOSE))
71 close (mem->fd);
72 g_mutex_clear (&mem->lock);
73 g_slice_free (GstFdMemory, mem);
74 GST_DEBUG ("%p: freed", mem);
75 #endif
76 }
77
78 static gpointer
gst_fd_mem_map(GstMemory * gmem,gsize maxsize,GstMapFlags flags)79 gst_fd_mem_map (GstMemory * gmem, gsize maxsize, GstMapFlags flags)
80 {
81 #ifdef HAVE_MMAP
82 GstFdMemory *mem = (GstFdMemory *) gmem;
83 gint prot;
84 gpointer ret = NULL;
85
86 if (gmem->parent)
87 return gst_fd_mem_map (gmem->parent, maxsize, flags);
88
89 prot = flags & GST_MAP_READ ? PROT_READ : 0;
90 prot |= flags & GST_MAP_WRITE ? PROT_WRITE : 0;
91
92 g_mutex_lock (&mem->lock);
93 /* do not mmap twice the buffer */
94 if (mem->data) {
95 /* only return address if mapping flags are a subset
96 * of the previous flags */
97 if ((mem->mmapping_flags & prot) == prot) {
98 ret = mem->data;
99 mem->mmap_count++;
100 }
101
102 goto out;
103 }
104
105 if (mem->fd != -1) {
106 gint flags;
107
108 flags =
109 (mem->flags & GST_FD_MEMORY_FLAG_MAP_PRIVATE) ? MAP_PRIVATE :
110 MAP_SHARED;
111
112 mem->data = mmap (0, gmem->maxsize, prot, flags, mem->fd, 0);
113 if (mem->data == MAP_FAILED) {
114 GstDebugLevel level;
115 mem->data = NULL;
116
117 switch (errno) {
118 case EACCES:
119 level = GST_LEVEL_INFO;
120 break;
121 default:
122 level = GST_LEVEL_ERROR;
123 break;
124 }
125
126 GST_CAT_LEVEL_LOG (GST_CAT_DEFAULT, level, NULL,
127 "%p: fd %d: mmap failed: %s", mem, mem->fd, g_strerror (errno));
128 goto out;
129 }
130 }
131
132 GST_DEBUG ("%p: fd %d: mapped %p", mem, mem->fd, mem->data);
133
134 if (mem->data) {
135 mem->mmapping_flags = prot;
136 mem->mmap_count++;
137 ret = mem->data;
138 }
139
140 out:
141 g_mutex_unlock (&mem->lock);
142 return ret;
143 #else /* !HAVE_MMAP */
144 return FALSE;
145 #endif
146 }
147
148 static void
gst_fd_mem_unmap(GstMemory * gmem)149 gst_fd_mem_unmap (GstMemory * gmem)
150 {
151 #ifdef HAVE_MMAP
152 GstFdMemory *mem = (GstFdMemory *) gmem;
153
154 if (gmem->parent)
155 return gst_fd_mem_unmap (gmem->parent);
156
157 if (mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED)
158 return;
159
160 g_mutex_lock (&mem->lock);
161 if (mem->data && !(--mem->mmap_count)) {
162 munmap ((void *) mem->data, gmem->maxsize);
163 mem->data = NULL;
164 mem->mmapping_flags = 0;
165 GST_DEBUG ("%p: fd %d unmapped", mem, mem->fd);
166 }
167 g_mutex_unlock (&mem->lock);
168 #endif
169 }
170
171 static GstMemory *
gst_fd_mem_share(GstMemory * gmem,gssize offset,gssize size)172 gst_fd_mem_share (GstMemory * gmem, gssize offset, gssize size)
173 {
174 #ifdef HAVE_MMAP
175 GstFdMemory *mem = (GstFdMemory *) gmem;
176 GstFdMemory *sub;
177 GstMemory *parent;
178
179 GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
180 size);
181
182 /* find the real parent */
183 if ((parent = mem->mem.parent) == NULL)
184 parent = (GstMemory *) mem;
185
186 if (size == -1)
187 size = gmem->maxsize - offset;
188
189 sub = g_slice_new0 (GstFdMemory);
190 /* the shared memory is always readonly */
191 gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
192 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
193 mem->mem.maxsize, mem->mem.align, mem->mem.offset + offset, size);
194
195 sub->fd = mem->fd;
196 g_mutex_init (&sub->lock);
197
198 return GST_MEMORY_CAST (sub);
199 #else /* !HAVE_MMAP */
200 return NULL;
201 #endif
202 }
203
204 G_DEFINE_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR);
205
206 static void
gst_fd_allocator_class_init(GstFdAllocatorClass * klass)207 gst_fd_allocator_class_init (GstFdAllocatorClass * klass)
208 {
209 GstAllocatorClass *allocator_class;
210
211 allocator_class = (GstAllocatorClass *) klass;
212
213 allocator_class->alloc = NULL;
214 allocator_class->free = gst_fd_mem_free;
215
216 GST_DEBUG_CATEGORY_INIT (gst_fdmemory_debug, "fdmemory", 0,
217 "GstFdMemory and GstFdAllocator");
218 }
219
220 static void
gst_fd_allocator_init(GstFdAllocator * allocator)221 gst_fd_allocator_init (GstFdAllocator * allocator)
222 {
223 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
224
225 alloc->mem_type = GST_ALLOCATOR_FD;
226
227 alloc->mem_map = gst_fd_mem_map;
228 alloc->mem_unmap = gst_fd_mem_unmap;
229 alloc->mem_share = gst_fd_mem_share;
230
231 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
232 }
233
234 /**
235 * gst_fd_allocator_new:
236 *
237 * Return a new fd allocator.
238 *
239 * Returns: (transfer full): a new fd allocator, or NULL if the allocator
240 * isn't available. Use gst_object_unref() to release the allocator after
241 * usage
242 *
243 * Since: 1.6
244 */
245 GstAllocator *
gst_fd_allocator_new(void)246 gst_fd_allocator_new (void)
247 {
248 GstAllocator *alloc;
249
250 alloc = g_object_new (GST_TYPE_FD_ALLOCATOR, NULL);
251 gst_object_ref_sink (alloc);
252
253 return alloc;
254 }
255
256 /**
257 * gst_fd_allocator_alloc:
258 * @allocator: allocator to be used for this memory
259 * @fd: file descriptor
260 * @size: memory size
261 * @flags: extra #GstFdMemoryFlags
262 *
263 * Return a %GstMemory that wraps a generic file descriptor.
264 *
265 * Returns: (transfer full): a GstMemory based on @allocator.
266 * When the buffer will be released the allocator will close the @fd unless
267 * the %GST_FD_MEMORY_FLAG_DONT_CLOSE flag is specified.
268 * The memory is only mmapped on gst_buffer_map() request.
269 *
270 * Since: 1.6
271 */
272 GstMemory *
gst_fd_allocator_alloc(GstAllocator * allocator,gint fd,gsize size,GstFdMemoryFlags flags)273 gst_fd_allocator_alloc (GstAllocator * allocator, gint fd, gsize size,
274 GstFdMemoryFlags flags)
275 {
276 #ifdef HAVE_MMAP
277 GstFdMemory *mem;
278
279 g_return_val_if_fail (GST_IS_FD_ALLOCATOR (allocator), NULL);
280
281 mem = g_slice_new0 (GstFdMemory);
282 gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator),
283 NULL, size, 0, 0, size);
284
285 mem->flags = flags;
286 mem->fd = fd;
287 g_mutex_init (&mem->lock);
288
289 GST_DEBUG ("%p: fd: %d size %" G_GSIZE_FORMAT, mem, mem->fd,
290 mem->mem.maxsize);
291
292 return (GstMemory *) mem;
293 #else /* !HAVE_MMAP */
294 return NULL;
295 #endif
296 }
297
298 /**
299 * gst_is_fd_memory:
300 * @mem: #GstMemory
301 *
302 * Check if @mem is memory backed by an fd
303 *
304 * Returns: %TRUE when @mem has an fd that can be retrieved with
305 * gst_fd_memory_get_fd().
306 *
307 * Since: 1.6
308 */
309 gboolean
gst_is_fd_memory(GstMemory * mem)310 gst_is_fd_memory (GstMemory * mem)
311 {
312 g_return_val_if_fail (mem != NULL, FALSE);
313
314 return GST_IS_FD_ALLOCATOR (mem->allocator);
315 }
316
317 /**
318 * gst_fd_memory_get_fd:
319 * @mem: #GstMemory
320 *
321 * Get the fd from @mem. Call gst_is_fd_memory() to check if @mem has
322 * an fd.
323 *
324 * Returns: the fd of @mem or -1 when there is no fd on @mem
325 *
326 * Since: 1.6
327 */
328 gint
gst_fd_memory_get_fd(GstMemory * mem)329 gst_fd_memory_get_fd (GstMemory * mem)
330 {
331 g_return_val_if_fail (mem != NULL, -1);
332 g_return_val_if_fail (GST_IS_FD_ALLOCATOR (mem->allocator), -1);
333
334 return ((GstFdMemory *) mem)->fd;
335 }
336