Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -20948,13 +20948,23 @@ void flecs_type_info_mark_in_use(
#endif

void flecs_default_ctor(
void *ptr,
int32_t count,
void *ptr,
int32_t count,
const ecs_type_info_t *ti)
{
ecs_os_memset(ptr, 0, ti->size * count);
}

void flecs_default_move(
void *dst_ptr,
void *src_ptr,
int32_t count,
const ecs_type_info_t *ti)
{
ecs_os_memcpy(dst_ptr, src_ptr, ti->size * count);
ecs_os_memset(src_ptr, 0, ti->size * count);
}

bool flecs_type_info_ctor(
void *ptr,
int32_t count,
Expand Down Expand Up @@ -21152,14 +21162,6 @@ void flecs_default_move_ctor_w_dtor(void *dst_ptr, void *src_ptr,
cl->dtor(src_ptr, count, ti);
}

static
void flecs_default_move(void *dst_ptr, void *src_ptr,
int32_t count, const ecs_type_info_t *ti)
{
const ecs_type_hooks_t *cl = &ti->hooks;
cl->move(dst_ptr, src_ptr, count, ti);
}

static
void flecs_default_dtor(void *dst_ptr, void *src_ptr,
int32_t count, const ecs_type_info_t *ti)
Expand Down Expand Up @@ -21424,7 +21426,21 @@ void ecs_set_hooks_id(
* ease of use, if no constructor is specified, set a default one that
* initializes the component to 0. */
if (!h->ctor && (h->dtor || h->copy || h->move)) {
ti->hooks.ctor = flecs_default_ctor;
ti->hooks.ctor = flecs_default_ctor;
}

/* If only ctor and dtor are set, default move to flecs_default_move which
* memcpys src to dst and zeros src. This avoids invoking the dtor on the
* source after a move. Skip when ctor/dtor/move are flagged illegal: in
* those cases h->ctor / h->dtor may point to illegal stubs and pairing
* them with a move is not meaningful. */
if (h->ctor && h->dtor && !h->move && !h->copy && !h->copy_ctor &&
!h->move_ctor && !h->move_dtor && !h->ctor_move_dtor &&
!(flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL |
ECS_TYPE_HOOK_CTOR_ILLEGAL |
ECS_TYPE_HOOK_DTOR_ILLEGAL)))
{
ti->hooks.move = flecs_default_move;
}

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

if (!h->move_ctor && !(flags & ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL)) {
if (h->move) {
if (ti->hooks.move) {
ti->hooks.move_ctor = flecs_default_move_ctor;
}
}

if (!h->ctor_move_dtor) {
ecs_flags32_t illegal_check = 0;
if (h->move) {
if (ti->hooks.move) {
illegal_check |= ECS_TYPE_HOOK_MOVE_ILLEGAL;
if (h->dtor) {
illegal_check |= ECS_TYPE_HOOK_DTOR_ILLEGAL;
Expand Down Expand Up @@ -21482,13 +21498,13 @@ void ecs_set_hooks_id(

if (!h->move_dtor) {
ecs_flags32_t illegal_check = 0;
if (h->move) {
if (ti->hooks.move) {
illegal_check |= ECS_TYPE_HOOK_MOVE_ILLEGAL;
if (h->dtor) {
illegal_check |= ECS_TYPE_HOOK_DTOR_ILLEGAL;
ti->hooks.move_dtor = flecs_default_move_w_dtor;
} else {
ti->hooks.move_dtor = flecs_default_move;
ti->hooks.move_dtor = ti->hooks.move;
}
} else {
if (h->dtor) {
Expand Down
18 changes: 16 additions & 2 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4955,8 +4955,22 @@ char* flecs_module_path_from_c(
*/
FLECS_API
void flecs_default_ctor(
void *ptr,
int32_t count,
void *ptr,
int32_t count,
const ecs_type_info_t *type_info);

/** Move that memcpys src to dst and zero-initializes src.
*
* @param dst_ptr Pointer to the destination value.
* @param src_ptr Pointer to the source value.
* @param count Number of elements to move.
* @param type_info Type info for the component.
*/
FLECS_API
void flecs_default_move(
void *dst_ptr,
void *src_ptr,
int32_t count,
const ecs_type_info_t *type_info);

/* Wrapper functions for invoking type hooks with fallback behavior. */
Expand Down
18 changes: 16 additions & 2 deletions include/flecs/private/api_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,22 @@ char* flecs_module_path_from_c(
*/
FLECS_API
void flecs_default_ctor(
void *ptr,
int32_t count,
void *ptr,
int32_t count,
const ecs_type_info_t *type_info);

/** Move that memcpys src to dst and zero-initializes src.
*
* @param dst_ptr Pointer to the destination value.
* @param src_ptr Pointer to the source value.
* @param count Number of elements to move.
* @param type_info Type info for the component.
*/
FLECS_API
void flecs_default_move(
void *dst_ptr,
void *src_ptr,
int32_t count,
const ecs_type_info_t *type_info);

/* Wrapper functions for invoking type hooks with fallback behavior. */
Expand Down
46 changes: 31 additions & 15 deletions src/type_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ void flecs_type_info_mark_in_use(
#endif

void flecs_default_ctor(
void *ptr,
int32_t count,
void *ptr,
int32_t count,
const ecs_type_info_t *ti)
{
ecs_os_memset(ptr, 0, ti->size * count);
}

void flecs_default_move(
void *dst_ptr,
void *src_ptr,
int32_t count,
const ecs_type_info_t *ti)
{
ecs_os_memcpy(dst_ptr, src_ptr, ti->size * count);
ecs_os_memset(src_ptr, 0, ti->size * count);
}

bool flecs_type_info_ctor(
void *ptr,
int32_t count,
Expand Down Expand Up @@ -222,14 +232,6 @@ void flecs_default_move_ctor_w_dtor(void *dst_ptr, void *src_ptr,
cl->dtor(src_ptr, count, ti);
}

static
void flecs_default_move(void *dst_ptr, void *src_ptr,
int32_t count, const ecs_type_info_t *ti)
{
const ecs_type_hooks_t *cl = &ti->hooks;
cl->move(dst_ptr, src_ptr, count, ti);
}

static
void flecs_default_dtor(void *dst_ptr, void *src_ptr,
int32_t count, const ecs_type_info_t *ti)
Expand Down Expand Up @@ -494,7 +496,21 @@ void ecs_set_hooks_id(
* ease of use, if no constructor is specified, set a default one that
* initializes the component to 0. */
if (!h->ctor && (h->dtor || h->copy || h->move)) {
ti->hooks.ctor = flecs_default_ctor;
ti->hooks.ctor = flecs_default_ctor;
}

/* If only ctor and dtor are set, default move to flecs_default_move which
* memcpys src to dst and zeros src. This avoids invoking the dtor on the
* source after a move. Skip when ctor/dtor/move are flagged illegal: in
* those cases h->ctor / h->dtor may point to illegal stubs and pairing
* them with a move is not meaningful. */
if (h->ctor && h->dtor && !h->move && !h->copy && !h->copy_ctor &&
!h->move_ctor && !h->move_dtor && !h->ctor_move_dtor &&
!(flags & (ECS_TYPE_HOOK_MOVE_ILLEGAL |
ECS_TYPE_HOOK_CTOR_ILLEGAL |
ECS_TYPE_HOOK_DTOR_ILLEGAL)))
{
ti->hooks.move = flecs_default_move;
}

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

if (!h->move_ctor && !(flags & ECS_TYPE_HOOK_MOVE_CTOR_ILLEGAL)) {
if (h->move) {
if (ti->hooks.move) {
ti->hooks.move_ctor = flecs_default_move_ctor;
}
}

if (!h->ctor_move_dtor) {
ecs_flags32_t illegal_check = 0;
if (h->move) {
if (ti->hooks.move) {
illegal_check |= ECS_TYPE_HOOK_MOVE_ILLEGAL;
if (h->dtor) {
illegal_check |= ECS_TYPE_HOOK_DTOR_ILLEGAL;
Expand Down Expand Up @@ -552,13 +568,13 @@ void ecs_set_hooks_id(

if (!h->move_dtor) {
ecs_flags32_t illegal_check = 0;
if (h->move) {
if (ti->hooks.move) {
illegal_check |= ECS_TYPE_HOOK_MOVE_ILLEGAL;
if (h->dtor) {
illegal_check |= ECS_TYPE_HOOK_DTOR_ILLEGAL;
ti->hooks.move_dtor = flecs_default_move_w_dtor;
} else {
ti->hooks.move_dtor = flecs_default_move;
ti->hooks.move_dtor = ti->hooks.move;
}
} else {
if (h->dtor) {
Expand Down
8 changes: 7 additions & 1 deletion test/core/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,13 @@
"has_in_on_add_hook_move",
"get_in_on_add_hook_new",
"get_in_on_add_hook_move",
"get_name_in_on_add_hook_move"
"get_name_in_on_add_hook_move",
"default_move_copies_and_zeros_src",
"default_move_copies_and_zeros_src_count",
"set_hooks_ctor_dtor_assigns_default_move",
"set_hooks_ctor_only_no_default_move",
"set_hooks_ctor_dtor_with_move_keeps_user_move",
"set_hooks_ctor_dtor_cascades_move_hooks"
]
}, {
"id": "Pairs",
Expand Down
Loading
Loading