buffer: fix _resize some more

Add more debug.
Alow resize to 0 bytes.
Do clipping correctly.
Add more unit tests. Also add a failing test: when we resize to 0 and then
try to resize back to the original size it fails because the memory was
removed.
This commit is contained in:
Wim Taymans 2011-07-12 12:00:58 +02:00
parent 8461249f22
commit a5ee20db3f
2 changed files with 25 additions and 5 deletions

View file

@ -820,13 +820,14 @@ gst_buffer_resize (GstBuffer * buffer, gssize offset, gsize size)
gsize bsize, bufsize, bufoffs, bufmax;
GstMemory *mem;
GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSIZE_FORMAT,
buffer, offset, size);
g_return_if_fail (gst_buffer_is_writable (buffer));
bufsize = gst_buffer_get_sizes (buffer, &bufoffs, &bufmax);
GST_CAT_LOG (GST_CAT_BUFFER, "trim %p %" G_GSSIZE_FORMAT "-%" G_GSIZE_FORMAT
" size:%" G_GSIZE_FORMAT " offs:%" G_GSIZE_FORMAT " max:%" G_GSIZE_FORMAT,
buffer, offset, size, bufsize, bufoffs, bufmax);
/* we can't go back further than the current offset or past the end of the
* buffer */
g_return_if_fail ((offset < 0 && bufoffs >= -offset) || (offset >= 0
@ -840,12 +841,13 @@ gst_buffer_resize (GstBuffer * buffer, gssize offset, gsize size)
len = GST_BUFFER_MEM_LEN (buffer);
/* copy and trim */
for (di = si = 0; si < len && size > 0; si++) {
for (di = si = 0; si < len; si++) {
mem = GST_BUFFER_MEM_PTR (buffer, si);
bsize = gst_memory_get_sizes (mem, NULL, NULL);
if ((gssize) bsize <= offset) {
/* remove buffer */
GST_CAT_LOG (GST_CAT_BUFFER, "remove memory %p", mem);
gst_memory_unref (mem);
offset -= bsize;
} else {
@ -857,7 +859,7 @@ gst_buffer_resize (GstBuffer * buffer, gssize offset, gsize size)
else
left = MIN (bsize - offset, size);
if (left) {
if (offset != 0 || left != bsize) {
/* we need to clip something */
if (GST_MEMORY_IS_WRITABLE (mem)) {
gst_memory_resize (mem, offset, left);

View file

@ -515,6 +515,24 @@ GST_START_TEST (test_resize)
fail_unless (offset == 0);
fail_unless (maxsize == maxalloc);
gst_buffer_resize (buf, 0, 0);
size = gst_buffer_get_sizes (buf, &offset, &maxsize);
fail_unless (size == 0);
fail_unless (offset == 0);
fail_unless (maxsize == maxalloc);
gst_buffer_resize (buf, 0, 100);
size = gst_buffer_get_sizes (buf, &offset, &maxsize);
fail_unless (size == 100);
fail_unless (offset == 0);
fail_unless (maxsize == maxalloc);
gst_buffer_resize (buf, 0, 100);
size = gst_buffer_get_sizes (buf, &offset, &maxsize);
fail_unless (size == 100);
fail_unless (offset == 0);
fail_unless (maxsize == maxalloc);
gst_buffer_unref (buf);
}