Adapt basicnif template to OTP changes

This commit is contained in:
Tuncer Ayaz 2011-07-19 17:31:04 +02:00
parent edf4bbc370
commit 112f74cac1
2 changed files with 21 additions and 15 deletions

View file

@ -1,4 +1,3 @@
#include "erl_nif.h" #include "erl_nif.h"
static ErlNifResourceType* {{module}}_RESOURCE; static ErlNifResourceType* {{module}}_RESOURCE;
@ -8,8 +7,10 @@ typedef struct
} {{module}}_handle; } {{module}}_handle;
// Prototypes // Prototypes
ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); static ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc,
ERL_NIF_TERM {{module}}_myfunction(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]); const ERL_NIF_TERM argv[]);
static ERL_NIF_TERM {{module}}_myfunction(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[]);
static ErlNifFunc nif_funcs[] = static ErlNifFunc nif_funcs[] =
{ {
@ -17,34 +18,39 @@ static ErlNifFunc nif_funcs[] =
{"myfunction", 1, {{module}}_myfunction} {"myfunction", 1, {{module}}_myfunction}
}; };
ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) static ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[])
{ {
{{module}}_handle* handle = enif_alloc_resource(env, {{module}}_handle* handle = enif_alloc_resource({{module}}_RESOURCE,
{{module}}_RESOURCE,
sizeof({{module}}_handle)); sizeof({{module}}_handle));
ERL_NIF_TERM result = enif_make_resource(env, handle); ERL_NIF_TERM result = enif_make_resource(env, handle);
enif_release_resource(env, handle); enif_release_resource(handle);
return enif_make_tuple2(env, enif_make_atom(env, "ok"), result); 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[]) static ERL_NIF_TERM {{module}}_myfunction(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[])
{ {
return enif_make_atom(env, "ok"); return enif_make_atom(env, "ok");
} }
static void {{module}}_resource_cleanup(ErlNifEnv* env, void* arg) static void {{module}}_resource_cleanup(ErlNifEnv* env, void* arg)
{ {
// Delete any dynamically allocated memory stored in {{module}}_handle /* Delete any dynamically allocated memory stored in {{module}}_handle */
// {{module}}_handle* handle = ({{module}}_handle*)arg; /* {{module}}_handle* handle = ({{module}}_handle*)arg; */
} }
static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
{ {
{{module}}_RESOURCE = enif_open_resource_type(env, "{{module}}_resource", ErlNifResourceFlags flags = ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER;
ErlNifResourceType* rt = enif_open_resource_type(env, NULL,
"{{module}}_resource",
&{{module}}_resource_cleanup, &{{module}}_resource_cleanup,
ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER, flags, NULL);
0); if (rt == NULL)
return -1;
return 0; return 0;
} }

View file

@ -27,7 +27,7 @@ init() ->
new() -> new() ->
?nif_stub. ?nif_stub.
myfunction(Ref) -> myfunction(_Ref) ->
?nif_stub. ?nif_stub.
%% =================================================================== %% ===================================================================