2 * Copyright (C) 2012 Wim Taymans <wim.taymans@gmail.be>
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.
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.
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include "my-memory.h"
32 _my_alloc (GstAllocator * allocator, gsize size,
33 GstAllocationParams * params, gpointer user_data)
36 gsize maxsize = size + params->prefix + params->padding;
38 GST_DEBUG ("alloc from allocator %p", allocator);
40 mem = g_slice_new (MyMemory);
42 gst_memory_init (GST_MEMORY_CAST (mem), params->flags, allocator, NULL,
43 maxsize, params->align, params->prefix, size);
47 return (GstMemory *) mem;
51 _my_free (GstAllocator * allocator, GstMemory * mem)
53 MyMemory *mmem = (MyMemory *) mem;
56 g_slice_free (MyMemory, mmem);
57 GST_DEBUG ("%p: freed", mmem);
61 _my_mem_map (MyMemory * mem, gsize maxsize, GstMapFlags flags)
66 if ((res = g_atomic_pointer_get (&mem->data)) != NULL)
69 res = g_malloc (maxsize);
71 if (g_atomic_pointer_compare_and_exchange (&mem->data, NULL, res))
77 GST_DEBUG ("%p: mapped %p", mem, res);
83 _my_mem_unmap (MyMemory * mem)
85 GST_DEBUG ("%p: unmapped", mem);
90 _my_mem_share (MyMemory * mem, gssize offset, gsize size)
95 GST_DEBUG ("%p: share %" G_GSSIZE_FORMAT " %" G_GSIZE_FORMAT, mem, offset,
98 /* find the real parent */
99 if ((parent = mem->mem.parent) == NULL)
100 parent = (GstMemory *) mem;
103 size = mem->mem.size - offset;
105 sub = g_slice_new (MyMemory);
106 /* the shared memory is always readonly */
107 gst_memory_init (GST_MEMORY_CAST (sub), GST_MINI_OBJECT_FLAGS (parent) |
108 GST_MINI_OBJECT_FLAG_LOCK_READONLY, mem->mem.allocator, parent,
109 mem->mem.maxsize, mem->mem.align, mem->mem.offset + offset, size);
111 /* install pointer */
112 sub->data = _my_mem_map (mem, mem->mem.maxsize, GST_MAP_READ);
124 GstAllocatorClass parent_class;
125 } MyMemoryAllocatorClass;
127 GType my_memory_allocator_get_type (void);
128 G_DEFINE_TYPE (MyMemoryAllocator, my_memory_allocator, GST_TYPE_ALLOCATOR);
131 my_memory_allocator_class_init (MyMemoryAllocatorClass * klass)
133 GstAllocatorClass *allocator_class;
135 allocator_class = (GstAllocatorClass *) klass;
137 allocator_class->alloc = _my_alloc;
138 allocator_class->free = _my_free;
142 my_memory_allocator_init (MyMemoryAllocator * allocator)
144 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
146 alloc->mem_type = "MyMemory";
147 alloc->mem_map = (GstMemoryMapFunction) _my_mem_map;
148 alloc->mem_unmap = (GstMemoryUnmapFunction) _my_mem_unmap;
149 alloc->mem_share = (GstMemoryShareFunction) _my_mem_share;
153 my_memory_init (void)
155 GstAllocator *allocator;
157 allocator = g_object_new (my_memory_allocator_get_type (), NULL);
159 gst_allocator_register ("MyMemory", allocator);