memory: make GstMemory a miniobject

This commit is contained in:
Wim Taymans 2012-06-14 16:30:12 +02:00
parent ec8ac0bb16
commit a1c5b00e72
6 changed files with 87 additions and 116 deletions

View file

@ -214,7 +214,7 @@ _get_merged_memory (GstBuffer * buffer, guint idx, guint length)
if (G_UNLIKELY (_is_span (mem + idx, length, &poffset, &parent))) {
if (parent->flags & GST_MEMORY_FLAG_NO_SHARE) {
if (GST_MEMORY_IS_NO_SHARE (parent)) {
GST_CAT_DEBUG (GST_CAT_PERFORMANCE, "copy for merge %p", parent);
result = gst_memory_copy (parent, poffset, size);
} else {
@ -402,7 +402,7 @@ gst_buffer_copy_into (GstBuffer * dest, GstBuffer * src,
gsize tocopy;
tocopy = MIN (bsize - skip, left);
if (mem->flags & GST_MEMORY_FLAG_NO_SHARE) {
if (GST_MEMORY_IS_NO_SHARE (mem)) {
/* no share, always copy then */
mem = gst_memory_copy (mem, skip, tocopy);
skip = 0;
@ -1118,7 +1118,7 @@ gst_buffer_resize_range (GstBuffer * buffer, guint idx, gint length,
} else {
GstMemory *tmp;
if (mem->flags & GST_MEMORY_FLAG_NO_SHARE)
if (GST_MEMORY_IS_NO_SHARE (mem))
tmp = gst_memory_copy (mem, offset, left);
else
tmp = gst_memory_share (mem, offset, left);

View file

@ -71,14 +71,7 @@
#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);
GST_DEFINE_MINI_OBJECT_TYPE (GstAllocator, gst_allocator);
@ -112,7 +105,6 @@ struct _GstAllocator
typedef struct
{
GstMemory mem;
gsize slice_size;
guint8 *data;
gpointer user_data;
GDestroyNotify notify;
@ -124,6 +116,20 @@ static GstAllocator *_default_allocator;
/* 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,
@ -131,16 +137,21 @@ _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,
slice_size);
mem->mem.mini_object.copy = (GstMiniObjectCopyFunction) _gst_memory_copy;
mem->mem.mini_object.dispose = NULL;
mem->mem.mini_object.free = (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;
mem->mem.align = align;
mem->mem.offset = offset;
mem->mem.size = size;
mem->slice_size = slice_size;
mem->data = data;
mem->user_data = user_data;
mem->notify = notify;
@ -243,7 +254,7 @@ _default_mem_free (GstMemoryDefault * mem)
if (mem->notify)
mem->notify (mem->user_data);
g_slice_free1 (mem->slice_size, mem);
g_slice_free1 (GST_MINI_OBJECT_SIZE (mem), mem);
}
static GstMemoryDefault *
@ -279,7 +290,7 @@ _default_mem_share (GstMemoryDefault * mem, gssize offset, gsize size)
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);
@ -363,11 +374,6 @@ _priv_gst_memory_initialize (void)
(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);
@ -418,60 +424,9 @@ gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data,
_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
@ -484,7 +439,7 @@ gst_memory_is_exclusive (GstMemory * mem)
{
g_return_val_if_fail (mem != NULL, FALSE);
return (g_atomic_int_get (&mem->refcount) == 1);
return GST_MINI_OBJECT_REFCOUNT_VALUE (mem) == 1;
}
/**
@ -737,10 +692,6 @@ gst_memory_copy (GstMemory * mem, gssize offset, gssize size)
copy = mem->allocator->info.mem_copy (mem, offset, size);
#ifndef GST_DISABLE_TRACE
_gst_alloc_trace_new (_gst_memory_trace, copy);
#endif
return copy;
}
@ -768,10 +719,6 @@ gst_memory_share (GstMemory * mem, gssize offset, gssize size)
shared = mem->allocator->info.mem_share (mem, offset, size);
#ifndef GST_DISABLE_TRACE
_gst_alloc_trace_new (_gst_memory_trace, shared);
#endif
return shared;
}
@ -855,10 +802,10 @@ gst_allocator_new (const GstMemoryInfo * info, gpointer user_data,
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 = g_slice_new0 (GstAllocator);
gst_mini_object_init (GST_MINI_OBJECT_CAST (allocator),
gst_allocator_get_type (), sizeof (GstAllocator));
GST_TYPE_ALLOCATOR, sizeof (GstAllocator));
allocator->mini_object.copy = (GstMiniObjectCopyFunction) _gst_allocator_copy;
allocator->mini_object.free = (GstMiniObjectFreeFunction) _gst_allocator_free;
@ -1057,8 +1004,5 @@ gst_allocator_alloc (GstAllocator * allocator, gsize size,
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;
}

View file

@ -66,12 +66,12 @@ GST_EXPORT gsize gst_memory_alignment;
* Flags for wrapped memory.
*/
typedef enum {
GST_MEMORY_FLAG_READONLY = (1 << 0),
GST_MEMORY_FLAG_NO_SHARE = (1 << 1),
GST_MEMORY_FLAG_ZERO_PREFIXED = (1 << 2),
GST_MEMORY_FLAG_ZERO_PADDED = (1 << 3),
GST_MEMORY_FLAG_READONLY = (GST_MINI_OBJECT_FLAG_LAST << 0),
GST_MEMORY_FLAG_NO_SHARE = (GST_MINI_OBJECT_FLAG_LAST << 1),
GST_MEMORY_FLAG_ZERO_PREFIXED = (GST_MINI_OBJECT_FLAG_LAST << 2),
GST_MEMORY_FLAG_ZERO_PADDED = (GST_MINI_OBJECT_FLAG_LAST << 3),
GST_MEMORY_FLAG_LAST = (1 << 16)
GST_MEMORY_FLAG_LAST = (GST_MINI_OBJECT_FLAG_LAST << 16)
} GstMemoryFlags;
/**
@ -80,7 +80,7 @@ typedef enum {
*
* A flags word containing #GstMemoryFlags flags set on @mem
*/
#define GST_MEMORY_FLAGS(mem) (GST_MEMORY_CAST (mem)->flags)
#define GST_MEMORY_FLAGS(mem) GST_MINI_OBJECT_FLAGS (mem)
/**
* GST_MEMORY_FLAG_IS_SET:
* @mem: a #GstMemory.
@ -88,7 +88,7 @@ typedef enum {
*
* Gives the status of a specific flag on a @mem.
*/
#define GST_MEMORY_FLAG_IS_SET(mem,flag) !!(GST_MEMORY_FLAGS (mem) & (flag))
#define GST_MEMORY_FLAG_IS_SET(mem,flag) GST_MINI_OBJECT_FLAG_IS_SET (mem,flag)
/**
* GST_MEMORY_FLAG_UNSET:
* @mem: a #GstMemory.
@ -96,7 +96,7 @@ typedef enum {
*
* Clear a specific flag on a @mem.
*/
#define GST_MEMORY_FLAG_UNSET(mem,flag) (GST_MEMORY_FLAGS (mem) &= ~(flag))
#define GST_MEMORY_FLAG_UNSET(mem,flag) GST_MINI_OBJECT_FLAG_UNSET (mem, flag)
/**
* GST_MEMORY_IS_READONLY:
@ -105,6 +105,13 @@ typedef enum {
* Check if @mem is readonly.
*/
#define GST_MEMORY_IS_READONLY(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_READONLY)
/**
* GST_MEMORY_IS_NO_SHARE:
* @mem: a #GstMemory.
*
* Check if @mem cannot be shared between buffers
*/
#define GST_MEMORY_IS_NO_SHARE(mem) GST_MEMORY_FLAG_IS_SET(mem,GST_MEMORY_FLAG_NO_SHARE)
/**
* GST_MEMORY_IS_ZERO_PREFIXED:
* @mem: a #GstMemory.
@ -123,9 +130,8 @@ typedef enum {
/**
* GstMemory:
* @mini_object: parent structure
* @allocator: pointer to the #GstAllocator
* @flags: memory flags
* @refcount: refcount
* @parent: parent memory block
* @state: private state
* @maxsize: the maximum size allocated
@ -137,10 +143,10 @@ typedef enum {
* as the first member of their structure.
*/
struct _GstMemory {
GstMiniObject mini_object;
GstAllocator *allocator;
GstMemoryFlags flags;
gint refcount;
GstMemory *parent;
volatile gint state;
gsize maxsize;
@ -421,8 +427,39 @@ GstMemory * gst_memory_new_wrapped (GstMemoryFlags flags, gpointer data, gsi
GDestroyNotify notify);
/* refcounting */
GstMemory * gst_memory_ref (GstMemory *mem);
void gst_memory_unref (GstMemory *mem);
/**
* gst_memory_ref:
* @memory: The memory to refcount
*
* Increase the refcount of this memory.
*
* Returns: (transfer full): @memory (for convenience when doing assignments)
*/
#ifdef _FOOL_GTK_DOC_
G_INLINE_FUNC GstMemory * gst_memory_ref (GstMemory * memory);
#endif
static inline GstMemory *
gst_memory_ref (GstMemory * memory)
{
return (GstMemory *) gst_mini_object_ref (GST_MINI_OBJECT_CAST (memory));
}
/**
* gst_memory_unref:
* @memory: (transfer full): the memory to refcount
*
* Decrease the refcount of an memory, freeing it if the refcount reaches 0.
*/
#ifdef _FOOL_GTK_DOC_
G_INLINE_FUNC void gst_memory_unref (GstMemory * memory);
#endif
static inline void
gst_memory_unref (GstMemory * memory)
{
gst_mini_object_unref (GST_MINI_OBJECT_CAST (memory));
}
gboolean gst_memory_is_exclusive (GstMemory *mem);

View file

@ -490,14 +490,6 @@ G_STMT_START { \
#define ASSERT_BUFFER_REFCOUNT(buffer, name, value) \
ASSERT_MINI_OBJECT_REFCOUNT(buffer, name, value)
#define ASSERT_MEMORY_REFCOUNT(memory, name, value) \
G_STMT_START { \
int rc; \
rc = memory->refcount; \
fail_unless (rc == value, \
name " (%p) refcount is %d instead of %d", memory, rc, value); \
} G_STMT_END
#define ASSERT_MINI_OBJECT_REFCOUNT(miniobj, name, value) \
G_STMT_START { \
int rc; \

View file

@ -55,7 +55,7 @@ GST_START_TEST (test_submemory)
fail_unless (sinfo.size == 2, "submemory has wrong size");
fail_unless (memcmp (info.data + 1, sinfo.data, 2) == 0,
"submemory contains the wrong data");
ASSERT_MEMORY_REFCOUNT (sub, "submemory", 1);
ASSERT_MINI_OBJECT_REFCOUNT (sub, "submemory", 1);
gst_memory_unmap (sub, &sinfo);
gst_memory_unref (sub);
@ -66,7 +66,7 @@ GST_START_TEST (test_submemory)
fail_unless (sinfo.size == 0, "submemory has wrong size");
fail_unless (memcmp (info.data + 1, sinfo.data, 0) == 0,
"submemory contains the wrong data");
ASSERT_MEMORY_REFCOUNT (sub, "submemory", 1);
ASSERT_MINI_OBJECT_REFCOUNT (sub, "submemory", 1);
gst_memory_unmap (sub, &sinfo);
gst_memory_unref (sub);
@ -197,11 +197,11 @@ GST_START_TEST (test_copy)
GstMapInfo info, sinfo;
memory = gst_allocator_alloc (NULL, 4, NULL);
ASSERT_MEMORY_REFCOUNT (memory, "memory", 1);
ASSERT_MINI_OBJECT_REFCOUNT (memory, "memory", 1);
copy = gst_memory_copy (memory, 0, -1);
ASSERT_MEMORY_REFCOUNT (memory, "memory", 1);
ASSERT_MEMORY_REFCOUNT (copy, "copy", 1);
ASSERT_MINI_OBJECT_REFCOUNT (memory, "memory", 1);
ASSERT_MINI_OBJECT_REFCOUNT (copy, "copy", 1);
/* memorys are copied and must point to different memory */
fail_if (memory == copy);

View file

@ -518,11 +518,9 @@ EXPORTS
gst_memory_make_mapped
gst_memory_map
gst_memory_new_wrapped
gst_memory_ref
gst_memory_resize
gst_memory_share
gst_memory_unmap
gst_memory_unref
gst_message_get_seqnum
gst_message_get_stream_status_object
gst_message_get_structure