diff options
author | Dave Smith <dizzyd@dizzyd.com> | 2010-03-25 22:32:53 -0600 |
---|---|---|
committer | Dave Smith <dizzyd@dizzyd.com> | 2010-03-25 22:32:53 -0600 |
commit | b2cfdcb34af8e8dab0661820572ec01aa55bf30e (patch) | |
tree | b9b6e1cd1d791fe7e4147e652a5d5814832a3dd6 | |
parent | 84f5108e178d62be5a3c12ab6f4e9d26f027d6f6 (diff) |
Adding basic NIF template
-rw-r--r-- | priv/templates/basicnif.c | 51 | ||||
-rw-r--r-- | priv/templates/basicnif.erl | 36 | ||||
-rw-r--r-- | priv/templates/basicnif.template | 3 |
3 files changed, 90 insertions, 0 deletions
diff --git a/priv/templates/basicnif.c b/priv/templates/basicnif.c new file mode 100644 index 0000000..36bf938 --- /dev/null +++ b/priv/templates/basicnif.c @@ -0,0 +1,51 @@ + +#include "erl_nif.h" + +static ErlNifResourceType* {{module}}_RESOURCE; + +typedef struct +{ +} {{module}}_handle; + +// Prototypes +ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); +ERL_NIF_TERM {{module}}_myfunction(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); + +static ErlNifFunc nif_funcs[] = +{ + {"new", 0, {{module}}_new}, + {"myfunction", 1, {{module}}_myfunction} +}; + +ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +{ + {{module}}_handle* handle = enif_alloc_resource(env, + {{module}}_RESOURCE, + sizeof({{module}}_handle)); + ERL_NIF_TERM result = enif_make_resource(env, handle); + enif_release_resource(env, handle); + return enif_make_tuple2(env, enif_make_atom(env, "ok"), result); +} + + +ERL_NIF_TERM {{module}}_myfunction(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) +{ + return enif_make_atom(env, "ok"); +} + +static void {{module}}_resource_cleanup(ErlNifEnv* env, void* arg) +{ + // Delete any dynamically allocated memory stored in {{module}}_handle + // {{module}}_handle* handle = ({{module}}_handle*)arg; +} + +static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) +{ + {{module}}_RESOURCE = enif_open_resource_type(env, "{{module}}_resource", + &{{module}}_resource_cleanup, + ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER, + 0); + return 0; +} + +ERL_NIF_INIT({{module}}, nif_funcs, &on_load, NULL, NULL, NULL); diff --git a/priv/templates/basicnif.erl b/priv/templates/basicnif.erl new file mode 100644 index 0000000..5f1ce11 --- /dev/null +++ b/priv/templates/basicnif.erl @@ -0,0 +1,36 @@ +-module({{module}}). + +-export([new/0, + myfunction/1]). + +-on_load(init/0). + +-ifdef(TEST). +-include_lib("eunit/include/eunit.hrl"). +-endif. + +init() -> + case code:priv_dir({{module}}) of + {error, bad_name} -> + SoName = filename:join("../priv", {{module}}); + Dir -> + SoName = filename:join(Dir, {{module}}) + end, + erlang:load_nif(SoName, 0). + +new() -> + "NIF library not loaded". + +myfunction(Ref) -> + "NIF library not loaded". + +%% =================================================================== +%% EUnit tests +%% =================================================================== +-ifdef(TEST). + +basic_test() -> + {ok, Ref} = new(), + ok = myfunction(Ref). + +-endif. diff --git a/priv/templates/basicnif.template b/priv/templates/basicnif.template new file mode 100644 index 0000000..6dde8bb --- /dev/null +++ b/priv/templates/basicnif.template @@ -0,0 +1,3 @@ +{variables, [{module, "mymodule"}]}. +{template, "basicnif.erl", "src/{{module}}.erl"}. +{template, "basicnif.c", "c_src/{{module}}.c"}. |