Skip to content

Commit 68ccb22

Browse files
committed
Add RefData, fix STM32 tests
Signed-off-by: Mateusz Front <[email protected]>
1 parent 587b2ee commit 68ccb22

File tree

7 files changed

+155
-121
lines changed

7 files changed

+155
-121
lines changed

src/libAtomVM/nifs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,7 @@ static NativeHandlerResult process_console_message(Context *ctx, term msg)
11421142
{
11431143
// msg is not in the port's heap
11441144
NativeHandlerResult result = NativeContinue;
1145-
if (UNLIKELY(memory_ensure_free_opt(ctx, 12, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
1145+
if (UNLIKELY(memory_ensure_free_opt(ctx, 13, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
11461146
fprintf(stderr, "Unable to allocate sufficient memory for console driver.\n");
11471147
AVM_ABORT();
11481148
}
@@ -1160,7 +1160,7 @@ static NativeHandlerResult process_console_message(Context *ctx, term msg)
11601160
term pid = term_get_tuple_element(msg, 1);
11611161
term ref = term_get_tuple_element(msg, 2);
11621162
term req = term_get_tuple_element(msg, 3);
1163-
uint64_t ref_ticks = term_to_ref_ticks(ref);
1163+
RefData ref_data = term_to_ref_data(ref);
11641164

11651165
if (is_tagged_tuple(req, PUT_CHARS_ATOM, 3)) {
11661166
term chars = term_get_tuple_element(req, 2);
@@ -1170,7 +1170,7 @@ static NativeHandlerResult process_console_message(Context *ctx, term msg)
11701170
printf("%s", str);
11711171
free(str);
11721172

1173-
term refcopy = term_from_ref_ticks(ref_ticks, &ctx->heap);
1173+
term refcopy = term_from_ref_data(ref_data, &ctx->heap);
11741174

11751175
term reply = term_alloc_tuple(3, &ctx->heap);
11761176
term_put_tuple_element(reply, 0, IO_REPLY_ATOM);

src/libAtomVM/term.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ extern "C" {
152152
#define BOXED_FUN_SIZE 3
153153
#define FLOAT_SIZE (sizeof(float_term_t) / sizeof(term) + 1)
154154
#define REF_SIZE ((int) ((sizeof(uint64_t) / sizeof(term)) + 1))
155-
#define TERM_BOXED_PROCESS_REF_SIZE (REF_SIZE + 1)
155+
// FIXME: The required size is REF_SIZE + 1, but then it's equal to
156+
// TERM_BOXED_REFERENCE_RESOURCE_SIZE on 32bit arch, and therefore
157+
// the process ref is indistinguishable from resource ref there
158+
#define TERM_BOXED_PROCESS_REF_SIZE 5
156159
#define TERM_BOXED_PROCESS_REF_HEADER (((TERM_BOXED_PROCESS_REF_SIZE - 1) << 6) | TERM_BOXED_REF)
157160
#if TERM_BYTES == 8
158161
#define EXTERNAL_PID_SIZE 3
@@ -248,6 +251,14 @@ extern "C" {
248251
typedef struct GlobalContext GlobalContext;
249252
#endif
250253

254+
typedef struct RefData RefData;
255+
256+
struct RefData
257+
{
258+
uint64_t ref_ticks;
259+
int32_t process_id;
260+
};
261+
251262
typedef struct PrinterFun PrinterFun;
252263

253264
typedef int (*printer_function_t)(PrinterFun *fun, const char *fmt, ...) PRINTF_FORMAT_ARGS(2, 3);
@@ -1999,7 +2010,7 @@ static inline term term_make_process_reference(int32_t process_id, uint64_t ref_
19992010
boxed_value[0] = TERM_BOXED_PROCESS_REF_HEADER;
20002011

20012012
#if TERM_BYTES == 4
2002-
boxed_value[1] = (ref_ticks >> 4);
2013+
boxed_value[1] = (ref_ticks >> 32);
20032014
boxed_value[2] = (ref_ticks & 0xFFFFFFFF);
20042015
boxed_value[3] = process_id;
20052016

@@ -2027,6 +2038,23 @@ static inline uint32_t term_process_ref_to_process_id(term rt)
20272038
#endif
20282039
}
20292040

2041+
static inline RefData term_to_ref_data(term t)
2042+
{
2043+
RefData ref_data;
2044+
ref_data.ref_ticks = term_to_ref_ticks(t);
2045+
ref_data.process_id = term_is_process_reference(t) ? term_process_ref_to_process_id(t) : 0;
2046+
return ref_data;
2047+
}
2048+
2049+
static inline term term_from_ref_data(RefData ref_data, Heap *heap)
2050+
{
2051+
if (ref_data.process_id) {
2052+
return term_make_process_reference(ref_data.process_id, ref_data.ref_ticks, heap);
2053+
} else {
2054+
return term_from_ref_ticks(ref_data.ref_ticks, heap);
2055+
}
2056+
}
2057+
20302058
/**
20312059
* @brief Make an external pid term from node, process_id, serial and creation
20322060
*

src/platforms/esp32/components/avm_builtins/network_driver.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
#define TCPIP_HOSTNAME_MAX_SIZE 255
5959

6060
#define TAG "network_driver"
61-
#define PORT_REPLY_SIZE (TUPLE_SIZE(2) + REF_SIZE)
61+
#define PORT_REPLY_SIZE (TUPLE_SIZE(2) + TERM_BOXED_PROCESS_REF_SIZE)
6262

6363
static const char *const ap_atom = ATOM_STR("\x2", "ap");
6464
static const char *const ap_channel_atom = ATOM_STR("\xA", "ap_channel");
@@ -110,7 +110,7 @@ struct ClientData
110110
GlobalContext *global;
111111
uint32_t port_process_id;
112112
uint32_t owner_process_id;
113-
uint64_t ref_ticks;
113+
RefData ref_data;
114114
};
115115

116116
static inline term make_atom(GlobalContext *global, AtomString atom_str)
@@ -131,7 +131,7 @@ static term tuple_from_addr(Heap *heap, uint32_t addr)
131131

132132
static void send_term(Heap *heap, struct ClientData *data, term t)
133133
{
134-
term ref = term_from_ref_ticks(data->ref_ticks, heap);
134+
term ref = term_from_ref_data(data->ref_data, heap);
135135
term msg = term_alloc_tuple(2, heap);
136136
term_put_tuple_element(msg, 0, ref);
137137
term_put_tuple_element(msg, 1, t);
@@ -659,7 +659,7 @@ static void start_network(Context *ctx, term pid, term ref, term config)
659659
data->global = ctx->global;
660660
data->port_process_id = ctx->process_id;
661661
data->owner_process_id = term_to_local_process_id(pid);
662-
data->ref_ticks = term_to_ref_ticks(ref);
662+
data->ref_data = term_to_ref_data(ref);
663663

664664
esp_err_t err;
665665

@@ -890,7 +890,7 @@ static NativeHandlerResult consume_mailbox(Context *ctx)
890890
return NativeContinue;
891891
}
892892

893-
//TODO: port this code to standard port (and gen_message)
893+
// TODO: port this code to standard port (and gen_message)
894894
term pid = term_get_tuple_element(msg, 0);
895895
term ref = term_get_tuple_element(msg, 1);
896896
term cmd = term_get_tuple_element(msg, 2);
@@ -922,7 +922,7 @@ static NativeHandlerResult consume_mailbox(Context *ctx)
922922
default: {
923923
ESP_LOGE(TAG, "Unrecognized command: %x", cmd);
924924
// {Ref, {error, badarg}}
925-
size_t heap_size = TUPLE_SIZE(2) + REF_SIZE + TUPLE_SIZE(2);
925+
size_t heap_size = TUPLE_SIZE(2) + TERM_BOXED_PROCESS_REF_SIZE + TUPLE_SIZE(2);
926926
if (UNLIKELY(memory_ensure_free(ctx, heap_size) != MEMORY_GC_OK)) {
927927
ESP_LOGE(TAG, "Unable to allocate heap space for error; no message sent");
928928
return NativeContinue;
@@ -932,7 +932,7 @@ static NativeHandlerResult consume_mailbox(Context *ctx)
932932
}
933933
} else {
934934
// {Ref, {error, badarg}}
935-
size_t heap_size = TUPLE_SIZE(2) + REF_SIZE + TUPLE_SIZE(2);
935+
size_t heap_size = TUPLE_SIZE(2) + TERM_BOXED_PROCESS_REF_SIZE + TUPLE_SIZE(2);
936936
if (UNLIKELY(memory_ensure_free(ctx, heap_size) != MEMORY_GC_OK)) {
937937
ESP_LOGE(TAG, "Unable to allocate heap space for error; no message sent");
938938
return NativeContinue;

0 commit comments

Comments
 (0)