Created
December 13, 2019 11:29
-
-
Save dch/7bd3a016c9c6a47a87feea8d2707866e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <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