Quellcode durchsuchen

basic packed struct support

Tobias Waldekranz.com vor 8 Jahren
Ursprung
Commit
8f5bb2d00d
2 geänderte Dateien mit 37 neuen und 1 gelöschten Zeilen
  1. 3 0
      type.c
  2. 34 1
      type_test.c

+ 3 - 0
type.c

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

+ 34 - 1
type_test.c

@@ -51,7 +51,7 @@ struct t1 {
51 51
 	int   b[5];
52 52
 	char  c[3];
53 53
 	short d;
54
-} *t1 = NULL;
54
+};
55 55
 
56 56
 static int struct_basic(void *_null)
57 57
 {
@@ -79,6 +79,39 @@ static int struct_basic(void *_null)
79 79
 }
80 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 115
 int main(void)
83 116
 {
84 117
 	struct unittest_ctx ctx = {