Created
March 29, 2010 17:47
-
-
Save anonymous/348161 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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