1
0
Fork 0
mirror of https://github.com/correl/rebar.git synced 2024-12-22 11:09:10 +00:00
rebar/priv/templates/basicnif.c

60 lines
1.9 KiB
C
Raw Normal View History

2010-03-26 04:32:53 +00:00
#include "erl_nif.h"
2012-01-05 21:41:06 +00:00
static ErlNifResourceType* {{module}}_RESOURCE = NULL;
2010-03-26 04:32:53 +00:00
typedef struct
{
} {{module}}_handle;
// Prototypes
2011-07-19 15:31:04 +00:00
static ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[]);
static ERL_NIF_TERM {{module}}_myfunction(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[]);
2010-03-26 04:32:53 +00:00
static ErlNifFunc nif_funcs[] =
{
{"new", 0, {{module}}_new},
{"myfunction", 1, {{module}}_myfunction}
};
2011-07-19 15:31:04 +00:00
static ERL_NIF_TERM {{module}}_new(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[])
2010-03-26 04:32:53 +00:00
{
2011-07-19 15:31:04 +00:00
{{module}}_handle* handle = enif_alloc_resource({{module}}_RESOURCE,
2010-03-26 04:32:53 +00:00
sizeof({{module}}_handle));
ERL_NIF_TERM result = enif_make_resource(env, handle);
2011-07-19 15:31:04 +00:00
enif_release_resource(handle);
2010-03-26 04:32:53 +00:00
return enif_make_tuple2(env, enif_make_atom(env, "ok"), result);
}
2011-07-19 15:31:04 +00:00
static ERL_NIF_TERM {{module}}_myfunction(ErlNifEnv* env, int argc,
const ERL_NIF_TERM argv[])
2010-03-26 04:32:53 +00:00
{
return enif_make_atom(env, "ok");
}
static void {{module}}_resource_cleanup(ErlNifEnv* env, void* arg)
{
2011-07-19 15:31:04 +00:00
/* Delete any dynamically allocated memory stored in {{module}}_handle */
/* {{module}}_handle* handle = ({{module}}_handle*)arg; */
2010-03-26 04:32:53 +00:00
}
static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info)
{
2011-07-19 15:31:04 +00:00
ErlNifResourceFlags flags = ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER;
ErlNifResourceType* rt = enif_open_resource_type(env, NULL,
"{{module}}_resource",
&{{module}}_resource_cleanup,
flags, NULL);
if (rt == NULL)
return -1;
2012-01-05 21:41:06 +00:00
{{module}}_RESOURCE = rt;
2010-03-26 04:32:53 +00:00
return 0;
}
ERL_NIF_INIT({{module}}, nif_funcs, &on_load, NULL, NULL, NULL);