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
11
votes
2 answers

erlang rebar escriptize & nifs

I can use nif's if I write the escript myself, however when I use rebar escriptize the nif functions cannot be found. I think it is because *.so objects are not getting packed like beam files. Here is an simple example; rebar.config: {deps, [ …
cashmere
  • 2,811
  • 1
  • 23
  • 32
10
votes
1 answer

Calling Erlang functions from NIF

Is it possible to call Erlang functions (callback funs) from NIFs? I read the doc(http://www.erlang.org/doc/man/erl_nif.html), but didn't find how to do that.
xin zhao
  • 600
  • 2
  • 11
9
votes
1 answer

Elixir NIF- Hello World example on x64 Mac OSX

Hi I'm trying to get the Hello World example for Erlang NIF (Native Implemented Function) shown here http://www.erlang.org/doc/man/erl_nif.html to work from Elixir on OSX 64bit. First I create the C-code: /* niftest.c */ #include "erl_nif.h" static…
GavinBrelstaff
  • 3,016
  • 2
  • 21
  • 39
7
votes
3 answers

Erlang NIF Test -- OS X Lion

I'm trying to compile the NIF Test from Erlang (http://www.erlang.org/doc/man/erl_nif.html) on Mac OS X Lion. I can't get it to compile. Am I missing a compiler flag? Here's the error I get: Computer:~ me $ gcc -fPIC -shared -o niftest.so niftest.c…
batman
  • 1,447
  • 5
  • 16
  • 27
7
votes
2 answers

Erlang, Is it possible to reload or upgrade a nif library without restart the shell?

I have a nif library and every time i recompile it, I must restart the shell to reload or upgrade this library. Here is my erlang code: -module(q4). -export([init/0]). -on_load(init/0). init() -> erlang:load_nif("./q4_nif", reload). Every…
Amin
  • 755
  • 6
  • 21
6
votes
1 answer

erl_nif.h not found

I try to use erlang nif, I made: #include But get error: fatal error: erl_nif.h: No such file or directory Where can i find it? I have installed erlang R14B (erts-5.8.1) OS Arch linux. Thank you.
0xAX
  • 20,957
  • 26
  • 117
  • 206
6
votes
1 answer

Problems with Erlang NIF and threads

I have a little problem with threads in Erlang NIFs. You can view my code here: http://pastebin.com/HMCj24Jp. The problem is that when I starting the thread it takes some arguments and starts the generate_binary function. This is okay but when I'm…
Burbas
  • 803
  • 7
  • 20
6
votes
1 answer

Erlang: NIFs and dialyzer warning

When implementing NIFs, Dialyzer gives me Function crc16/1 has no local return probably because I do exit in the .erl module (like the official docs recommend): -module(my_nifs). -export([crc16/1]). -on_load(init/0). init() -> ok =…
GabiMe
  • 18,105
  • 28
  • 76
  • 113
6
votes
1 answer

NIF to wrap my multi-threaded C++ code

I have a C++ code that implement a special protocol over the serial port. The code is multi-threaded and internally polls the serial port and do its own cyclic processing. I would like to call this driver from erlang and also receive events from…
iCode
  • 4,308
  • 10
  • 44
  • 77
5
votes
0 answers

Erlang, pass an nif object between functions

I write a C nif code and in function new, it creates a stack struct with enif_alloc_resource and returns that. when i use function enif_make_resources, it always returns <<>> in erlang. Here is my C code: #include "erl_nif.h" static…
Amin
  • 755
  • 6
  • 21
4
votes
0 answers

Erlang NIF crashes on unavailable resources when calling enif_thread_create without enif_thread_join

My project has a NIF that is called from the main Erlang app. It needs to calculate and train an NN model using OpenNN, so it is called with a new Erlang NIF thread: Code in project C code excerpt: ..... res =…
halfway258
  • 115
  • 13
4
votes
2 answers

Value of binary changing after NIF calls Erlang

I intend to manipulate binaries using NIFs for an app which I'm planning to code in Erlang. The gist links to the cpp file and erl file for the NIF are given below. [Erl Gist Link] https://gist.github.com/abhijitiitr/3a5bc97184d6dd32f97b [C++ Gist…
abips
  • 209
  • 3
  • 9
3
votes
3 answers

Creating a Erlang binary through NIF with data pointing to a char *

I'm doing a C-Erlang integration using NIF and I'm having some trouble to initialize a Binary and add a char* pointing to its data. I'm doing this way: ErlNifBinary *output_binary; enif_alloc_binary(500, output_binary); strcpy(output_binary->data,…
Rodrigo Flores
  • 2,411
  • 18
  • 17
3
votes
2 answers

What is the best way to represent a C array in Erlang?

What is the best way to represent a C array in Erlang? I plan on passing a C array to Erlang using NIF. The array is composed of bytes and I'll need to modify some of the bytes as the program runs. Any help is appreciated.
Xavier
  • 8,828
  • 13
  • 64
  • 98
3
votes
0 answers

Running Google Mock from an Erlang NIF

I work on a complex Erlang NIF written in C++. I want to be able to run unit tests and mocks by calling an exported NIF function that will initialise the google mock/test framework, passing in the correct arguments, and then call…
GCUGreyArea
  • 141
  • 2
  • 7
1
2 3 4