• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.be>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "my-memory.h"
21 
22 typedef struct
23 {
24   GstMemory mem;
25 
26   gpointer data;
27 
28 } MyMemory;
29 
30 
31 static GstMemory *
_my_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)32 _my_alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params)
33 {
34   MyMemory *mem;
35   gsize maxsize = size + params->prefix + params->padding;
36 
37   GST_DEBUG ("alloc from allocator %p", allocator);
38 
39   mem = g_slice_new (MyMemory);
40 
41   gst_memory_init (GST_MEMORY_CAST (mem), params->flags, allocator, NULL,
42       maxsize, params->align, params->prefix, size);
43 
44   mem->data = NULL;
45 
46   return (GstMemory *) mem;
47 }
48 
49 static void
_my_free(GstAllocator * allocator,GstMemory * mem)50 _my_free (GstAllocator * allocator, GstMemory * mem)
51 {
52   MyMemory *mmem = (MyMemory *) mem;
53 
54   g_free (mmem->data);
55   g_slice_free (MyMemory, mmem);
56   GST_DEBUG ("%p: freed", mmem);
57 }
58 
59 static gpointer
_my_mem_map(MyMemory * mem,gsize maxsize,GstMapFlags flags)60 _my_mem_map (MyMemory * mem, gsize maxsize, GstMapFlags flags)
61 {
62   gpointer res;
63 
64   while (TRUE) {
65     if ((res = g_atomic_pointer_get (&mem->data)) != NULL)
66       break;
67 
68     res = g_malloc (maxsize);
69 
70     if (g_atomic_pointer_compare_and_exchange (&mem->data, NULL, res))
71       break;
72 
73     g_free (res);
74   }
75 
76   GST_DEBUG ("%p: mapped %p", mem, res);
77 
78   return res;
79 }
80 
81 static gboolean
_my_mem_unmap(MyMemory * mem)82 _my_mem_unmap (MyMemory * mem)
83 {
84   GST_DEBUG ("%p: unmapped", mem);
85   return TRUE;
86 }
87 
88 static MyMemory *
_my_mem_share(MyMemory * mem,gssize offset,gsize size)89 _my_mem_share (MyMemory * mem, gssize offset, gsize size)
90 {
91   MyMemory *sub;
92   GstMemory *parent;
93 
94   GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
95       size);
96 
97   /* find the real parent */
98   if ((parent = mem->mem.parent) == NULL)
99     parent = (GstMemory *) mem;
100 
101   if (size == -1)
102     size = mem->mem.size - offset;
103 
104   sub = g_slice_new (MyMemory);
105   /* the shared memory is always readonly */
106   gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
107       GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
108       mem->mem.maxsize, mem->mem.align, mem->mem.offset + offset, size);
109 
110   /* install pointer */
111   sub->data = _my_mem_map (mem, mem->mem.maxsize, GST_MAP_READ);
112 
113   return sub;
114 }
115 
116 typedef struct
117 {
118   GstAllocator parent;
119 } MyMemoryAllocator;
120 
121 typedef struct
122 {
123   GstAllocatorClass parent_class;
124 } MyMemoryAllocatorClass;
125 
126 GType my_memory_allocator_get_type (void);
127 G_DEFINE_TYPE (MyMemoryAllocator, my_memory_allocator, GST_TYPE_ALLOCATOR);
128 
129 static void
my_memory_allocator_class_init(MyMemoryAllocatorClass * klass)130 my_memory_allocator_class_init (MyMemoryAllocatorClass * klass)
131 {
132   GstAllocatorClass *allocator_class;
133 
134   allocator_class = (GstAllocatorClass *) klass;
135 
136   allocator_class->alloc = _my_alloc;
137   allocator_class->free = _my_free;
138 }
139 
140 static void
my_memory_allocator_init(MyMemoryAllocator * allocator)141 my_memory_allocator_init (MyMemoryAllocator * allocator)
142 {
143   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
144 
145   alloc->mem_type = "MyMemory";
146   alloc->mem_map = (GstMemoryMapFunction) _my_mem_map;
147   alloc->mem_unmap = (GstMemoryUnmapFunction) _my_mem_unmap;
148   alloc->mem_share = (GstMemoryShareFunction) _my_mem_share;
149 }
150 
151 void
my_memory_init(void)152 my_memory_init (void)
153 {
154   GstAllocator *allocator;
155 
156   allocator = g_object_new (my_memory_allocator_get_type (), NULL);
157 
158   gst_allocator_register ("MyMemory", allocator);
159 }
160