drivers/video: Keep the old state in case of fail

and ensure the internal state get clear if sz equals zero

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-12-04 23:21:56 +08:00 committed by Petro Karashchenko
parent ae3cadf03c
commit d09f1fb186

View File

@ -46,24 +46,13 @@ static void init_buf_chain(video_framebuff_t *fbuf)
fbuf->vbuf_tail = NULL; fbuf->vbuf_tail = NULL;
tmp = fbuf->vbuf_alloced; tmp = fbuf->vbuf_alloced;
for (i = 0; i < fbuf->container_size - 1; i++) for (i = 1; i < fbuf->container_size; i++)
{ {
tmp->next = &tmp[1]; tmp->next = &tmp[1];
tmp++; tmp++;
} }
} }
static void cleanup_container(video_framebuff_t *fbuf)
{
if (fbuf->vbuf_alloced)
{
memset(fbuf->vbuf_alloced,
0,
sizeof(vbuf_container_t)*fbuf->container_size);
init_buf_chain(fbuf);
}
}
static inline int is_last_one(video_framebuff_t *fbuf) static inline int is_last_one(video_framebuff_t *fbuf)
{ {
return fbuf->vbuf_top == fbuf->vbuf_tail ? 1 : 0; return fbuf->vbuf_top == fbuf->vbuf_tail ? 1 : 0;
@ -110,31 +99,27 @@ void video_framebuff_uninit(video_framebuff_t *fbuf)
int video_framebuff_realloc_container(video_framebuff_t *fbuf, int sz) int video_framebuff_realloc_container(video_framebuff_t *fbuf, int sz)
{ {
vbuf_container_t *vbuf;
if (fbuf->container_size == sz) if (fbuf->container_size == sz)
{ {
return OK; return OK;
} }
if (fbuf->vbuf_alloced != NULL) vbuf = kmm_realloc(fbuf->vbuf_alloced, sizeof(vbuf_container_t) * sz);
if (vbuf != NULL)
{ {
kmm_free(fbuf->vbuf_alloced); memset(vbuf, 0, sizeof(vbuf_container_t) * sz);
fbuf->vbuf_alloced = NULL; }
fbuf->container_size = 0; else if (sz != 0)
{
return -ENOMEM;
} }
if (sz > 0) fbuf->vbuf_alloced = vbuf;
{ fbuf->container_size = sz;
fbuf->vbuf_alloced =
(vbuf_container_t *)kmm_malloc(sizeof(vbuf_container_t) * sz);
if (fbuf->vbuf_alloced == NULL)
{
return -ENOMEM;
}
fbuf->container_size = sz; init_buf_chain(fbuf);
}
cleanup_container(fbuf);
return OK; return OK;
} }