Tuesday, February 26, 2008

sizeof(struct v4l2_requestbuffers) different in kernel and in user compiled program

$ANDROID_M5_KERNEL/kernel/include/linux/videodev2.h:560
struct v4l2_requestbuffers
{
__u32 count;
enum v4l2_buf_type type;
enum v4l2_memory memory;
__u32 reserved[2];
};
The sizeof(struct v4l2_requestbuffers) is 16 bytes running in kernel, while 20 bytes running in user compiled native program. I checked the structure size in detail and found that the sizeof(enum v4l2_buf_type) and sizeof(enum v4l2_memory) are both 1. The actual size of this structure is 4 + 1 + 1 + 8 = 14 bytes. The two continuous enum occupy 4 bytes for DWORD alignment.

In user compiled native program, the sizeof(enum) is 4. Total sizeof(struct) is 20 bytes. Only the size will not cause big problems. But the variable VIDIOC_REQBUFS will be affected by the size of the structure. See following _IOWR definition.
#define _IOWR(type,nr,size) _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
That causes the VIDIOC_REQBUFS can't be matched in the kernel code.

To fix the problem, change the structure to the following:
struct v4l2_requestbuffers
{
__u32 count;
enum v4l2_buf_type type;
__u32 reserved[2];
enum v4l2_memory memory;
};
For DWORD alignment, both enum will occupy 4 bytes although each one is 1 byte long in kernel.

2 comments:

rtm said...

the short enum problem discussed on http://groups.google.com/group/android-porting/browse_thread/thread/95d1719173a8099a/5253078ae988bd47?#5253078ae988bd47

sasha said...

In a few short years, the Google Android operating system has gained immense foothold in the smartphone market.

best android apps

best iphone appssu