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 } else if ((mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED)
101 && mem->mmap_count == 0
102 && mprotect (mem->data, gmem->maxsize, prot) == 0) {
103 ret = mem->data;
104 mem->mmapping_flags = prot;
105 mem->mmap_count++;
106 }
107
108 goto out;
109 }
110
111 if (mem->fd != -1) {
112 gint flags;
113
114 flags =
115 (mem->flags & GST_FD_MEMORY_FLAG_MAP_PRIVATE) ? MAP_PRIVATE :
116 MAP_SHARED;
117
118 mem->data = mmap (0, gmem->maxsize, prot, flags, mem->fd, 0);
119 if (mem->data == MAP_FAILED) {
120 GstDebugLevel level;
121 mem->data = NULL;
122
123 switch (errno) {
124 case EACCES:
125 level = GST_LEVEL_INFO;
126 break;
127 default:
128 level = GST_LEVEL_ERROR;
129 break;
130 }
131
132 GST_CAT_LEVEL_LOG (GST_CAT_DEFAULT, level, NULL,
133 "%p: fd %d: mmap failed: %s", mem, mem->fd, g_strerror (errno));
134 goto out;
135 }
136 }
137
138 GST_DEBUG ("%p: fd %d: mapped %p", mem, mem->fd, mem->data);
139
140 if (mem->data) {
141 mem->mmapping_flags = prot;
142 mem->mmap_count++;
143 ret = mem->data;
144 }
145
146 out:
147 g_mutex_unlock (&mem->lock);
148 return ret;
149 #else /* !HAVE_MMAP */
150 return FALSE;
151 #endif
152 }
153
154 static void
gst_fd_mem_unmap(GstMemory * gmem)155 gst_fd_mem_unmap (GstMemory * gmem)
156 {
157 #ifdef HAVE_MMAP
158 GstFdMemory *mem = (GstFdMemory *) gmem;
159
160 if (gmem->parent)
161 return gst_fd_mem_unmap (gmem->parent);
162
163 if (mem->flags & GST_FD_MEMORY_FLAG_KEEP_MAPPED) {
164 g_mutex_lock (&mem->lock);
165 mem->mmap_count--;
166 g_mutex_unlock (&mem->lock);
167 return;
168 }
169
170 g_mutex_lock (&mem->lock);
171 if (mem->data && !(--mem->mmap_count)) {
172 munmap ((void *) mem->data, gmem->maxsize);
173 mem->data = NULL;
174 mem->mmapping_flags = 0;
175 GST_DEBUG ("%p: fd %d unmapped", mem, mem->fd);
176 }
177 g_mutex_unlock (&mem->lock);
178 #endif
179 }
180
181 static GstMemory *
gst_fd_mem_share(GstMemory * gmem,gssize offset,gssize size)182 gst_fd_mem_share (GstMemory * gmem, gssize offset, gssize size)
183 {
184 #ifdef HAVE_MMAP
185 GstFdMemory *mem = (GstFdMemory *) gmem;
186 GstFdMemory *sub;
187 GstMemory *parent;
188
189 GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
190 size);
191
192 /* find the real parent */
193 if ((parent = mem->mem.parent) == NULL)
194 parent = (GstMemory *) mem;
195
196 if (size == -1)
197 size = gmem->maxsize - offset;
198
199 sub = g_slice_new0 (GstFdMemory);
200 /* the shared memory is always readonly */
201 gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
202 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
203 mem->mem.maxsize, mem->mem.align, mem->mem.offset + offset, size);
204
205 sub->fd = mem->fd;
206 g_mutex_init (&sub->lock);
207
208 return GST_MEMORY_CAST (sub);
209 #else /* !HAVE_MMAP */
210 return NULL;
211 #endif
212 }
213
214 G_DEFINE_TYPE (GstFdAllocator, gst_fd_allocator, GST_TYPE_ALLOCATOR);
215
216 static void
gst_fd_allocator_class_init(GstFdAllocatorClass * klass)217 gst_fd_allocator_class_init (GstFdAllocatorClass * klass)
218 {
219 GstAllocatorClass *allocator_class;
220
221 allocator_class = (GstAllocatorClass *) klass;
222
223 allocator_class->alloc = NULL;
224 allocator_class->free = gst_fd_mem_free;
225
226 GST_DEBUG_CATEGORY_INIT (gst_fdmemory_debug, "fdmemory", 0,
227 "GstFdMemory and GstFdAllocator");
228 }
229
230 static void
gst_fd_allocator_init(GstFdAllocator * allocator)231 gst_fd_allocator_init (GstFdAllocator * allocator)
232 {
233 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
234
235 alloc->mem_type = GST_ALLOCATOR_FD;
236
237 alloc->mem_map = gst_fd_mem_map;
238 alloc->mem_unmap = gst_fd_mem_unmap;
239 alloc->mem_share = gst_fd_mem_share;
240
241 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
242 }
243
244 /**
245 * gst_fd_allocator_new:
246 *
247 * Return a new fd allocator.
248 *
249 * Returns: (transfer full): a new fd allocator, or NULL if the allocator
250 * isn't available. Use gst_object_unref() to release the allocator after
251 * usage
252 *
253 * Since: 1.6
254 */
255 GstAllocator *
gst_fd_allocator_new(void)256 gst_fd_allocator_new (void)
257 {
258 GstAllocator *alloc;
259
260 alloc = g_object_new (GST_TYPE_FD_ALLOCATOR, NULL);
261 gst_object_ref_sink (alloc);
262
263 return alloc;
264 }
265
266 /**
267 * gst_fd_allocator_alloc:
268 * @allocator: allocator to be used for this memory
269 * @fd: file descriptor
270 * @size: memory size
271 * @flags: extra #GstFdMemoryFlags
272 *
273 * Return a %GstMemory that wraps a generic file descriptor.
274 *
275 * Returns: (transfer full): a GstMemory based on @allocator.
276 * When the buffer will be released the allocator will close the @fd unless
277 * the %GST_FD_MEMORY_FLAG_DONT_CLOSE flag is specified.
278 * The memory is only mmapped on gst_buffer_map() request.
279 *
280 * Since: 1.6
281 */
282 GstMemory *
gst_fd_allocator_alloc(GstAllocator * allocator,gint fd,gsize size,GstFdMemoryFlags flags)283 gst_fd_allocator_alloc (GstAllocator * allocator, gint fd, gsize size,
284 GstFdMemoryFlags flags)
285 {
286 #ifdef HAVE_MMAP
287 GstFdMemory *mem;
288
289 g_return_val_if_fail (GST_IS_FD_ALLOCATOR (allocator), NULL);
290
291 mem = g_slice_new0 (GstFdMemory);
292 gst_memory_init (GST_MEMORY_CAST (mem), 0, GST_ALLOCATOR_CAST (allocator),
293 NULL, size, 0, 0, size);
294
295 mem->flags = flags;
296 mem->fd = fd;
297 g_mutex_init (&mem->lock);
298
299 GST_DEBUG ("%p: fd: %d size %" G_GSIZE_FORMAT, mem, mem->fd,
300 mem->mem.maxsize);
301
302 return (GstMemory *) mem;
303 #else /* !HAVE_MMAP */
304 return NULL;
305 #endif
306 }
307
308 /**
309 * gst_is_fd_memory:
310 * @mem: #GstMemory
311 *
312 * Check if @mem is memory backed by an fd
313 *
314 * Returns: %TRUE when @mem has an fd that can be retrieved with
315 * gst_fd_memory_get_fd().
316 *
317 * Since: 1.6
318 */
319 gboolean
gst_is_fd_memory(GstMemory * mem)320 gst_is_fd_memory (GstMemory * mem)
321 {
322 g_return_val_if_fail (mem != NULL, FALSE);
323
324 return GST_IS_FD_ALLOCATOR (mem->allocator);
325 }
326
327 /**
328 * gst_fd_memory_get_fd:
329 * @mem: #GstMemory
330 *
331 * Get the fd from @mem. Call gst_is_fd_memory() to check if @mem has
332 * an fd.
333 *
334 * Returns: the fd of @mem or -1 when there is no fd on @mem
335 *
336 * Since: 1.6
337 */
338 gint
gst_fd_memory_get_fd(GstMemory * mem)339 gst_fd_memory_get_fd (GstMemory * mem)
340 {
341 g_return_val_if_fail (mem != NULL, -1);
342 g_return_val_if_fail (GST_IS_FD_ALLOCATOR (mem->allocator), -1);
343
344 return ((GstFdMemory *) mem)->fd;
345 }
346