[v3,v4l-utils] common: Use posix_memalign for allocating userptr buffers
Commit Message
When dealing with a userptr pointing to a buffer in userspace,
videobuf2 swaps the corresponding physical pages with other pages
so we have a contiguous area of memory for DMA. This only works if
the userptr is page aligned.
The current way of allocating user buffers using malloc only
guarantees alignment up to `alignof(max_align_t)`, which is usually
16. So replace malloc with posix_memalign to ensure the returned
pointer is on a page boundary.
Signed-off-by: Brandon Cheo Fusi <fusibrandon13@gmail.com>
---
utils/common/v4l-helpers.h | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
@@ -1661,10 +1661,11 @@ static inline int v4l_queue_alloc_bufs(struct v4l_fd *f,
return 0;
for (b = from; b < v4l_queue_g_buffers(q); b++) {
for (p = 0; p < v4l_queue_g_num_planes(q); p++) {
- void *m = malloc(v4l_queue_g_length(q, p));
-
- if (m == NULL)
- return errno;
+ void *m;
+ int ret = posix_memalign(&m, getpagesize(),
+ v4l_queue_g_length(q, p));
+ if (ret)
+ return ret;
v4l_queue_s_userptr(q, b, p, m);
}
}