diff options
author | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2014-11-18 22:52:06 +0200 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2016-05-24 03:03:01 +0300 |
commit | c9601d407a7ca943e53fa49a5a5f69b868c6ed11 (patch) | |
tree | 248d13a9e817ba3c73fc50e2d655b4cd12013e10 /lib/xlnx-buffers.c | |
parent | 3653f5e4d877176a30035d9123ac65daf46ba787 (diff) |
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'lib/xlnx-buffers.c')
-rw-r--r-- | lib/xlnx-buffers.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/xlnx-buffers.c b/lib/xlnx-buffers.c new file mode 100644 index 0000000..34f873a --- /dev/null +++ b/lib/xlnx-buffers.c @@ -0,0 +1,80 @@ +/* + * Xilinx Video Library - Buffers Pool + * + * Copyright (C) 2014-2016 Ideas on board Oy + * + * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com> + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 2.1 of the License, or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + */ + +#include <stdlib.h> +#include <string.h> + +#include "xlnx-buffers.h" + +struct xlnx_video_buffers_pool *xlnx_video_buffers_pool_new(unsigned int nbufs) +{ + struct xlnx_video_buffer *buffers; + struct xlnx_video_buffers_pool *pool; + unsigned int i; + + pool = malloc(sizeof *pool); + if (pool == NULL) + return NULL; + + buffers = malloc(sizeof *buffers * nbufs); + if (buffers == NULL) { + free(pool); + return NULL; + } + + memset(buffers, 0, sizeof *buffers * nbufs); + + for (i = 0; i < nbufs; ++i) + buffers[i].index = i; + + pool->nbufs = nbufs; + pool->buffers = buffers; + + return pool; +} + +void xlnx_video_buffers_pool_delete(struct xlnx_video_buffers_pool *pool) +{ + unsigned int i; + + for (i = 0; i < pool->nbufs; ++i) { + if (pool->buffers[i].allocated) + free(pool->buffers[i].mem); + } + + free(pool->buffers); + free(pool); +} + +int xlnx_video_buffers_pool_alloc(struct xlnx_video_buffers_pool *pool, + size_t size, size_t align) +{ + unsigned int i; + int ret; + + for (i = 0; i < pool->nbufs; ++i) { + ret = posix_memalign(&pool->buffers[i].mem, align, size); + if (ret != 0) + return -ret; + + pool->buffers[i].size = size; + pool->buffers[i].allocated = true; + } + + return 0; +} |