Browse Source

basic packed struct support

Tobias Waldekranz.com 8 years ago
parent
commit
8f5bb2d00d
2 changed files with 37 additions and 1 deletions
  1. 3 0
      type.c
  2. 34 1
      type_test.c

+ 3 - 0
type.c

180
 	struct tfield *f;
180
 	struct tfield *f;
181
 	ssize_t falign, align = -EINVAL;
181
 	ssize_t falign, align = -EINVAL;
182
 
182
 
183
+	if (t->sou.packed)
184
+		return 1;
185
+
183
 	tfields_foreach(f, t->sou.fields) {
186
 	tfields_foreach(f, t->sou.fields) {
184
 		falign = type_alignof(f->type);
187
 		falign = type_alignof(f->type);
185
 
188
 

+ 34 - 1
type_test.c

51
 	int   b[5];
51
 	int   b[5];
52
 	char  c[3];
52
 	char  c[3];
53
 	short d;
53
 	short d;
54
-} *t1 = NULL;
54
+};
55
 
55
 
56
 static int struct_basic(void *_null)
56
 static int struct_basic(void *_null)
57
 {
57
 {
79
 }
79
 }
80
 UNITTEST(struct_basic);
80
 UNITTEST(struct_basic);
81
 
81
 
82
+struct t1_packed {
83
+	char  a;
84
+	int   b[5];
85
+	char  c[3];
86
+	short d;
87
+} __attribute__((packed));
88
+
89
+static int struct_packed(void *_null)
90
+{
91
+	struct tfield t1_fields[] = {
92
+		{ .name = "a", .type = &t_char },
93
+		{ .name = "b", .type = type_array_of(&t_int, 5) },
94
+		{ .name = "c", .type = type_array_of(&t_char, 3) },
95
+		{ .name = "d", .type = &t_short },
96
+
97
+		{ .type = &t_void }
98
+	};
99
+	struct type t_t1 = {
100
+		.ttype = T_STRUCT,
101
+		.sou = { .name = "t1", .fields = t1_fields, .packed = 1 },
102
+	};
103
+
104
+	unittest_eq(type_offsetof(&t_t1, "a"), offsetof(struct t1_packed, a));
105
+	unittest_eq(type_offsetof(&t_t1, "b"), offsetof(struct t1_packed, b));
106
+	unittest_eq(type_offsetof(&t_t1, "c"), offsetof(struct t1_packed, c));
107
+	unittest_eq(type_offsetof(&t_t1, "d"), offsetof(struct t1_packed, d));
108
+
109
+	unittest_eq(type_alignof(&t_t1), __alignof__(struct t1_packed));
110
+	unittest_eq(type_sizeof(&t_t1), sizeof(struct t1_packed));
111
+	return 0;
112
+}
113
+UNITTEST(struct_packed);
114
+
82
 int main(void)
115
 int main(void)
83
 {
116
 {
84
 	struct unittest_ctx ctx = {
117
 	struct unittest_ctx ctx = {