public
anonymous / gist:348161
Created

  • Download Gist
gistfile1.hrl
Erlang
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.