Questions tagged [erlang-nif]

Erlang Native Implemented Function

NIFs were introduced in Erlang/OTP R13B03 as an experimental feature. It is a simpler and more efficient way of calling C-code than using port drivers. NIFs are most suitable for synchronous functions, that do some relatively short calculations without side effects and return the result. Although in recent versions become a mature feature with wide adoption. Modern NIF API allows performing wide range of task including spawning threads, performing IO operations and perform lengthy work using experimental (R18-19) dirty schedulers. NIFs became preferred choice for integrating foreign code in Erlang/OTP distribution (binary, maps, ssl).

A NIF is a function that is implemented in C instead of Erlang. NIFs appear as any other functions to the callers. They belong to a module and are called like any other Erlang functions. The NIFs of a module are compiled and linked into a dynamic loadable, shared library (SO in UNIX, DLL in Windows). The NIF library must be loaded at runtime by the Erlang code of the module.

As a NIF library is dynamically linked into the emulator process, this is the fastest way of calling C-code from Erlang (alongside port drivers). Calling NIFs requires no context switches. But it is also the least safe because a crash in a NIF brings the emulator down too.

60 questions
0
votes
2 answers

How to case switch in erlang using a function value?

This program is crashing despite seeming to work. I don't understand why though. I'm trying to accomplish a deep merge and need conditional logic. Given the following list: ManOne = #{ "Bob" => #{"Sagget" => #{}} } ManTwo = #{ "Bob" =>…
user3505901
  • 408
  • 1
  • 6
  • 19
0
votes
1 answer

Erlang NIF weird iolist behavior

I just started experimenting with Erlang NIFS and got stuck with this problem and I wonder if there's anything one can do about this. Here is the NIF: #include "erl_nif.h" static ERL_NIF_TERM test_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM…
kristóf baján
  • 473
  • 1
  • 5
  • 15
0
votes
1 answer

Passing BIGINT between Erlang VM and the NIFs

Is there an efficient way to pass BIGINT (integers exceeding 64 bits for x86_64/amd64 architectures) between Erlang VM and the NIFs? So far I haven't found a supporting function in the enif module. Maybe converting BIGINTs to binaries will help, but…
jj1bdx
  • 1,117
  • 1
  • 15
  • 32
0
votes
1 answer

Proper resource handling in Elixir NIF

I am trying to implent a NIF for simple linear algebra. Here is my internal structure for matrix: typedef struct la_matrix { uint rows, columns; double **data; } la_matrix; And here is a "constructor" for…
Vanzef
  • 453
  • 1
  • 5
  • 17
0
votes
0 answers

Getting dependencies paths of the root project in Mix

As my project consists of some mix projects using nifs, I'm trying to make a custom compiler, so that *.c files could be compiled along with these mix projects. Let's say I have a project A, which has a dependency of B, that has some native code…
Com Piler
  • 257
  • 5
  • 14
0
votes
1 answer

difference between error_logger and error_logger_tty_h handler

I know both error_logger and error_logger_tty_h are swappable handlers of gen_event error_logger. From their source code, I know error_logger report messages ending up erlang:display, and error_logger_tty_h ending up `io:format(user, String,…
keroro520
  • 459
  • 4
  • 12
0
votes
1 answer

Enif_send function from created thread issue

I tried to invoke enif_send from a created thread. Mainly, I have an erlang process which listen to a received message emitted from my nif. Firstly, in my enif function I got the pid of my erlang process and I cast it, then I call enif_send to send…
H. SLF
  • 61
  • 8
0
votes
1 answer

Erlang nif does not upgrades

I write a nif library in erlang. Also i write load, upgrade and unload functions. This is my code: #include "erl_nif.h" int checksum(char *s) { return…
Amin
  • 755
  • 6
  • 21
0
votes
1 answer

Using Erlang's NIF, How should I use malloc on ERL_NIF_TERM?

I'm using Erlang's NIF, and the result of the C function is an array that I want to send back to erlang in the form of a list of tuples of three points, each is a tuple of two doubles. to create this array currently I'm doing this: ans =…
Deddy
  • 169
  • 1
  • 13
0
votes
1 answer

Calling NIF in Erlide IDE

Is it possible to call NIF functions from an Erlang program using Erlide if so is there any tutorial,article to help me run my first example
Bou6
  • 84
  • 2
  • 10
0
votes
1 answer

Raw pointer type for interior mutable struct

I’m doing some Rust FFI work for the Erlang NIF API, and I have these: ErlNifEnv *enif_alloc_env(); void enif_free_env(ErlNifEnv* env); This ErlNifEnv pointer gets passed to a variety of other functions but the user will never deref the pointer. …
goertzenator
  • 1,960
  • 18
  • 28
0
votes
1 answer

Erlang: Using 32 bit NIFs under a 64 bit VM

Is there a way to use a 32bit NIF from a 64 bit Erlang (under Windows)? Seems impossible, but maybe there is a way to achieve this?
GabiMe
  • 18,105
  • 28
  • 76
  • 113
0
votes
1 answer

Returning Image Pointer to Erlang

I am trying to use openCV with the Erlang NIF. So I want to do a basic thing and that's just to read a picture and send back the pointer to erlang. and be able to again send back the pointer received to C and just show the pic so the niftest.cpp…
Khashayar
  • 2,014
  • 3
  • 22
  • 31
0
votes
2 answers

Return Pointer from Erlang C NIF

When writing an Erlang C NIF, how can a pointer, created in C let's say an array, be returned to Erlang for later use by the same Erlang process in another NIF call? #define LENGTH = 50; int *a, array[LENGTH]; a = enif_alloc(LENGTH *…
BAR
  • 15,909
  • 27
  • 97
  • 185
0
votes
1 answer

Protobuf message and memcpy inside erlang nif

I'm using protobuf inside nif function (erlang nif) and need to create resource of protobuf message type. I wrote something like this: ERL_NIF_TERM create_resource(ErlNifEnv *env, const MyClass &msg) { size_t size = sizeof(MyClass); MyClass…
Def_NulL
  • 87
  • 4
1 2 3
4