public
Last active

ARM gcc structure alignment

  • Download Gist
test.h
C
1 2 3 4 5 6 7 8 9 10 11
#ifndef TEST_DEFINED
#define TEST_DEFINED
 
#define OFFSET(x, y) &((x *)0)->y
 
typedef unsigned int __u32;
typedef int __s32;
typedef long long __s64;
typedef unsigned char __u8;
 
#endif
test1.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include "test.h"
 
#include <stdio.h>
#include <time.h>
 
struct v4l2_event_vsync {
/* Can be V4L2_FIELD_ANY, _NONE, _TOP or _BOTTOM */
__u8 field;
} __attribute__ ((packed));
 
struct v4l2_event {
__u32 type;
union {
struct v4l2_event_vsync vsync;
__u8 data[64];
} u;
__u32 pending;
__u32 sequence;
struct timespec timestamp;
__u32 reserved[9];
};
 
int main() {
printf("sizeof v4l2_event = %d\n", sizeof(struct v4l2_event));
 
printf("offsets of structure members\n");
printf("offset of \'type\' = %d\n", (int)OFFSET(struct v4l2_event, type));
printf("offset of \'u\' = %d\n", (int)OFFSET(struct v4l2_event, u));
printf("offset of \'pending\' = %d\n", (int)OFFSET(struct v4l2_event, pending));
printf("offset of \'sequence\' = %d\n", (int)OFFSET(struct v4l2_event, sequence));
printf("offset of \'timestamp\' = %d\n", (int)OFFSET(struct v4l2_event, timestamp));
printf("offset of \'reserved\' = %d\n", (int)OFFSET(struct v4l2_event, reserved));
 
return 0;
}
test2.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#include "test.h"
 
#include <stdio.h>
#include <time.h>
 
struct v4l2_event_vsync {
/* Can be V4L2_FIELD_ANY, _NONE, _TOP or _BOTTOM */
__u8 field;
} __attribute__ ((packed));
 
struct v4l2_event_ctrl {
__u32 changes;
__u32 type;
union {
__s32 value;
__s64 value64;
};
__u32 flags;
__s32 minimum;
__s32 maximum;
__s32 step;
__s32 default_value;
};
 
struct v4l2_event_frame_sync {
__u32 frame_sequence;
};
 
struct v4l2_event {
__u32 type;
union {
struct v4l2_event_vsync vsync;
struct v4l2_event_ctrl ctrl;
struct v4l2_event_frame_sync frame_sync;
__u8 data[64];
} u;
__u32 pending;
__u32 sequence;
struct timespec timestamp;
__u32 id;
__u32 reserved[8];
};
 
 
int main() {
printf("sizeof v4l2_event = %d\n", sizeof(struct v4l2_event));
printf("sizeof v4l2_event_ctrl = %d\n", sizeof(struct v4l2_event_ctrl));
 
printf("offsets of structure members\n");
printf("offset of \'type\' = %d\n", (int)OFFSET(struct v4l2_event, type));
printf("offset of \'u\' = %d\n", (int)OFFSET(struct v4l2_event, u));
printf("offset of \'pending\' = %d\n", (int)OFFSET(struct v4l2_event, pending));
printf("offset of \'sequence\' = %d\n", (int)OFFSET(struct v4l2_event, sequence));
printf("offset of \'timestamp\' = %d\n", (int)OFFSET(struct v4l2_event, timestamp));
printf("offset of \'id\' = %d\n", (int)OFFSET(struct v4l2_event, id));
printf("offset of \'reserved\' = %d\n", (int)OFFSET(struct v4l2_event, reserved));
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.