memory: more improvements

This commit is contained in:
Wim Taymans 2011-03-20 11:42:39 +01:00
parent b35c0df940
commit dd74a925c9
2 changed files with 59 additions and 39 deletions

View file

@ -51,8 +51,9 @@ _default_mem_init (GstMemoryDefault * mem, GstMemory * parent,
gsize slice_size, gpointer data, GFreeFunc free_func, gsize slice_size, gpointer data, GFreeFunc free_func,
gsize maxsize, gsize offset, gsize size) gsize maxsize, gsize offset, gsize size)
{ {
mem->mem.refcount = 1;
mem->mem.impl = data ? _default_mem_impl : _default_sub_impl; mem->mem.impl = data ? _default_mem_impl : _default_sub_impl;
mem->mem.flags = 0;
mem->mem.refcount = 1;
mem->mem.parent = parent ? gst_memory_ref (parent) : NULL; mem->mem.parent = parent ? gst_memory_ref (parent) : NULL;
mem->slice_size = slice_size; mem->slice_size = slice_size;
mem->data = data; mem->data = data;
@ -67,14 +68,42 @@ _default_mem_new (GstMemory * parent, gpointer data,
GFreeFunc free_func, gsize maxsize, gsize offset, gsize size) GFreeFunc free_func, gsize maxsize, gsize offset, gsize size)
{ {
GstMemoryDefault *mem; GstMemoryDefault *mem;
gsize slice_size;
mem = g_slice_new (GstMemoryDefault); slice_size = sizeof (GstMemoryDefault);
_default_mem_init (mem, parent, sizeof (GstMemoryDefault),
mem = g_slice_alloc (slice_size);
_default_mem_init (mem, parent, slice_size,
data, free_func, maxsize, offset, size); data, free_func, maxsize, offset, size);
return mem; return mem;
} }
static GstMemoryDefault *
_default_mem_new_block (gsize maxsize, gsize align, gsize offset, gsize size)
{
GstMemoryDefault *mem;
gsize aoffset, slice_size;
guint8 *data;
/* alloc header and data in one block */
slice_size = sizeof (GstMemoryDefault) + maxsize + align;
mem = g_slice_alloc (slice_size);
if (mem == NULL)
return NULL;
data = (guint8 *) mem + sizeof (GstMemoryDefault);
if ((aoffset = ((guintptr) data & align)))
aoffset = align - aoffset;
_default_mem_init (mem, NULL, slice_size, data, NULL, maxsize + align,
aoffset + offset, size);
return mem;
}
static gsize static gsize
_default_mem_get_sizes (GstMemoryDefault * mem, gsize * maxsize) _default_mem_get_sizes (GstMemoryDefault * mem, gsize * maxsize)
{ {
@ -158,11 +187,9 @@ static GstMemoryDefault *
_default_mem_copy (GstMemoryDefault * mem) _default_mem_copy (GstMemoryDefault * mem)
{ {
GstMemoryDefault *copy; GstMemoryDefault *copy;
gpointer data;
data = g_memdup (mem->data, mem->maxsize); copy = _default_mem_new_block (mem->maxsize, 0, mem->offset, mem->size);
copy = _default_mem_new (NULL, data, g_free, mem->maxsize, memcpy (copy->data + copy->offset, mem->data, mem->maxsize);
mem->offset, mem->size);
return copy; return copy;
} }
@ -222,16 +249,13 @@ _default_mem_span (GstMemoryDefault * mem1, gsize offset,
span = span =
_default_mem_sub (parent, mem1->offset - parent->offset + offset, size); _default_mem_sub (parent, mem1->offset - parent->offset + offset, size);
} else { } else {
guint8 *data;
gsize len1; gsize len1;
data = g_malloc (size);
len1 = mem1->size - offset; len1 = mem1->size - offset;
memcpy (data, mem1->data + mem1->offset + offset, len1); span = _default_mem_new_block (size, 0, 0, size);
memcpy (data + len1, mem2->data + mem2->offset, size - len1); memcpy (span->data, mem1->data + mem1->offset + offset, len1);
memcpy (span->data + len1, mem2->data + mem2->offset, size - len1);
span = _default_mem_new (NULL, data, g_free, size, 0, size);
} }
return span; return span;
} }
@ -251,15 +275,14 @@ static GstMemory *
_fallback_copy (GstMemory * mem) _fallback_copy (GstMemory * mem)
{ {
GstMemoryDefault *copy; GstMemoryDefault *copy;
gpointer data, cdata; gpointer data;
gsize size; gsize size;
data = gst_memory_map (mem, &size, NULL, GST_MAP_READ); data = gst_memory_map (mem, &size, NULL, GST_MAP_READ);
cdata = g_memdup (data, size); copy = _default_mem_new_block (size, 0, 0, size);
memcpy (copy->data, data, size);
gst_memory_unmap (mem, data, size); gst_memory_unmap (mem, data, size);
copy = _default_mem_new (NULL, cdata, g_free, size, 0, size);
return (GstMemory *) copy; return (GstMemory *) copy;
} }
@ -287,22 +310,20 @@ static GstMemory *
_fallback_span (GstMemory * mem1, gsize offset, GstMemory * mem2, gsize size) _fallback_span (GstMemory * mem1, gsize offset, GstMemory * mem2, gsize size)
{ {
GstMemoryDefault *span; GstMemoryDefault *span;
guint8 *data, *dest; guint8 *data;
gsize ssize, len1; gsize ssize, len1;
dest = g_malloc (size); span = _default_mem_new_block (size, 0, 0, size);
data = gst_memory_map (mem1, &ssize, NULL, GST_MAP_READ); data = gst_memory_map (mem1, &ssize, NULL, GST_MAP_READ);
len1 = ssize - offset; len1 = ssize - offset;
memcpy (dest, data + offset, len1); memcpy (span->data, data + offset, len1);
gst_memory_unmap (mem1, data, size); gst_memory_unmap (mem1, data, size);
data = gst_memory_map (mem2, &ssize, NULL, GST_MAP_READ); data = gst_memory_map (mem2, &ssize, NULL, GST_MAP_READ);
memcpy (dest + len1, data, ssize - len1); memcpy (span->data + len1, data, ssize - len1);
gst_memory_unmap (mem2, data, size); gst_memory_unmap (mem2, data, size);
span = _default_mem_new (NULL, dest, g_free, size, 0, size);
return (GstMemory *) span; return (GstMemory *) span;
} }
@ -485,23 +506,20 @@ GstMemory *
gst_memory_new_alloc (gsize maxsize, gsize align) gst_memory_new_alloc (gsize maxsize, gsize align)
{ {
GstMemoryDefault *mem; GstMemoryDefault *mem;
guint8 *data;
gsize offset, size;
/* alloc header and data in one block */ mem = _default_mem_new_block (maxsize, align, 0, 0);
size = sizeof (GstMemoryDefault) + maxsize + align;
return (GstMemory *) mem;
mem = g_slice_alloc (size); }
if (mem == NULL)
return NULL; GstMemory *
gst_memory_new_copy (gsize maxsize, gsize align, gpointer data,
data = (guint8 *) mem + sizeof (GstMemoryDefault); gsize offset, gsize size)
{
if ((offset = ((guintptr) data & align))) GstMemoryDefault *mem;
offset = align - offset;
mem = _default_mem_new_block (maxsize, align, offset, size);
_default_mem_init (mem, NULL, size, data, NULL, maxsize + align, offset, memcpy (mem->data, data, maxsize);
maxsize);
return (GstMemory *) mem; return (GstMemory *) mem;
} }

View file

@ -126,6 +126,8 @@ GstMemory * gst_memory_span (GstMemory *mem1, gsize offset,
GstMemory * gst_memory_new_wrapped (gpointer data, GFreeFunc free_func, GstMemory * gst_memory_new_wrapped (gpointer data, GFreeFunc free_func,
gsize maxsize, gsize offset, gsize size); gsize maxsize, gsize offset, gsize size);
GstMemory * gst_memory_new_alloc (gsize maxsize, gsize align); GstMemory * gst_memory_new_alloc (gsize maxsize, gsize align);
GstMemory * gst_memory_new_copy (gsize maxsize, gsize align, gpointer data,
gsize offset, gsize size);
const GstMemoryImpl * gst_memory_register (const gchar *impl, const GstMemoryInfo *info); const GstMemoryImpl * gst_memory_register (const gchar *impl, const GstMemoryInfo *info);