Skip to content

Commit

Permalink
#1016 allow meta computed alignment to be smaller than actual alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Aug 4, 2023
1 parent e2041a4 commit 61076ac
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 10 deletions.
13 changes: 9 additions & 4 deletions flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -25274,10 +25274,15 @@ int flecs_init_type(
return -1;
}
if (comp->size == size && comp->alignment != alignment) {
ecs_err("computed size for '%s' matches with actual type but "
"alignment is different (%d vs. %d)", ecs_get_name(world, type),
alignment, comp->alignment);
return -1;
if (comp->alignment < alignment) {
ecs_err("computed size for '%s' matches with actual type but "
"alignment is different (%d vs. %d)", ecs_get_name(world, type),
alignment, comp->alignment);
} else {
/* If this is an existing type, the alignment can be larger but
* not smaller than the computed alignment. */
alignment = comp->alignment;
}
}

meta_type->partial = comp->size != size;
Expand Down
13 changes: 9 additions & 4 deletions src/addons/meta/meta.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,15 @@ int flecs_init_type(
return -1;
}
if (comp->size == size && comp->alignment != alignment) {
ecs_err("computed size for '%s' matches with actual type but "
"alignment is different (%d vs. %d)", ecs_get_name(world, type),
alignment, comp->alignment);
return -1;
if (comp->alignment < alignment) {
ecs_err("computed size for '%s' matches with actual type but "
"alignment is different (%d vs. %d)", ecs_get_name(world, type),
alignment, comp->alignment);
} else {
/* If this is an existing type, the alignment can be larger but
* not smaller than the computed alignment. */
alignment = comp->alignment;
}
}

meta_type->partial = comp->size != size;
Expand Down
3 changes: 2 additions & 1 deletion test/meta/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"invalid_warning_range",
"overlapping_error_warning_range",
"overlapping_value_error_range",
"overlapping_value_warning_range"
"overlapping_value_warning_range",
"struct_w_16_alignment"
]
}, {
"id": "NestedStructTypes",
Expand Down
51 changes: 51 additions & 0 deletions test/meta/src/StructTypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,54 @@ void StructTypes_overlapping_value_warning_range() {

ecs_fini(world);
}

void StructTypes_struct_w_16_alignment() {
#ifndef _MSC_VER
typedef __attribute((aligned(16)))
#else
typedef __declspec(align(16))
#endif
struct T {
float x;
float y;
float z;
float w;
} T;

ecs_world_t *world = ecs_init();

ECS_COMPONENT(world, T);

ecs_entity_t t = ecs_struct(world, {
.entity = ecs_id(T),
.members = {
{"x", ecs_id(ecs_f32_t)},
{"y", ecs_id(ecs_f32_t)},
{"z", ecs_id(ecs_f32_t)},
{"w", ecs_id(ecs_f32_t)}
}
});

test_assert(t != 0);
test_str(ecs_get_name(world, t), "T");

meta_test_struct(world, t, T);
meta_test_member(world, t, T, x, ecs_id(ecs_f32_t), 1);
meta_test_member(world, t, T, y, ecs_id(ecs_f32_t), 1);
meta_test_member(world, t, T, z, ecs_id(ecs_f32_t), 1);
meta_test_member(world, t, T, w, ecs_id(ecs_f32_t), 1);

const EcsComponent *cptr = ecs_get(world, t, EcsComponent);
test_assert(cptr != NULL);
test_int(cptr->size, sizeof(T));
test_int(cptr->alignment, 16);

const EcsMetaType *mptr = ecs_get(world, t, EcsMetaType);
test_assert(mptr != NULL);
test_bool(mptr->partial, false);
test_bool(mptr->existing, true);
test_int(mptr->size, sizeof(T));
test_int(mptr->alignment, 16);

ecs_fini(world);
}
7 changes: 6 additions & 1 deletion test/meta/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ void StructTypes_invalid_warning_range(void);
void StructTypes_overlapping_error_warning_range(void);
void StructTypes_overlapping_value_error_range(void);
void StructTypes_overlapping_value_warning_range(void);
void StructTypes_struct_w_16_alignment(void);

// Testsuite 'NestedStructTypes'
void NestedStructTypes_1_bool(void);
Expand Down Expand Up @@ -1345,6 +1346,10 @@ bake_test_case StructTypes_testcases[] = {
{
"overlapping_value_warning_range",
StructTypes_overlapping_value_warning_range
},
{
"struct_w_16_alignment",
StructTypes_struct_w_16_alignment
}
};

Expand Down Expand Up @@ -4679,7 +4684,7 @@ static bake_test_suite suites[] = {
"StructTypes",
NULL,
NULL,
27,
28,
StructTypes_testcases
},
{
Expand Down

0 comments on commit 61076ac

Please sign in to comment.