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
2
votes
1 answer

Erlang NIF from single process storing structs in memory

Im writing an Erlang C NIF that will only be used by one Erlang process. I want to create a struct that will hold an array of pointers. I need this to exist in between the process' calls to the NIF. What I need insight to is the proper way to do…
BAR
  • 15,909
  • 27
  • 97
  • 185
1
vote
1 answer

:erlang.load_nif/2 finds shared library file inside original project but can't find it if the project gets imported

I've build a small elixir application that uses NIF functions to execute some c++ code. The nifs are loaded via: def load_nifs do :erlang.load_nif('/', 0) :ok end and this works fine. Now I want to integrate…
1
vote
0 answers

How to call `ERL_NIF_TERM` when I know that it is a funciton?

My goal is to write a NIF that calls arbitrary Erlang function like fun(N) -> N + 1 end. or {M, F, A}, or any other valid way to do it. I need to operaite on a result out of the Erlang function. As far as I see, the docs don't say much about how to…
Zazaeil
  • 3,900
  • 2
  • 14
  • 31
1
vote
1 answer

How to do Memory-mapped IO in Erlang?

I have been considering using Erlang for an embedded system. The one thing I am missing in my research is the ability to do direct memory mapping. Is this expected to be done via a NIF (Native Interface) or some other method (if so, what)?
NWS
  • 3,080
  • 1
  • 19
  • 34
1
vote
0 answers

NIFs Segmentation Fault without creating erl_crash.dump however code is working fine when ran normally without NIFs

I am working on integrating a Third Party C++ API in Elixir using NIFs. Here is the code sample that is shared and is working fine when I run it like a binary. class Client: public AbstractThirdPartyClient { public: int ConnectionSuccess(int…
1
vote
1 answer

Elixir CRC will not compile on MAC OS

My elixir project indirectly uses crc which has NIFs. They won't compile. It look like the linked does not see the libraries. rtp_to_hls ●  rm -rf deps/ _build/ && mix deps.get && mix compile ==> crc C checksum_xor.c C crc_8.c C …
Haito
  • 2,039
  • 1
  • 24
  • 35
1
vote
1 answer

Recurrent Neural network in Erlang

I am currently working on an erlang project and need to create a game bot as a side feature. But to my surprise I was unable to find a single library that I can use to create RNN in erlang. What should I do? How do I implement RNN in erlang? Is is…
1
vote
1 answer

Creating a hardware accelleration pipeline for BEAM

I have a conceptual idea for a pipeline enabling GPU acceleration in Erlang. The API would consist of higher order functions that take a fun, a binary and some flags, do a runtime check on the BEAM bytecode to see if the fun can be compiled to…
JeremiahB
  • 896
  • 8
  • 15
1
vote
1 answer

Erlang NIFs: Threads locking

Can NIF implementations use the regular C/C++ thread locking primitives or must they use the NIF APIs (enif_mutex_lock(..), enif_mutex_create(..), etc.)
GabiMe
  • 18,105
  • 28
  • 76
  • 113
1
vote
1 answer

Can erlang use named pipes instead of sockets?

NGINX and other servers offer the option to use named pipes (mkfifo). Can erlang use these instead of ports for nif interaction. What if I wanted to make 70,000 connections to my NIF (don't judge).
BreezyChick89
  • 1,369
  • 2
  • 10
  • 14
1
vote
1 answer

erl_nif_thread and erlang process communication

Can we communicate with erl_nif thread created and normal erlang process? We can send messages from nif thread to erlang process using nif_send but can nif thread receive messages from any erlang process like normal erlang process do?
Adi
  • 2,364
  • 1
  • 17
  • 23
0
votes
2 answers

How to use NIF to interact with C code that keeps state across calls (i.e., linked lists as NIF)

I wanted to create a linked-list data structure that was implemented in C. The idea was that one would create a linked list ll:new() -> listId. ListId, above, represents a "pointer" of some type that would get passed back to the C code which would…
Jr0
  • 2,131
  • 13
  • 23
0
votes
0 answers

Erlang NIF not linking properly on Mac M1

I'm having problems linking Erlang to a shared C library (dylib on Mac) using erlang.mk, and would really like a small amount of help I'm running a Mac M1 with Erlang installed via kerl The C library is called Sunvox, a small software…
Justin
  • 4,649
  • 6
  • 33
  • 71
0
votes
3 answers

How to use binary strings in Elixir NIF

How do I get the char* data from an ERL_NIF_TERM coming from an Elixir binary string? I see term_to_binary/1 function but it does not seem to be the right thing.
thodg
  • 1,773
  • 15
  • 24
0
votes
0 answers

NIFs raise Segmentation Fault while loading function has try catch block to handle the exception

I am working on integrating a C++ api with Elixir, by using NIFs. Now whenever I load the NIF, I want to initialize some variables/data which will be persisted across NIF calls. I was trying to achieve the same by load function mentioned in the…