mp_image: make ref error handling slightly readable

I think this is slightly more readable than this repeated "fail |= !".
This commit is contained in:
wm4 2018-03-01 14:11:42 +01:00 committed by Jan Ekström
parent 55c88fdb8f
commit 39e03f6072
1 changed files with 9 additions and 10 deletions

View File

@ -300,14 +300,13 @@ void mp_image_unref_data(struct mp_image *img)
}
}
static bool ref_buffer(AVBufferRef **dst)
static void ref_buffer(bool *ok, AVBufferRef **dst)
{
if (*dst) {
*dst = av_buffer_ref(*dst);
if (!*dst)
return false;
ok = false;
}
return true;
}
// Return a new reference to img. The returned reference is owned by the caller,
@ -324,20 +323,20 @@ struct mp_image *mp_image_new_ref(struct mp_image *img)
talloc_set_destructor(new, mp_image_destructor);
*new = *img;
bool fail = false;
bool ok = true;
for (int p = 0; p < MP_MAX_PLANES; p++)
fail |= !ref_buffer(&new->bufs[p]);
ref_buffer(&ok, &new->bufs[p]);
fail |= !ref_buffer(&new->hwctx);
fail |= !ref_buffer(&new->icc_profile);
fail |= !ref_buffer(&new->a53_cc);
ref_buffer(&ok, &new->hwctx);
ref_buffer(&ok, &new->icc_profile);
ref_buffer(&ok, &new->a53_cc);
new->ff_side_data = talloc_memdup(NULL, new->ff_side_data,
new->num_ff_side_data * sizeof(new->ff_side_data[0]));
for (int n = 0; n < new->num_ff_side_data; n++)
fail |= !ref_buffer(&new->ff_side_data[n].buf);
ref_buffer(&ok, &new->ff_side_data[n].buf);
if (!fail)
if (ok)
return new;
// Do this after _all_ bufs were changed; we don't want it to free bufs