2 * Copyright (C) 2011 Wim Taymans <wim.taymans@gmail.be>
4 * gstmemory.c: memory block handling
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gst_private.h"
26 #include "gstmemory.h"
46 static const GstMemoryImpl *_default_mem_impl;
47 static const GstMemoryImpl *_default_sub_impl;
50 _default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
51 GstMemory * parent, gsize slice_size, gpointer data,
52 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
54 mem->mem.impl = data ? _default_mem_impl : _default_sub_impl;
55 mem->mem.flags = flags;
56 mem->mem.refcount = 1;
57 mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
58 mem->slice_size = slice_size;
60 mem->free_func = free_func;
61 mem->maxsize = maxsize;
66 static GstMemoryDefault *
67 _default_mem_new (GstMemoryFlags flags, GstMemory * parent, gpointer data,
68 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
70 GstMemoryDefault *mem;
73 slice_size = sizeof (GstMemoryDefault);
75 mem = g_slice_alloc (slice_size);
76 _default_mem_init (mem, flags, parent, slice_size,
77 data, free_func, maxsize, offset, size);
82 static GstMemoryDefault *
83 _default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
85 GstMemoryDefault *mem;
86 gsize aoffset, slice_size;
89 /* alloc header and data in one block */
90 slice_size = sizeof (GstMemoryDefault) + maxsize + align;
92 mem = g_slice_alloc (slice_size);
96 data = (guint8 *) mem + sizeof (GstMemoryDefault);
98 if ((aoffset = ((guintptr) data & align)))
99 aoffset = align - aoffset;
101 _default_mem_init (mem, 0, NULL, slice_size, data, NULL, maxsize + align,
102 aoffset + offset, size);
108 _default_mem_get_sizes (GstMemoryDefault * mem, gsize * maxsize)
111 *maxsize = mem->maxsize;
117 _default_mem_trim (GstMemoryDefault * mem, gsize offset, gsize size)
119 g_return_if_fail (size + mem->offset + offset <= mem->maxsize);
121 mem->offset += offset;
126 _default_mem_map (GstMemoryDefault * mem, gsize * size, gsize * maxsize,
132 *maxsize = mem->maxsize;
134 return mem->data + mem->offset;
138 _default_sub_map (GstMemoryDefault * mem, gsize * size, gsize * maxsize,
143 data = gst_memory_map (mem->mem.parent, size, maxsize, flags);
148 *maxsize -= mem->offset;
150 return data + mem->offset;
154 _default_mem_unmap (GstMemoryDefault * mem, gpointer data, gsize size)
161 _default_sub_unmap (GstMemoryDefault * mem, gpointer data, gsize size)
169 gst_memory_unmap (mem->mem.parent, ptr - mem->offset, size + mem->offset);
175 _default_mem_free (GstMemoryDefault * mem)
178 gst_memory_unref (mem->mem.parent);
181 mem->free_func (mem->data);
183 g_slice_free1 (mem->slice_size, mem);
186 static GstMemoryDefault *
187 _default_mem_copy (GstMemoryDefault * mem, gsize offset, gsize size)
189 GstMemoryDefault *copy;
192 size = mem->size > offset ? mem->size - offset : 0;
193 copy = _default_mem_new_block (mem->maxsize, 0, mem->offset + offset, size);
194 memcpy (copy->data, mem->data, mem->maxsize);
199 static GstMemoryDefault *
200 _default_mem_sub (GstMemoryDefault * mem, gsize offset, gsize size)
202 GstMemoryDefault *sub;
205 /* find the real parent */
206 if ((parent = mem->mem.parent) == NULL)
207 parent = (GstMemory *) mem;
210 size = mem->size - offset;
212 sub = _default_mem_new (parent->flags, parent, mem->data, NULL, mem->maxsize,
213 mem->offset + offset, size);
219 _default_mem_is_span (GstMemoryDefault * mem1, GstMemoryDefault * mem2,
223 *offset = mem1->offset;
225 /* and memory is contiguous */
226 return mem1->data + mem1->offset + mem1->size == mem2->data + mem2->offset;
230 _fallback_copy (GstMemory * mem, gsize offset, gsize size)
232 GstMemoryDefault *copy;
236 data = gst_memory_map (mem, &msize, NULL, GST_MAP_READ);
238 size = msize > offset ? msize - offset : 0;
239 copy = _default_mem_new_block (size, 0, 0, size);
240 memcpy (copy->data, data + offset, size);
241 gst_memory_unmap (mem, data, msize);
243 return (GstMemory *) copy;
247 _fallback_sub (GstMemory * mem, gsize offset, gsize size)
249 GstMemoryDefault *sub;
252 /* find the real parent */
253 parent = mem->parent ? mem->parent : mem;
255 sub = _default_mem_new (0, parent, NULL, NULL, size, offset, size);
257 return (GstMemory *) sub;
261 _fallback_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
267 _gst_memory_init (void)
269 static const GstMemoryInfo _mem_info = {
270 (GstMemoryGetSizesFunction) _default_mem_get_sizes,
271 (GstMemoryTrimFunction) _default_mem_trim,
272 (GstMemoryMapFunction) _default_mem_map,
273 (GstMemoryUnmapFunction) _default_mem_unmap,
274 (GstMemoryFreeFunction) _default_mem_free,
275 (GstMemoryCopyFunction) _default_mem_copy,
276 (GstMemorySubFunction) _default_mem_sub,
277 (GstMemoryIsSpanFunction) _default_mem_is_span
279 static const GstMemoryInfo _sub_info = {
280 (GstMemoryGetSizesFunction) _default_mem_get_sizes,
281 (GstMemoryTrimFunction) _default_mem_trim,
282 (GstMemoryMapFunction) _default_sub_map,
283 (GstMemoryUnmapFunction) _default_sub_unmap,
284 (GstMemoryFreeFunction) _default_mem_free,
290 _default_mem_impl = gst_memory_register ("GstMemoryDefault", &_mem_info);
291 _default_sub_impl = gst_memory_register ("GstMemorySubbuffer", &_sub_info);
295 * gst_memory_register:
301 const GstMemoryImpl *
302 gst_memory_register (const gchar * name, const GstMemoryInfo * info)
306 #define INSTALL_FALLBACK(_t) \
307 if (impl->info._t == NULL) impl->info._t = _fallback_ ##_t;
309 g_return_val_if_fail (name != NULL, NULL);
310 g_return_val_if_fail (info != NULL, NULL);
311 g_return_val_if_fail (info->get_sizes != NULL, NULL);
312 g_return_val_if_fail (info->trim != NULL, NULL);
313 g_return_val_if_fail (info->map != NULL, NULL);
314 g_return_val_if_fail (info->unmap != NULL, NULL);
315 g_return_val_if_fail (info->free != NULL, NULL);
317 impl = g_slice_new (GstMemoryImpl);
318 impl->name = g_quark_from_string (name);
320 INSTALL_FALLBACK (copy);
321 INSTALL_FALLBACK (sub);
322 INSTALL_FALLBACK (is_span);
324 GST_DEBUG ("register \"%s\" of size %" G_GSIZE_FORMAT, name);
327 g_static_rw_lock_writer_lock (&lock);
328 g_hash_table_insert (memoryimpl, (gpointer) name, (gpointer) impl);
329 g_static_rw_lock_writer_unlock (&lock);
331 #undef INSTALL_FALLBACK
337 gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
338 GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
340 GstMemoryDefault *mem;
342 mem = _default_mem_new (flags, NULL, data, free_func, maxsize, offset, size);
344 return (GstMemory *) mem;
348 gst_memory_new_alloc (gsize maxsize, gsize align)
350 GstMemoryDefault *mem;
352 mem = _default_mem_new_block (maxsize, align, 0, maxsize);
354 return (GstMemory *) mem;
358 gst_memory_new_copy (gsize maxsize, gsize align, gpointer data,
359 gsize offset, gsize size)
361 GstMemoryDefault *mem;
363 mem = _default_mem_new_block (maxsize, align, offset, size);
364 memcpy (mem->data, data, maxsize);
366 return (GstMemory *) mem;
373 * Returns: @mem with increased refcount
376 gst_memory_ref (GstMemory * mem)
378 g_return_val_if_fail (mem != NULL, NULL);
380 g_atomic_int_inc (&mem->refcount);
390 gst_memory_unref (GstMemory * mem)
392 g_return_if_fail (mem != NULL);
393 g_return_if_fail (mem->impl != NULL);
395 if (g_atomic_int_dec_and_test (&mem->refcount))
396 mem->impl->info.free (mem);
400 * gst_memory_get_sizes:
402 * @maxsize: pointer to maxsize
404 * Returns: the current size of @mem
407 gst_memory_get_sizes (GstMemory * mem, gsize * maxsize)
409 g_return_val_if_fail (mem != NULL, 0);
411 return mem->impl->info.get_sizes (mem, maxsize);
415 gst_memory_map (GstMemory * mem, gsize * size, gsize * maxsize,
418 g_return_val_if_fail (mem != NULL, NULL);
420 return mem->impl->info.map (mem, size, maxsize, flags);
424 gst_memory_unmap (GstMemory * mem, gpointer data, gsize size)
426 g_return_val_if_fail (mem != NULL, FALSE);
428 return mem->impl->info.unmap (mem, data, size);
432 gst_memory_copy (GstMemory * mem, gsize offset, gsize size)
434 g_return_val_if_fail (mem != NULL, NULL);
436 return mem->impl->info.copy (mem, offset, size);
440 gst_memory_trim (GstMemory * mem, gsize offset, gsize size)
442 g_return_if_fail (mem != NULL);
444 mem->impl->info.trim (mem, offset, size);
448 gst_memory_sub (GstMemory * mem, gsize offset, gsize size)
450 g_return_val_if_fail (mem != NULL, NULL);
452 return mem->impl->info.sub (mem, offset, size);
456 gst_memory_is_span (GstMemory * mem1, GstMemory * mem2, gsize * offset)
458 g_return_val_if_fail (mem1 != NULL, FALSE);
459 g_return_val_if_fail (mem2 != NULL, FALSE);
461 /* need to have the same implementation */
462 if (mem1->impl != mem2->impl)
465 /* need to have the same parent */
466 if (mem1->parent == NULL || mem1->parent != mem2->parent)
469 /* and memory is contiguous */
470 if (!mem1->impl->info.is_span (mem1, mem2, offset))