Skip to content

Commit 4a887ed

Browse files
committed
Switch from using move to move_dtor in assign_ptr_w_id function.
See this discord conversation for more background: https://discord.com/channels/633826290415435777/633826290415435781/902342988158623744
1 parent e73abab commit 4a887ed

File tree

4 files changed

+234
-8
lines changed

4 files changed

+234
-8
lines changed

flecs.c

Lines changed: 117 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22379,6 +22379,103 @@ void sys_ctor_init_zero(
2237922379
memset(ptr, 0, size * (size_t)count);
2238022380
}
2238122381

22382+
/* System move */
22383+
static
22384+
void ecs_colsystem_move(
22385+
ecs_world_t *world,
22386+
ecs_entity_t component,
22387+
const ecs_entity_t *dst_entities,
22388+
const ecs_entity_t *src_entities,
22389+
void *dst_ptr,
22390+
void *src_ptr,
22391+
size_t size,
22392+
int32_t count,
22393+
void *ctx)
22394+
{
22395+
(void)world;
22396+
(void)dst_entities;
22397+
(void)src_entities;
22398+
(void)component;
22399+
(void)ctx;
22400+
(void)size;
22401+
22402+
EcsSystem *dst_data = dst_ptr;
22403+
EcsSystem *src_data = src_ptr;
22404+
22405+
int i;
22406+
for (i = 0; i < count; i ++) {
22407+
EcsSystem *dst = &dst_data[i];
22408+
EcsSystem *src = &src_data[i];
22409+
// ecs_entity_t dst_e = dst_entities[i];
22410+
// ecs_entity_t src_e = src_entities[i];
22411+
22412+
// if (!ecs_is_alive(world, dst_e)) {
22413+
// /* This can happen when a set is deferred while a system is being
22414+
// * cleaned up. The operation will be discarded, but the destructor
22415+
// * still needs to be invoked for the value */
22416+
// continue;
22417+
// }
22418+
22419+
// /* Invoke Deactivated action for active systems */
22420+
// if (dst->query && dst->query != src->query && ecs_query_table_count(dst->query)) {
22421+
// invoke_status_action(world, dst_e, dst, EcsSystemDeactivated);
22422+
// }
22423+
22424+
// /* Invoke Disabled action for enabled systems */
22425+
// if (!ecs_has_id(world, dst_e, EcsDisabled)) {
22426+
// invoke_status_action(world, dst_e, dst, EcsSystemDisabled);
22427+
// }
22428+
22429+
if (dst->ctx != src->ctx && dst->ctx_free) {
22430+
dst->ctx_free(dst->ctx);
22431+
}
22432+
22433+
if (dst->status_ctx != src->status_ctx && dst->status_ctx_free) {
22434+
dst->status_ctx_free(dst->status_ctx);
22435+
}
22436+
22437+
if (dst->binding_ctx != src->binding_ctx && dst->binding_ctx_free) {
22438+
dst->binding_ctx_free(dst->binding_ctx);
22439+
}
22440+
22441+
if (dst->query && dst->query != src->query) {
22442+
ecs_query_fini(dst->query);
22443+
}
22444+
22445+
dst->action = src->action;
22446+
dst->entity = src->entity;
22447+
dst->query = src->query;
22448+
dst->status_action = src->status_action;
22449+
dst->tick_source = src->tick_source;
22450+
dst->invoke_count = src->invoke_count;
22451+
dst->time_spent = src->time_spent;
22452+
dst->time_passed = src->time_passed;
22453+
dst->self = src->self;
22454+
dst->ctx = src->ctx;
22455+
dst->status_ctx = src->status_ctx;
22456+
dst->binding_ctx = src->binding_ctx;
22457+
dst->ctx_free = src->ctx_free;
22458+
dst->status_ctx_free = src->status_ctx_free;
22459+
dst->binding_ctx_free = src->binding_ctx_free;
22460+
22461+
src->action = NULL;
22462+
src->entity = 0;
22463+
src->query = NULL;
22464+
src->status_action = NULL;
22465+
src->tick_source = 0;
22466+
src->invoke_count = 0;
22467+
src->time_spent = 0;
22468+
src->time_passed = 0;
22469+
src->self = 0;
22470+
src->ctx = NULL;
22471+
src->status_ctx = NULL;
22472+
src->binding_ctx = NULL;
22473+
src->ctx_free = NULL;
22474+
src->status_ctx_free = NULL;
22475+
src->binding_ctx_free = NULL;
22476+
}
22477+
}
22478+
2238222479
/* System destructor */
2238322480
static
2238422481
void ecs_colsystem_dtor(
@@ -22626,6 +22723,7 @@ void FlecsSystemImport(
2262622723
ecs_set_component_actions_w_id(world, ecs_id(EcsSystem),
2262722724
&(EcsComponentLifecycle) {
2262822725
.ctor = sys_ctor_init_zero,
22726+
.move = ecs_colsystem_move,
2262922727
.dtor = ecs_colsystem_dtor
2263022728
});
2263122729

@@ -22954,6 +23052,20 @@ static ECS_CTOR(EcsPipelineQuery, ptr, {
2295423052
memset(ptr, 0, _size);
2295523053
})
2295623054

23055+
static ECS_MOVE(EcsPipelineQuery, dst, src, {
23056+
ecs_vector_free(dst->ops);
23057+
23058+
dst->query = src->query;
23059+
dst->build_query = src->build_query;
23060+
dst->match_count = src->match_count;
23061+
dst->ops = src->ops;
23062+
23063+
src->query = NULL;
23064+
src->build_query = NULL;
23065+
src->match_count = 0;
23066+
src->ops = NULL;
23067+
})
23068+
2295723069
static ECS_DTOR(EcsPipelineQuery, ptr, {
2295823070
ecs_vector_free(ptr->ops);
2295923071
})
@@ -23661,6 +23773,7 @@ void FlecsPipelineImport(
2366123773
/* Set ctor and dtor for PipelineQuery */
2366223774
ecs_set(world, ecs_id(EcsPipelineQuery), EcsComponentLifecycle, {
2366323775
.ctor = ecs_ctor(EcsPipelineQuery),
23776+
.move = ecs_move(EcsPipelineQuery),
2366423777
.dtor = ecs_dtor(EcsPipelineQuery)
2366523778
});
2366623779

@@ -33928,10 +34041,10 @@ ecs_entity_t assign_ptr_w_id(
3392834041
const ecs_type_info_t *cdata = get_c_info(world, real_id);
3392934042
if (cdata) {
3393034043
if (is_move) {
33931-
ecs_move_t move = cdata->lifecycle.move;
33932-
if (move) {
33933-
move(world, real_id, &entity, &entity, dst, ptr, size, 1,
33934-
cdata->lifecycle.ctx);
34044+
ecs_move_ctor_t move_dtor = cdata->lifecycle.move_dtor;
34045+
if (move_dtor) {
34046+
move_dtor(world, real_id, &cdata->lifecycle, &entity,
34047+
&entity, dst, ptr, size, 1, cdata->lifecycle.ctx);
3393534048
} else {
3393634049
ecs_os_memcpy(dst, ptr, flecs_from_size_t(size));
3393734050
}

src/addons/pipeline/pipeline.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ static ECS_CTOR(EcsPipelineQuery, ptr, {
88
memset(ptr, 0, _size);
99
})
1010

11+
static ECS_MOVE(EcsPipelineQuery, dst, src, {
12+
ecs_vector_free(dst->ops);
13+
14+
dst->query = src->query;
15+
dst->build_query = src->build_query;
16+
dst->match_count = src->match_count;
17+
dst->ops = src->ops;
18+
19+
src->query = NULL;
20+
src->build_query = NULL;
21+
src->match_count = 0;
22+
src->ops = NULL;
23+
})
24+
1125
static ECS_DTOR(EcsPipelineQuery, ptr, {
1226
ecs_vector_free(ptr->ops);
1327
})
@@ -715,6 +729,7 @@ void FlecsPipelineImport(
715729
/* Set ctor and dtor for PipelineQuery */
716730
ecs_set(world, ecs_id(EcsPipelineQuery), EcsComponentLifecycle, {
717731
.ctor = ecs_ctor(EcsPipelineQuery),
732+
.move = ecs_move(EcsPipelineQuery),
718733
.dtor = ecs_dtor(EcsPipelineQuery)
719734
});
720735

src/addons/system/system.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,103 @@ void sys_ctor_init_zero(
309309
memset(ptr, 0, size * (size_t)count);
310310
}
311311

312+
/* System move */
313+
static
314+
void ecs_colsystem_move(
315+
ecs_world_t *world,
316+
ecs_entity_t component,
317+
const ecs_entity_t *dst_entities,
318+
const ecs_entity_t *src_entities,
319+
void *dst_ptr,
320+
void *src_ptr,
321+
size_t size,
322+
int32_t count,
323+
void *ctx)
324+
{
325+
(void)world;
326+
(void)dst_entities;
327+
(void)src_entities;
328+
(void)component;
329+
(void)ctx;
330+
(void)size;
331+
332+
EcsSystem *dst_data = dst_ptr;
333+
EcsSystem *src_data = src_ptr;
334+
335+
int i;
336+
for (i = 0; i < count; i ++) {
337+
EcsSystem *dst = &dst_data[i];
338+
EcsSystem *src = &src_data[i];
339+
// ecs_entity_t dst_e = dst_entities[i];
340+
// ecs_entity_t src_e = src_entities[i];
341+
342+
// if (!ecs_is_alive(world, dst_e)) {
343+
// /* This can happen when a set is deferred while a system is being
344+
// * cleaned up. The operation will be discarded, but the destructor
345+
// * still needs to be invoked for the value */
346+
// continue;
347+
// }
348+
349+
// /* Invoke Deactivated action for active systems */
350+
// if (dst->query && dst->query != src->query && ecs_query_table_count(dst->query)) {
351+
// invoke_status_action(world, dst_e, dst, EcsSystemDeactivated);
352+
// }
353+
354+
// /* Invoke Disabled action for enabled systems */
355+
// if (!ecs_has_id(world, dst_e, EcsDisabled)) {
356+
// invoke_status_action(world, dst_e, dst, EcsSystemDisabled);
357+
// }
358+
359+
if (dst->ctx != src->ctx && dst->ctx_free) {
360+
dst->ctx_free(dst->ctx);
361+
}
362+
363+
if (dst->status_ctx != src->status_ctx && dst->status_ctx_free) {
364+
dst->status_ctx_free(dst->status_ctx);
365+
}
366+
367+
if (dst->binding_ctx != src->binding_ctx && dst->binding_ctx_free) {
368+
dst->binding_ctx_free(dst->binding_ctx);
369+
}
370+
371+
if (dst->query && dst->query != src->query) {
372+
ecs_query_fini(dst->query);
373+
}
374+
375+
dst->action = src->action;
376+
dst->entity = src->entity;
377+
dst->query = src->query;
378+
dst->status_action = src->status_action;
379+
dst->tick_source = src->tick_source;
380+
dst->invoke_count = src->invoke_count;
381+
dst->time_spent = src->time_spent;
382+
dst->time_passed = src->time_passed;
383+
dst->self = src->self;
384+
dst->ctx = src->ctx;
385+
dst->status_ctx = src->status_ctx;
386+
dst->binding_ctx = src->binding_ctx;
387+
dst->ctx_free = src->ctx_free;
388+
dst->status_ctx_free = src->status_ctx_free;
389+
dst->binding_ctx_free = src->binding_ctx_free;
390+
391+
src->action = NULL;
392+
src->entity = 0;
393+
src->query = NULL;
394+
src->status_action = NULL;
395+
src->tick_source = 0;
396+
src->invoke_count = 0;
397+
src->time_spent = 0;
398+
src->time_passed = 0;
399+
src->self = 0;
400+
src->ctx = NULL;
401+
src->status_ctx = NULL;
402+
src->binding_ctx = NULL;
403+
src->ctx_free = NULL;
404+
src->status_ctx_free = NULL;
405+
src->binding_ctx_free = NULL;
406+
}
407+
}
408+
312409
/* System destructor */
313410
static
314411
void ecs_colsystem_dtor(
@@ -556,6 +653,7 @@ void FlecsSystemImport(
556653
ecs_set_component_actions_w_id(world, ecs_id(EcsSystem),
557654
&(EcsComponentLifecycle) {
558655
.ctor = sys_ctor_init_zero,
656+
.move = ecs_colsystem_move,
559657
.dtor = ecs_colsystem_dtor
560658
});
561659

src/entity.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2816,10 +2816,10 @@ ecs_entity_t assign_ptr_w_id(
28162816
const ecs_type_info_t *cdata = get_c_info(world, real_id);
28172817
if (cdata) {
28182818
if (is_move) {
2819-
ecs_move_t move = cdata->lifecycle.move;
2820-
if (move) {
2821-
move(world, real_id, &entity, &entity, dst, ptr, size, 1,
2822-
cdata->lifecycle.ctx);
2819+
ecs_move_ctor_t move_dtor = cdata->lifecycle.move_dtor;
2820+
if (move_dtor) {
2821+
move_dtor(world, real_id, &cdata->lifecycle, &entity,
2822+
&entity, dst, ptr, size, 1, cdata->lifecycle.ctx);
28232823
} else {
28242824
ecs_os_memcpy(dst, ptr, flecs_from_size_t(size));
28252825
}

0 commit comments

Comments
 (0)