mat-hek

mat-hek

Membrane Core Team

Unifex - same code for NIFs and C nodes

Hello everyone,

pleased to announce Unifex, a tool that simplifies integrating elixir with C/C++. Unifex has been out for some time already but since v0.3 released today, it supports generating C nodes - lightweight programs acting as Erlang nodes. Though communication with NIFs is way faster than with separate nodes, a failing node doesn’t crash the entire VM, moreover, it can be supervised like a normal process. Unifex allows choosing between NIFs and C nodes at any point, even in runtime.

Approach

Here are some key facts about Unifex:

  • Native code is kept separate from elixir code
  • Native code doesn’t depend on erlang libraries, but on Unifex abstraction, that aims to be more user friendly than erlang APIs
  • Unifex works by generating boilerplate code in the compile-time, based on DSL in elixir
  • Unifex aims to support the intersection of sets of data types from Elixir and C
  • Unifex relies on Bundlex, our tool for compiling native code

Here’s how it looks like in short: firstly, you describe the interface with spec-like DSL:

module Example

interface [NIF, CNode]

spec foo(num :: int) :: {:ok :: label, answer :: int}

then you provide implementation:

#include "_generated/example.h"

UNIFEX_TERM foo(UnifexEnv* env, int num) {
  return foo_result_ok(env, num);
}

and finally, you use it either as NIF

defmodule Example do
  use Unifex.Loader
end
iex> Example.foo(10)
{:ok, 10}

or as CNode

iex> require Unifex.CNode
iex> {:ok, cnode} = Unifex.CNode.start_link(:example)
iex> Unifex.CNode.call(cnode, :foo, [10])
{:ok, 10}

A detailed description of how to place that in a Mix project can be found here.

Use cases

We’re using Unifex heavily at Membrane, mainly for wrapping some complex, stateful libs, and that’s where Unifex suits the most IMHO.

Status

Though we’ve been using Unifex for NIFs for some time, adding C nodes required lots of changes that are quite fresh. We’re validating them while developing Membrane ICE plugin.

Feedback and contributions are most welcome :wink:

Credits: @Feliks, @mickel8, @bblaszkow06, @mkaput, Software Mansion

Most Liked

mat-hek

mat-hek

Membrane Core Team

Unifex v0.4.0 released :tada:

This version adds the ability to define custom types and use them in Unifex specs. For now, enums and structs are supported. Declaring them generates C enums and C structs, respectively, for example:

type position :: :manager | :developer | :intern | :product_owner

type personal_data :: %PersonalData{
  age: int,
  expirience: int,
  name: string
}

will generate

enum Position_t { MANAGER, DEVELOPER, INTERN, PRODUCT_OWNER };
typedef enum Position_t Position;

struct personal_data_t {
  int age;
  int expirience;
  char *name;
};
typedef struct personal_data_t personal_data;

and can be used like

spec get_salary(person :: personal_data, person_position :: position) ::
       {:ok :: label, salary :: int}
       | {:error :: label, :unemployed :: label}

Docs, GitHub

Credits go to @Feliks, thanks!

Where Next?

Popular in Announcing Top

zorbash
I created Kitto a framework for dashboards inspired by Dashing. The distributed characteristics of Elixir and the low memory footprint...
New
Crowdhailer
Experimenting with this code. OK.try do user <- fetch_user(1) cart <- fetch_cart(1) order = checkout(cart, user) save_orde...
New
josevalim
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below. Hi everyone, We a...
New
ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
trisolaran
Hi! :waving_hand: I would like to present LiveSelect, a little library that I wrote to easily add a dynamic selection input to your LV f...
198 11220 107
New
markmark206
simple_feature_flags is a tiny package that lets you turn features on or off based on which environment (e.g. localhost, staging, product...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement