Skip to content

Commit eac7682

Browse files
committed
wip
Signed-off-by: Mateusz Front <[email protected]>
1 parent 587b2ee commit eac7682

File tree

8 files changed

+156
-123
lines changed

8 files changed

+156
-123
lines changed

src/libAtomVM/globalcontext.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct RegisteredProcess
6262

6363
GlobalContext *globalcontext_new(void)
6464
{
65+
printf("DUPA %d\n", REF_SIZE);
66+
printf("DUPA %d\n", TERM_BOXED_PROCESS_REF_SIZE);
6567
GlobalContext *glb = malloc(sizeof(GlobalContext));
6668
if (IS_NULL_PTR(glb)) {
6769
return NULL;

src/libAtomVM/nifs.c

Lines changed: 4 additions & 4 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);
@@ -4204,7 +4204,7 @@ static term nif_erlang_fun_info_2(Context *ctx, int argc, term argv[])
42044204
RAISE_ERROR(BADARG_ATOM);
42054205
}
42064206

4207-
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2), 2, (term[]){ key, value }, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
4207+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2), 2, (term[]) { key, value }, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
42084208
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
42094209
}
42104210
term fun_info_tuple = term_alloc_tuple(2, &ctx->heap);

src/libAtomVM/term.h

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ 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+
#define TERM_BOXED_PROCESS_REF_SIZE 5
156156
#define TERM_BOXED_PROCESS_REF_HEADER (((TERM_BOXED_PROCESS_REF_SIZE - 1) << 6) | TERM_BOXED_REF)
157157
#if TERM_BYTES == 8
158158
#define EXTERNAL_PID_SIZE 3
@@ -248,6 +248,14 @@ extern "C" {
248248
typedef struct GlobalContext GlobalContext;
249249
#endif
250250

251+
typedef struct RefData RefData;
252+
253+
struct RefData
254+
{
255+
uint64_t ref_ticks;
256+
int32_t process_id;
257+
};
258+
251259
typedef struct PrinterFun PrinterFun;
252260

253261
typedef int (*printer_function_t)(PrinterFun *fun, const char *fmt, ...) PRINTF_FORMAT_ARGS(2, 3);
@@ -1896,7 +1904,7 @@ static inline bool term_is_nomatch_binary_pos_len(BinaryPosLen pos_len)
18961904

18971905
static inline BinaryPosLen term_nomatch_binary_pos_len(void)
18981906
{
1899-
return (BinaryPosLen){ .pos = -1, .len = -1 };
1907+
return (BinaryPosLen) { .pos = -1, .len = -1 };
19001908
}
19011909

19021910
/**
@@ -1999,7 +2007,7 @@ static inline term term_make_process_reference(int32_t process_id, uint64_t ref_
19992007
boxed_value[0] = TERM_BOXED_PROCESS_REF_HEADER;
20002008

20012009
#if TERM_BYTES == 4
2002-
boxed_value[1] = (ref_ticks >> 4);
2010+
boxed_value[1] = (ref_ticks >> 32);
20032011
boxed_value[2] = (ref_ticks & 0xFFFFFFFF);
20042012
boxed_value[3] = process_id;
20052013

@@ -2027,6 +2035,23 @@ static inline uint32_t term_process_ref_to_process_id(term rt)
20272035
#endif
20282036
}
20292037

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

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)