Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
static int load(ErlNifEnv* env, void** priv, ERL_NIF_TERM info) {
ex_pigpio_priv* data = enif_alloc(sizeof(ex_pigpio_priv));
//...
data->atom_ok = enif_make_atom(env, "ok");
data->atom_error = enif_make_atom(env, "error");
ErlNifEnv represents an environment that can host Erlang terms. All terms in an environment are valid as long as the environment is valid. ErlNifEnv is an opaque type and pointers to it can only be passed on to API functions. There are two types of environments; process bound and process independent.
A process bound environment is passed as the first argument to all NIFs. All function arguments passed to a NIF will belong to that environment. The return value from a NIF must also be a term belonging to the same environment. In addition a process bound environment contains transient information about the calling Erlang process. The environment is only valid in the thread where it was supplied as argument until the NIF returns. It is thus useless and dangerous to store pointers to process bound environments between NIF calls.
data->atom_ok = enif_make_atom(env, "ok");
Normally the lifetime of a term is determined by its environment.
However, atoms are an exception to this rule, which allows you to
prefabricate atoms in static variables.
This exceptions is undocumented but widely used (by myself included in
crypto). There is a small risk that an introduction of atom garbage
collection in some non predictable future release will have to break
this feature.
Не могли бы вы поделиться официальной документацией о том, как написать NIF в Elixir?
enif_make_atom является не задокументированным исключением из общего правила.
Си-код не является главной темой статьиИ тем не менее, C-код составляет половину статьи
А вот котов я не люблю и повторно говорю, что я никому не рекомендовал имплементировать load, как это сделал яНет, вы не поняли, это я вам указываю на ошибку в вашей библиотеке, которую необходимо исправить.
cfags = ['CFLAGS=', ['-I', :code.root_dir(), '/erts-', :erlang.system_info(:version), '/include']]
System.cmd("make", [cflags, ...
Как написать свой NIF в Elixir