Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove reinterpret_casts when assigning ecs_ctx_free_t which were breaking aliasing rules #1416

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
36 changes: 13 additions & 23 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16905,10 +16905,10 @@ namespace _
struct placement_new_tag_t{};
constexpr placement_new_tag_t placement_new_tag{};
template<class Ty> inline void destruct_obj(Ty* _ptr) { _ptr->~Ty(); }
template<class Ty> inline void free_obj(Ty* _ptr) {
template<class Ty> inline void free_obj(void* _ptr) {
if (_ptr) {
destruct_obj(_ptr);
ecs_os_free(_ptr);
destruct_obj(static_cast<Ty *>(_ptr));
ecs_os_free(_ptr);
}
}

Expand Down Expand Up @@ -27461,8 +27461,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_add = Delegate::run_add;
ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_add = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_add = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -27478,8 +27477,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_remove = Delegate::run_remove;
ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_remove = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_remove = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -27495,8 +27493,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_set = Delegate::run_set;
ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_set = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_set = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand Down Expand Up @@ -27548,8 +27545,7 @@ component<T>& constant(const char *name, T value) {
if (!result) {
result = FLECS_NEW(BindingCtx);
h.binding_ctx = result;
h.binding_ctx_free = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<BindingCtx>);
h.binding_ctx_free = _::free_obj<BindingCtx>;
}
return result;
}
Expand Down Expand Up @@ -30372,8 +30368,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.run = Delegate::run;
desc_.run_ctx = ctx;
desc_.run_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.run_ctx_free = _::free_obj<Delegate>;
return T(world_, &desc_);
}

Expand All @@ -30385,8 +30380,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.run = Delegate::run;
desc_.run_ctx = ctx;
desc_.run_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.run_ctx_free = _::free_obj<Delegate>;
return each(FLECS_FWD(each_func));
}

Expand All @@ -30397,8 +30391,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.callback = Delegate::run;
desc_.callback_ctx = ctx;
desc_.callback_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.callback_ctx_free = _::free_obj<Delegate>;
return T(world_, &desc_);
}

Expand Down Expand Up @@ -30615,8 +30608,7 @@ namespace _ {
{
using Delegate = _::entity_observer_delegate<Func>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
}

template <typename Evt, if_not_t<is_empty<Evt>::value> = 0>
Expand All @@ -30627,8 +30619,7 @@ namespace _ {
{
using Delegate = _::entity_payload_observer_delegate<Func, Evt>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
}
};
}
Expand All @@ -30639,8 +30630,7 @@ inline const Self& entity_builder<Self>::observe(flecs::entity_t evt, Func&& f)
using Delegate = _::entity_observer_delegate<Func>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));

_::entity_observer_create(world_, evt, id_, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
_::entity_observer_create(world_, evt, id_, Delegate::run, ctx, _::free_obj<Delegate>);

return to_base();
}
Expand Down
12 changes: 4 additions & 8 deletions include/flecs/addons/cpp/component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_add = Delegate::run_add;
ctx->on_add = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_add = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_add = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -470,8 +469,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_remove = Delegate::run_remove;
ctx->on_remove = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_remove = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_remove = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -487,8 +485,7 @@ struct component : untyped_component {
BindingCtx *ctx = get_binding_ctx(h);
h.on_set = Delegate::run_set;
ctx->on_set = FLECS_NEW(Delegate)(FLECS_FWD(func));
ctx->free_on_set = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<Delegate>);
ctx->free_on_set = _::free_obj<Delegate>;
ecs_set_hooks_id(world_, id_, &h);
return *this;
}
Expand All @@ -505,8 +502,7 @@ struct component : untyped_component {
if (!result) {
result = FLECS_NEW(BindingCtx);
h.binding_ctx = result;
h.binding_ctx_free = reinterpret_cast<ecs_ctx_free_t>(
_::free_obj<BindingCtx>);
h.binding_ctx_free = _::free_obj<BindingCtx>;
}
return result;
}
Expand Down
9 changes: 3 additions & 6 deletions include/flecs/addons/cpp/mixins/event/impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ namespace _ {
{
using Delegate = _::entity_observer_delegate<Func>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
}

template <typename Evt, if_not_t<is_empty<Evt>::value> = 0>
Expand All @@ -64,8 +63,7 @@ namespace _ {
{
using Delegate = _::entity_payload_observer_delegate<Func, Evt>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
entity_observer_create(world, _::type<Evt>::id(world), entity, Delegate::run, ctx, _::free_obj<Delegate>);
}
};
}
Expand All @@ -76,8 +74,7 @@ inline const Self& entity_builder<Self>::observe(flecs::entity_t evt, Func&& f)
using Delegate = _::entity_observer_delegate<Func>;
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(f));

_::entity_observer_create(world_, evt, id_, Delegate::run, ctx,
reinterpret_cast<ecs_ctx_free_t>(_::free_obj<Delegate>));
_::entity_observer_create(world_, evt, id_, Delegate::run, ctx, _::free_obj<Delegate>);

return to_base();
}
Expand Down
9 changes: 3 additions & 6 deletions include/flecs/addons/cpp/utils/node_builder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.run = Delegate::run;
desc_.run_ctx = ctx;
desc_.run_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.run_ctx_free = _::free_obj<Delegate>;
return T(world_, &desc_);
}

Expand All @@ -50,8 +49,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.run = Delegate::run;
desc_.run_ctx = ctx;
desc_.run_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.run_ctx_free = _::free_obj<Delegate>;
return each(FLECS_FWD(each_func));
}

Expand All @@ -62,8 +60,7 @@ struct node_builder : IBuilder<Base, Components ...>
auto ctx = FLECS_NEW(Delegate)(FLECS_FWD(func));
desc_.callback = Delegate::run;
desc_.callback_ctx = ctx;
desc_.callback_ctx_free = reinterpret_cast<
ecs_ctx_free_t>(_::free_obj<Delegate>);
desc_.callback_ctx_free = _::free_obj<Delegate>;
return T(world_, &desc_);
}

Expand Down
6 changes: 3 additions & 3 deletions include/flecs/addons/cpp/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ namespace _
struct placement_new_tag_t{};
constexpr placement_new_tag_t placement_new_tag{};
template<class Ty> inline void destruct_obj(Ty* _ptr) { _ptr->~Ty(); }
template<class Ty> inline void free_obj(Ty* _ptr) {
template<class Ty> inline void free_obj(void* _ptr) {
if (_ptr) {
destruct_obj(_ptr);
ecs_os_free(_ptr);
destruct_obj(static_cast<Ty *>(_ptr));
ecs_os_free(_ptr);
}
}

Expand Down
Loading