Skip to content

Commit 9c0c662

Browse files
committed
Context: add small helper to improve readability
Add term_pid_or_port_from_context() helper, rather than: if (ctx->native_handler) port ... else process ... Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 011006e commit 9c0c662

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/libAtomVM/context.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,15 @@ void context_destroy(Context *ctx)
248248
free(ctx);
249249
}
250250

251+
static inline term term_pid_or_port_from_context(const Context *ctx)
252+
{
253+
if (ctx->native_handler != NULL) {
254+
return term_port_from_local_process_id(ctx->process_id);
255+
} else {
256+
return term_from_local_process_id(ctx->process_id);
257+
}
258+
}
259+
251260
void context_process_kill_signal(Context *ctx, struct TermSignal *signal)
252261
{
253262
// exit_reason is one of the roots when garbage collecting
@@ -589,11 +598,7 @@ static struct ResourceContextMonitor *context_monitors_handle_terminate(Context
589598
// Prepare the message on ctx's heap which will be freed afterwards.
590599
term info_tuple = term_alloc_tuple(3, &ctx->heap);
591600
term_put_tuple_element(info_tuple, 0, EXIT_ATOM);
592-
if (ctx->native_handler != NULL) {
593-
term_put_tuple_element(info_tuple, 1, term_port_from_local_process_id(ctx->process_id));
594-
} else {
595-
term_put_tuple_element(info_tuple, 1, term_from_local_process_id(ctx->process_id));
596-
}
601+
term_put_tuple_element(info_tuple, 1, term_pid_or_port_from_context(ctx));
597602
term_put_tuple_element(info_tuple, 2, ctx->exit_reason);
598603
mailbox_send_term_signal(target, LinkExitSignal, info_tuple);
599604
}
@@ -616,16 +621,15 @@ static struct ResourceContextMonitor *context_monitors_handle_terminate(Context
616621
// Prepare the message on ctx's heap which will be freed afterwards.
617622
term ref = term_from_ref_ticks(monitored_monitor->ref_ticks, &ctx->heap);
618623

624+
term port_or_process = term_pid_or_port_from_context(ctx);
625+
term port_or_process_atom
626+
= term_is_local_port(port_or_process) ? PORT_ATOM : PROCESS_ATOM;
627+
619628
term info_tuple = term_alloc_tuple(5, &ctx->heap);
620629
term_put_tuple_element(info_tuple, 0, DOWN_ATOM);
621630
term_put_tuple_element(info_tuple, 1, ref);
622-
if (ctx->native_handler != NULL) {
623-
term_put_tuple_element(info_tuple, 2, PORT_ATOM);
624-
term_put_tuple_element(info_tuple, 3, term_port_from_local_process_id(ctx->process_id));
625-
} else {
626-
term_put_tuple_element(info_tuple, 2, PROCESS_ATOM);
627-
term_put_tuple_element(info_tuple, 3, term_from_local_process_id(ctx->process_id));
628-
}
631+
term_put_tuple_element(info_tuple, 2, port_or_process_atom);
632+
term_put_tuple_element(info_tuple, 3, port_or_process);
629633
term_put_tuple_element(info_tuple, 4, ctx->exit_reason);
630634

631635
mailbox_send_term_signal(target, MonitorDownSignal, info_tuple);
@@ -770,12 +774,7 @@ void context_ack_unlink(Context *ctx, term link_pid, uint64_t unlink_id, bool pr
770774
target = globalcontext_get_process_lock(ctx->global, local_process_id);
771775
}
772776
if (LIKELY(target)) {
773-
term self_pid;
774-
if (ctx->native_handler != NULL) {
775-
self_pid = term_port_from_local_process_id(ctx->process_id);
776-
} else {
777-
self_pid = term_from_local_process_id(ctx->process_id);
778-
}
777+
term self_pid = term_pid_or_port_from_context(ctx);
779778
mailbox_send_immediate_ref_signal(target, UnlinkIDAckSignal, self_pid, unlink_id);
780779
if (!process_table_locked) {
781780
globalcontext_get_process_unlock(ctx->global, target);

0 commit comments

Comments
 (0)