#include "gst_private.h"
#include "gstmemory.h"
-#ifndef GST_DISABLE_TRACE
-#include "gsttrace.h"
-static GstAllocTrace *_gst_memory_trace;
-static GstAllocTrace *_gst_allocator_trace;
-#endif
-
-G_DEFINE_BOXED_TYPE (GstMemory, gst_memory, (GBoxedCopyFunc) gst_memory_ref,
- (GBoxedFreeFunc) gst_memory_unref);
+GST_DEFINE_MINI_OBJECT_TYPE (GstMemory, gst_memory);
-G_DEFINE_BOXED_TYPE (GstAllocator, gst_allocator,
- (GBoxedCopyFunc) gst_allocator_ref, (GBoxedFreeFunc) gst_allocator_unref);
+GST_DEFINE_MINI_OBJECT_TYPE (GstAllocator, gst_allocator);
G_DEFINE_BOXED_TYPE (GstAllocationParams, gst_allocation_params,
(GBoxedCopyFunc) gst_allocation_params_copy,
struct _GstAllocator
{
- gint refcount;
+ GstMiniObject mini_object;
GstMemoryInfo info;
/* our predefined allocators */
static GstAllocator *_default_mem_impl;
+static GstMemory *
+_gst_memory_copy (GstMemory * mem)
+{
+ return gst_memory_copy (mem, 0, -1);
+}
+
+static void
+_gst_memory_free (GstMemory * mem)
+{
+ /* there should be no outstanding mappings */
+ g_return_if_fail (g_atomic_int_get (&mem->state) < 4);
+ mem->allocator->info.mem_free (mem);
+}
+
/* initialize the fields */
static void
_default_mem_init (GstMemoryDefault * mem, GstMemoryFlags flags,
gsize maxsize, gsize offset, gsize size, gsize align,
gpointer user_data, GDestroyNotify notify)
{
+ gst_mini_object_init (GST_MINI_OBJECT_CAST (mem), GST_TYPE_MEMORY,
+ (GstMiniObjectCopyFunction) _gst_memory_copy, NULL,
+ (GstMiniObjectFreeFunction) _gst_memory_free);
+
+ mem->mem.mini_object.flags = flags;
+
mem->mem.allocator = _default_mem_impl;
- mem->mem.flags = flags;
- mem->mem.refcount = 1;
mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
mem->mem.state = (flags & GST_MEMORY_FLAG_READONLY ? 0x1 : 0);
mem->mem.maxsize = maxsize;
}
static gpointer
-_default_mem_map (GstMemoryDefault * mem, GstMapFlags flags)
+_default_mem_map (GstMemoryDefault * mem, gsize maxsize, GstMapFlags flags)
{
return mem->data;
}
copy =
_default_mem_new_block (0, mem->mem.maxsize, 0, mem->mem.offset + offset,
size);
+ GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
+ "memcpy %" G_GSIZE_FORMAT " memory %p -> %p", mem->mem.maxsize, mem,
+ copy);
memcpy (copy->data, mem->data, mem->mem.maxsize);
- GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
return copy;
}
size = mem->mem.size - offset;
sub =
- _default_mem_new (parent->flags, parent, mem->data,
+ _default_mem_new (GST_MINI_OBJECT_FLAGS (parent), parent, mem->data,
mem->mem.maxsize, mem->mem.offset + offset, size, mem->mem.align, NULL,
NULL);
return NULL;
}
+ GST_CAT_DEBUG (GST_CAT_PERFORMANCE,
+ "memcpy %" G_GSSIZE_FORMAT " memory %p -> %p", size, mem, copy);
memcpy (dinfo.data, sinfo.data + offset, size);
- GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy memory %p -> %p", mem, copy);
gst_memory_unmap (copy, &dinfo);
gst_memory_unmap (mem, &sinfo);
(GstMemoryIsSpanFunction) _default_mem_is_span,
};
-#ifndef GST_DISABLE_TRACE
- _gst_memory_trace = _gst_alloc_trace_register ("GstMemory", -1);
- _gst_allocator_trace = _gst_alloc_trace_register ("GstAllocator", -1);
-#endif
-
g_rw_lock_init (&lock);
allocators = g_hash_table_new (g_str_hash, g_str_equal);
_default_mem_new (flags, NULL, data, maxsize, offset, size, 0, user_data,
notify);
-#ifndef GST_DISABLE_TRACE
- _gst_alloc_trace_new (_gst_memory_trace, mem);
-#endif
-
return (GstMemory *) mem;
}
/**
- * gst_memory_ref:
- * @mem: a #GstMemory
- *
- * Increases the refcount of @mem.
- *
- * Returns: @mem with increased refcount
- */
-GstMemory *
-gst_memory_ref (GstMemory * mem)
-{
- g_return_val_if_fail (mem != NULL, NULL);
-
- GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
- mem->refcount + 1);
-
- g_atomic_int_inc (&mem->refcount);
-
- return mem;
-}
-
-/**
- * gst_memory_unref:
- * @mem: a #GstMemory
- *
- * Decreases the refcount of @mem. When the refcount reaches 0, the free
- * function of @mem will be called.
- */
-void
-gst_memory_unref (GstMemory * mem)
-{
- g_return_if_fail (mem != NULL);
- g_return_if_fail (mem->allocator != NULL);
-
- GST_CAT_TRACE (GST_CAT_MEMORY, "memory %p, %d->%d", mem, mem->refcount,
- mem->refcount - 1);
-
- if (g_atomic_int_dec_and_test (&mem->refcount)) {
- /* there should be no outstanding mappings */
- g_return_if_fail (g_atomic_int_get (&mem->state) < 4);
-#ifndef GST_DISABLE_TRACE
- _gst_alloc_trace_free (_gst_memory_trace, mem);
-#endif
- mem->allocator->info.mem_free (mem);
- }
-}
-
-/**
* gst_memory_is_exclusive:
* @mem: a #GstMemory
*
{
g_return_val_if_fail (mem != NULL, FALSE);
- return (g_atomic_int_get (&mem->refcount) == 1);
+ return GST_MINI_OBJECT_REFCOUNT_VALUE (mem) == 1;
}
/**
copy = mem->allocator->info.mem_copy (mem, offset, size);
-#ifndef GST_DISABLE_TRACE
- _gst_alloc_trace_new (_gst_memory_trace, copy);
-#endif
-
return copy;
}
shared = mem->allocator->info.mem_share (mem, offset, size);
-#ifndef GST_DISABLE_TRACE
- _gst_alloc_trace_new (_gst_memory_trace, shared);
-#endif
-
return shared;
}
return TRUE;
}
+static void
+_gst_allocator_free (GstAllocator * allocator)
+{
+ if (allocator->notify)
+ allocator->notify (allocator->user_data);
+
+ g_slice_free1 (sizeof (GstAllocator), allocator);
+}
+
+static GstAllocator *
+_gst_allocator_copy (GstAllocator * allocator)
+{
+ return gst_allocator_ref (allocator);
+}
+
/**
* gst_allocator_new:
* @info: a #GstMemoryInfo
{
GstAllocator *allocator;
-#define INSTALL_FALLBACK(_t) \
- if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
-
g_return_val_if_fail (info != NULL, NULL);
g_return_val_if_fail (info->alloc != NULL, NULL);
g_return_val_if_fail (info->mem_map != NULL, NULL);
g_return_val_if_fail (info->mem_free != NULL, NULL);
g_return_val_if_fail (info->mem_share != NULL, NULL);
- allocator = g_slice_new (GstAllocator);
- allocator->refcount = 1;
+ allocator = g_slice_new0 (GstAllocator);
+
+ gst_mini_object_init (GST_MINI_OBJECT_CAST (allocator), GST_TYPE_ALLOCATOR,
+ (GstMiniObjectCopyFunction) _gst_allocator_copy, NULL,
+ (GstMiniObjectFreeFunction) _gst_allocator_free);
+
allocator->info = *info;
allocator->user_data = user_data;
allocator->notify = notify;
+
+#define INSTALL_FALLBACK(_t) \
+ if (allocator->info._t == NULL) allocator->info._t = _fallback_ ##_t;
INSTALL_FALLBACK (mem_copy);
INSTALL_FALLBACK (mem_is_span);
#undef INSTALL_FALLBACK
GST_CAT_DEBUG (GST_CAT_MEMORY, "new allocator %p", allocator);
-#ifndef GST_DISABLE_TRACE
- _gst_alloc_trace_new (_gst_allocator_trace, allocator);
-#endif
-
return allocator;
}
}
/**
- * gst_allocator_ref:
- * @allocator: a #GstAllocator
- *
- * Increases the refcount of @allocator.
- *
- * Returns: @allocator with increased refcount
- */
-GstAllocator *
-gst_allocator_ref (GstAllocator * allocator)
-{
- g_return_val_if_fail (allocator != NULL, NULL);
-
- GST_CAT_TRACE (GST_CAT_MEMORY, "allocator %p, %d->%d", allocator,
- allocator->refcount, allocator->refcount + 1);
-
- g_atomic_int_inc (&allocator->refcount);
-
- return allocator;
-}
-
-/**
- * gst_allocator_unref:
- * @allocator: a #GstAllocator
- *
- * Decreases the refcount of @allocator. When the refcount reaches 0, the notify
- * function of @allocator will be called and the allocator will be freed.
- */
-void
-gst_allocator_unref (GstAllocator * allocator)
-{
- g_return_if_fail (allocator != NULL);
-
- GST_CAT_TRACE (GST_CAT_MEMORY, "allocator %p, %d->%d", allocator,
- allocator->refcount, allocator->refcount - 1);
-
- if (g_atomic_int_dec_and_test (&allocator->refcount)) {
- if (allocator->notify)
- allocator->notify (allocator->user_data);
-#ifndef GST_DISABLE_TRACE
- _gst_alloc_trace_free (_gst_allocator_trace, allocator);
-#endif
- g_slice_free1 (sizeof (GstAllocator), allocator);
- }
-}
-
-/**
* gst_allocator_register:
* @name: the name of the allocator
* @allocator: (transfer full): #GstAllocator
mem = allocator->info.alloc (allocator, size, params, allocator->user_data);
-#ifndef GST_DISABLE_TRACE
- _gst_alloc_trace_new (_gst_memory_trace, mem);
-#endif
return mem;
}