Skip to content

anonymous /gist:348161
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#include "erl_nif.h"
typedef struct {
int calls;
} PrivData;
static int load(ErlNifEnv* env, void** priv_data) {
PrivData* data = enif_alloc(env, sizeof(PrivData));
if (data == NULL) return -1;
data->calls = 0;
*priv_data = data;
return 0;
}
static int reload(ErlNifEnv* env, void** priv_data) {
return 0;
}
static void unload(ErlNifEnv* env, void* priv_data) {
enif_free(env, priv_data);
}
static ERL_NIF_TERM get(ErlNifEnv* env)
{
PrivData* data = (PrivData*) enif_get_data(env);
data->calls++;
return enif_make_int(env, data->calls);
}
static ERL_NIF_TERM increase(ErlNifEnv* env, ERL_NIF_TERM a1) {
int i;
enif_get_int(env, a1, &i);
PrivData* data = (PrivData*) enif_get_data(env);
return enif_make_int(env, i);
}
static ErlNifFunc nif_funcs[] =
{
{"increase", 1, increase},
{"get", 0, get}
};
ERL_NIF_INIT(niftest,nif_funcs,load,reload,NULL,unload);
% Erlang code ---
-module(niftest).
-export([init/0, increase/1, get/0]).
init() -> erlang:load_nif("./niftest", 0).
increase(_A) -> "Not implemented".
get() -> "Not implemented".
% Output ---
1> c(niftest).
{ok,niftest}
2> niftest:init().
ok
3> niftest:increase(5).
31360452
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.