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

Add fun_info/2 #1351

Merged
merged 1 commit into from
Dec 22, 2024
Merged

Add fun_info/2 #1351

merged 1 commit into from
Dec 22, 2024

Conversation

jakub-gonet
Copy link
Contributor

@jakub-gonet jakub-gonet commented Oct 22, 2024

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

libs/estdlib/src/erlang.erl Show resolved Hide resolved
Comment on lines 3526 to 3702
if (UNLIKELY(memory_ensure_free_opt(ctx, TUPLE_SIZE(2), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like there need to be better system for using those. GC invalidates any pointers to data on process' heap and some functions use it internally. Not only it makes several GC runs but also prevents allocating terms one by one.

Initially, I implemented both /1 and /2 as NIF but experienced problems of invalid pointers when I tried to turn a cstring into a list – it calls a GC so creating second list invalidates pointer to the first one.
I assume the correct way is to put the first one as a root but then the cstring -> list function needs to take two another parameters.
It's a hard problem but I thought about passing some struct that accumulates how much memory is needed and does it as one pass, something like adding an out parameter GCTracker* tracker that is used before final allocation. This necessarily splits memory calculation and memory allocation.

src/libAtomVM/defaultatoms.c Show resolved Hide resolved
@jakub-gonet
Copy link
Contributor Author

Sorry for straining CI so much. Function name representation changed frequently in Erlang (OTP 21, 24, 25 has all different reprs).

@bettio
Copy link
Collaborator

bettio commented Nov 2, 2024

2 side comments: I would rebase this on top of main branch, we are adding new features on that branch now. Also I think this change is worth being mentioned in the changelog.

@jakub-gonet jakub-gonet changed the base branch from release-0.6 to main November 3, 2024 12:44
@jakub-gonet
Copy link
Contributor Author

Done.

case MODULE_ATOM: {
const term *boxed_value = term_to_const_term_ptr(fun);
if (term_is_external_fun(fun)) {
term module_atom = boxed_value[1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding some inline functions to term.h (or use existing ones) in order to access fun inner structure, so we don't leak outside of term.h the fun layout.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I used single function for that with null checks for arguments. If this adds too much branching, I can split them into three functions.
Also passing entire context, not sure if it should take global and heap separately instead.

tests/erlang_tests/test_fun_info.erl Outdated Show resolved Hide resolved
src/libAtomVM/term.h Outdated Show resolved Hide resolved
src/libAtomVM/defaultatoms.c Show resolved Hide resolved
src/libAtomVM/nifs.c Outdated Show resolved Hide resolved
Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are quite close, just the last changes.
I suggest also rebasing it on latest main.

src/libAtomVM/defaultatoms.c Show resolved Hide resolved
src/libAtomVM/term.c Outdated Show resolved Hide resolved
src/libAtomVM/term.c Outdated Show resolved Hide resolved
Signed-off-by: Jakub Gonet <[email protected]>
@jakub-gonet
Copy link
Contributor Author

I noticed that I only preserved value across GC so besides other changes I fixed that as well.

src/libAtomVM/nifs.c Show resolved Hide resolved
src/libAtomVM/nifs.c Show resolved Hide resolved
@jakub-gonet jakub-gonet requested a review from bettio December 12, 2024 21:49
@jakub-gonet
Copy link
Contributor Author

@bettio do you want me to rebase on latest main to potentially fix ESP CI problems and resolve conflicts?

@bettio bettio merged commit bb4a03b into atomvm:main Dec 22, 2024
93 of 102 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants