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

How to use a variable as pass by reference in Erlang?

Why is my output not getting reflected in Lst1? -module(pmap). -export([start/0,test/2]). test(Lst1,0) -> {ok, [Temp]} = io:fread( "Input the edge weight ", "~d" ), lists:append([Lst1,[Temp]]), io:fwrite("~w~n",[Lst1]); test(Lst1,V)…
3
votes
1 answer

Creating dirty threads in erlang nif

I am a little bit confused, when I create a dirty NIF (for example, by setting the appropriate flags value for the dirty NIF in its ErlNifFunc entry), this creates a dirty scheduler that runs on a dirty thread. I understand that I can have only N…
ziv
  • 53
  • 3
3
votes
1 answer

unicode collation NIF running slower than Pure Erlang implementation

I'm trying to optimise existing unicode collation library(written in Erlang) by rewriting it as a NIF implementation. Prime reason is because collation is CPU intensive operation. Link to implementation: https://github.com/abhi-bit/merger Unicode…
Abhi
  • 33
  • 1
  • 4
3
votes
1 answer

Erlang: Interfacing with Xalan: port driver or nif?

I'd like to get a real XSLT processor working with erlang. Which would be the best interface, nif or port driver? According to the nif documentation, nif calls block the runtime, so they should not take long. Is processing a long xml document too…
mwt
  • 175
  • 6
3
votes
1 answer

Why does the nif function block the Erlang VM from scheduling other processes?

When the Erlang VM beam runs some code written in C,the other processes written in Erlang was not scheduled. For example: static ERL_NIF_TERM nifsleep(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) { sleep(10); …
3
votes
1 answer

How do you setup an Erlang NIF project with rebar?

I looked through the questions here on StackOverflow and Googled around for an example of setting up a Basic NIF project in rebar for wrapping a C++ library. I used to library project on GitHub as a guide: https://github.com/tuncer/re2 My project…
lenards
  • 141
  • 1
  • 8
3
votes
1 answer

Why does Erlang not have a C NIF drop-in replacement for calloc?

Why does Erlang have a C NIF drop-in replacement for malloc, enif_alloc, but not calloc? Thereby forcing one to use memset() after enif_alloc for array access.
BAR
  • 15,909
  • 27
  • 97
  • 185
3
votes
2 answers

Development environment for erlang on Windows

I have an erlang project that includes NIFs with OS specific functions that work with raw sockets. I want to port this project on Windows which means changing the functions in NIFs to work on Windows. What is the preferred IDE for erlang…
3
votes
1 answer

Passing a binary to erlang nif

I'm writing some Erlang code that basically accepts some binary data from a TCP connection and then uses a C nif to decrypt the data and return the decrypted data. The problem is that I can't seem to figure out how to modify the passed in…
tkblackbelt
  • 391
  • 2
  • 10
2
votes
1 answer

Can Erlang interface to Go like it does with C (e.g. NIF)?

I have a C NIF in my Erlang project that has limitations, due to libraries that it relies on (unstable libraries, or libraries that I need that don't exist). I think that I can do what I want to do, in Go. Can Erlang interface to Go like this?
HotBlanket
  • 97
  • 8
2
votes
1 answer

Erlang NIF number return types

I'm experimenting with NIFs and I'm confused about what number types Erlang is working with, because I'm getting some weirdness with my precisions. Here's an example: erlang:band(18446744073709551614, 5) == 4 And from inside a NIF which looks…
whitfin
  • 4,539
  • 6
  • 39
  • 67
2
votes
1 answer

Not able to load .nif module of aerospike erlang client in ubuntu

I had installed aerospike and erlang(OTP17) on the Ubuntu(12.04) machine. Aerospike service is running fine. I had installed aerospike erlang driver. I'm not able to connect to aerospike using erlang driver, its failing to load .nif file I tried…
LIL
  • 51
  • 1
2
votes
3 answers

Erlang: blocking C NIF call behavior

I have observed a blocking behavior of C NIFs when they were being called concurrently by many Erlang processes. Can it be made non-blocking? Is there a mutex at work here which I'm not able to comprehend? P.S. A basic "Hello world" NIF can be…
abips
  • 209
  • 3
  • 9
2
votes
1 answer

Manipulating Binaries in C NIF Erlang

I'm an Erlang enthusiast and a newbie Erlang Prorammer. I recently had to face a data crunching problem in Erlang. Hence I decided to use NIFs. I have two list of proplists and I have to return the hash join of the two proplists based upon a…
abips
  • 209
  • 3
  • 9
2
votes
1 answer

Allocating memory in Erlang C NIF

Why would one use void *enif_alloc_resource(ErlNifResourceType* type, unsigned size) as opposed to void *enif_alloc(size_t size) when trying to allocate memory from an Erlang C NIF? Reference does not specify much as to…
BAR
  • 15,909
  • 27
  • 97
  • 185