Skip to content

Commit 3dd7ce6

Browse files
committed
Set default move hook when only ctor/dtor are specified
1 parent be588d8 commit 3dd7ce6

8 files changed

Lines changed: 299 additions & 41 deletions

File tree

distr/flecs.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20948,13 +20948,23 @@ void flecs_type_info_mark_in_use(
2094820948
#endif
2094920949

2095020950
void flecs_default_ctor(
20951-
void *ptr,
20952-
int32_t count,
20951+
void *ptr,
20952+
int32_t count,
2095320953
const ecs_type_info_t *ti)
2095420954
{
2095520955
ecs_os_memset(ptr, 0, ti->size * count);
2095620956
}
2095720957

20958+
void flecs_default_move(
20959+
void *dst_ptr,
20960+
void *src_ptr,
20961+
int32_t count,
20962+
const ecs_type_info_t *ti)
20963+
{
20964+
ecs_os_memcpy(dst_ptr, src_ptr, ti->size * count);
20965+
ecs_os_memset(src_ptr, 0, ti->size * count);
20966+
}
20967+
2095820968
bool flecs_type_info_ctor(
2095920969
void *ptr,
2096020970
int32_t count,
@@ -21152,14 +21162,6 @@ void flecs_default_move_ctor_w_dtor(void *dst_ptr, void *src_ptr,
2115221162
cl->dtor(src_ptr, count, ti);
2115321163
}
2115421164

21155-
static
21156-
void flecs_default_move(void *dst_ptr, void *src_ptr,
21157-
int32_t count, const ecs_type_info_t *ti)
21158-
{
21159-
const ecs_type_hooks_t *cl = &ti->hooks;
21160-
cl->move(dst_ptr, src_ptr, count, ti);
21161-
}
21162-
2116321165
static
2116421166
void flecs_default_dtor(void *dst_ptr, void *src_ptr,
2116521167
int32_t count, const ecs_type_info_t *ti)
@@ -21424,7 +21426,21 @@ void ecs_set_hooks_id(
2142421426
* ease of use, if no constructor is specified, set a default one that
2142521427
* initializes the component to 0. */
2142621428
if (!h->ctor && (h->dtor || h->copy || h->move)) {
21427-
ti->hooks.ctor = flecs_default_ctor;
21429+
ti->hooks.ctor = flecs_default_ctor;
21430+
}
21431+
21432+
/* If only ctor and dtor are set, default move to flecs_default_move which
21433+
* memcpys src to dst and zeros src. This avoids invoking the dtor on the
21434+
* source after a move. Skip when ctor/dtor/move are flagged illegal: in
21435+
* those cases h->ctor / h->dtor may point to illegal stubs and pairing
21436+
* them with a move is not meaningful. */
21437+
if (h->ctor && h->dtor && !h->move && !h->copy && !h->copy_ctor &&
21438+
!h->move_ctor && !h->move_dtor && !h->ctor_move_dtor &&
21439+
!(flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL |
21440+
ECS_TYPE_HOOK_CTOR_ILLEGAL |
21441+
ECS_TYPE_HOOK_DTOR_ILLEGAL)))
21442+
{
21443+
ti->hooks.move = flecs_default_move;
2142821444
}
2142921445

2143021446
/* Set default copy ctor, move ctor and merge */
@@ -21435,14 +21451,14 @@ void ecs_set_hooks_id(
2143521451
}
2143621452

2143721453
if (!h->move_ctor && !(flags & ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL)) {
21438-
if (h->move) {
21454+
if (ti->hooks.move) {
2143921455
ti->hooks.move_ctor = flecs_default_move_ctor;
2144021456
}
2144121457
}
2144221458

2144321459
if (!h->ctor_move_dtor) {
2144421460
ecs_flags32_t illegal_check = 0;
21445-
if (h->move) {
21461+
if (ti->hooks.move) {
2144621462
illegal_check |= ECS_TYPE_HOOK_MOVE_ILLEGAL;
2144721463
if (h->dtor) {
2144821464
illegal_check |= ECS_TYPE_HOOK_DTOR_ILLEGAL;
@@ -21482,13 +21498,13 @@ void ecs_set_hooks_id(
2148221498

2148321499
if (!h->move_dtor) {
2148421500
ecs_flags32_t illegal_check = 0;
21485-
if (h->move) {
21501+
if (ti->hooks.move) {
2148621502
illegal_check |= ECS_TYPE_HOOK_MOVE_ILLEGAL;
2148721503
if (h->dtor) {
2148821504
illegal_check |= ECS_TYPE_HOOK_DTOR_ILLEGAL;
2148921505
ti->hooks.move_dtor = flecs_default_move_w_dtor;
2149021506
} else {
21491-
ti->hooks.move_dtor = flecs_default_move;
21507+
ti->hooks.move_dtor = ti->hooks.move;
2149221508
}
2149321509
} else {
2149421510
if (h->dtor) {

distr/flecs.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4955,8 +4955,22 @@ char* flecs_module_path_from_c(
49554955
*/
49564956
FLECS_API
49574957
void flecs_default_ctor(
4958-
void *ptr,
4959-
int32_t count,
4958+
void *ptr,
4959+
int32_t count,
4960+
const ecs_type_info_t *type_info);
4961+
4962+
/** Move that memcpys src to dst and zero-initializes src.
4963+
*
4964+
* @param dst_ptr Pointer to the destination value.
4965+
* @param src_ptr Pointer to the source value.
4966+
* @param count Number of elements to move.
4967+
* @param type_info Type info for the component.
4968+
*/
4969+
FLECS_API
4970+
void flecs_default_move(
4971+
void *dst_ptr,
4972+
void *src_ptr,
4973+
int32_t count,
49604974
const ecs_type_info_t *type_info);
49614975

49624976
/* Wrapper functions for invoking type hooks with fallback behavior. */

include/flecs/private/api_support.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,22 @@ char* flecs_module_path_from_c(
4848
*/
4949
FLECS_API
5050
void flecs_default_ctor(
51-
void *ptr,
52-
int32_t count,
51+
void *ptr,
52+
int32_t count,
53+
const ecs_type_info_t *type_info);
54+
55+
/** Move that memcpys src to dst and zero-initializes src.
56+
*
57+
* @param dst_ptr Pointer to the destination value.
58+
* @param src_ptr Pointer to the source value.
59+
* @param count Number of elements to move.
60+
* @param type_info Type info for the component.
61+
*/
62+
FLECS_API
63+
void flecs_default_move(
64+
void *dst_ptr,
65+
void *src_ptr,
66+
int32_t count,
5367
const ecs_type_info_t *type_info);
5468

5569
/* Wrapper functions for invoking type hooks with fallback behavior. */

src/type_info.c

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,23 @@ void flecs_type_info_mark_in_use(
1818
#endif
1919

2020
void flecs_default_ctor(
21-
void *ptr,
22-
int32_t count,
21+
void *ptr,
22+
int32_t count,
2323
const ecs_type_info_t *ti)
2424
{
2525
ecs_os_memset(ptr, 0, ti->size * count);
2626
}
2727

28+
void flecs_default_move(
29+
void *dst_ptr,
30+
void *src_ptr,
31+
int32_t count,
32+
const ecs_type_info_t *ti)
33+
{
34+
ecs_os_memcpy(dst_ptr, src_ptr, ti->size * count);
35+
ecs_os_memset(src_ptr, 0, ti->size * count);
36+
}
37+
2838
bool flecs_type_info_ctor(
2939
void *ptr,
3040
int32_t count,
@@ -222,14 +232,6 @@ void flecs_default_move_ctor_w_dtor(void *dst_ptr, void *src_ptr,
222232
cl->dtor(src_ptr, count, ti);
223233
}
224234

225-
static
226-
void flecs_default_move(void *dst_ptr, void *src_ptr,
227-
int32_t count, const ecs_type_info_t *ti)
228-
{
229-
const ecs_type_hooks_t *cl = &ti->hooks;
230-
cl->move(dst_ptr, src_ptr, count, ti);
231-
}
232-
233235
static
234236
void flecs_default_dtor(void *dst_ptr, void *src_ptr,
235237
int32_t count, const ecs_type_info_t *ti)
@@ -494,7 +496,21 @@ void ecs_set_hooks_id(
494496
* ease of use, if no constructor is specified, set a default one that
495497
* initializes the component to 0. */
496498
if (!h->ctor && (h->dtor || h->copy || h->move)) {
497-
ti->hooks.ctor = flecs_default_ctor;
499+
ti->hooks.ctor = flecs_default_ctor;
500+
}
501+
502+
/* If only ctor and dtor are set, default move to flecs_default_move which
503+
* memcpys src to dst and zeros src. This avoids invoking the dtor on the
504+
* source after a move. Skip when ctor/dtor/move are flagged illegal: in
505+
* those cases h->ctor / h->dtor may point to illegal stubs and pairing
506+
* them with a move is not meaningful. */
507+
if (h->ctor && h->dtor && !h->move && !h->copy && !h->copy_ctor &&
508+
!h->move_ctor && !h->move_dtor && !h->ctor_move_dtor &&
509+
!(flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL |
510+
ECS_TYPE_HOOK_CTOR_ILLEGAL |
511+
ECS_TYPE_HOOK_DTOR_ILLEGAL)))
512+
{
513+
ti->hooks.move = flecs_default_move;
498514
}
499515

500516
/* Set default copy ctor, move ctor and merge */
@@ -505,14 +521,14 @@ void ecs_set_hooks_id(
505521
}
506522

507523
if (!h->move_ctor && !(flags & ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL)) {
508-
if (h->move) {
524+
if (ti->hooks.move) {
509525
ti->hooks.move_ctor = flecs_default_move_ctor;
510526
}
511527
}
512528

513529
if (!h->ctor_move_dtor) {
514530
ecs_flags32_t illegal_check = 0;
515-
if (h->move) {
531+
if (ti->hooks.move) {
516532
illegal_check |= ECS_TYPE_HOOK_MOVE_ILLEGAL;
517533
if (h->dtor) {
518534
illegal_check |= ECS_TYPE_HOOK_DTOR_ILLEGAL;
@@ -552,13 +568,13 @@ void ecs_set_hooks_id(
552568

553569
if (!h->move_dtor) {
554570
ecs_flags32_t illegal_check = 0;
555-
if (h->move) {
571+
if (ti->hooks.move) {
556572
illegal_check |= ECS_TYPE_HOOK_MOVE_ILLEGAL;
557573
if (h->dtor) {
558574
illegal_check |= ECS_TYPE_HOOK_DTOR_ILLEGAL;
559575
ti->hooks.move_dtor = flecs_default_move_w_dtor;
560576
} else {
561-
ti->hooks.move_dtor = flecs_default_move;
577+
ti->hooks.move_dtor = ti->hooks.move;
562578
}
563579
} else {
564580
if (h->dtor) {

test/core/project.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,13 @@
17141714
"has_in_on_add_hook_move",
17151715
"get_in_on_add_hook_new",
17161716
"get_in_on_add_hook_move",
1717-
"get_name_in_on_add_hook_move"
1717+
"get_name_in_on_add_hook_move",
1718+
"default_move_copies_and_zeros_src",
1719+
"default_move_copies_and_zeros_src_count",
1720+
"set_hooks_ctor_dtor_assigns_default_move",
1721+
"set_hooks_ctor_only_no_default_move",
1722+
"set_hooks_ctor_dtor_with_move_keeps_user_move",
1723+
"set_hooks_ctor_dtor_cascades_move_hooks"
17181724
]
17191725
}, {
17201726
"id": "Pairs",

0 commit comments

Comments
 (0)