From 5444ca9d5fb255ea35fc60bf1cb27d29135cfbf3 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Wed, 2 Jun 2010 14:18:29 +0200 Subject: Align userspace buffers to a page size boundary Some devices need userspace buffers to be page-aligned. Use the posix_memalign function instead of malloc to allocate page-aligned buffers. Signed-off-by: Laurent Pinchart --- yavta.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/yavta.c b/yavta.c index 3d42915..63d96e3 100644 --- a/yavta.c +++ b/yavta.c @@ -270,6 +270,7 @@ static int video_alloc_buffers(struct device *dev, int nbufs) { struct v4l2_requestbuffers rb; struct v4l2_buffer buf; + int page_size; void **bufmem; unsigned int i; int ret; @@ -291,6 +292,8 @@ static int video_alloc_buffers(struct device *dev, int nbufs) if (bufmem == NULL) return -ENOMEM; + page_size = getpagesize(); + /* Map the buffers. */ for (i = 0; i < rb.count; ++i) { memset(&buf, 0, sizeof buf); @@ -315,9 +318,9 @@ static int video_alloc_buffers(struct device *dev, int nbufs) break; case V4L2_MEMORY_USERPTR: - bufmem[i] = malloc(buf.length); - if (bufmem[i] == NULL) { - printf("Unable to allocate buffer %u (%d)\n", i, errno); + ret = posix_memalign(&bufmem[i], page_size, buf.length); + if (ret < 0) { + printf("Unable to allocate buffer %u (%d)\n", i, ret); return -ENOMEM; } printf("Buffer %u allocated at address %p.\n", i, bufmem[i]); -- cgit v1.2.3