Skip to content

Commit 8651323

Browse files
committed
CR
Signed-off-by: Mateusz Front <mateusz.front@swmansion.com>
1 parent 3ad27e3 commit 8651323

7 files changed

Lines changed: 36 additions & 85 deletions

File tree

libs/estdlib/src/erlang.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1624,7 +1624,7 @@ raise(_Class, _Reason, _Stacktrace) ->
16241624

16251625
%%-----------------------------------------------------------------------------
16261626
%% @returns A reference aliasing the calling process.
1627-
%% @doc Creates an alias for the callling process. The alias can be used
1627+
%% @doc Creates an alias for the calling process. The alias can be used
16281628
%% to send messages to the process like the PID. The alias can also be
16291629
%% created along with a monitor - see `monitor/3'. The alias can be
16301630
%% removed by calling `unalias/1'.

src/libAtomVM/context.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ struct Monitor *monitor_link_new(term link_pid)
847847
}
848848
}
849849

850-
struct Monitor *monitor_new(term monitor_pid, RefData ref_data, bool is_monitoring)
850+
struct Monitor *monitor_new(term monitor_pid, RefData *ref_data, bool is_monitoring)
851851
{
852852
struct MonitorLocalMonitor *monitor = malloc(sizeof(struct MonitorLocalMonitor));
853853
if (IS_NULL_PTR(monitor)) {
@@ -859,12 +859,12 @@ struct Monitor *monitor_new(term monitor_pid, RefData ref_data, bool is_monitori
859859
monitor->monitor.monitor_type = CONTEXT_MONITOR_MONITORED_LOCAL;
860860
}
861861
monitor->monitor_obj = monitor_pid;
862-
monitor->ref_data = ref_data;
862+
monitor->ref_data = *ref_data;
863863

864864
return &monitor->monitor;
865865
}
866866

867-
struct Monitor *monitor_registeredname_monitor_new(int32_t monitor_process_id, term monitor_name, RefData ref_data)
867+
struct Monitor *monitor_registeredname_monitor_new(int32_t monitor_process_id, term monitor_name, RefData *ref_data)
868868
{
869869
struct MonitorLocalRegisteredNameMonitor *monitor = malloc(sizeof(struct MonitorLocalRegisteredNameMonitor));
870870
if (IS_NULL_PTR(monitor)) {
@@ -873,19 +873,19 @@ struct Monitor *monitor_registeredname_monitor_new(int32_t monitor_process_id, t
873873
monitor->monitor.monitor_type = CONTEXT_MONITOR_MONITORING_LOCAL_REGISTEREDNAME;
874874
monitor->monitor_process_id = monitor_process_id;
875875
monitor->monitor_name = monitor_name;
876-
monitor->ref_data = ref_data;
876+
monitor->ref_data = *ref_data;
877877

878878
return &monitor->monitor;
879879
}
880880

881-
struct Monitor *monitor_alias_new(RefData ref_data, enum ContextMonitorAliasType alias_type)
881+
struct Monitor *monitor_alias_new(RefData *ref_data, enum ContextMonitorAliasType alias_type)
882882
{
883883
struct MonitorAlias *monitor = malloc(sizeof(struct MonitorAlias));
884884
if (IS_NULL_PTR(monitor)) {
885885
return NULL;
886886
}
887887
monitor->monitor.monitor_type = CONTEXT_MONITOR_ALIAS;
888-
monitor->ref_data = ref_data;
888+
monitor->ref_data = *ref_data;
889889
monitor->alias_type = alias_type;
890890

891891
return &monitor->monitor;

src/libAtomVM/context.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,9 @@ struct Monitor *monitor_link_new(term link_pid);
530530
* @param is_monitoring if ctx is the monitoring process
531531
* @return the allocated monitor or NULL if allocation failed
532532
*/
533-
struct Monitor *monitor_new(term monitor_pid, RefData ref_data, bool is_monitoring);
533+
struct Monitor *monitor_new(term monitor_pid, RefData *ref_data, bool is_monitoring);
534534

535-
struct Monitor *monitor_alias_new(RefData ref_data, enum ContextMonitorAliasType alias_type);
535+
struct Monitor *monitor_alias_new(RefData *ref_data, enum ContextMonitorAliasType alias_type);
536536

537537
/**
538538
* @brief Create a monitor on a process by registered name.
@@ -542,7 +542,7 @@ struct Monitor *monitor_alias_new(RefData ref_data, enum ContextMonitorAliasType
542542
* @param ref_data reference of the monitor
543543
* @return the allocated monitor or NULL if allocation failed
544544
*/
545-
struct Monitor *monitor_registeredname_monitor_new(int32_t monitor_process_id, term monitor_name, RefData ref_data);
545+
struct Monitor *monitor_registeredname_monitor_new(int32_t monitor_process_id, term monitor_name, RefData *ref_data);
546546

547547
/**
548548
* @brief Create a resource monitor.
@@ -601,17 +601,15 @@ void context_demonitor(Context *ctx, uint64_t ref_ticks);
601601

602602
/**
603603
* @brief Find a process alias
604-
* @details Called within the process only
605604
*
606605
* @param ctx the context being executed
607-
* @param ref_ticks reference of the alias to remove
606+
* @param ref_ticks reference of the alias to find
608607
* @return found alias or NULL
609608
*/
610609
struct MonitorAlias *context_find_alias(Context *ctx, uint64_t ref_ticks);
611610

612611
/**
613612
* @brief Remove an alias of a process
614-
* @details Called within the process only
615613
*
616614
* @param alias The alias to remove, can be obtained using context_find_alias
617615
*/

src/libAtomVM/external_term.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,8 @@ static int calculate_heap_usage(const uint8_t *external_term_buf, size_t remaini
13661366
if (external_term_buf[4] == strlen("nonode@nohost") && memcmp(external_term_buf + 5, "nonode@nohost", strlen("nonode@nohost")) == 0) {
13671367
if (len == 2) {
13681368
heap_size = TERM_BOXED_REFERENCE_SHORT_SIZE;
1369+
} else if (len == 3) {
1370+
heap_size = TERM_BOXED_REFERENCE_PROCESS_SIZE;
13691371
} else if (len == 4) {
13701372
heap_size = TERM_BOXED_REFERENCE_RESOURCE_SIZE;
13711373
}

src/libAtomVM/globalcontext.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@
4646
extern "C" {
4747
#endif
4848

49-
#define INVALID_PROCESS_ID 0
50-
5149
struct Context;
5250

5351
#ifndef TYPEDEF_CONTEXT

src/libAtomVM/nifs.c

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,29 +1440,25 @@ static term do_spawn(Context *ctx, Context *new_ctx, size_t arity, size_t n_free
14401440
context_destroy(new_ctx);
14411441
return term_invalid_term();
14421442
}
1443-
ref_data.ref_ticks = globalcontext_get_ref_ticks(ctx->global);
14441443
struct Monitor *alias_monitor = NULL;
14451444
if (is_alias) {
1446-
ref_data = (RefData){
1447-
.type = RefTypeProcess,
1448-
.process = { .ref_ticks = globalcontext_get_ref_ticks(ctx->global), .process_id = ctx->process_id }
1449-
};
1450-
alias_monitor = monitor_alias_new(ref_data, alias_type);
1445+
ref_data = (RefData){ .ref_ticks = globalcontext_get_ref_ticks(ctx->global), .process_id = ctx->process_id };
1446+
alias_monitor = monitor_alias_new(&ref_data, alias_type);
14511447
if (IS_NULL_PTR(alias_monitor)) {
14521448
context_destroy(new_ctx);
14531449
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
14541450
}
14551451
} else {
1456-
ref_data = (RefData){ .type = RefTypeShort, .ref_ticks = globalcontext_get_ref_ticks(ctx->global) };
1452+
ref_data = (RefData){ .ref_ticks = globalcontext_get_ref_ticks(ctx->global), .process_id = INVALID_PROCESS_ID };
14571453
}
14581454

1459-
struct Monitor *new_monitor = monitor_new(term_from_local_process_id(ctx->process_id), ref_data, false);
1455+
struct Monitor *new_monitor = monitor_new(term_from_local_process_id(ctx->process_id), &ref_data, false);
14601456
if (IS_NULL_PTR(new_monitor)) {
14611457
context_destroy(new_ctx);
14621458
free(alias_monitor);
14631459
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
14641460
}
1465-
struct Monitor *self_monitor = monitor_new(new_pid, ref_data, true);
1461+
struct Monitor *self_monitor = monitor_new(new_pid, &ref_data, true);
14661462
if (IS_NULL_PTR(self_monitor)) {
14671463
free(alias_monitor);
14681464
free(new_monitor);
@@ -4485,31 +4481,28 @@ static term nif_erlang_monitor(Context *ctx, int argc, term argv[])
44854481
RefData ref_data;
44864482
struct Monitor *alias_monitor = NULL;
44874483
if (is_alias) {
4488-
ref_data = (RefData){
4489-
.type = RefTypeProcess,
4490-
.process = { .ref_ticks = globalcontext_get_ref_ticks(ctx->global), .process_id = ctx->process_id }
4491-
};
4492-
alias_monitor = monitor_alias_new(ref_data, alias_type);
4484+
ref_data = (RefData){ .ref_ticks = globalcontext_get_ref_ticks(ctx->global), .process_id = ctx->process_id };
4485+
alias_monitor = monitor_alias_new(&ref_data, alias_type);
44934486
if (IS_NULL_PTR(alias_monitor)) {
44944487
globalcontext_get_process_unlock(ctx->global, target);
44954488
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
44964489
}
44974490
} else {
4498-
ref_data = (RefData){ .type = RefTypeShort, .ref_ticks = globalcontext_get_ref_ticks(ctx->global) };
4491+
ref_data = (RefData){ .ref_ticks = globalcontext_get_ref_ticks(ctx->global), .process_id = INVALID_PROCESS_ID };
44994492
}
45004493
struct Monitor *self_monitor;
45014494
if (term_is_atom(target_proc)) {
4502-
self_monitor = monitor_registeredname_monitor_new(local_process_id, target_proc, ref_data);
4495+
self_monitor = monitor_registeredname_monitor_new(local_process_id, target_proc, &ref_data);
45034496
} else {
4504-
self_monitor = monitor_new(target_pid, ref_data, true);
4497+
self_monitor = monitor_new(target_pid, &ref_data, true);
45054498
}
45064499
if (IS_NULL_PTR(self_monitor)) {
45074500
globalcontext_get_process_unlock(ctx->global, target);
45084501
free(alias_monitor);
45094502
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
45104503
}
45114504
term monitoring_pid = term_from_local_process_id(ctx->process_id);
4512-
struct Monitor *other_monitor = monitor_new(monitoring_pid, ref_data, false);
4505+
struct Monitor *other_monitor = monitor_new(monitoring_pid, &ref_data, false);
45134506
if (IS_NULL_PTR(other_monitor)) {
45144507
free(alias_monitor);
45154508
free(self_monitor);
@@ -6695,14 +6688,9 @@ static term nif_erlang_alias(Context *ctx, int argc, term argv[])
66956688
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
66966689
}
66976690

6698-
RefData ref_data = {
6699-
.type = RefTypeProcess,
6700-
.process = {
6701-
.ref_ticks = globalcontext_get_ref_ticks(ctx->global),
6702-
.process_id = ctx->process_id }
6703-
};
6691+
RefData ref_data = { .ref_ticks = globalcontext_get_ref_ticks(ctx->global), .process_id = ctx->process_id };
67046692
term process_ref = term_from_ref_data(&ref_data, &ctx->heap);
6705-
struct Monitor *monitor = monitor_alias_new(ref_data, ContextMonitorAliasExplicitUnalias);
6693+
struct Monitor *monitor = monitor_alias_new(&ref_data, ContextMonitorAliasExplicitUnalias);
67066694
if (IS_NULL_PTR(monitor)) {
67076695
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
67086696
}

src/libAtomVM/term.h

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,10 @@ _Static_assert(TERM_BOXED_REFERENCE_PROCESS_SIZE <= TERM_BOXED_REFERENCE_MAX_SIZ
191191
#define REFERENCE_RESOURCE_CONS_OFFSET 2
192192

193193
#if TERM_BYTES == 4
194-
#define REFERENCE_PROCESS_PID_OFFSET 2
194+
#define REFERENCE_PROCESS_PID_OFFSET 3
195195

196196
#elif TERM_BYTES == 8
197-
#define REFERENCE_PROCESS_PID_OFFSET 1
197+
#define REFERENCE_PROCESS_PID_OFFSET 2
198198
#endif
199199

200200
#define LIST_SIZE(num_elements, element_size) ((num_elements) * ((element_size) + CONS_SIZE))
@@ -205,6 +205,8 @@ _Static_assert(TERM_BOXED_REFERENCE_PROCESS_SIZE <= TERM_BOXED_REFERENCE_MAX_SIZ
205205
#define LIST_HEAD_INDEX 1
206206
#define LIST_TAIL_INDEX 0
207207

208+
#define INVALID_PROCESS_ID 0
209+
208210
#define TERM_BINARY_SIZE_IS_HEAP(size) ((size) < REFC_BINARY_MIN)
209211

210212
#if TERM_BYTES == 4
@@ -280,31 +282,11 @@ enum RefType
280282
RefTypeExternal
281283
};
282284

283-
struct ProcessRefData
284-
{
285-
uint64_t ref_ticks;
286-
int32_t process_id;
287-
};
288-
289-
struct ExternalRefData
290-
{
291-
term node;
292-
uint32_t creation;
293-
uint16_t len;
294-
const uint32_t *words;
295-
};
296-
297285
typedef struct RefData RefData;
298286
struct RefData
299287
{
300-
enum RefType type;
301-
union
302-
{
303-
uint64_t ref_ticks;
304-
struct ProcessRefData process;
305-
void *resource;
306-
struct ExternalRefData external;
307-
};
288+
uint64_t ref_ticks;
289+
int32_t process_id;
308290
};
309291

310292
typedef struct PrinterFun PrinterFun;
@@ -3124,27 +3106,10 @@ static inline term term_from_resource(void *resource, Heap *heap)
31243106

31253107
static inline term term_from_ref_data(RefData *ref_data, Heap *heap)
31263108
{
3127-
switch (ref_data->type) {
3128-
case RefTypeShort: {
3129-
return term_from_ref_ticks(ref_data->ref_ticks, heap);
3130-
}
3131-
case RefTypeProcess: {
3132-
return term_make_process_reference(ref_data->process.process_id, ref_data->ref_ticks, heap);
3133-
}
3134-
case RefTypeResource: {
3135-
return term_from_resource(ref_data->resource, heap);
3136-
}
3137-
case RefTypeExternal: {
3138-
return term_make_external_reference(
3139-
ref_data->external.node,
3140-
ref_data->external.len,
3141-
ref_data->external.words,
3142-
ref_data->external.creation,
3143-
heap);
3144-
}
3145-
default: {
3146-
UNREACHABLE();
3147-
}
3109+
if (ref_data->process_id == INVALID_PROCESS_ID) {
3110+
return term_from_ref_ticks(ref_data->ref_ticks, heap);
3111+
} else {
3112+
return term_make_process_reference(ref_data->process_id, ref_data->ref_ticks, heap);
31483113
}
31493114
}
31503115

0 commit comments

Comments
 (0)