Skip to content

Instantly share code, notes, and snippets.

@dch
Created Dec 13, 2019
Embed
What would you like to do?
#include <stdint.h>
#include <erl_nif.h>
/* a basic nif that get a binary and returns a binary, with some error wrapping */
static
ERL_NIF_TERM get_bin_addr(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
if (!enif_is_binary(env, argv[0])) {
ERL_NIF_TERM error = enif_make_atom(env, "error");
ERL_NIF_TERM reason = enif_make_atom(env, "badarg");
return enif_make_tuple2(env, error, reason);
}
ErlNifBinary binary;
enif_inspect_binary(env, argv[0], &binary);
ERL_NIF_TERM ok = enif_make_atom(env, "ok");
ERL_NIF_TERM addr_term = enif_make_uint64(env, (uint64_t)binary.data);
return enif_make_tuple2(env, ok, addr_term);
}
static ErlNifFunc nif_funcs[] = {
{"addr", 1, get_bin_addr}
};
ERL_NIF_INIT(bin, nif_funcs, NULL, NULL, NULL, NULL)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment